backtest-kit 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.cjs CHANGED
@@ -586,1664 +586,2015 @@ var emitters = /*#__PURE__*/Object.freeze({
586
586
  walkerStopSubject: walkerStopSubject
587
587
  });
588
588
 
589
- const INTERVAL_MINUTES$4 = {
590
- "1m": 1,
591
- "3m": 3,
592
- "5m": 5,
593
- "15m": 15,
594
- "30m": 30,
595
- "1h": 60,
596
- "2h": 120,
597
- "4h": 240,
598
- "6h": 360,
599
- "8h": 480,
600
- };
589
+ const IS_WINDOWS = os.platform() === "win32";
601
590
  /**
602
- * Validates that all candles have valid OHLCV data without anomalies.
603
- * Detects incomplete candles from Binance API by checking for abnormally low prices or volumes.
604
- * Incomplete candles often have prices like 0.1 instead of normal 100,000 or zero volume.
591
+ * Atomically writes data to a file, ensuring the operation either fully completes or leaves the original file unchanged.
592
+ * Uses a temporary file with a rename strategy on POSIX systems for atomicity, or direct writing with sync on Windows (or when POSIX rename is skipped).
605
593
  *
606
- * @param candles - Array of candle data to validate
607
- * @throws Error if any candles have anomalous OHLCV values
608
- */
609
- const VALIDATE_NO_INCOMPLETE_CANDLES_FN = (candles) => {
610
- if (candles.length === 0) {
611
- return;
594
+ *
595
+ * @param {string} file - The file parameter.
596
+ * @param {string | Buffer} data - The data to be processed or validated.
597
+ * @param {Options | BufferEncoding} options - The options parameter (optional).
598
+ * @throws {Error} Throws an error if the write, sync, or rename operation fails, after attempting cleanup of temporary files.
599
+ *
600
+ * @example
601
+ * // Basic usage with default options
602
+ * await writeFileAtomic("output.txt", "Hello, world!");
603
+ * // Writes "Hello, world!" to "output.txt" atomically
604
+ *
605
+ * @example
606
+ * // Custom options and Buffer data
607
+ * const buffer = Buffer.from("Binary data");
608
+ * await writeFileAtomic("data.bin", buffer, { encoding: "binary", mode: 0o644, tmpPrefix: "temp-" });
609
+ * // Writes binary data to "data.bin" with custom permissions and temp prefix
610
+ *
611
+ * @example
612
+ * // Using encoding shorthand
613
+ * await writeFileAtomic("log.txt", "Log entry", "utf16le");
614
+ * // Writes "Log entry" to "log.txt" in UTF-16LE encoding
615
+ *
616
+ * @remarks
617
+ * This function ensures atomicity to prevent partial writes:
618
+ * - On POSIX systems (non-Windows, unless `GLOBAL_CONFIG.CC_SKIP_POSIX_RENAME` is true):
619
+ * - Writes data to a temporary file (e.g., `.tmp-<random>-filename`) in the same directory.
620
+ * - Uses `crypto.randomBytes` to generate a unique temporary name, reducing collision risk.
621
+ * - Syncs the data to disk and renames the temporary file to the target file atomically with `fs.rename`.
622
+ * - Cleans up the temporary file on failure, swallowing cleanup errors to prioritize throwing the original error.
623
+ * - On Windows (or when POSIX rename is skipped):
624
+ * - Writes directly to the target file, syncing data to disk to minimize corruption risk (though not fully atomic).
625
+ * - Closes the file handle on failure without additional cleanup.
626
+ * - Accepts `options` as an object or a string (interpreted as `encoding`), defaulting to `{ encoding: "utf8", mode: 0o666, tmpPrefix: ".tmp-" }`.
627
+ * Useful in the agent swarm system for safely writing configuration files, logs, or state data where partial writes could cause corruption.
628
+ *
629
+ * @see {@link https://nodejs.org/api/fs.html#fspromiseswritefilefile-data-options|fs.promises.writeFile} for file writing details.
630
+ * @see {@link https://nodejs.org/api/crypto.html#cryptorandombytessize-callback|crypto.randomBytes} for temporary file naming.
631
+ * @see {@link ../config/params|GLOBAL_CONFIG} for configuration impacting POSIX behavior.
632
+ */
633
+ async function writeFileAtomic(file, data, options = {}) {
634
+ if (typeof options === "string") {
635
+ options = { encoding: options };
612
636
  }
613
- // Calculate reference price (median or average depending on candle count)
614
- const allPrices = candles.flatMap((c) => [c.open, c.high, c.low, c.close]);
615
- const validPrices = allPrices.filter(p => p > 0);
616
- let referencePrice;
617
- if (candles.length >= GLOBAL_CONFIG.CC_GET_CANDLES_MIN_CANDLES_FOR_MEDIAN) {
618
- // Use median for reliable statistics with enough data
619
- const sortedPrices = [...validPrices].sort((a, b) => a - b);
620
- referencePrice = sortedPrices[Math.floor(sortedPrices.length / 2)] || 0;
637
+ else if (!options) {
638
+ options = {};
621
639
  }
622
- else {
623
- // Use average for small datasets (more stable than median)
624
- const sum = validPrices.reduce((acc, p) => acc + p, 0);
625
- referencePrice = validPrices.length > 0 ? sum / validPrices.length : 0;
640
+ const { encoding = "utf8", mode = 0o666, tmpPrefix = ".tmp-" } = options;
641
+ let fileHandle = null;
642
+ if (IS_WINDOWS) {
643
+ try {
644
+ // Create and write to temporary file
645
+ fileHandle = await fs.open(file, "w", mode);
646
+ // Write data to the temp file
647
+ await fileHandle.writeFile(data, { encoding });
648
+ // Ensure data is flushed to disk
649
+ await fileHandle.sync();
650
+ // Close the file before rename
651
+ await fileHandle.close();
652
+ }
653
+ catch (error) {
654
+ // Clean up if something went wrong
655
+ if (fileHandle) {
656
+ await fileHandle.close().catch(() => { });
657
+ }
658
+ throw error; // Re-throw the original error
659
+ }
660
+ return;
626
661
  }
627
- if (referencePrice === 0) {
628
- throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: cannot calculate reference price (all prices are zero)`);
662
+ // Create a temporary filename in the same directory
663
+ const dir = path.dirname(file);
664
+ const filename = path.basename(file);
665
+ const tmpFile = path.join(dir, `${tmpPrefix}${crypto.randomBytes(6).toString("hex")}-${filename}`);
666
+ try {
667
+ // Create and write to temporary file
668
+ fileHandle = await fs.open(tmpFile, "w", mode);
669
+ // Write data to the temp file
670
+ await fileHandle.writeFile(data, { encoding });
671
+ // Ensure data is flushed to disk
672
+ await fileHandle.sync();
673
+ // Close the file before rename
674
+ await fileHandle.close();
675
+ fileHandle = null;
676
+ // Atomically replace the target file with our temp file
677
+ await fs.rename(tmpFile, file);
629
678
  }
630
- const minValidPrice = referencePrice / GLOBAL_CONFIG.CC_GET_CANDLES_PRICE_ANOMALY_THRESHOLD_FACTOR;
631
- for (let i = 0; i < candles.length; i++) {
632
- const candle = candles[i];
633
- // Check for invalid numeric values
634
- if (!Number.isFinite(candle.open) ||
635
- !Number.isFinite(candle.high) ||
636
- !Number.isFinite(candle.low) ||
637
- !Number.isFinite(candle.close) ||
638
- !Number.isFinite(candle.volume) ||
639
- !Number.isFinite(candle.timestamp)) {
640
- throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: candle[${i}] has invalid numeric values (NaN or Infinity)`);
679
+ catch (error) {
680
+ // Clean up if something went wrong
681
+ if (fileHandle) {
682
+ await fileHandle.close().catch(() => { });
641
683
  }
642
- // Check for negative values
643
- if (candle.open <= 0 ||
644
- candle.high <= 0 ||
645
- candle.low <= 0 ||
646
- candle.close <= 0 ||
647
- candle.volume < 0) {
648
- throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: candle[${i}] has zero or negative values`);
684
+ // Try to remove the temporary file
685
+ try {
686
+ await fs.unlink(tmpFile).catch(() => { });
649
687
  }
650
- // Check for anomalously low prices (incomplete candle indicator)
651
- if (candle.open < minValidPrice ||
652
- candle.high < minValidPrice ||
653
- candle.low < minValidPrice ||
654
- candle.close < minValidPrice) {
655
- throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: candle[${i}] has anomalously low price. ` +
656
- `OHLC: [${candle.open}, ${candle.high}, ${candle.low}, ${candle.close}], ` +
657
- `reference: ${referencePrice}, threshold: ${minValidPrice}`);
688
+ catch (_) {
689
+ // Ignore errors during cleanup
658
690
  }
691
+ throw error;
659
692
  }
660
- };
661
- /**
662
- * Retries the getCandles function with specified retry count and delay.
663
- * @param dto - Data transfer object containing symbol, interval, and limit
664
- * @param since - Date object representing the start time for fetching candles
665
- * @param self - Instance of ClientExchange
666
- * @returns Promise resolving to array of candle data
667
- */
668
- const GET_CANDLES_FN = async (dto, since, self) => {
669
- let lastError;
670
- for (let i = 0; i !== GLOBAL_CONFIG.CC_GET_CANDLES_RETRY_COUNT; i++) {
693
+ }
694
+
695
+ var _a$2;
696
+ const BASE_WAIT_FOR_INIT_SYMBOL = Symbol("wait-for-init");
697
+ const PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_PERSIST_SIGNAL_ADAPTER = "PersistSignalUtils.usePersistSignalAdapter";
698
+ const PERSIST_SIGNAL_UTILS_METHOD_NAME_READ_DATA = "PersistSignalUtils.readSignalData";
699
+ const PERSIST_SIGNAL_UTILS_METHOD_NAME_WRITE_DATA = "PersistSignalUtils.writeSignalData";
700
+ const PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_JSON = "PersistSignalUtils.useJson";
701
+ const PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_DUMMY = "PersistSignalUtils.useDummy";
702
+ const PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_PERSIST_SCHEDULE_ADAPTER = "PersistScheduleUtils.usePersistScheduleAdapter";
703
+ const PERSIST_SCHEDULE_UTILS_METHOD_NAME_READ_DATA = "PersistScheduleUtils.readScheduleData";
704
+ const PERSIST_SCHEDULE_UTILS_METHOD_NAME_WRITE_DATA = "PersistScheduleUtils.writeScheduleData";
705
+ const PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_JSON = "PersistScheduleUtils.useJson";
706
+ const PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_DUMMY = "PersistScheduleUtils.useDummy";
707
+ const PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_PERSIST_PARTIAL_ADAPTER = "PersistPartialUtils.usePersistPartialAdapter";
708
+ const PERSIST_PARTIAL_UTILS_METHOD_NAME_READ_DATA = "PersistPartialUtils.readPartialData";
709
+ const PERSIST_PARTIAL_UTILS_METHOD_NAME_WRITE_DATA = "PersistPartialUtils.writePartialData";
710
+ const PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_JSON = "PersistPartialUtils.useJson";
711
+ const PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_DUMMY = "PersistPartialUtils.useDummy";
712
+ const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_PERSIST_BREAKEVEN_ADAPTER = "PersistBreakevenUtils.usePersistBreakevenAdapter";
713
+ const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_READ_DATA = "PersistBreakevenUtils.readBreakevenData";
714
+ const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_WRITE_DATA = "PersistBreakevenUtils.writeBreakevenData";
715
+ const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_JSON = "PersistBreakevenUtils.useJson";
716
+ const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_DUMMY = "PersistBreakevenUtils.useDummy";
717
+ const PERSIST_RISK_UTILS_METHOD_NAME_USE_PERSIST_RISK_ADAPTER = "PersistRiskUtils.usePersistRiskAdapter";
718
+ const PERSIST_RISK_UTILS_METHOD_NAME_READ_DATA = "PersistRiskUtils.readPositionData";
719
+ const PERSIST_RISK_UTILS_METHOD_NAME_WRITE_DATA = "PersistRiskUtils.writePositionData";
720
+ const PERSIST_RISK_UTILS_METHOD_NAME_USE_JSON = "PersistRiskUtils.useJson";
721
+ const PERSIST_RISK_UTILS_METHOD_NAME_USE_DUMMY = "PersistRiskUtils.useDummy";
722
+ const PERSIST_BASE_METHOD_NAME_CTOR = "PersistBase.CTOR";
723
+ const PERSIST_BASE_METHOD_NAME_WAIT_FOR_INIT = "PersistBase.waitForInit";
724
+ const PERSIST_BASE_METHOD_NAME_READ_VALUE = "PersistBase.readValue";
725
+ const PERSIST_BASE_METHOD_NAME_WRITE_VALUE = "PersistBase.writeValue";
726
+ const PERSIST_BASE_METHOD_NAME_HAS_VALUE = "PersistBase.hasValue";
727
+ const PERSIST_BASE_METHOD_NAME_KEYS = "PersistBase.keys";
728
+ const BASE_WAIT_FOR_INIT_FN_METHOD_NAME = "PersistBase.waitForInitFn";
729
+ const BASE_UNLINK_RETRY_COUNT = 5;
730
+ const BASE_UNLINK_RETRY_DELAY = 1000;
731
+ const BASE_WAIT_FOR_INIT_FN = async (self) => {
732
+ bt.loggerService.debug(BASE_WAIT_FOR_INIT_FN_METHOD_NAME, {
733
+ entityName: self.entityName,
734
+ directory: self._directory,
735
+ });
736
+ await fs.mkdir(self._directory, { recursive: true });
737
+ for await (const key of self.keys()) {
671
738
  try {
672
- const result = await self.params.getCandles(dto.symbol, dto.interval, since, dto.limit, self.params.execution.context.backtest);
673
- VALIDATE_NO_INCOMPLETE_CANDLES_FN(result);
674
- return result;
739
+ await self.readValue(key);
675
740
  }
676
- catch (err) {
677
- const message = `ClientExchange GET_CANDLES_FN: attempt ${i + 1} failed for symbol=${dto.symbol}, interval=${dto.interval}, since=${since.toISOString()}, limit=${dto.limit}}`;
678
- const payload = {
679
- error: functoolsKit.errorData(err),
680
- message: functoolsKit.getErrorMessage(err),
681
- };
682
- self.params.logger.warn(message, payload);
683
- console.warn(message, payload);
684
- lastError = err;
685
- await functoolsKit.sleep(GLOBAL_CONFIG.CC_GET_CANDLES_RETRY_DELAY_MS);
741
+ catch {
742
+ const filePath = self._getFilePath(key);
743
+ console.error(`backtest-kit PersistBase found invalid document for filePath=${filePath} entityName=${self.entityName}`);
744
+ if (await functoolsKit.not(BASE_WAIT_FOR_INIT_UNLINK_FN(filePath))) {
745
+ console.error(`backtest-kit PersistBase failed to remove invalid document for filePath=${filePath} entityName=${self.entityName}`);
746
+ }
686
747
  }
687
748
  }
688
- throw lastError;
689
749
  };
750
+ const BASE_WAIT_FOR_INIT_UNLINK_FN = async (filePath) => functoolsKit.trycatch(functoolsKit.retry(async () => {
751
+ try {
752
+ await fs.unlink(filePath);
753
+ return true;
754
+ }
755
+ catch (error) {
756
+ console.error(`backtest-kit PersistBase unlink failed for filePath=${filePath} error=${functoolsKit.getErrorMessage(error)}`);
757
+ throw error;
758
+ }
759
+ }, BASE_UNLINK_RETRY_COUNT, BASE_UNLINK_RETRY_DELAY), {
760
+ defaultValue: false,
761
+ });
690
762
  /**
691
- * Wrapper to call onCandleData callback with error handling.
692
- * Catches and logs any errors thrown by the user-provided callback.
693
- *
694
- * @param self - ClientExchange instance reference
695
- * @param symbol - Trading pair symbol
696
- * @param interval - Candle interval
697
- * @param since - Start date for candle data
698
- * @param limit - Number of candles
699
- * @param data - Array of candle data
700
- */
701
- const CALL_CANDLE_DATA_CALLBACKS_FN = functoolsKit.trycatch(async (self, symbol, interval, since, limit, data) => {
702
- if (self.params.callbacks?.onCandleData) {
703
- await self.params.callbacks.onCandleData(symbol, interval, since, limit, data);
704
- }
705
- }, {
706
- fallback: (error) => {
707
- const message = "ClientExchange CALL_CANDLE_DATA_CALLBACKS_FN thrown";
708
- const payload = {
709
- error: functoolsKit.errorData(error),
710
- message: functoolsKit.getErrorMessage(error),
711
- };
712
- bt.loggerService.warn(message, payload);
713
- console.warn(message, payload);
714
- errorEmitter.next(error);
715
- },
716
- });
717
- /**
718
- * Client implementation for exchange data access.
763
+ * Base class for file-based persistence with atomic writes.
719
764
  *
720
765
  * Features:
721
- * - Historical candle fetching (backwards from execution context)
722
- * - Future candle fetching (forwards for backtest)
723
- * - VWAP calculation from last 5 1m candles
724
- * - Price/quantity formatting for exchange
725
- *
726
- * All methods use prototype functions for memory efficiency.
766
+ * - Atomic file writes using writeFileAtomic
767
+ * - Auto-validation and cleanup of corrupted files
768
+ * - Async generator support for iteration
769
+ * - Retry logic for file deletion
727
770
  *
728
771
  * @example
729
772
  * ```typescript
730
- * const exchange = new ClientExchange({
731
- * exchangeName: "binance",
732
- * getCandles: async (symbol, interval, since, limit) => [...],
733
- * formatPrice: async (symbol, price) => price.toFixed(2),
734
- * formatQuantity: async (symbol, quantity) => quantity.toFixed(8),
735
- * execution: executionService,
736
- * logger: loggerService,
737
- * });
738
- *
739
- * const candles = await exchange.getCandles("BTCUSDT", "1m", 100);
740
- * const vwap = await exchange.getAveragePrice("BTCUSDT");
773
+ * const persist = new PersistBase("my-entity", "./data");
774
+ * await persist.waitForInit(true);
775
+ * await persist.writeValue("key1", { data: "value" });
776
+ * const value = await persist.readValue("key1");
741
777
  * ```
742
778
  */
743
- class ClientExchange {
744
- constructor(params) {
745
- this.params = params;
746
- }
779
+ class PersistBase {
747
780
  /**
748
- * Fetches historical candles backwards from execution context time.
781
+ * Creates new persistence instance.
749
782
  *
750
- * @param symbol - Trading pair symbol
751
- * @param interval - Candle interval
752
- * @param limit - Number of candles to fetch
753
- * @returns Promise resolving to array of candles
783
+ * @param entityName - Unique entity type identifier
784
+ * @param baseDir - Base directory for all entities (default: ./dump/data)
754
785
  */
755
- async getCandles(symbol, interval, limit) {
756
- this.params.logger.debug(`ClientExchange getCandles`, {
757
- symbol,
758
- interval,
759
- limit,
786
+ constructor(entityName, baseDir = path.join(process.cwd(), "logs/data")) {
787
+ this.entityName = entityName;
788
+ this.baseDir = baseDir;
789
+ this[_a$2] = functoolsKit.singleshot(async () => await BASE_WAIT_FOR_INIT_FN(this));
790
+ bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_CTOR, {
791
+ entityName: this.entityName,
792
+ baseDir,
760
793
  });
761
- const step = INTERVAL_MINUTES$4[interval];
762
- const adjust = step * limit;
763
- if (!adjust) {
764
- throw new Error(`ClientExchange unknown time adjust for interval=${interval}`);
765
- }
766
- const since = new Date(this.params.execution.context.when.getTime() - adjust * 60 * 1000);
767
- let allData = [];
768
- // If limit exceeds CC_MAX_CANDLES_PER_REQUEST, fetch data in chunks
769
- if (limit > GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST) {
770
- let remaining = limit;
771
- let currentSince = new Date(since.getTime());
772
- while (remaining > 0) {
773
- const chunkLimit = Math.min(remaining, GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST);
774
- const chunkData = await GET_CANDLES_FN({ symbol, interval, limit: chunkLimit }, currentSince, this);
775
- allData.push(...chunkData);
776
- remaining -= chunkLimit;
777
- if (remaining > 0) {
778
- // Move currentSince forward by the number of candles fetched
779
- currentSince = new Date(currentSince.getTime() + chunkLimit * step * 60 * 1000);
780
- }
781
- }
782
- }
783
- else {
784
- allData = await GET_CANDLES_FN({ symbol, interval, limit }, since, this);
785
- }
786
- // Filter candles to strictly match the requested range
787
- const whenTimestamp = this.params.execution.context.when.getTime();
788
- const sinceTimestamp = since.getTime();
789
- const stepMs = step * 60 * 1000;
790
- const filteredData = allData.filter((candle) => candle.timestamp >= sinceTimestamp && candle.timestamp < whenTimestamp + stepMs);
791
- // Apply distinct by timestamp to remove duplicates
792
- const uniqueData = Array.from(new Map(filteredData.map((candle) => [candle.timestamp, candle])).values());
793
- if (filteredData.length !== uniqueData.length) {
794
- const msg = `ClientExchange Removed ${filteredData.length - uniqueData.length} duplicate candles by timestamp`;
795
- this.params.logger.warn(msg);
796
- console.warn(msg);
797
- }
798
- if (uniqueData.length < limit) {
799
- const msg = `ClientExchange Expected ${limit} candles, got ${uniqueData.length}`;
800
- this.params.logger.warn(msg);
801
- console.warn(msg);
802
- }
803
- await CALL_CANDLE_DATA_CALLBACKS_FN(this, symbol, interval, since, limit, uniqueData);
804
- return uniqueData;
794
+ this._directory = path.join(this.baseDir, this.entityName);
805
795
  }
806
796
  /**
807
- * Fetches future candles forwards from execution context time.
808
- * Used in backtest mode to get candles for signal duration.
797
+ * Computes file path for entity ID.
809
798
  *
810
- * @param symbol - Trading pair symbol
811
- * @param interval - Candle interval
812
- * @param limit - Number of candles to fetch
813
- * @returns Promise resolving to array of candles
814
- * @throws Error if trying to fetch future candles in live mode
799
+ * @param entityId - Entity identifier
800
+ * @returns Full file path to entity JSON file
815
801
  */
816
- async getNextCandles(symbol, interval, limit) {
817
- this.params.logger.debug(`ClientExchange getNextCandles`, {
818
- symbol,
819
- interval,
820
- limit,
802
+ _getFilePath(entityId) {
803
+ return path.join(this.baseDir, this.entityName, `${entityId}.json`);
804
+ }
805
+ async waitForInit(initial) {
806
+ bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_WAIT_FOR_INIT, {
807
+ entityName: this.entityName,
808
+ initial,
821
809
  });
822
- const since = new Date(this.params.execution.context.when.getTime());
823
- const now = Date.now();
824
- // Вычисляем конечное время запроса
825
- const step = INTERVAL_MINUTES$4[interval];
826
- const endTime = since.getTime() + limit * step * 60 * 1000;
827
- // Проверяем что запрошенный период не заходит за Date.now()
828
- if (endTime > now) {
829
- return [];
810
+ await this[BASE_WAIT_FOR_INIT_SYMBOL]();
811
+ }
812
+ async readValue(entityId) {
813
+ bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_READ_VALUE, {
814
+ entityName: this.entityName,
815
+ entityId,
816
+ });
817
+ try {
818
+ const filePath = this._getFilePath(entityId);
819
+ const fileContent = await fs.readFile(filePath, "utf-8");
820
+ return JSON.parse(fileContent);
830
821
  }
831
- let allData = [];
832
- // If limit exceeds CC_MAX_CANDLES_PER_REQUEST, fetch data in chunks
833
- if (limit > GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST) {
834
- let remaining = limit;
835
- let currentSince = new Date(since.getTime());
836
- while (remaining > 0) {
837
- const chunkLimit = Math.min(remaining, GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST);
838
- const chunkData = await GET_CANDLES_FN({ symbol, interval, limit: chunkLimit }, currentSince, this);
839
- allData.push(...chunkData);
840
- remaining -= chunkLimit;
841
- if (remaining > 0) {
842
- // Move currentSince forward by the number of candles fetched
843
- currentSince = new Date(currentSince.getTime() + chunkLimit * step * 60 * 1000);
844
- }
822
+ catch (error) {
823
+ if (error?.code === "ENOENT") {
824
+ throw new Error(`Entity ${this.entityName}:${entityId} not found`);
845
825
  }
826
+ throw new Error(`Failed to read entity ${this.entityName}:${entityId}: ${functoolsKit.getErrorMessage(error)}`);
846
827
  }
847
- else {
848
- allData = await GET_CANDLES_FN({ symbol, interval, limit }, since, this);
849
- }
850
- // Filter candles to strictly match the requested range
851
- const sinceTimestamp = since.getTime();
852
- const filteredData = allData.filter((candle) => candle.timestamp >= sinceTimestamp && candle.timestamp < endTime);
853
- // Apply distinct by timestamp to remove duplicates
854
- const uniqueData = Array.from(new Map(filteredData.map((candle) => [candle.timestamp, candle])).values());
855
- if (filteredData.length !== uniqueData.length) {
856
- const msg = `ClientExchange getNextCandles: Removed ${filteredData.length - uniqueData.length} duplicate candles by timestamp`;
857
- this.params.logger.warn(msg);
858
- console.warn(msg);
828
+ }
829
+ async hasValue(entityId) {
830
+ bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_HAS_VALUE, {
831
+ entityName: this.entityName,
832
+ entityId,
833
+ });
834
+ try {
835
+ const filePath = this._getFilePath(entityId);
836
+ await fs.access(filePath);
837
+ return true;
859
838
  }
860
- if (uniqueData.length < limit) {
861
- const msg = `ClientExchange getNextCandles: Expected ${limit} candles, got ${uniqueData.length}`;
862
- this.params.logger.warn(msg);
863
- console.warn(msg);
839
+ catch (error) {
840
+ if (error?.code === "ENOENT") {
841
+ return false;
842
+ }
843
+ throw new Error(`Failed to check existence of entity ${this.entityName}:${entityId}: ${functoolsKit.getErrorMessage(error)}`);
864
844
  }
865
- await CALL_CANDLE_DATA_CALLBACKS_FN(this, symbol, interval, since, limit, uniqueData);
866
- return uniqueData;
867
845
  }
868
- /**
869
- * Calculates VWAP (Volume Weighted Average Price) from last N 1m candles.
870
- * The number of candles is configurable via GLOBAL_CONFIG.CC_AVG_PRICE_CANDLES_COUNT.
871
- *
872
- * Formula:
873
- * - Typical Price = (high + low + close) / 3
874
- * - VWAP = sum(typical_price * volume) / sum(volume)
875
- *
876
- * If volume is zero, returns simple average of close prices.
877
- *
878
- * @param symbol - Trading pair symbol
879
- * @returns Promise resolving to VWAP price
880
- * @throws Error if no candles available
881
- */
882
- async getAveragePrice(symbol) {
883
- this.params.logger.debug(`ClientExchange getAveragePrice`, {
884
- symbol,
846
+ async writeValue(entityId, entity) {
847
+ bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_WRITE_VALUE, {
848
+ entityName: this.entityName,
849
+ entityId,
885
850
  });
886
- const candles = await this.getCandles(symbol, "1m", GLOBAL_CONFIG.CC_AVG_PRICE_CANDLES_COUNT);
887
- if (candles.length === 0) {
888
- throw new Error(`ClientExchange getAveragePrice: no candles data for symbol=${symbol}`);
851
+ try {
852
+ const filePath = this._getFilePath(entityId);
853
+ const serializedData = JSON.stringify(entity);
854
+ await writeFileAtomic(filePath, serializedData, "utf-8");
889
855
  }
890
- // VWAP (Volume Weighted Average Price)
891
- // Используем типичную цену (typical price) = (high + low + close) / 3
892
- const sumPriceVolume = candles.reduce((acc, candle) => {
893
- const typicalPrice = (candle.high + candle.low + candle.close) / 3;
894
- return acc + typicalPrice * candle.volume;
895
- }, 0);
896
- const totalVolume = candles.reduce((acc, candle) => acc + candle.volume, 0);
897
- if (totalVolume === 0) {
898
- // Если объем нулевой, возвращаем простое среднее close цен
899
- const sum = candles.reduce((acc, candle) => acc + candle.close, 0);
900
- return sum / candles.length;
856
+ catch (error) {
857
+ throw new Error(`Failed to write entity ${this.entityName}:${entityId}: ${functoolsKit.getErrorMessage(error)}`);
901
858
  }
902
- const vwap = sumPriceVolume / totalVolume;
903
- return vwap;
904
859
  }
905
860
  /**
906
- * Formats quantity according to exchange-specific rules for the given symbol.
907
- * Applies proper decimal precision and rounding based on symbol's lot size filters.
861
+ * Async generator yielding all entity IDs.
862
+ * Sorted alphanumerically.
863
+ * Used internally by waitForInit for validation.
908
864
  *
909
- * @param symbol - Trading pair symbol
910
- * @param quantity - Raw quantity to format
911
- * @returns Promise resolving to formatted quantity as string
865
+ * @returns AsyncGenerator yielding entity IDs
866
+ * @throws Error if reading fails
912
867
  */
913
- async formatQuantity(symbol, quantity) {
914
- this.params.logger.debug("binanceService formatQuantity", {
915
- symbol,
916
- quantity,
868
+ async *keys() {
869
+ bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_KEYS, {
870
+ entityName: this.entityName,
917
871
  });
918
- return await this.params.formatQuantity(symbol, quantity, this.params.execution.context.backtest);
872
+ try {
873
+ const files = await fs.readdir(this._directory);
874
+ const entityIds = files
875
+ .filter((file) => file.endsWith(".json"))
876
+ .map((file) => file.slice(0, -5))
877
+ .sort((a, b) => a.localeCompare(b, undefined, {
878
+ numeric: true,
879
+ sensitivity: "base",
880
+ }));
881
+ for (const entityId of entityIds) {
882
+ yield entityId;
883
+ }
884
+ }
885
+ catch (error) {
886
+ throw new Error(`Failed to read keys for ${this.entityName}: ${functoolsKit.getErrorMessage(error)}`);
887
+ }
919
888
  }
889
+ }
890
+ _a$2 = BASE_WAIT_FOR_INIT_SYMBOL;
891
+ // @ts-ignore
892
+ PersistBase = functoolsKit.makeExtendable(PersistBase);
893
+ /**
894
+ * Dummy persist adapter that discards all writes.
895
+ * Used for disabling persistence.
896
+ */
897
+ class PersistDummy {
920
898
  /**
921
- * Formats price according to exchange-specific rules for the given symbol.
922
- * Applies proper decimal precision and rounding based on symbol's price filters.
923
- *
924
- * @param symbol - Trading pair symbol
925
- * @param price - Raw price to format
926
- * @returns Promise resolving to formatted price as string
899
+ * No-op initialization function.
900
+ * @returns Promise that resolves immediately
927
901
  */
928
- async formatPrice(symbol, price) {
929
- this.params.logger.debug("binanceService formatPrice", {
930
- symbol,
931
- price,
932
- });
933
- return await this.params.formatPrice(symbol, price, this.params.execution.context.backtest);
902
+ async waitForInit() {
934
903
  }
935
904
  /**
936
- * Fetches order book for a trading pair.
937
- *
938
- * Calculates time range based on execution context time (when) and
939
- * CC_ORDER_BOOK_TIME_OFFSET_MINUTES, then delegates to the exchange
940
- * schema implementation which may use or ignore the time range.
941
- *
942
- * @param symbol - Trading pair symbol
943
- * @param depth - Maximum depth levels (default: CC_ORDER_BOOK_MAX_DEPTH_LEVELS)
944
- * @returns Promise resolving to order book data
945
- * @throws Error if getOrderBook is not implemented
905
+ * No-op read function.
906
+ * @returns Promise that resolves with empty object
946
907
  */
947
- async getOrderBook(symbol, depth = GLOBAL_CONFIG.CC_ORDER_BOOK_MAX_DEPTH_LEVELS) {
948
- this.params.logger.debug("ClientExchange getOrderBook", {
949
- symbol,
950
- depth,
951
- });
952
- const to = new Date(this.params.execution.context.when.getTime());
953
- const from = new Date(to.getTime() - GLOBAL_CONFIG.CC_ORDER_BOOK_TIME_OFFSET_MINUTES * 60 * 1000);
954
- return await this.params.getOrderBook(symbol, depth, from, to, this.params.execution.context.backtest);
908
+ async readValue() {
909
+ return {};
910
+ }
911
+ /**
912
+ * No-op has value check.
913
+ * @returns Promise that resolves to false
914
+ */
915
+ async hasValue() {
916
+ return false;
917
+ }
918
+ /**
919
+ * No-op write function.
920
+ * @returns Promise that resolves immediately
921
+ */
922
+ async writeValue() {
923
+ }
924
+ /**
925
+ * No-op keys generator.
926
+ * @returns Empty async generator
927
+ */
928
+ async *keys() {
929
+ // Empty generator - no keys
955
930
  }
956
931
  }
957
-
958
- /**
959
- * Default implementation for getCandles.
960
- * Throws an error indicating the method is not implemented.
961
- */
962
- const DEFAULT_GET_CANDLES_FN$1 = async (_symbol, _interval, _since, _limit, _backtest) => {
963
- throw new Error(`getCandles is not implemented for this exchange`);
964
- };
965
- /**
966
- * Default implementation for formatQuantity.
967
- * Returns Bitcoin precision on Binance (8 decimal places).
968
- */
969
- const DEFAULT_FORMAT_QUANTITY_FN$1 = async (_symbol, quantity, _backtest) => {
970
- return quantity.toFixed(8);
971
- };
972
- /**
973
- * Default implementation for formatPrice.
974
- * Returns Bitcoin precision on Binance (2 decimal places).
975
- */
976
- const DEFAULT_FORMAT_PRICE_FN$1 = async (_symbol, price, _backtest) => {
977
- return price.toFixed(2);
978
- };
979
- /**
980
- * Default implementation for getOrderBook.
981
- * Throws an error indicating the method is not implemented.
982
- *
983
- * @param _symbol - Trading pair symbol (unused)
984
- * @param _depth - Maximum depth levels (unused)
985
- * @param _from - Start of time range (unused - can be ignored in live implementations)
986
- * @param _to - End of time range (unused - can be ignored in live implementations)
987
- * @param _backtest - Whether running in backtest mode (unused)
988
- */
989
- const DEFAULT_GET_ORDER_BOOK_FN$1 = async (_symbol, _depth, _from, _to, _backtest) => {
990
- throw new Error(`getOrderBook is not implemented for this exchange`);
991
- };
992
932
  /**
993
- * Connection service routing exchange operations to correct ClientExchange instance.
994
- *
995
- * Routes all IExchange method calls to the appropriate exchange implementation
996
- * based on methodContextService.context.exchangeName. Uses memoization to cache
997
- * ClientExchange instances for performance.
933
+ * Utility class for managing signal persistence.
998
934
  *
999
- * Key features:
1000
- * - Automatic exchange routing via method context
1001
- * - Memoized ClientExchange instances by exchangeName
1002
- * - Implements full IExchange interface
1003
- * - Logging for all operations
935
+ * Features:
936
+ * - Memoized storage instances per strategy
937
+ * - Custom adapter support
938
+ * - Atomic read/write operations
939
+ * - Crash-safe signal state management
1004
940
  *
1005
- * @example
1006
- * ```typescript
1007
- * // Used internally by framework
1008
- * const candles = await exchangeConnectionService.getCandles(
1009
- * "BTCUSDT", "1h", 100
1010
- * );
1011
- * // Automatically routes to correct exchange based on methodContext
1012
- * ```
941
+ * Used by ClientStrategy for live mode persistence.
1013
942
  */
1014
- class ExchangeConnectionService {
943
+ class PersistSignalUtils {
1015
944
  constructor() {
1016
- this.loggerService = inject(TYPES.loggerService);
1017
- this.executionContextService = inject(TYPES.executionContextService);
1018
- this.exchangeSchemaService = inject(TYPES.exchangeSchemaService);
1019
- this.methodContextService = inject(TYPES.methodContextService);
1020
- /**
1021
- * Retrieves memoized ClientExchange instance for given exchange name.
1022
- *
1023
- * Creates ClientExchange on first call, returns cached instance on subsequent calls.
1024
- * Cache key is exchangeName string.
1025
- *
1026
- * @param exchangeName - Name of registered exchange schema
1027
- * @returns Configured ClientExchange instance
1028
- */
1029
- this.getExchange = functoolsKit.memoize(([exchangeName]) => `${exchangeName}`, (exchangeName) => {
1030
- const { getCandles = DEFAULT_GET_CANDLES_FN$1, formatPrice = DEFAULT_FORMAT_PRICE_FN$1, formatQuantity = DEFAULT_FORMAT_QUANTITY_FN$1, getOrderBook = DEFAULT_GET_ORDER_BOOK_FN$1, callbacks } = this.exchangeSchemaService.get(exchangeName);
1031
- return new ClientExchange({
1032
- execution: this.executionContextService,
1033
- logger: this.loggerService,
1034
- exchangeName,
1035
- getCandles,
1036
- formatPrice,
1037
- formatQuantity,
1038
- getOrderBook,
1039
- callbacks,
1040
- });
1041
- });
1042
- /**
1043
- * Fetches historical candles for symbol using configured exchange.
1044
- *
1045
- * Routes to exchange determined by methodContextService.context.exchangeName.
1046
- *
1047
- * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
1048
- * @param interval - Candle interval (e.g., "1h", "1d")
1049
- * @param limit - Maximum number of candles to fetch
1050
- * @returns Promise resolving to array of candle data
1051
- */
1052
- this.getCandles = async (symbol, interval, limit) => {
1053
- this.loggerService.log("exchangeConnectionService getCandles", {
1054
- symbol,
1055
- interval,
1056
- limit,
1057
- });
1058
- return await this.getExchange(this.methodContextService.context.exchangeName).getCandles(symbol, interval, limit);
1059
- };
945
+ this.PersistSignalFactory = PersistBase;
946
+ this.getSignalStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistSignalFactory, [
947
+ `${symbol}_${strategyName}_${exchangeName}`,
948
+ `./dump/data/signal/`,
949
+ ]));
1060
950
  /**
1061
- * Fetches next batch of candles relative to executionContext.when.
951
+ * Reads persisted signal data for a symbol and strategy.
1062
952
  *
1063
- * Returns candles that come after the current execution timestamp.
1064
- * Used for backtest progression and live trading updates.
953
+ * Called by ClientStrategy.waitForInit() to restore state.
954
+ * Returns null if no signal exists.
1065
955
  *
1066
- * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
1067
- * @param interval - Candle interval (e.g., "1h", "1d")
1068
- * @param limit - Maximum number of candles to fetch
1069
- * @returns Promise resolving to array of candle data
956
+ * @param symbol - Trading pair symbol
957
+ * @param strategyName - Strategy identifier
958
+ * @param exchangeName - Exchange identifier
959
+ * @returns Promise resolving to signal or null
1070
960
  */
1071
- this.getNextCandles = async (symbol, interval, limit) => {
1072
- this.loggerService.log("exchangeConnectionService getNextCandles", {
1073
- symbol,
1074
- interval,
1075
- limit,
1076
- });
1077
- return await this.getExchange(this.methodContextService.context.exchangeName).getNextCandles(symbol, interval, limit);
961
+ this.readSignalData = async (symbol, strategyName, exchangeName) => {
962
+ bt.loggerService.info(PERSIST_SIGNAL_UTILS_METHOD_NAME_READ_DATA);
963
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
964
+ const isInitial = !this.getSignalStorage.has(key);
965
+ const stateStorage = this.getSignalStorage(symbol, strategyName, exchangeName);
966
+ await stateStorage.waitForInit(isInitial);
967
+ if (await stateStorage.hasValue(symbol)) {
968
+ return await stateStorage.readValue(symbol);
969
+ }
970
+ return null;
1078
971
  };
1079
972
  /**
1080
- * Retrieves current average price for symbol.
973
+ * Writes signal data to disk with atomic file writes.
1081
974
  *
1082
- * In live mode: fetches real-time average price from exchange API.
1083
- * In backtest mode: calculates VWAP from candles in current timeframe.
975
+ * Called by ClientStrategy.setPendingSignal() to persist state.
976
+ * Uses atomic writes to prevent corruption on crashes.
1084
977
  *
1085
- * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
1086
- * @returns Promise resolving to average price
978
+ * @param signalRow - Signal data (null to clear)
979
+ * @param symbol - Trading pair symbol
980
+ * @param strategyName - Strategy identifier
981
+ * @param exchangeName - Exchange identifier
982
+ * @returns Promise that resolves when write is complete
1087
983
  */
1088
- this.getAveragePrice = async (symbol) => {
1089
- this.loggerService.log("exchangeConnectionService getAveragePrice", {
1090
- symbol,
1091
- });
1092
- return await this.getExchange(this.methodContextService.context.exchangeName).getAveragePrice(symbol);
1093
- };
1094
- /**
1095
- * Formats price according to exchange-specific precision rules.
1096
- *
1097
- * Ensures price meets exchange requirements for decimal places and tick size.
1098
- *
1099
- * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
1100
- * @param price - Raw price value to format
1101
- * @returns Promise resolving to formatted price string
1102
- */
1103
- this.formatPrice = async (symbol, price) => {
1104
- this.loggerService.log("exchangeConnectionService getAveragePrice", {
1105
- symbol,
1106
- price,
1107
- });
1108
- return await this.getExchange(this.methodContextService.context.exchangeName).formatPrice(symbol, price);
984
+ this.writeSignalData = async (signalRow, symbol, strategyName, exchangeName) => {
985
+ bt.loggerService.info(PERSIST_SIGNAL_UTILS_METHOD_NAME_WRITE_DATA);
986
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
987
+ const isInitial = !this.getSignalStorage.has(key);
988
+ const stateStorage = this.getSignalStorage(symbol, strategyName, exchangeName);
989
+ await stateStorage.waitForInit(isInitial);
990
+ await stateStorage.writeValue(symbol, signalRow);
1109
991
  };
992
+ }
993
+ /**
994
+ * Registers a custom persistence adapter.
995
+ *
996
+ * @param Ctor - Custom PersistBase constructor
997
+ *
998
+ * @example
999
+ * ```typescript
1000
+ * class RedisPersist extends PersistBase {
1001
+ * async readValue(id) { return JSON.parse(await redis.get(id)); }
1002
+ * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1003
+ * }
1004
+ * PersistSignalAdapter.usePersistSignalAdapter(RedisPersist);
1005
+ * ```
1006
+ */
1007
+ usePersistSignalAdapter(Ctor) {
1008
+ bt.loggerService.info(PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_PERSIST_SIGNAL_ADAPTER);
1009
+ this.PersistSignalFactory = Ctor;
1010
+ }
1011
+ /**
1012
+ * Switches to the default JSON persist adapter.
1013
+ * All future persistence writes will use JSON storage.
1014
+ */
1015
+ useJson() {
1016
+ bt.loggerService.log(PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_JSON);
1017
+ this.usePersistSignalAdapter(PersistBase);
1018
+ }
1019
+ /**
1020
+ * Switches to a dummy persist adapter that discards all writes.
1021
+ * All future persistence writes will be no-ops.
1022
+ */
1023
+ useDummy() {
1024
+ bt.loggerService.log(PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_DUMMY);
1025
+ this.usePersistSignalAdapter(PersistDummy);
1026
+ }
1027
+ }
1028
+ /**
1029
+ * Global singleton instance of PersistSignalUtils.
1030
+ * Used by ClientStrategy for signal persistence.
1031
+ *
1032
+ * @example
1033
+ * ```typescript
1034
+ * // Custom adapter
1035
+ * PersistSignalAdapter.usePersistSignalAdapter(RedisPersist);
1036
+ *
1037
+ * // Read signal
1038
+ * const signal = await PersistSignalAdapter.readSignalData("my-strategy", "BTCUSDT");
1039
+ *
1040
+ * // Write signal
1041
+ * await PersistSignalAdapter.writeSignalData(signal, "my-strategy", "BTCUSDT");
1042
+ * ```
1043
+ */
1044
+ const PersistSignalAdapter = new PersistSignalUtils();
1045
+ /**
1046
+ * Utility class for managing risk active positions persistence.
1047
+ *
1048
+ * Features:
1049
+ * - Memoized storage instances per risk profile
1050
+ * - Custom adapter support
1051
+ * - Atomic read/write operations for RiskData
1052
+ * - Crash-safe position state management
1053
+ *
1054
+ * Used by ClientRisk for live mode persistence of active positions.
1055
+ */
1056
+ class PersistRiskUtils {
1057
+ constructor() {
1058
+ this.PersistRiskFactory = PersistBase;
1059
+ this.getRiskStorage = functoolsKit.memoize(([riskName, exchangeName]) => `${riskName}:${exchangeName}`, (riskName, exchangeName) => Reflect.construct(this.PersistRiskFactory, [
1060
+ `${riskName}_${exchangeName}`,
1061
+ `./dump/data/risk/`,
1062
+ ]));
1110
1063
  /**
1111
- * Formats quantity according to exchange-specific precision rules.
1064
+ * Reads persisted active positions for a risk profile.
1112
1065
  *
1113
- * Ensures quantity meets exchange requirements for decimal places and lot size.
1066
+ * Called by ClientRisk.waitForInit() to restore state.
1067
+ * Returns empty Map if no positions exist.
1114
1068
  *
1115
- * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
1116
- * @param quantity - Raw quantity value to format
1117
- * @returns Promise resolving to formatted quantity string
1069
+ * @param riskName - Risk profile identifier
1070
+ * @param exchangeName - Exchange identifier
1071
+ * @returns Promise resolving to Map of active positions
1118
1072
  */
1119
- this.formatQuantity = async (symbol, quantity) => {
1120
- this.loggerService.log("exchangeConnectionService getAveragePrice", {
1121
- symbol,
1122
- quantity,
1123
- });
1124
- return await this.getExchange(this.methodContextService.context.exchangeName).formatQuantity(symbol, quantity);
1073
+ this.readPositionData = async (riskName, exchangeName) => {
1074
+ bt.loggerService.info(PERSIST_RISK_UTILS_METHOD_NAME_READ_DATA);
1075
+ const key = `${riskName}:${exchangeName}`;
1076
+ const isInitial = !this.getRiskStorage.has(key);
1077
+ const stateStorage = this.getRiskStorage(riskName, exchangeName);
1078
+ await stateStorage.waitForInit(isInitial);
1079
+ const RISK_STORAGE_KEY = "positions";
1080
+ if (await stateStorage.hasValue(RISK_STORAGE_KEY)) {
1081
+ return await stateStorage.readValue(RISK_STORAGE_KEY);
1082
+ }
1083
+ return [];
1125
1084
  };
1126
1085
  /**
1127
- * Fetches order book for a trading pair using configured exchange.
1086
+ * Writes active positions to disk with atomic file writes.
1128
1087
  *
1129
- * Routes to exchange determined by methodContextService.context.exchangeName.
1130
- * The ClientExchange will calculate time range and pass it to the schema
1131
- * implementation, which may use (backtest) or ignore (live) the parameters.
1088
+ * Called by ClientRisk after addSignal/removeSignal to persist state.
1089
+ * Uses atomic writes to prevent corruption on crashes.
1132
1090
  *
1133
- * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
1134
- * @param depth - Maximum depth levels (default: CC_ORDER_BOOK_MAX_DEPTH_LEVELS)
1135
- * @returns Promise resolving to order book data
1091
+ * @param positions - Map of active positions
1092
+ * @param riskName - Risk profile identifier
1093
+ * @param exchangeName - Exchange identifier
1094
+ * @returns Promise that resolves when write is complete
1136
1095
  */
1137
- this.getOrderBook = async (symbol, depth) => {
1138
- this.loggerService.log("exchangeConnectionService getOrderBook", {
1139
- symbol,
1140
- depth,
1141
- });
1142
- return await this.getExchange(this.methodContextService.context.exchangeName).getOrderBook(symbol, depth);
1096
+ this.writePositionData = async (riskRow, riskName, exchangeName) => {
1097
+ bt.loggerService.info(PERSIST_RISK_UTILS_METHOD_NAME_WRITE_DATA);
1098
+ const key = `${riskName}:${exchangeName}`;
1099
+ const isInitial = !this.getRiskStorage.has(key);
1100
+ const stateStorage = this.getRiskStorage(riskName, exchangeName);
1101
+ await stateStorage.waitForInit(isInitial);
1102
+ const RISK_STORAGE_KEY = "positions";
1103
+ await stateStorage.writeValue(RISK_STORAGE_KEY, riskRow);
1143
1104
  };
1144
1105
  }
1106
+ /**
1107
+ * Registers a custom persistence adapter.
1108
+ *
1109
+ * @param Ctor - Custom PersistBase constructor
1110
+ *
1111
+ * @example
1112
+ * ```typescript
1113
+ * class RedisPersist extends PersistBase {
1114
+ * async readValue(id) { return JSON.parse(await redis.get(id)); }
1115
+ * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1116
+ * }
1117
+ * PersistRiskAdapter.usePersistRiskAdapter(RedisPersist);
1118
+ * ```
1119
+ */
1120
+ usePersistRiskAdapter(Ctor) {
1121
+ bt.loggerService.info(PERSIST_RISK_UTILS_METHOD_NAME_USE_PERSIST_RISK_ADAPTER);
1122
+ this.PersistRiskFactory = Ctor;
1123
+ }
1124
+ /**
1125
+ * Switches to the default JSON persist adapter.
1126
+ * All future persistence writes will use JSON storage.
1127
+ */
1128
+ useJson() {
1129
+ bt.loggerService.log(PERSIST_RISK_UTILS_METHOD_NAME_USE_JSON);
1130
+ this.usePersistRiskAdapter(PersistBase);
1131
+ }
1132
+ /**
1133
+ * Switches to a dummy persist adapter that discards all writes.
1134
+ * All future persistence writes will be no-ops.
1135
+ */
1136
+ useDummy() {
1137
+ bt.loggerService.log(PERSIST_RISK_UTILS_METHOD_NAME_USE_DUMMY);
1138
+ this.usePersistRiskAdapter(PersistDummy);
1139
+ }
1145
1140
  }
1146
-
1147
1141
  /**
1148
- * Calculates profit/loss for a closed signal with slippage and fees.
1149
- *
1150
- * For signals with partial closes:
1151
- * - Calculates weighted PNL: Σ(percent_i × pnl_i) for each partial + (remaining% × final_pnl)
1152
- * - Each partial close has its own fees and slippage
1153
- * - Total fees = 2 × (number of partial closes + 1 final close) × CC_PERCENT_FEE
1154
- *
1155
- * Formula breakdown:
1156
- * 1. Apply slippage to open/close prices (worse execution)
1157
- * - LONG: buy higher (+slippage), sell lower (-slippage)
1158
- * - SHORT: sell lower (-slippage), buy higher (+slippage)
1159
- * 2. Calculate raw PNL percentage
1160
- * - LONG: ((closePrice - openPrice) / openPrice) * 100
1161
- * - SHORT: ((openPrice - closePrice) / openPrice) * 100
1162
- * 3. Subtract total fees (0.1% * 2 = 0.2% per transaction)
1163
- *
1164
- * @param signal - Closed signal with position details and optional partial history
1165
- * @param priceClose - Actual close price at final exit
1166
- * @returns PNL data with percentage and prices
1142
+ * Global singleton instance of PersistRiskUtils.
1143
+ * Used by ClientRisk for active positions persistence.
1167
1144
  *
1168
1145
  * @example
1169
1146
  * ```typescript
1170
- * // Signal without partial closes
1171
- * const pnl = toProfitLossDto(
1172
- * {
1173
- * position: "long",
1174
- * priceOpen: 100,
1175
- * },
1176
- * 110 // close at +10%
1177
- * );
1178
- * console.log(pnl.pnlPercentage); // ~9.6% (after slippage and fees)
1147
+ * // Custom adapter
1148
+ * PersistRiskAdapter.usePersistRiskAdapter(RedisPersist);
1179
1149
  *
1180
- * // Signal with partial closes
1181
- * const pnlPartial = toProfitLossDto(
1182
- * {
1183
- * position: "long",
1184
- * priceOpen: 100,
1185
- * _partial: [
1186
- * { type: "profit", percent: 30, price: 120 }, // +20% on 30%
1187
- * { type: "profit", percent: 40, price: 115 }, // +15% on 40%
1188
- * ],
1189
- * },
1190
- * 105 // final close at +5% for remaining 30%
1191
- * );
1192
- * // Weighted PNL = 30% × 20% + 40% × 15% + 30% × 5% = 6% + 6% + 1.5% = 13.5% (before fees)
1150
+ * // Read positions
1151
+ * const positions = await PersistRiskAdapter.readPositionData("my-risk");
1152
+ *
1153
+ * // Write positions
1154
+ * await PersistRiskAdapter.writePositionData(positionsMap, "my-risk");
1193
1155
  * ```
1194
1156
  */
1195
- const toProfitLossDto = (signal, priceClose) => {
1196
- const priceOpen = signal.priceOpen;
1197
- // Calculate weighted PNL with partial closes
1198
- if (signal._partial && signal._partial.length > 0) {
1199
- let totalWeightedPnl = 0;
1200
- let totalFees = 0;
1201
- // Calculate PNL for each partial close
1202
- for (const partial of signal._partial) {
1203
- const partialPercent = partial.percent;
1204
- const partialPrice = partial.price;
1205
- // Apply slippage to prices
1206
- let priceOpenWithSlippage;
1207
- let priceCloseWithSlippage;
1208
- if (signal.position === "long") {
1209
- priceOpenWithSlippage = priceOpen * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1210
- priceCloseWithSlippage = partialPrice * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1211
- }
1212
- else {
1213
- priceOpenWithSlippage = priceOpen * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1214
- priceCloseWithSlippage = partialPrice * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1215
- }
1216
- // Calculate PNL for this partial
1217
- let partialPnl;
1218
- if (signal.position === "long") {
1219
- partialPnl = ((priceCloseWithSlippage - priceOpenWithSlippage) / priceOpenWithSlippage) * 100;
1220
- }
1221
- else {
1222
- partialPnl = ((priceOpenWithSlippage - priceCloseWithSlippage) / priceOpenWithSlippage) * 100;
1223
- }
1224
- // Weight by percentage of position closed
1225
- const weightedPnl = (partialPercent / 100) * partialPnl;
1226
- totalWeightedPnl += weightedPnl;
1227
- // Each partial has fees for open + close (2 transactions)
1228
- totalFees += GLOBAL_CONFIG.CC_PERCENT_FEE * 2;
1229
- }
1230
- // Calculate PNL for remaining position (if any)
1231
- // Compute totalClosed from _partial array
1232
- const totalClosed = signal._partial.reduce((sum, p) => sum + p.percent, 0);
1233
- const remainingPercent = 100 - totalClosed;
1234
- if (remainingPercent > 0) {
1235
- // Apply slippage
1236
- let priceOpenWithSlippage;
1237
- let priceCloseWithSlippage;
1238
- if (signal.position === "long") {
1239
- priceOpenWithSlippage = priceOpen * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1240
- priceCloseWithSlippage = priceClose * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1241
- }
1242
- else {
1243
- priceOpenWithSlippage = priceOpen * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1244
- priceCloseWithSlippage = priceClose * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1245
- }
1246
- // Calculate PNL for remaining
1247
- let remainingPnl;
1248
- if (signal.position === "long") {
1249
- remainingPnl = ((priceCloseWithSlippage - priceOpenWithSlippage) / priceOpenWithSlippage) * 100;
1250
- }
1251
- else {
1252
- remainingPnl = ((priceOpenWithSlippage - priceCloseWithSlippage) / priceOpenWithSlippage) * 100;
1157
+ const PersistRiskAdapter = new PersistRiskUtils();
1158
+ /**
1159
+ * Utility class for managing scheduled signal persistence.
1160
+ *
1161
+ * Features:
1162
+ * - Memoized storage instances per strategy
1163
+ * - Custom adapter support
1164
+ * - Atomic read/write operations for scheduled signals
1165
+ * - Crash-safe scheduled signal state management
1166
+ *
1167
+ * Used by ClientStrategy for live mode persistence of scheduled signals (_scheduledSignal).
1168
+ */
1169
+ class PersistScheduleUtils {
1170
+ constructor() {
1171
+ this.PersistScheduleFactory = PersistBase;
1172
+ this.getScheduleStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistScheduleFactory, [
1173
+ `${symbol}_${strategyName}_${exchangeName}`,
1174
+ `./dump/data/schedule/`,
1175
+ ]));
1176
+ /**
1177
+ * Reads persisted scheduled signal data for a symbol and strategy.
1178
+ *
1179
+ * Called by ClientStrategy.waitForInit() to restore scheduled signal state.
1180
+ * Returns null if no scheduled signal exists.
1181
+ *
1182
+ * @param symbol - Trading pair symbol
1183
+ * @param strategyName - Strategy identifier
1184
+ * @param exchangeName - Exchange identifier
1185
+ * @returns Promise resolving to scheduled signal or null
1186
+ */
1187
+ this.readScheduleData = async (symbol, strategyName, exchangeName) => {
1188
+ bt.loggerService.info(PERSIST_SCHEDULE_UTILS_METHOD_NAME_READ_DATA);
1189
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
1190
+ const isInitial = !this.getScheduleStorage.has(key);
1191
+ const stateStorage = this.getScheduleStorage(symbol, strategyName, exchangeName);
1192
+ await stateStorage.waitForInit(isInitial);
1193
+ if (await stateStorage.hasValue(symbol)) {
1194
+ return await stateStorage.readValue(symbol);
1253
1195
  }
1254
- // Weight by remaining percentage
1255
- const weightedRemainingPnl = (remainingPercent / 100) * remainingPnl;
1256
- totalWeightedPnl += weightedRemainingPnl;
1257
- // Final close also has fees
1258
- totalFees += GLOBAL_CONFIG.CC_PERCENT_FEE * 2;
1259
- }
1260
- // Subtract total fees from weighted PNL
1261
- const pnlPercentage = totalWeightedPnl - totalFees;
1262
- return {
1263
- pnlPercentage,
1264
- priceOpen,
1265
- priceClose,
1196
+ return null;
1197
+ };
1198
+ /**
1199
+ * Writes scheduled signal data to disk with atomic file writes.
1200
+ *
1201
+ * Called by ClientStrategy.setScheduledSignal() to persist state.
1202
+ * Uses atomic writes to prevent corruption on crashes.
1203
+ *
1204
+ * @param scheduledSignalRow - Scheduled signal data (null to clear)
1205
+ * @param symbol - Trading pair symbol
1206
+ * @param strategyName - Strategy identifier
1207
+ * @param exchangeName - Exchange identifier
1208
+ * @returns Promise that resolves when write is complete
1209
+ */
1210
+ this.writeScheduleData = async (scheduledSignalRow, symbol, strategyName, exchangeName) => {
1211
+ bt.loggerService.info(PERSIST_SCHEDULE_UTILS_METHOD_NAME_WRITE_DATA);
1212
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
1213
+ const isInitial = !this.getScheduleStorage.has(key);
1214
+ const stateStorage = this.getScheduleStorage(symbol, strategyName, exchangeName);
1215
+ await stateStorage.waitForInit(isInitial);
1216
+ await stateStorage.writeValue(symbol, scheduledSignalRow);
1266
1217
  };
1267
1218
  }
1268
- // Original logic for signals without partial closes
1269
- let priceOpenWithSlippage;
1270
- let priceCloseWithSlippage;
1271
- if (signal.position === "long") {
1272
- // LONG: покупаем дороже, продаем дешевле
1273
- priceOpenWithSlippage = priceOpen * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1274
- priceCloseWithSlippage = priceClose * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1275
- }
1276
- else {
1277
- // SHORT: продаем дешевле, покупаем дороже
1278
- priceOpenWithSlippage = priceOpen * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1279
- priceCloseWithSlippage = priceClose * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
1219
+ /**
1220
+ * Registers a custom persistence adapter.
1221
+ *
1222
+ * @param Ctor - Custom PersistBase constructor
1223
+ *
1224
+ * @example
1225
+ * ```typescript
1226
+ * class RedisPersist extends PersistBase {
1227
+ * async readValue(id) { return JSON.parse(await redis.get(id)); }
1228
+ * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1229
+ * }
1230
+ * PersistScheduleAdapter.usePersistScheduleAdapter(RedisPersist);
1231
+ * ```
1232
+ */
1233
+ usePersistScheduleAdapter(Ctor) {
1234
+ bt.loggerService.info(PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_PERSIST_SCHEDULE_ADAPTER);
1235
+ this.PersistScheduleFactory = Ctor;
1280
1236
  }
1281
- // Применяем комиссию дважды (при открытии и закрытии)
1282
- const totalFee = GLOBAL_CONFIG.CC_PERCENT_FEE * 2;
1283
- let pnlPercentage;
1284
- if (signal.position === "long") {
1285
- // LONG: прибыль при росте цены
1286
- pnlPercentage =
1287
- ((priceCloseWithSlippage - priceOpenWithSlippage) /
1288
- priceOpenWithSlippage) *
1289
- 100;
1237
+ /**
1238
+ * Switches to the default JSON persist adapter.
1239
+ * All future persistence writes will use JSON storage.
1240
+ */
1241
+ useJson() {
1242
+ bt.loggerService.log(PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_JSON);
1243
+ this.usePersistScheduleAdapter(PersistBase);
1290
1244
  }
1291
- else {
1292
- // SHORT: прибыль при падении цены
1293
- pnlPercentage =
1294
- ((priceOpenWithSlippage - priceCloseWithSlippage) /
1295
- priceOpenWithSlippage) *
1296
- 100;
1245
+ /**
1246
+ * Switches to a dummy persist adapter that discards all writes.
1247
+ * All future persistence writes will be no-ops.
1248
+ */
1249
+ useDummy() {
1250
+ bt.loggerService.log(PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_DUMMY);
1251
+ this.usePersistScheduleAdapter(PersistDummy);
1297
1252
  }
1298
- // Вычитаем комиссии
1299
- pnlPercentage -= totalFee;
1300
- return {
1301
- pnlPercentage,
1302
- priceOpen,
1303
- priceClose,
1304
- };
1305
- };
1306
-
1307
- const IS_WINDOWS = os.platform() === "win32";
1253
+ }
1308
1254
  /**
1309
- * Atomically writes data to a file, ensuring the operation either fully completes or leaves the original file unchanged.
1310
- * Uses a temporary file with a rename strategy on POSIX systems for atomicity, or direct writing with sync on Windows (or when POSIX rename is skipped).
1311
- *
1312
- *
1313
- * @param {string} file - The file parameter.
1314
- * @param {string | Buffer} data - The data to be processed or validated.
1315
- * @param {Options | BufferEncoding} options - The options parameter (optional).
1316
- * @throws {Error} Throws an error if the write, sync, or rename operation fails, after attempting cleanup of temporary files.
1317
- *
1318
- * @example
1319
- * // Basic usage with default options
1320
- * await writeFileAtomic("output.txt", "Hello, world!");
1321
- * // Writes "Hello, world!" to "output.txt" atomically
1322
- *
1323
- * @example
1324
- * // Custom options and Buffer data
1325
- * const buffer = Buffer.from("Binary data");
1326
- * await writeFileAtomic("data.bin", buffer, { encoding: "binary", mode: 0o644, tmpPrefix: "temp-" });
1327
- * // Writes binary data to "data.bin" with custom permissions and temp prefix
1255
+ * Global singleton instance of PersistScheduleUtils.
1256
+ * Used by ClientStrategy for scheduled signal persistence.
1328
1257
  *
1329
1258
  * @example
1330
- * // Using encoding shorthand
1331
- * await writeFileAtomic("log.txt", "Log entry", "utf16le");
1332
- * // Writes "Log entry" to "log.txt" in UTF-16LE encoding
1259
+ * ```typescript
1260
+ * // Custom adapter
1261
+ * PersistScheduleAdapter.usePersistScheduleAdapter(RedisPersist);
1333
1262
  *
1334
- * @remarks
1335
- * This function ensures atomicity to prevent partial writes:
1336
- * - On POSIX systems (non-Windows, unless `GLOBAL_CONFIG.CC_SKIP_POSIX_RENAME` is true):
1337
- * - Writes data to a temporary file (e.g., `.tmp-<random>-filename`) in the same directory.
1338
- * - Uses `crypto.randomBytes` to generate a unique temporary name, reducing collision risk.
1339
- * - Syncs the data to disk and renames the temporary file to the target file atomically with `fs.rename`.
1340
- * - Cleans up the temporary file on failure, swallowing cleanup errors to prioritize throwing the original error.
1341
- * - On Windows (or when POSIX rename is skipped):
1342
- * - Writes directly to the target file, syncing data to disk to minimize corruption risk (though not fully atomic).
1343
- * - Closes the file handle on failure without additional cleanup.
1344
- * - Accepts `options` as an object or a string (interpreted as `encoding`), defaulting to `{ encoding: "utf8", mode: 0o666, tmpPrefix: ".tmp-" }`.
1345
- * Useful in the agent swarm system for safely writing configuration files, logs, or state data where partial writes could cause corruption.
1263
+ * // Read scheduled signal
1264
+ * const scheduled = await PersistScheduleAdapter.readScheduleData("my-strategy", "BTCUSDT");
1346
1265
  *
1347
- * @see {@link https://nodejs.org/api/fs.html#fspromiseswritefilefile-data-options|fs.promises.writeFile} for file writing details.
1348
- * @see {@link https://nodejs.org/api/crypto.html#cryptorandombytessize-callback|crypto.randomBytes} for temporary file naming.
1349
- * @see {@link ../config/params|GLOBAL_CONFIG} for configuration impacting POSIX behavior.
1350
- */
1351
- async function writeFileAtomic(file, data, options = {}) {
1352
- if (typeof options === "string") {
1353
- options = { encoding: options };
1354
- }
1355
- else if (!options) {
1356
- options = {};
1357
- }
1358
- const { encoding = "utf8", mode = 0o666, tmpPrefix = ".tmp-" } = options;
1359
- let fileHandle = null;
1360
- if (IS_WINDOWS) {
1361
- try {
1362
- // Create and write to temporary file
1363
- fileHandle = await fs.open(file, "w", mode);
1364
- // Write data to the temp file
1365
- await fileHandle.writeFile(data, { encoding });
1366
- // Ensure data is flushed to disk
1367
- await fileHandle.sync();
1368
- // Close the file before rename
1369
- await fileHandle.close();
1370
- }
1371
- catch (error) {
1372
- // Clean up if something went wrong
1373
- if (fileHandle) {
1374
- await fileHandle.close().catch(() => { });
1375
- }
1376
- throw error; // Re-throw the original error
1377
- }
1378
- return;
1379
- }
1380
- // Create a temporary filename in the same directory
1381
- const dir = path.dirname(file);
1382
- const filename = path.basename(file);
1383
- const tmpFile = path.join(dir, `${tmpPrefix}${crypto.randomBytes(6).toString("hex")}-${filename}`);
1384
- try {
1385
- // Create and write to temporary file
1386
- fileHandle = await fs.open(tmpFile, "w", mode);
1387
- // Write data to the temp file
1388
- await fileHandle.writeFile(data, { encoding });
1389
- // Ensure data is flushed to disk
1390
- await fileHandle.sync();
1391
- // Close the file before rename
1392
- await fileHandle.close();
1393
- fileHandle = null;
1394
- // Atomically replace the target file with our temp file
1395
- await fs.rename(tmpFile, file);
1396
- }
1397
- catch (error) {
1398
- // Clean up if something went wrong
1399
- if (fileHandle) {
1400
- await fileHandle.close().catch(() => { });
1401
- }
1402
- // Try to remove the temporary file
1403
- try {
1404
- await fs.unlink(tmpFile).catch(() => { });
1405
- }
1406
- catch (_) {
1407
- // Ignore errors during cleanup
1408
- }
1409
- throw error;
1410
- }
1411
- }
1412
-
1413
- var _a$2;
1414
- const BASE_WAIT_FOR_INIT_SYMBOL = Symbol("wait-for-init");
1415
- const PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_PERSIST_SIGNAL_ADAPTER = "PersistSignalUtils.usePersistSignalAdapter";
1416
- const PERSIST_SIGNAL_UTILS_METHOD_NAME_READ_DATA = "PersistSignalUtils.readSignalData";
1417
- const PERSIST_SIGNAL_UTILS_METHOD_NAME_WRITE_DATA = "PersistSignalUtils.writeSignalData";
1418
- const PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_JSON = "PersistSignalUtils.useJson";
1419
- const PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_DUMMY = "PersistSignalUtils.useDummy";
1420
- const PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_PERSIST_SCHEDULE_ADAPTER = "PersistScheduleUtils.usePersistScheduleAdapter";
1421
- const PERSIST_SCHEDULE_UTILS_METHOD_NAME_READ_DATA = "PersistScheduleUtils.readScheduleData";
1422
- const PERSIST_SCHEDULE_UTILS_METHOD_NAME_WRITE_DATA = "PersistScheduleUtils.writeScheduleData";
1423
- const PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_JSON = "PersistScheduleUtils.useJson";
1424
- const PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_DUMMY = "PersistScheduleUtils.useDummy";
1425
- const PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_PERSIST_PARTIAL_ADAPTER = "PersistPartialUtils.usePersistPartialAdapter";
1426
- const PERSIST_PARTIAL_UTILS_METHOD_NAME_READ_DATA = "PersistPartialUtils.readPartialData";
1427
- const PERSIST_PARTIAL_UTILS_METHOD_NAME_WRITE_DATA = "PersistPartialUtils.writePartialData";
1428
- const PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_JSON = "PersistPartialUtils.useJson";
1429
- const PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_DUMMY = "PersistPartialUtils.useDummy";
1430
- const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_PERSIST_BREAKEVEN_ADAPTER = "PersistBreakevenUtils.usePersistBreakevenAdapter";
1431
- const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_READ_DATA = "PersistBreakevenUtils.readBreakevenData";
1432
- const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_WRITE_DATA = "PersistBreakevenUtils.writeBreakevenData";
1433
- const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_JSON = "PersistBreakevenUtils.useJson";
1434
- const PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_DUMMY = "PersistBreakevenUtils.useDummy";
1435
- const PERSIST_RISK_UTILS_METHOD_NAME_USE_PERSIST_RISK_ADAPTER = "PersistRiskUtils.usePersistRiskAdapter";
1436
- const PERSIST_RISK_UTILS_METHOD_NAME_READ_DATA = "PersistRiskUtils.readPositionData";
1437
- const PERSIST_RISK_UTILS_METHOD_NAME_WRITE_DATA = "PersistRiskUtils.writePositionData";
1438
- const PERSIST_RISK_UTILS_METHOD_NAME_USE_JSON = "PersistRiskUtils.useJson";
1439
- const PERSIST_RISK_UTILS_METHOD_NAME_USE_DUMMY = "PersistRiskUtils.useDummy";
1440
- const PERSIST_BASE_METHOD_NAME_CTOR = "PersistBase.CTOR";
1441
- const PERSIST_BASE_METHOD_NAME_WAIT_FOR_INIT = "PersistBase.waitForInit";
1442
- const PERSIST_BASE_METHOD_NAME_READ_VALUE = "PersistBase.readValue";
1443
- const PERSIST_BASE_METHOD_NAME_WRITE_VALUE = "PersistBase.writeValue";
1444
- const PERSIST_BASE_METHOD_NAME_HAS_VALUE = "PersistBase.hasValue";
1445
- const PERSIST_BASE_METHOD_NAME_KEYS = "PersistBase.keys";
1446
- const BASE_WAIT_FOR_INIT_FN_METHOD_NAME = "PersistBase.waitForInitFn";
1447
- const BASE_UNLINK_RETRY_COUNT = 5;
1448
- const BASE_UNLINK_RETRY_DELAY = 1000;
1449
- const BASE_WAIT_FOR_INIT_FN = async (self) => {
1450
- bt.loggerService.debug(BASE_WAIT_FOR_INIT_FN_METHOD_NAME, {
1451
- entityName: self.entityName,
1452
- directory: self._directory,
1453
- });
1454
- await fs.mkdir(self._directory, { recursive: true });
1455
- for await (const key of self.keys()) {
1456
- try {
1457
- await self.readValue(key);
1458
- }
1459
- catch {
1460
- const filePath = self._getFilePath(key);
1461
- console.error(`backtest-kit PersistBase found invalid document for filePath=${filePath} entityName=${self.entityName}`);
1462
- if (await functoolsKit.not(BASE_WAIT_FOR_INIT_UNLINK_FN(filePath))) {
1463
- console.error(`backtest-kit PersistBase failed to remove invalid document for filePath=${filePath} entityName=${self.entityName}`);
1464
- }
1465
- }
1466
- }
1467
- };
1468
- const BASE_WAIT_FOR_INIT_UNLINK_FN = async (filePath) => functoolsKit.trycatch(functoolsKit.retry(async () => {
1469
- try {
1470
- await fs.unlink(filePath);
1471
- return true;
1472
- }
1473
- catch (error) {
1474
- console.error(`backtest-kit PersistBase unlink failed for filePath=${filePath} error=${functoolsKit.getErrorMessage(error)}`);
1475
- throw error;
1476
- }
1477
- }, BASE_UNLINK_RETRY_COUNT, BASE_UNLINK_RETRY_DELAY), {
1478
- defaultValue: false,
1479
- });
1266
+ * // Write scheduled signal
1267
+ * await PersistScheduleAdapter.writeScheduleData(scheduled, "my-strategy", "BTCUSDT");
1268
+ * ```
1269
+ */
1270
+ const PersistScheduleAdapter = new PersistScheduleUtils();
1480
1271
  /**
1481
- * Base class for file-based persistence with atomic writes.
1272
+ * Utility class for managing partial profit/loss levels persistence.
1482
1273
  *
1483
1274
  * Features:
1484
- * - Atomic file writes using writeFileAtomic
1485
- * - Auto-validation and cleanup of corrupted files
1486
- * - Async generator support for iteration
1487
- * - Retry logic for file deletion
1275
+ * - Memoized storage instances per symbol:strategyName
1276
+ * - Custom adapter support
1277
+ * - Atomic read/write operations for partial data
1278
+ * - Crash-safe partial state management
1488
1279
  *
1489
- * @example
1490
- * ```typescript
1491
- * const persist = new PersistBase("my-entity", "./data");
1492
- * await persist.waitForInit(true);
1493
- * await persist.writeValue("key1", { data: "value" });
1494
- * const value = await persist.readValue("key1");
1495
- * ```
1280
+ * Used by ClientPartial for live mode persistence of profit/loss levels.
1496
1281
  */
1497
- class PersistBase {
1498
- /**
1499
- * Creates new persistence instance.
1500
- *
1501
- * @param entityName - Unique entity type identifier
1502
- * @param baseDir - Base directory for all entities (default: ./dump/data)
1503
- */
1504
- constructor(entityName, baseDir = path.join(process.cwd(), "logs/data")) {
1505
- this.entityName = entityName;
1506
- this.baseDir = baseDir;
1507
- this[_a$2] = functoolsKit.singleshot(async () => await BASE_WAIT_FOR_INIT_FN(this));
1508
- bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_CTOR, {
1509
- entityName: this.entityName,
1510
- baseDir,
1511
- });
1512
- this._directory = path.join(this.baseDir, this.entityName);
1513
- }
1514
- /**
1515
- * Computes file path for entity ID.
1516
- *
1517
- * @param entityId - Entity identifier
1518
- * @returns Full file path to entity JSON file
1519
- */
1520
- _getFilePath(entityId) {
1521
- return path.join(this.baseDir, this.entityName, `${entityId}.json`);
1522
- }
1523
- async waitForInit(initial) {
1524
- bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_WAIT_FOR_INIT, {
1525
- entityName: this.entityName,
1526
- initial,
1527
- });
1528
- await this[BASE_WAIT_FOR_INIT_SYMBOL]();
1529
- }
1530
- async readValue(entityId) {
1531
- bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_READ_VALUE, {
1532
- entityName: this.entityName,
1533
- entityId,
1534
- });
1535
- try {
1536
- const filePath = this._getFilePath(entityId);
1537
- const fileContent = await fs.readFile(filePath, "utf-8");
1538
- return JSON.parse(fileContent);
1539
- }
1540
- catch (error) {
1541
- if (error?.code === "ENOENT") {
1542
- throw new Error(`Entity ${this.entityName}:${entityId} not found`);
1282
+ class PersistPartialUtils {
1283
+ constructor() {
1284
+ this.PersistPartialFactory = PersistBase;
1285
+ this.getPartialStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistPartialFactory, [
1286
+ `${symbol}_${strategyName}_${exchangeName}`,
1287
+ `./dump/data/partial/`,
1288
+ ]));
1289
+ /**
1290
+ * Reads persisted partial data for a symbol and strategy.
1291
+ *
1292
+ * Called by ClientPartial.waitForInit() to restore state.
1293
+ * Returns empty object if no partial data exists.
1294
+ *
1295
+ * @param symbol - Trading pair symbol
1296
+ * @param strategyName - Strategy identifier
1297
+ * @param signalId - Signal identifier
1298
+ * @param exchangeName - Exchange identifier
1299
+ * @returns Promise resolving to partial data record
1300
+ */
1301
+ this.readPartialData = async (symbol, strategyName, signalId, exchangeName) => {
1302
+ bt.loggerService.info(PERSIST_PARTIAL_UTILS_METHOD_NAME_READ_DATA);
1303
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
1304
+ const isInitial = !this.getPartialStorage.has(key);
1305
+ const stateStorage = this.getPartialStorage(symbol, strategyName, exchangeName);
1306
+ await stateStorage.waitForInit(isInitial);
1307
+ if (await stateStorage.hasValue(signalId)) {
1308
+ return await stateStorage.readValue(signalId);
1543
1309
  }
1544
- throw new Error(`Failed to read entity ${this.entityName}:${entityId}: ${functoolsKit.getErrorMessage(error)}`);
1545
- }
1310
+ return {};
1311
+ };
1312
+ /**
1313
+ * Writes partial data to disk with atomic file writes.
1314
+ *
1315
+ * Called by ClientPartial after profit/loss level changes to persist state.
1316
+ * Uses atomic writes to prevent corruption on crashes.
1317
+ *
1318
+ * @param partialData - Record of signal IDs to partial data
1319
+ * @param symbol - Trading pair symbol
1320
+ * @param strategyName - Strategy identifier
1321
+ * @param signalId - Signal identifier
1322
+ * @param exchangeName - Exchange identifier
1323
+ * @returns Promise that resolves when write is complete
1324
+ */
1325
+ this.writePartialData = async (partialData, symbol, strategyName, signalId, exchangeName) => {
1326
+ bt.loggerService.info(PERSIST_PARTIAL_UTILS_METHOD_NAME_WRITE_DATA);
1327
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
1328
+ const isInitial = !this.getPartialStorage.has(key);
1329
+ const stateStorage = this.getPartialStorage(symbol, strategyName, exchangeName);
1330
+ await stateStorage.waitForInit(isInitial);
1331
+ await stateStorage.writeValue(signalId, partialData);
1332
+ };
1546
1333
  }
1547
- async hasValue(entityId) {
1548
- bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_HAS_VALUE, {
1549
- entityName: this.entityName,
1550
- entityId,
1551
- });
1552
- try {
1553
- const filePath = this._getFilePath(entityId);
1554
- await fs.access(filePath);
1555
- return true;
1556
- }
1557
- catch (error) {
1558
- if (error?.code === "ENOENT") {
1559
- return false;
1560
- }
1561
- throw new Error(`Failed to check existence of entity ${this.entityName}:${entityId}: ${functoolsKit.getErrorMessage(error)}`);
1562
- }
1334
+ /**
1335
+ * Registers a custom persistence adapter.
1336
+ *
1337
+ * @param Ctor - Custom PersistBase constructor
1338
+ *
1339
+ * @example
1340
+ * ```typescript
1341
+ * class RedisPersist extends PersistBase {
1342
+ * async readValue(id) { return JSON.parse(await redis.get(id)); }
1343
+ * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1344
+ * }
1345
+ * PersistPartialAdapter.usePersistPartialAdapter(RedisPersist);
1346
+ * ```
1347
+ */
1348
+ usePersistPartialAdapter(Ctor) {
1349
+ bt.loggerService.info(PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_PERSIST_PARTIAL_ADAPTER);
1350
+ this.PersistPartialFactory = Ctor;
1563
1351
  }
1564
- async writeValue(entityId, entity) {
1565
- bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_WRITE_VALUE, {
1566
- entityName: this.entityName,
1567
- entityId,
1568
- });
1569
- try {
1570
- const filePath = this._getFilePath(entityId);
1571
- const serializedData = JSON.stringify(entity);
1572
- await writeFileAtomic(filePath, serializedData, "utf-8");
1573
- }
1574
- catch (error) {
1575
- throw new Error(`Failed to write entity ${this.entityName}:${entityId}: ${functoolsKit.getErrorMessage(error)}`);
1576
- }
1352
+ /**
1353
+ * Switches to the default JSON persist adapter.
1354
+ * All future persistence writes will use JSON storage.
1355
+ */
1356
+ useJson() {
1357
+ bt.loggerService.log(PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_JSON);
1358
+ this.usePersistPartialAdapter(PersistBase);
1577
1359
  }
1578
1360
  /**
1579
- * Async generator yielding all entity IDs.
1580
- * Sorted alphanumerically.
1581
- * Used internally by waitForInit for validation.
1582
- *
1583
- * @returns AsyncGenerator yielding entity IDs
1584
- * @throws Error if reading fails
1361
+ * Switches to a dummy persist adapter that discards all writes.
1362
+ * All future persistence writes will be no-ops.
1585
1363
  */
1586
- async *keys() {
1587
- bt.loggerService.debug(PERSIST_BASE_METHOD_NAME_KEYS, {
1588
- entityName: this.entityName,
1589
- });
1590
- try {
1591
- const files = await fs.readdir(this._directory);
1592
- const entityIds = files
1593
- .filter((file) => file.endsWith(".json"))
1594
- .map((file) => file.slice(0, -5))
1595
- .sort((a, b) => a.localeCompare(b, undefined, {
1596
- numeric: true,
1597
- sensitivity: "base",
1598
- }));
1599
- for (const entityId of entityIds) {
1600
- yield entityId;
1601
- }
1602
- }
1603
- catch (error) {
1604
- throw new Error(`Failed to read keys for ${this.entityName}: ${functoolsKit.getErrorMessage(error)}`);
1605
- }
1364
+ useDummy() {
1365
+ bt.loggerService.log(PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_DUMMY);
1366
+ this.usePersistPartialAdapter(PersistDummy);
1606
1367
  }
1607
1368
  }
1608
- _a$2 = BASE_WAIT_FOR_INIT_SYMBOL;
1609
- // @ts-ignore
1610
- PersistBase = functoolsKit.makeExtendable(PersistBase);
1611
1369
  /**
1612
- * Dummy persist adapter that discards all writes.
1613
- * Used for disabling persistence.
1370
+ * Global singleton instance of PersistPartialUtils.
1371
+ * Used by ClientPartial for partial profit/loss levels persistence.
1372
+ *
1373
+ * @example
1374
+ * ```typescript
1375
+ * // Custom adapter
1376
+ * PersistPartialAdapter.usePersistPartialAdapter(RedisPersist);
1377
+ *
1378
+ * // Read partial data
1379
+ * const partialData = await PersistPartialAdapter.readPartialData("BTCUSDT", "my-strategy");
1380
+ *
1381
+ * // Write partial data
1382
+ * await PersistPartialAdapter.writePartialData(partialData, "BTCUSDT", "my-strategy");
1383
+ * ```
1614
1384
  */
1615
- class PersistDummy {
1616
- /**
1617
- * No-op initialization function.
1618
- * @returns Promise that resolves immediately
1619
- */
1620
- async waitForInit() {
1385
+ const PersistPartialAdapter = new PersistPartialUtils();
1386
+ /**
1387
+ * Persistence utility class for breakeven state management.
1388
+ *
1389
+ * Handles reading and writing breakeven state to disk.
1390
+ * Uses memoized PersistBase instances per symbol-strategy pair.
1391
+ *
1392
+ * Features:
1393
+ * - Atomic file writes via PersistBase.writeValue()
1394
+ * - Lazy initialization on first access
1395
+ * - Singleton pattern for global access
1396
+ * - Custom adapter support via usePersistBreakevenAdapter()
1397
+ *
1398
+ * File structure:
1399
+ * ```
1400
+ * ./dump/data/breakeven/
1401
+ * ├── BTCUSDT_my-strategy/
1402
+ * │ └── state.json // { "signal-id-1": { reached: true }, ... }
1403
+ * └── ETHUSDT_other-strategy/
1404
+ * └── state.json
1405
+ * ```
1406
+ *
1407
+ * @example
1408
+ * ```typescript
1409
+ * // Read breakeven data
1410
+ * const breakevenData = await PersistBreakevenAdapter.readBreakevenData("BTCUSDT", "my-strategy");
1411
+ * // Returns: { "signal-id": { reached: true }, ... }
1412
+ *
1413
+ * // Write breakeven data
1414
+ * await PersistBreakevenAdapter.writeBreakevenData(breakevenData, "BTCUSDT", "my-strategy");
1415
+ * ```
1416
+ */
1417
+ class PersistBreakevenUtils {
1418
+ constructor() {
1419
+ /**
1420
+ * Factory for creating PersistBase instances.
1421
+ * Can be replaced via usePersistBreakevenAdapter().
1422
+ */
1423
+ this.PersistBreakevenFactory = PersistBase;
1424
+ /**
1425
+ * Memoized storage factory for breakeven data.
1426
+ * Creates one PersistBase instance per symbol-strategy-exchange combination.
1427
+ * Key format: "symbol:strategyName:exchangeName"
1428
+ *
1429
+ * @param symbol - Trading pair symbol
1430
+ * @param strategyName - Strategy identifier
1431
+ * @param exchangeName - Exchange identifier
1432
+ * @returns PersistBase instance for this symbol-strategy-exchange combination
1433
+ */
1434
+ this.getBreakevenStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistBreakevenFactory, [
1435
+ `${symbol}_${strategyName}_${exchangeName}`,
1436
+ `./dump/data/breakeven/`,
1437
+ ]));
1438
+ /**
1439
+ * Reads persisted breakeven data for a symbol and strategy.
1440
+ *
1441
+ * Called by ClientBreakeven.waitForInit() to restore state.
1442
+ * Returns empty object if no breakeven data exists.
1443
+ *
1444
+ * @param symbol - Trading pair symbol
1445
+ * @param strategyName - Strategy identifier
1446
+ * @param signalId - Signal identifier
1447
+ * @param exchangeName - Exchange identifier
1448
+ * @returns Promise resolving to breakeven data record
1449
+ */
1450
+ this.readBreakevenData = async (symbol, strategyName, signalId, exchangeName) => {
1451
+ bt.loggerService.info(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_READ_DATA);
1452
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
1453
+ const isInitial = !this.getBreakevenStorage.has(key);
1454
+ const stateStorage = this.getBreakevenStorage(symbol, strategyName, exchangeName);
1455
+ await stateStorage.waitForInit(isInitial);
1456
+ if (await stateStorage.hasValue(signalId)) {
1457
+ return await stateStorage.readValue(signalId);
1458
+ }
1459
+ return {};
1460
+ };
1461
+ /**
1462
+ * Writes breakeven data to disk.
1463
+ *
1464
+ * Called by ClientBreakeven._persistState() after state changes.
1465
+ * Creates directory and file if they don't exist.
1466
+ * Uses atomic writes to prevent data corruption.
1467
+ *
1468
+ * @param breakevenData - Breakeven data record to persist
1469
+ * @param symbol - Trading pair symbol
1470
+ * @param strategyName - Strategy identifier
1471
+ * @param signalId - Signal identifier
1472
+ * @param exchangeName - Exchange identifier
1473
+ * @returns Promise that resolves when write is complete
1474
+ */
1475
+ this.writeBreakevenData = async (breakevenData, symbol, strategyName, signalId, exchangeName) => {
1476
+ bt.loggerService.info(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_WRITE_DATA);
1477
+ const key = `${symbol}:${strategyName}:${exchangeName}`;
1478
+ const isInitial = !this.getBreakevenStorage.has(key);
1479
+ const stateStorage = this.getBreakevenStorage(symbol, strategyName, exchangeName);
1480
+ await stateStorage.waitForInit(isInitial);
1481
+ await stateStorage.writeValue(signalId, breakevenData);
1482
+ };
1621
1483
  }
1622
1484
  /**
1623
- * No-op read function.
1624
- * @returns Promise that resolves with empty object
1485
+ * Registers a custom persistence adapter.
1486
+ *
1487
+ * @param Ctor - Custom PersistBase constructor
1488
+ *
1489
+ * @example
1490
+ * ```typescript
1491
+ * class RedisPersist extends PersistBase {
1492
+ * async readValue(id) { return JSON.parse(await redis.get(id)); }
1493
+ * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1494
+ * }
1495
+ * PersistBreakevenAdapter.usePersistBreakevenAdapter(RedisPersist);
1496
+ * ```
1625
1497
  */
1626
- async readValue() {
1627
- return {};
1498
+ usePersistBreakevenAdapter(Ctor) {
1499
+ bt.loggerService.info(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_PERSIST_BREAKEVEN_ADAPTER);
1500
+ this.PersistBreakevenFactory = Ctor;
1628
1501
  }
1629
1502
  /**
1630
- * No-op has value check.
1631
- * @returns Promise that resolves to false
1503
+ * Switches to the default JSON persist adapter.
1504
+ * All future persistence writes will use JSON storage.
1632
1505
  */
1633
- async hasValue() {
1634
- return false;
1506
+ useJson() {
1507
+ bt.loggerService.log(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_JSON);
1508
+ this.usePersistBreakevenAdapter(PersistBase);
1635
1509
  }
1636
1510
  /**
1637
- * No-op write function.
1638
- * @returns Promise that resolves immediately
1511
+ * Switches to a dummy persist adapter that discards all writes.
1512
+ * All future persistence writes will be no-ops.
1639
1513
  */
1640
- async writeValue() {
1514
+ useDummy() {
1515
+ bt.loggerService.log(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_DUMMY);
1516
+ this.usePersistBreakevenAdapter(PersistDummy);
1641
1517
  }
1642
1518
  }
1643
1519
  /**
1644
- * Utility class for managing signal persistence.
1520
+ * Global singleton instance of PersistBreakevenUtils.
1521
+ * Used by ClientBreakeven for breakeven state persistence.
1522
+ *
1523
+ * @example
1524
+ * ```typescript
1525
+ * // Custom adapter
1526
+ * PersistBreakevenAdapter.usePersistBreakevenAdapter(RedisPersist);
1527
+ *
1528
+ * // Read breakeven data
1529
+ * const breakevenData = await PersistBreakevenAdapter.readBreakevenData("BTCUSDT", "my-strategy");
1530
+ *
1531
+ * // Write breakeven data
1532
+ * await PersistBreakevenAdapter.writeBreakevenData(breakevenData, "BTCUSDT", "my-strategy");
1533
+ * ```
1534
+ */
1535
+ const PersistBreakevenAdapter = new PersistBreakevenUtils();
1536
+ /**
1537
+ * Utility class for managing candles cache persistence.
1645
1538
  *
1646
1539
  * Features:
1647
- * - Memoized storage instances per strategy
1648
- * - Custom adapter support
1540
+ * - Each candle stored as separate JSON file: ${exchangeName}/${symbol}/${interval}/${timestamp}.json
1541
+ * - Cache validation: returns cached data if file count matches requested limit
1542
+ * - Automatic cache invalidation and refresh when data is incomplete
1649
1543
  * - Atomic read/write operations
1650
- * - Crash-safe signal state management
1651
1544
  *
1652
- * Used by ClientStrategy for live mode persistence.
1545
+ * Used by ClientExchange for candle data caching.
1653
1546
  */
1654
- class PersistSignalUtils {
1547
+ class PersistCandleUtils {
1655
1548
  constructor() {
1656
- this.PersistSignalFactory = PersistBase;
1657
- this.getSignalStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistSignalFactory, [
1658
- `${symbol}_${strategyName}_${exchangeName}`,
1659
- `./dump/data/signal/`,
1549
+ this.PersistCandlesFactory = PersistBase;
1550
+ this.getCandlesStorage = functoolsKit.memoize(([symbol, interval, exchangeName]) => `${symbol}:${interval}:${exchangeName}`, (symbol, interval, exchangeName) => Reflect.construct(this.PersistCandlesFactory, [
1551
+ `${exchangeName}/${symbol}/${interval}`,
1552
+ `./dump/data/candles/`,
1660
1553
  ]));
1661
1554
  /**
1662
- * Reads persisted signal data for a symbol and strategy.
1663
- *
1664
- * Called by ClientStrategy.waitForInit() to restore state.
1665
- * Returns null if no signal exists.
1555
+ * Reads cached candles for a specific exchange, symbol, and interval.
1556
+ * Returns candles only if cache contains exactly the requested limit.
1666
1557
  *
1667
1558
  * @param symbol - Trading pair symbol
1668
- * @param strategyName - Strategy identifier
1559
+ * @param interval - Candle interval
1669
1560
  * @param exchangeName - Exchange identifier
1670
- * @returns Promise resolving to signal or null
1561
+ * @param limit - Number of candles requested
1562
+ * @param sinceTimestamp - Start timestamp (inclusive)
1563
+ * @param untilTimestamp - End timestamp (exclusive)
1564
+ * @returns Promise resolving to array of candles or null if cache is incomplete
1671
1565
  */
1672
- this.readSignalData = async (symbol, strategyName, exchangeName) => {
1673
- bt.loggerService.info(PERSIST_SIGNAL_UTILS_METHOD_NAME_READ_DATA);
1674
- const key = `${symbol}:${strategyName}:${exchangeName}`;
1675
- const isInitial = !this.getSignalStorage.has(key);
1676
- const stateStorage = this.getSignalStorage(symbol, strategyName, exchangeName);
1566
+ this.readCandlesData = async (symbol, interval, exchangeName, limit, sinceTimestamp, untilTimestamp) => {
1567
+ bt.loggerService.info("PersistCandleUtils.readCandlesData", {
1568
+ symbol,
1569
+ interval,
1570
+ exchangeName,
1571
+ limit,
1572
+ sinceTimestamp,
1573
+ untilTimestamp,
1574
+ });
1575
+ const key = `${symbol}:${interval}:${exchangeName}`;
1576
+ const isInitial = !this.getCandlesStorage.has(key);
1577
+ const stateStorage = this.getCandlesStorage(symbol, interval, exchangeName);
1677
1578
  await stateStorage.waitForInit(isInitial);
1678
- if (await stateStorage.hasValue(symbol)) {
1679
- return await stateStorage.readValue(symbol);
1579
+ // Collect all cached candles within the time range
1580
+ const cachedCandles = [];
1581
+ for await (const timestamp of stateStorage.keys()) {
1582
+ const ts = Number(timestamp);
1583
+ if (ts >= sinceTimestamp && ts < untilTimestamp) {
1584
+ try {
1585
+ const candle = await stateStorage.readValue(timestamp);
1586
+ cachedCandles.push(candle);
1587
+ }
1588
+ catch {
1589
+ // Skip invalid candles
1590
+ continue;
1591
+ }
1592
+ }
1680
1593
  }
1681
- return null;
1594
+ // Sort by timestamp ascending
1595
+ cachedCandles.sort((a, b) => a.timestamp - b.timestamp);
1596
+ return cachedCandles;
1682
1597
  };
1683
1598
  /**
1684
- * Writes signal data to disk with atomic file writes.
1685
- *
1686
- * Called by ClientStrategy.setPendingSignal() to persist state.
1687
- * Uses atomic writes to prevent corruption on crashes.
1599
+ * Writes candles to cache with atomic file writes.
1600
+ * Each candle is stored as a separate JSON file named by its timestamp.
1688
1601
  *
1689
- * @param signalRow - Signal data (null to clear)
1602
+ * @param candles - Array of candle data to cache
1690
1603
  * @param symbol - Trading pair symbol
1691
- * @param strategyName - Strategy identifier
1604
+ * @param interval - Candle interval
1692
1605
  * @param exchangeName - Exchange identifier
1693
- * @returns Promise that resolves when write is complete
1606
+ * @returns Promise that resolves when all writes are complete
1694
1607
  */
1695
- this.writeSignalData = async (signalRow, symbol, strategyName, exchangeName) => {
1696
- bt.loggerService.info(PERSIST_SIGNAL_UTILS_METHOD_NAME_WRITE_DATA);
1697
- const key = `${symbol}:${strategyName}:${exchangeName}`;
1698
- const isInitial = !this.getSignalStorage.has(key);
1699
- const stateStorage = this.getSignalStorage(symbol, strategyName, exchangeName);
1608
+ this.writeCandlesData = async (candles, symbol, interval, exchangeName) => {
1609
+ bt.loggerService.info("PersistCandleUtils.writeCandlesData", {
1610
+ symbol,
1611
+ interval,
1612
+ exchangeName,
1613
+ candleCount: candles.length,
1614
+ });
1615
+ const key = `${symbol}:${interval}:${exchangeName}`;
1616
+ const isInitial = !this.getCandlesStorage.has(key);
1617
+ const stateStorage = this.getCandlesStorage(symbol, interval, exchangeName);
1700
1618
  await stateStorage.waitForInit(isInitial);
1701
- await stateStorage.writeValue(symbol, signalRow);
1619
+ // Write each candle as a separate file
1620
+ for (const candle of candles) {
1621
+ if (await functoolsKit.not(stateStorage.hasValue(String(candle.timestamp)))) {
1622
+ await stateStorage.writeValue(String(candle.timestamp), candle);
1623
+ }
1624
+ }
1702
1625
  };
1703
1626
  }
1704
1627
  /**
1705
1628
  * Registers a custom persistence adapter.
1706
1629
  *
1707
1630
  * @param Ctor - Custom PersistBase constructor
1708
- *
1709
- * @example
1710
- * ```typescript
1711
- * class RedisPersist extends PersistBase {
1712
- * async readValue(id) { return JSON.parse(await redis.get(id)); }
1713
- * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1714
- * }
1715
- * PersistSignalAdapter.usePersistSignalAdapter(RedisPersist);
1716
- * ```
1717
1631
  */
1718
- usePersistSignalAdapter(Ctor) {
1719
- bt.loggerService.info(PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_PERSIST_SIGNAL_ADAPTER);
1720
- this.PersistSignalFactory = Ctor;
1632
+ usePersistCandleAdapter(Ctor) {
1633
+ bt.loggerService.info("PersistCandleUtils.usePersistCandleAdapter");
1634
+ this.PersistCandlesFactory = Ctor;
1721
1635
  }
1722
1636
  /**
1723
1637
  * Switches to the default JSON persist adapter.
1724
1638
  * All future persistence writes will use JSON storage.
1725
1639
  */
1726
1640
  useJson() {
1727
- bt.loggerService.log(PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_JSON);
1728
- this.usePersistSignalAdapter(PersistBase);
1641
+ bt.loggerService.log("PersistCandleUtils.useJson");
1642
+ this.usePersistCandleAdapter(PersistBase);
1729
1643
  }
1730
1644
  /**
1731
1645
  * Switches to a dummy persist adapter that discards all writes.
1732
1646
  * All future persistence writes will be no-ops.
1733
1647
  */
1734
1648
  useDummy() {
1735
- bt.loggerService.log(PERSIST_SIGNAL_UTILS_METHOD_NAME_USE_DUMMY);
1736
- this.usePersistSignalAdapter(PersistDummy);
1649
+ bt.loggerService.log("PersistCandleUtils.useDummy");
1650
+ this.usePersistCandleAdapter(PersistDummy);
1737
1651
  }
1738
1652
  }
1739
1653
  /**
1740
- * Global singleton instance of PersistSignalUtils.
1741
- * Used by ClientStrategy for signal persistence.
1654
+ * Global singleton instance of PersistCandleUtils.
1655
+ * Used by ClientExchange for candle data caching.
1656
+ *
1657
+ * @example
1658
+ * ```typescript
1659
+ * // Read cached candles
1660
+ * const candles = await PersistCandleAdapter.readCandlesData(
1661
+ * "BTCUSDT", "1m", "binance", 100, since.getTime(), until.getTime()
1662
+ * );
1663
+ *
1664
+ * // Write candles to cache
1665
+ * await PersistCandleAdapter.writeCandlesData(candles, "BTCUSDT", "1m", "binance");
1666
+ * ```
1667
+ */
1668
+ const PersistCandleAdapter = new PersistCandleUtils();
1669
+
1670
+ const INTERVAL_MINUTES$4 = {
1671
+ "1m": 1,
1672
+ "3m": 3,
1673
+ "5m": 5,
1674
+ "15m": 15,
1675
+ "30m": 30,
1676
+ "1h": 60,
1677
+ "2h": 120,
1678
+ "4h": 240,
1679
+ "6h": 360,
1680
+ "8h": 480,
1681
+ };
1682
+ /**
1683
+ * Validates that all candles have valid OHLCV data without anomalies.
1684
+ * Detects incomplete candles from Binance API by checking for abnormally low prices or volumes.
1685
+ * Incomplete candles often have prices like 0.1 instead of normal 100,000 or zero volume.
1686
+ *
1687
+ * @param candles - Array of candle data to validate
1688
+ * @throws Error if any candles have anomalous OHLCV values
1689
+ */
1690
+ const VALIDATE_NO_INCOMPLETE_CANDLES_FN = (candles) => {
1691
+ if (candles.length === 0) {
1692
+ return;
1693
+ }
1694
+ // Calculate reference price (median or average depending on candle count)
1695
+ const allPrices = candles.flatMap((c) => [c.open, c.high, c.low, c.close]);
1696
+ const validPrices = allPrices.filter((p) => p > 0);
1697
+ let referencePrice;
1698
+ if (candles.length >= GLOBAL_CONFIG.CC_GET_CANDLES_MIN_CANDLES_FOR_MEDIAN) {
1699
+ // Use median for reliable statistics with enough data
1700
+ const sortedPrices = [...validPrices].sort((a, b) => a - b);
1701
+ referencePrice = sortedPrices[Math.floor(sortedPrices.length / 2)] || 0;
1702
+ }
1703
+ else {
1704
+ // Use average for small datasets (more stable than median)
1705
+ const sum = validPrices.reduce((acc, p) => acc + p, 0);
1706
+ referencePrice = validPrices.length > 0 ? sum / validPrices.length : 0;
1707
+ }
1708
+ if (referencePrice === 0) {
1709
+ throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: cannot calculate reference price (all prices are zero)`);
1710
+ }
1711
+ const minValidPrice = referencePrice /
1712
+ GLOBAL_CONFIG.CC_GET_CANDLES_PRICE_ANOMALY_THRESHOLD_FACTOR;
1713
+ for (let i = 0; i < candles.length; i++) {
1714
+ const candle = candles[i];
1715
+ // Check for invalid numeric values
1716
+ if (!Number.isFinite(candle.open) ||
1717
+ !Number.isFinite(candle.high) ||
1718
+ !Number.isFinite(candle.low) ||
1719
+ !Number.isFinite(candle.close) ||
1720
+ !Number.isFinite(candle.volume) ||
1721
+ !Number.isFinite(candle.timestamp)) {
1722
+ throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: candle[${i}] has invalid numeric values (NaN or Infinity)`);
1723
+ }
1724
+ // Check for negative values
1725
+ if (candle.open <= 0 ||
1726
+ candle.high <= 0 ||
1727
+ candle.low <= 0 ||
1728
+ candle.close <= 0 ||
1729
+ candle.volume < 0) {
1730
+ throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: candle[${i}] has zero or negative values`);
1731
+ }
1732
+ // Check for anomalously low prices (incomplete candle indicator)
1733
+ if (candle.open < minValidPrice ||
1734
+ candle.high < minValidPrice ||
1735
+ candle.low < minValidPrice ||
1736
+ candle.close < minValidPrice) {
1737
+ throw new Error(`VALIDATE_NO_INCOMPLETE_CANDLES_FN: candle[${i}] has anomalously low price. ` +
1738
+ `OHLC: [${candle.open}, ${candle.high}, ${candle.low}, ${candle.close}], ` +
1739
+ `reference: ${referencePrice}, threshold: ${minValidPrice}`);
1740
+ }
1741
+ }
1742
+ };
1743
+ /**
1744
+ * Attempts to read candles from cache.
1745
+ * Validates cache consistency (no gaps in timestamps) before returning.
1746
+ *
1747
+ * @param dto - Data transfer object containing symbol, interval, and limit
1748
+ * @param sinceTimestamp - Start timestamp in milliseconds
1749
+ * @param untilTimestamp - End timestamp in milliseconds
1750
+ * @param self - Instance of ClientExchange
1751
+ * @returns Cached candles array or null if cache miss or inconsistent
1752
+ */
1753
+ const READ_CANDLES_CACHE_FN$1 = functoolsKit.trycatch(async (dto, sinceTimestamp, untilTimestamp, self) => {
1754
+ const cachedCandles = await PersistCandleAdapter.readCandlesData(dto.symbol, dto.interval, self.params.exchangeName, dto.limit, sinceTimestamp, untilTimestamp);
1755
+ // Return cached data only if we have exactly the requested limit
1756
+ if (cachedCandles.length === dto.limit) {
1757
+ self.params.logger.debug(`ClientExchange READ_CANDLES_CACHE_FN: cache hit for symbol=${dto.symbol}, interval=${dto.interval}, limit=${dto.limit}`);
1758
+ return cachedCandles;
1759
+ }
1760
+ self.params.logger.warn(`ClientExchange READ_CANDLES_CACHE_FN: cache inconsistent (count or range mismatch) for symbol=${dto.symbol}, interval=${dto.interval}, limit=${dto.limit}`);
1761
+ return null;
1762
+ }, {
1763
+ fallback: async (error) => {
1764
+ const message = `ClientExchange READ_CANDLES_CACHE_FN: cache read failed`;
1765
+ const payload = {
1766
+ error: functoolsKit.errorData(error),
1767
+ message: functoolsKit.getErrorMessage(error),
1768
+ };
1769
+ bt.loggerService.warn(message, payload);
1770
+ console.warn(message, payload);
1771
+ errorEmitter.next(error);
1772
+ },
1773
+ defaultValue: null,
1774
+ });
1775
+ /**
1776
+ * Writes candles to cache with error handling.
1777
+ *
1778
+ * @param candles - Array of candle data to cache
1779
+ * @param dto - Data transfer object containing symbol, interval, and limit
1780
+ * @param self - Instance of ClientExchange
1781
+ */
1782
+ const WRITE_CANDLES_CACHE_FN$1 = functoolsKit.trycatch(functoolsKit.queued(async (candles, dto, self) => {
1783
+ await PersistCandleAdapter.writeCandlesData(candles, dto.symbol, dto.interval, self.params.exchangeName);
1784
+ self.params.logger.debug(`ClientExchange WRITE_CANDLES_CACHE_FN: cache updated for symbol=${dto.symbol}, interval=${dto.interval}, count=${candles.length}`);
1785
+ }), {
1786
+ fallback: async (error) => {
1787
+ const message = `ClientExchange WRITE_CANDLES_CACHE_FN: cache write failed`;
1788
+ const payload = {
1789
+ error: functoolsKit.errorData(error),
1790
+ message: functoolsKit.getErrorMessage(error),
1791
+ };
1792
+ bt.loggerService.warn(message, payload);
1793
+ console.warn(message, payload);
1794
+ errorEmitter.next(error);
1795
+ },
1796
+ defaultValue: null,
1797
+ });
1798
+ /**
1799
+ * Retries the getCandles function with specified retry count and delay.
1800
+ * Uses cache to avoid redundant API calls.
1801
+ *
1802
+ * Cache logic:
1803
+ * - Checks if cached candles exist for the time range
1804
+ * - If cache has exactly dto.limit candles, returns cached data
1805
+ * - Otherwise, fetches from API and updates cache
1806
+ *
1807
+ * @param dto - Data transfer object containing symbol, interval, and limit
1808
+ * @param since - Date object representing the start time for fetching candles
1809
+ * @param self - Instance of ClientExchange
1810
+ * @returns Promise resolving to array of candle data
1811
+ */
1812
+ const GET_CANDLES_FN = async (dto, since, self) => {
1813
+ const step = INTERVAL_MINUTES$4[dto.interval];
1814
+ const sinceTimestamp = since.getTime();
1815
+ const untilTimestamp = sinceTimestamp + dto.limit * step * 60 * 1000;
1816
+ // Try to read from cache first
1817
+ const cachedCandles = await READ_CANDLES_CACHE_FN$1(dto, sinceTimestamp, untilTimestamp, self);
1818
+ if (cachedCandles !== null) {
1819
+ return cachedCandles;
1820
+ }
1821
+ // Cache miss or error - fetch from API
1822
+ let lastError;
1823
+ for (let i = 0; i !== GLOBAL_CONFIG.CC_GET_CANDLES_RETRY_COUNT; i++) {
1824
+ try {
1825
+ const result = await self.params.getCandles(dto.symbol, dto.interval, since, dto.limit, self.params.execution.context.backtest);
1826
+ VALIDATE_NO_INCOMPLETE_CANDLES_FN(result);
1827
+ // Write to cache after successful fetch
1828
+ await WRITE_CANDLES_CACHE_FN$1(result, dto, self);
1829
+ return result;
1830
+ }
1831
+ catch (err) {
1832
+ const message = `ClientExchange GET_CANDLES_FN: attempt ${i + 1} failed for symbol=${dto.symbol}, interval=${dto.interval}, since=${since.toISOString()}, limit=${dto.limit}}`;
1833
+ const payload = {
1834
+ error: functoolsKit.errorData(err),
1835
+ message: functoolsKit.getErrorMessage(err),
1836
+ };
1837
+ self.params.logger.warn(message, payload);
1838
+ console.warn(message, payload);
1839
+ lastError = err;
1840
+ await functoolsKit.sleep(GLOBAL_CONFIG.CC_GET_CANDLES_RETRY_DELAY_MS);
1841
+ }
1842
+ }
1843
+ throw lastError;
1844
+ };
1845
+ /**
1846
+ * Wrapper to call onCandleData callback with error handling.
1847
+ * Catches and logs any errors thrown by the user-provided callback.
1848
+ *
1849
+ * @param self - ClientExchange instance reference
1850
+ * @param symbol - Trading pair symbol
1851
+ * @param interval - Candle interval
1852
+ * @param since - Start date for candle data
1853
+ * @param limit - Number of candles
1854
+ * @param data - Array of candle data
1855
+ */
1856
+ const CALL_CANDLE_DATA_CALLBACKS_FN = functoolsKit.trycatch(async (self, symbol, interval, since, limit, data) => {
1857
+ if (self.params.callbacks?.onCandleData) {
1858
+ await self.params.callbacks.onCandleData(symbol, interval, since, limit, data);
1859
+ }
1860
+ }, {
1861
+ fallback: (error) => {
1862
+ const message = "ClientExchange CALL_CANDLE_DATA_CALLBACKS_FN thrown";
1863
+ const payload = {
1864
+ error: functoolsKit.errorData(error),
1865
+ message: functoolsKit.getErrorMessage(error),
1866
+ };
1867
+ bt.loggerService.warn(message, payload);
1868
+ console.warn(message, payload);
1869
+ errorEmitter.next(error);
1870
+ },
1871
+ });
1872
+ /**
1873
+ * Client implementation for exchange data access.
1874
+ *
1875
+ * Features:
1876
+ * - Historical candle fetching (backwards from execution context)
1877
+ * - Future candle fetching (forwards for backtest)
1878
+ * - VWAP calculation from last 5 1m candles
1879
+ * - Price/quantity formatting for exchange
1880
+ *
1881
+ * All methods use prototype functions for memory efficiency.
1742
1882
  *
1743
1883
  * @example
1744
1884
  * ```typescript
1745
- * // Custom adapter
1746
- * PersistSignalAdapter.usePersistSignalAdapter(RedisPersist);
1747
- *
1748
- * // Read signal
1749
- * const signal = await PersistSignalAdapter.readSignalData("my-strategy", "BTCUSDT");
1885
+ * const exchange = new ClientExchange({
1886
+ * exchangeName: "binance",
1887
+ * getCandles: async (symbol, interval, since, limit) => [...],
1888
+ * formatPrice: async (symbol, price) => price.toFixed(2),
1889
+ * formatQuantity: async (symbol, quantity) => quantity.toFixed(8),
1890
+ * execution: executionService,
1891
+ * logger: loggerService,
1892
+ * });
1750
1893
  *
1751
- * // Write signal
1752
- * await PersistSignalAdapter.writeSignalData(signal, "my-strategy", "BTCUSDT");
1894
+ * const candles = await exchange.getCandles("BTCUSDT", "1m", 100);
1895
+ * const vwap = await exchange.getAveragePrice("BTCUSDT");
1753
1896
  * ```
1754
1897
  */
1755
- const PersistSignalAdapter = new PersistSignalUtils();
1756
- /**
1757
- * Utility class for managing risk active positions persistence.
1758
- *
1759
- * Features:
1760
- * - Memoized storage instances per risk profile
1761
- * - Custom adapter support
1762
- * - Atomic read/write operations for RiskData
1763
- * - Crash-safe position state management
1764
- *
1765
- * Used by ClientRisk for live mode persistence of active positions.
1766
- */
1767
- class PersistRiskUtils {
1768
- constructor() {
1769
- this.PersistRiskFactory = PersistBase;
1770
- this.getRiskStorage = functoolsKit.memoize(([riskName, exchangeName]) => `${riskName}:${exchangeName}`, (riskName, exchangeName) => Reflect.construct(this.PersistRiskFactory, [
1771
- `${riskName}_${exchangeName}`,
1772
- `./dump/data/risk/`,
1773
- ]));
1774
- /**
1775
- * Reads persisted active positions for a risk profile.
1776
- *
1777
- * Called by ClientRisk.waitForInit() to restore state.
1778
- * Returns empty Map if no positions exist.
1779
- *
1780
- * @param riskName - Risk profile identifier
1781
- * @param exchangeName - Exchange identifier
1782
- * @returns Promise resolving to Map of active positions
1783
- */
1784
- this.readPositionData = async (riskName, exchangeName) => {
1785
- bt.loggerService.info(PERSIST_RISK_UTILS_METHOD_NAME_READ_DATA);
1786
- const key = `${riskName}:${exchangeName}`;
1787
- const isInitial = !this.getRiskStorage.has(key);
1788
- const stateStorage = this.getRiskStorage(riskName, exchangeName);
1789
- await stateStorage.waitForInit(isInitial);
1790
- const RISK_STORAGE_KEY = "positions";
1791
- if (await stateStorage.hasValue(RISK_STORAGE_KEY)) {
1792
- return await stateStorage.readValue(RISK_STORAGE_KEY);
1793
- }
1794
- return [];
1795
- };
1796
- /**
1797
- * Writes active positions to disk with atomic file writes.
1798
- *
1799
- * Called by ClientRisk after addSignal/removeSignal to persist state.
1800
- * Uses atomic writes to prevent corruption on crashes.
1801
- *
1802
- * @param positions - Map of active positions
1803
- * @param riskName - Risk profile identifier
1804
- * @param exchangeName - Exchange identifier
1805
- * @returns Promise that resolves when write is complete
1806
- */
1807
- this.writePositionData = async (riskRow, riskName, exchangeName) => {
1808
- bt.loggerService.info(PERSIST_RISK_UTILS_METHOD_NAME_WRITE_DATA);
1809
- const key = `${riskName}:${exchangeName}`;
1810
- const isInitial = !this.getRiskStorage.has(key);
1811
- const stateStorage = this.getRiskStorage(riskName, exchangeName);
1812
- await stateStorage.waitForInit(isInitial);
1813
- const RISK_STORAGE_KEY = "positions";
1814
- await stateStorage.writeValue(RISK_STORAGE_KEY, riskRow);
1815
- };
1898
+ class ClientExchange {
1899
+ constructor(params) {
1900
+ this.params = params;
1816
1901
  }
1817
1902
  /**
1818
- * Registers a custom persistence adapter.
1819
- *
1820
- * @param Ctor - Custom PersistBase constructor
1903
+ * Fetches historical candles backwards from execution context time.
1821
1904
  *
1822
- * @example
1823
- * ```typescript
1824
- * class RedisPersist extends PersistBase {
1825
- * async readValue(id) { return JSON.parse(await redis.get(id)); }
1826
- * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1827
- * }
1828
- * PersistRiskAdapter.usePersistRiskAdapter(RedisPersist);
1829
- * ```
1830
- */
1831
- usePersistRiskAdapter(Ctor) {
1832
- bt.loggerService.info(PERSIST_RISK_UTILS_METHOD_NAME_USE_PERSIST_RISK_ADAPTER);
1833
- this.PersistRiskFactory = Ctor;
1834
- }
1835
- /**
1836
- * Switches to the default JSON persist adapter.
1837
- * All future persistence writes will use JSON storage.
1905
+ * @param symbol - Trading pair symbol
1906
+ * @param interval - Candle interval
1907
+ * @param limit - Number of candles to fetch
1908
+ * @returns Promise resolving to array of candles
1838
1909
  */
1839
- useJson() {
1840
- bt.loggerService.log(PERSIST_RISK_UTILS_METHOD_NAME_USE_JSON);
1841
- this.usePersistRiskAdapter(PersistBase);
1910
+ async getCandles(symbol, interval, limit) {
1911
+ this.params.logger.debug(`ClientExchange getCandles`, {
1912
+ symbol,
1913
+ interval,
1914
+ limit,
1915
+ });
1916
+ const step = INTERVAL_MINUTES$4[interval];
1917
+ const adjust = step * limit;
1918
+ if (!adjust) {
1919
+ throw new Error(`ClientExchange unknown time adjust for interval=${interval}`);
1920
+ }
1921
+ const since = new Date(this.params.execution.context.when.getTime() - adjust * 60 * 1000);
1922
+ let allData = [];
1923
+ // If limit exceeds CC_MAX_CANDLES_PER_REQUEST, fetch data in chunks
1924
+ if (limit > GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST) {
1925
+ let remaining = limit;
1926
+ let currentSince = new Date(since.getTime());
1927
+ while (remaining > 0) {
1928
+ const chunkLimit = Math.min(remaining, GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST);
1929
+ const chunkData = await GET_CANDLES_FN({ symbol, interval, limit: chunkLimit }, currentSince, this);
1930
+ allData.push(...chunkData);
1931
+ remaining -= chunkLimit;
1932
+ if (remaining > 0) {
1933
+ // Move currentSince forward by the number of candles fetched
1934
+ currentSince = new Date(currentSince.getTime() + chunkLimit * step * 60 * 1000);
1935
+ }
1936
+ }
1937
+ }
1938
+ else {
1939
+ allData = await GET_CANDLES_FN({ symbol, interval, limit }, since, this);
1940
+ }
1941
+ // Filter candles to strictly match the requested range
1942
+ const whenTimestamp = this.params.execution.context.when.getTime();
1943
+ const sinceTimestamp = since.getTime();
1944
+ const filteredData = allData.filter((candle) => candle.timestamp >= sinceTimestamp &&
1945
+ candle.timestamp < whenTimestamp);
1946
+ // Apply distinct by timestamp to remove duplicates
1947
+ const uniqueData = Array.from(new Map(filteredData.map((candle) => [candle.timestamp, candle])).values());
1948
+ if (filteredData.length !== uniqueData.length) {
1949
+ const msg = `ClientExchange Removed ${filteredData.length - uniqueData.length} duplicate candles by timestamp`;
1950
+ this.params.logger.warn(msg);
1951
+ console.warn(msg);
1952
+ }
1953
+ if (uniqueData.length < limit) {
1954
+ const msg = `ClientExchange Expected ${limit} candles, got ${uniqueData.length}`;
1955
+ this.params.logger.warn(msg);
1956
+ console.warn(msg);
1957
+ }
1958
+ await CALL_CANDLE_DATA_CALLBACKS_FN(this, symbol, interval, since, limit, uniqueData);
1959
+ return uniqueData;
1842
1960
  }
1843
1961
  /**
1844
- * Switches to a dummy persist adapter that discards all writes.
1845
- * All future persistence writes will be no-ops.
1962
+ * Fetches future candles forwards from execution context time.
1963
+ * Used in backtest mode to get candles for signal duration.
1964
+ *
1965
+ * @param symbol - Trading pair symbol
1966
+ * @param interval - Candle interval
1967
+ * @param limit - Number of candles to fetch
1968
+ * @returns Promise resolving to array of candles
1969
+ * @throws Error if trying to fetch future candles in live mode
1846
1970
  */
1847
- useDummy() {
1848
- bt.loggerService.log(PERSIST_RISK_UTILS_METHOD_NAME_USE_DUMMY);
1849
- this.usePersistRiskAdapter(PersistDummy);
1850
- }
1851
- }
1852
- /**
1853
- * Global singleton instance of PersistRiskUtils.
1854
- * Used by ClientRisk for active positions persistence.
1855
- *
1856
- * @example
1857
- * ```typescript
1858
- * // Custom adapter
1859
- * PersistRiskAdapter.usePersistRiskAdapter(RedisPersist);
1860
- *
1861
- * // Read positions
1862
- * const positions = await PersistRiskAdapter.readPositionData("my-risk");
1863
- *
1864
- * // Write positions
1865
- * await PersistRiskAdapter.writePositionData(positionsMap, "my-risk");
1866
- * ```
1867
- */
1868
- const PersistRiskAdapter = new PersistRiskUtils();
1869
- /**
1870
- * Utility class for managing scheduled signal persistence.
1871
- *
1872
- * Features:
1873
- * - Memoized storage instances per strategy
1874
- * - Custom adapter support
1875
- * - Atomic read/write operations for scheduled signals
1876
- * - Crash-safe scheduled signal state management
1877
- *
1878
- * Used by ClientStrategy for live mode persistence of scheduled signals (_scheduledSignal).
1879
- */
1880
- class PersistScheduleUtils {
1881
- constructor() {
1882
- this.PersistScheduleFactory = PersistBase;
1883
- this.getScheduleStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistScheduleFactory, [
1884
- `${symbol}_${strategyName}_${exchangeName}`,
1885
- `./dump/data/schedule/`,
1886
- ]));
1887
- /**
1888
- * Reads persisted scheduled signal data for a symbol and strategy.
1889
- *
1890
- * Called by ClientStrategy.waitForInit() to restore scheduled signal state.
1891
- * Returns null if no scheduled signal exists.
1892
- *
1893
- * @param symbol - Trading pair symbol
1894
- * @param strategyName - Strategy identifier
1895
- * @param exchangeName - Exchange identifier
1896
- * @returns Promise resolving to scheduled signal or null
1897
- */
1898
- this.readScheduleData = async (symbol, strategyName, exchangeName) => {
1899
- bt.loggerService.info(PERSIST_SCHEDULE_UTILS_METHOD_NAME_READ_DATA);
1900
- const key = `${symbol}:${strategyName}:${exchangeName}`;
1901
- const isInitial = !this.getScheduleStorage.has(key);
1902
- const stateStorage = this.getScheduleStorage(symbol, strategyName, exchangeName);
1903
- await stateStorage.waitForInit(isInitial);
1904
- if (await stateStorage.hasValue(symbol)) {
1905
- return await stateStorage.readValue(symbol);
1971
+ async getNextCandles(symbol, interval, limit) {
1972
+ this.params.logger.debug(`ClientExchange getNextCandles`, {
1973
+ symbol,
1974
+ interval,
1975
+ limit,
1976
+ });
1977
+ const since = new Date(this.params.execution.context.when.getTime());
1978
+ const now = Date.now();
1979
+ // Вычисляем конечное время запроса
1980
+ const step = INTERVAL_MINUTES$4[interval];
1981
+ const endTime = since.getTime() + limit * step * 60 * 1000;
1982
+ // Проверяем что запрошенный период не заходит за Date.now()
1983
+ if (endTime > now) {
1984
+ return [];
1985
+ }
1986
+ let allData = [];
1987
+ // If limit exceeds CC_MAX_CANDLES_PER_REQUEST, fetch data in chunks
1988
+ if (limit > GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST) {
1989
+ let remaining = limit;
1990
+ let currentSince = new Date(since.getTime());
1991
+ while (remaining > 0) {
1992
+ const chunkLimit = Math.min(remaining, GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST);
1993
+ const chunkData = await GET_CANDLES_FN({ symbol, interval, limit: chunkLimit }, currentSince, this);
1994
+ allData.push(...chunkData);
1995
+ remaining -= chunkLimit;
1996
+ if (remaining > 0) {
1997
+ // Move currentSince forward by the number of candles fetched
1998
+ currentSince = new Date(currentSince.getTime() + chunkLimit * step * 60 * 1000);
1999
+ }
1906
2000
  }
1907
- return null;
1908
- };
1909
- /**
1910
- * Writes scheduled signal data to disk with atomic file writes.
1911
- *
1912
- * Called by ClientStrategy.setScheduledSignal() to persist state.
1913
- * Uses atomic writes to prevent corruption on crashes.
1914
- *
1915
- * @param scheduledSignalRow - Scheduled signal data (null to clear)
1916
- * @param symbol - Trading pair symbol
1917
- * @param strategyName - Strategy identifier
1918
- * @param exchangeName - Exchange identifier
1919
- * @returns Promise that resolves when write is complete
1920
- */
1921
- this.writeScheduleData = async (scheduledSignalRow, symbol, strategyName, exchangeName) => {
1922
- bt.loggerService.info(PERSIST_SCHEDULE_UTILS_METHOD_NAME_WRITE_DATA);
1923
- const key = `${symbol}:${strategyName}:${exchangeName}`;
1924
- const isInitial = !this.getScheduleStorage.has(key);
1925
- const stateStorage = this.getScheduleStorage(symbol, strategyName, exchangeName);
1926
- await stateStorage.waitForInit(isInitial);
1927
- await stateStorage.writeValue(symbol, scheduledSignalRow);
1928
- };
2001
+ }
2002
+ else {
2003
+ allData = await GET_CANDLES_FN({ symbol, interval, limit }, since, this);
2004
+ }
2005
+ // Filter candles to strictly match the requested range
2006
+ const sinceTimestamp = since.getTime();
2007
+ const filteredData = allData.filter((candle) => candle.timestamp >= sinceTimestamp && candle.timestamp < endTime);
2008
+ // Apply distinct by timestamp to remove duplicates
2009
+ const uniqueData = Array.from(new Map(filteredData.map((candle) => [candle.timestamp, candle])).values());
2010
+ if (filteredData.length !== uniqueData.length) {
2011
+ const msg = `ClientExchange getNextCandles: Removed ${filteredData.length - uniqueData.length} duplicate candles by timestamp`;
2012
+ this.params.logger.warn(msg);
2013
+ console.warn(msg);
2014
+ }
2015
+ if (uniqueData.length < limit) {
2016
+ const msg = `ClientExchange getNextCandles: Expected ${limit} candles, got ${uniqueData.length}`;
2017
+ this.params.logger.warn(msg);
2018
+ console.warn(msg);
2019
+ }
2020
+ await CALL_CANDLE_DATA_CALLBACKS_FN(this, symbol, interval, since, limit, uniqueData);
2021
+ return uniqueData;
1929
2022
  }
1930
2023
  /**
1931
- * Registers a custom persistence adapter.
2024
+ * Calculates VWAP (Volume Weighted Average Price) from last N 1m candles.
2025
+ * The number of candles is configurable via GLOBAL_CONFIG.CC_AVG_PRICE_CANDLES_COUNT.
1932
2026
  *
1933
- * @param Ctor - Custom PersistBase constructor
2027
+ * Formula:
2028
+ * - Typical Price = (high + low + close) / 3
2029
+ * - VWAP = sum(typical_price * volume) / sum(volume)
1934
2030
  *
1935
- * @example
1936
- * ```typescript
1937
- * class RedisPersist extends PersistBase {
1938
- * async readValue(id) { return JSON.parse(await redis.get(id)); }
1939
- * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
1940
- * }
1941
- * PersistScheduleAdapter.usePersistScheduleAdapter(RedisPersist);
1942
- * ```
2031
+ * If volume is zero, returns simple average of close prices.
2032
+ *
2033
+ * @param symbol - Trading pair symbol
2034
+ * @returns Promise resolving to VWAP price
2035
+ * @throws Error if no candles available
1943
2036
  */
1944
- usePersistScheduleAdapter(Ctor) {
1945
- bt.loggerService.info(PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_PERSIST_SCHEDULE_ADAPTER);
1946
- this.PersistScheduleFactory = Ctor;
2037
+ async getAveragePrice(symbol) {
2038
+ this.params.logger.debug(`ClientExchange getAveragePrice`, {
2039
+ symbol,
2040
+ });
2041
+ const candles = await this.getCandles(symbol, "1m", GLOBAL_CONFIG.CC_AVG_PRICE_CANDLES_COUNT);
2042
+ if (candles.length === 0) {
2043
+ throw new Error(`ClientExchange getAveragePrice: no candles data for symbol=${symbol}`);
2044
+ }
2045
+ // VWAP (Volume Weighted Average Price)
2046
+ // Используем типичную цену (typical price) = (high + low + close) / 3
2047
+ const sumPriceVolume = candles.reduce((acc, candle) => {
2048
+ const typicalPrice = (candle.high + candle.low + candle.close) / 3;
2049
+ return acc + typicalPrice * candle.volume;
2050
+ }, 0);
2051
+ const totalVolume = candles.reduce((acc, candle) => acc + candle.volume, 0);
2052
+ if (totalVolume === 0) {
2053
+ // Если объем нулевой, возвращаем простое среднее close цен
2054
+ const sum = candles.reduce((acc, candle) => acc + candle.close, 0);
2055
+ return sum / candles.length;
2056
+ }
2057
+ const vwap = sumPriceVolume / totalVolume;
2058
+ return vwap;
1947
2059
  }
1948
2060
  /**
1949
- * Switches to the default JSON persist adapter.
1950
- * All future persistence writes will use JSON storage.
2061
+ * Formats quantity according to exchange-specific rules for the given symbol.
2062
+ * Applies proper decimal precision and rounding based on symbol's lot size filters.
2063
+ *
2064
+ * @param symbol - Trading pair symbol
2065
+ * @param quantity - Raw quantity to format
2066
+ * @returns Promise resolving to formatted quantity as string
1951
2067
  */
1952
- useJson() {
1953
- bt.loggerService.log(PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_JSON);
1954
- this.usePersistScheduleAdapter(PersistBase);
2068
+ async formatQuantity(symbol, quantity) {
2069
+ this.params.logger.debug("binanceService formatQuantity", {
2070
+ symbol,
2071
+ quantity,
2072
+ });
2073
+ return await this.params.formatQuantity(symbol, quantity, this.params.execution.context.backtest);
1955
2074
  }
1956
2075
  /**
1957
- * Switches to a dummy persist adapter that discards all writes.
1958
- * All future persistence writes will be no-ops.
2076
+ * Formats price according to exchange-specific rules for the given symbol.
2077
+ * Applies proper decimal precision and rounding based on symbol's price filters.
2078
+ *
2079
+ * @param symbol - Trading pair symbol
2080
+ * @param price - Raw price to format
2081
+ * @returns Promise resolving to formatted price as string
1959
2082
  */
1960
- useDummy() {
1961
- bt.loggerService.log(PERSIST_SCHEDULE_UTILS_METHOD_NAME_USE_DUMMY);
1962
- this.usePersistScheduleAdapter(PersistDummy);
1963
- }
1964
- }
1965
- /**
1966
- * Global singleton instance of PersistScheduleUtils.
1967
- * Used by ClientStrategy for scheduled signal persistence.
1968
- *
1969
- * @example
1970
- * ```typescript
1971
- * // Custom adapter
1972
- * PersistScheduleAdapter.usePersistScheduleAdapter(RedisPersist);
1973
- *
1974
- * // Read scheduled signal
1975
- * const scheduled = await PersistScheduleAdapter.readScheduleData("my-strategy", "BTCUSDT");
1976
- *
1977
- * // Write scheduled signal
1978
- * await PersistScheduleAdapter.writeScheduleData(scheduled, "my-strategy", "BTCUSDT");
1979
- * ```
1980
- */
1981
- const PersistScheduleAdapter = new PersistScheduleUtils();
1982
- /**
1983
- * Utility class for managing partial profit/loss levels persistence.
1984
- *
1985
- * Features:
1986
- * - Memoized storage instances per symbol:strategyName
1987
- * - Custom adapter support
1988
- * - Atomic read/write operations for partial data
1989
- * - Crash-safe partial state management
1990
- *
1991
- * Used by ClientPartial for live mode persistence of profit/loss levels.
1992
- */
1993
- class PersistPartialUtils {
1994
- constructor() {
1995
- this.PersistPartialFactory = PersistBase;
1996
- this.getPartialStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistPartialFactory, [
1997
- `${symbol}_${strategyName}_${exchangeName}`,
1998
- `./dump/data/partial/`,
1999
- ]));
2000
- /**
2001
- * Reads persisted partial data for a symbol and strategy.
2002
- *
2003
- * Called by ClientPartial.waitForInit() to restore state.
2004
- * Returns empty object if no partial data exists.
2005
- *
2006
- * @param symbol - Trading pair symbol
2007
- * @param strategyName - Strategy identifier
2008
- * @param signalId - Signal identifier
2009
- * @param exchangeName - Exchange identifier
2010
- * @returns Promise resolving to partial data record
2011
- */
2012
- this.readPartialData = async (symbol, strategyName, signalId, exchangeName) => {
2013
- bt.loggerService.info(PERSIST_PARTIAL_UTILS_METHOD_NAME_READ_DATA);
2014
- const key = `${symbol}:${strategyName}:${exchangeName}`;
2015
- const isInitial = !this.getPartialStorage.has(key);
2016
- const stateStorage = this.getPartialStorage(symbol, strategyName, exchangeName);
2017
- await stateStorage.waitForInit(isInitial);
2018
- if (await stateStorage.hasValue(signalId)) {
2019
- return await stateStorage.readValue(signalId);
2020
- }
2021
- return {};
2022
- };
2023
- /**
2024
- * Writes partial data to disk with atomic file writes.
2025
- *
2026
- * Called by ClientPartial after profit/loss level changes to persist state.
2027
- * Uses atomic writes to prevent corruption on crashes.
2028
- *
2029
- * @param partialData - Record of signal IDs to partial data
2030
- * @param symbol - Trading pair symbol
2031
- * @param strategyName - Strategy identifier
2032
- * @param signalId - Signal identifier
2033
- * @param exchangeName - Exchange identifier
2034
- * @returns Promise that resolves when write is complete
2035
- */
2036
- this.writePartialData = async (partialData, symbol, strategyName, signalId, exchangeName) => {
2037
- bt.loggerService.info(PERSIST_PARTIAL_UTILS_METHOD_NAME_WRITE_DATA);
2038
- const key = `${symbol}:${strategyName}:${exchangeName}`;
2039
- const isInitial = !this.getPartialStorage.has(key);
2040
- const stateStorage = this.getPartialStorage(symbol, strategyName, exchangeName);
2041
- await stateStorage.waitForInit(isInitial);
2042
- await stateStorage.writeValue(signalId, partialData);
2043
- };
2083
+ async formatPrice(symbol, price) {
2084
+ this.params.logger.debug("binanceService formatPrice", {
2085
+ symbol,
2086
+ price,
2087
+ });
2088
+ return await this.params.formatPrice(symbol, price, this.params.execution.context.backtest);
2044
2089
  }
2045
2090
  /**
2046
- * Registers a custom persistence adapter.
2091
+ * Fetches raw candles with flexible date/limit parameters.
2047
2092
  *
2048
- * @param Ctor - Custom PersistBase constructor
2093
+ * Compatibility layer that:
2094
+ * - RAW MODE (sDate + eDate + limit): fetches exactly as specified, NO look-ahead bias protection
2095
+ * - Other modes: respects execution context and prevents look-ahead bias
2049
2096
  *
2050
- * @example
2051
- * ```typescript
2052
- * class RedisPersist extends PersistBase {
2053
- * async readValue(id) { return JSON.parse(await redis.get(id)); }
2054
- * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
2055
- * }
2056
- * PersistPartialAdapter.usePersistPartialAdapter(RedisPersist);
2057
- * ```
2058
- */
2059
- usePersistPartialAdapter(Ctor) {
2060
- bt.loggerService.info(PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_PERSIST_PARTIAL_ADAPTER);
2061
- this.PersistPartialFactory = Ctor;
2062
- }
2063
- /**
2064
- * Switches to the default JSON persist adapter.
2065
- * All future persistence writes will use JSON storage.
2097
+ * Parameter combinations:
2098
+ * 1. sDate + eDate + limit: RAW MODE - fetches exactly as specified, no validation against when
2099
+ * 2. sDate + eDate: calculates limit from date range, validates endTimestamp <= when
2100
+ * 3. eDate + limit: calculates sDate backward, validates endTimestamp <= when
2101
+ * 4. sDate + limit: fetches forward, validates endTimestamp <= when
2102
+ * 5. Only limit: uses execution.context.when as reference (backward)
2103
+ *
2104
+ * Edge cases:
2105
+ * - If calculated limit is 0 or negative: throws error
2106
+ * - If sDate >= eDate: throws error
2107
+ * - If startTimestamp >= endTimestamp: throws error
2108
+ * - If endTimestamp > when (non-RAW modes only): throws error to prevent look-ahead bias
2109
+ *
2110
+ * @param symbol - Trading pair symbol
2111
+ * @param interval - Candle interval
2112
+ * @param limit - Optional number of candles to fetch
2113
+ * @param sDate - Optional start date in milliseconds
2114
+ * @param eDate - Optional end date in milliseconds
2115
+ * @returns Promise resolving to array of candles
2116
+ * @throws Error if parameters are invalid or conflicting
2066
2117
  */
2067
- useJson() {
2068
- bt.loggerService.log(PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_JSON);
2069
- this.usePersistPartialAdapter(PersistBase);
2118
+ async getRawCandles(symbol, interval, limit, sDate, eDate) {
2119
+ this.params.logger.debug(`ClientExchange getRawCandles`, {
2120
+ symbol,
2121
+ interval,
2122
+ limit,
2123
+ sDate,
2124
+ eDate,
2125
+ });
2126
+ const step = INTERVAL_MINUTES$4[interval];
2127
+ const stepMs = step * 60 * 1000;
2128
+ const when = this.params.execution.context.when.getTime();
2129
+ let startTimestamp;
2130
+ let endTimestamp;
2131
+ let candleLimit;
2132
+ let isRawMode = false;
2133
+ // Case 1: sDate + eDate + limit - RAW MODE (no look-ahead bias protection)
2134
+ if (sDate !== undefined &&
2135
+ eDate !== undefined &&
2136
+ limit !== undefined) {
2137
+ isRawMode = true;
2138
+ if (sDate >= eDate) {
2139
+ throw new Error(`ClientExchange getRawCandles: sDate (${sDate}) must be less than eDate (${eDate})`);
2140
+ }
2141
+ startTimestamp = sDate;
2142
+ endTimestamp = eDate;
2143
+ candleLimit = limit;
2144
+ }
2145
+ // Case 2: sDate + eDate - calculate limit, respect backtest context
2146
+ else if (sDate !== undefined && eDate !== undefined && limit === undefined) {
2147
+ if (sDate >= eDate) {
2148
+ throw new Error(`ClientExchange getRawCandles: sDate (${sDate}) must be less than eDate (${eDate})`);
2149
+ }
2150
+ startTimestamp = sDate;
2151
+ endTimestamp = eDate;
2152
+ const rangeDuration = endTimestamp - startTimestamp;
2153
+ candleLimit = Math.floor(rangeDuration / stepMs);
2154
+ if (candleLimit <= 0) {
2155
+ throw new Error(`ClientExchange getRawCandles: calculated limit is ${candleLimit} for range [${sDate}, ${eDate}]`);
2156
+ }
2157
+ }
2158
+ // Case 3: eDate + limit - calculate sDate backward, respect backtest context
2159
+ else if (eDate !== undefined && limit !== undefined && sDate === undefined) {
2160
+ endTimestamp = eDate;
2161
+ startTimestamp = eDate - limit * stepMs;
2162
+ candleLimit = limit;
2163
+ }
2164
+ // Case 4: sDate + limit - fetch forward, respect backtest context
2165
+ else if (sDate !== undefined && limit !== undefined && eDate === undefined) {
2166
+ startTimestamp = sDate;
2167
+ endTimestamp = sDate + limit * stepMs;
2168
+ candleLimit = limit;
2169
+ }
2170
+ // Case 5: Only limit - use execution context (backward from when)
2171
+ else if (limit !== undefined && sDate === undefined && eDate === undefined) {
2172
+ endTimestamp = when;
2173
+ startTimestamp = when - limit * stepMs;
2174
+ candleLimit = limit;
2175
+ }
2176
+ // Invalid combination
2177
+ else {
2178
+ throw new Error(`ClientExchange getRawCandles: invalid parameter combination. Must provide either (limit), (eDate+limit), (sDate+limit), (sDate+eDate), or (sDate+eDate+limit)`);
2179
+ }
2180
+ // Validate timestamps
2181
+ if (startTimestamp >= endTimestamp) {
2182
+ throw new Error(`ClientExchange getRawCandles: startTimestamp (${startTimestamp}) >= endTimestamp (${endTimestamp})`);
2183
+ }
2184
+ // Check if trying to fetch future data (prevent look-ahead bias)
2185
+ // ONLY for non-RAW modes - RAW MODE bypasses this check
2186
+ if (!isRawMode && endTimestamp > when) {
2187
+ throw new Error(`ClientExchange getRawCandles: endTimestamp (${endTimestamp}) is beyond execution context when (${when}) - look-ahead bias prevented`);
2188
+ }
2189
+ const since = new Date(startTimestamp);
2190
+ let allData = [];
2191
+ // Fetch data in chunks if limit exceeds max per request
2192
+ if (candleLimit > GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST) {
2193
+ let remaining = candleLimit;
2194
+ let currentSince = new Date(since.getTime());
2195
+ while (remaining > 0) {
2196
+ const chunkLimit = Math.min(remaining, GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST);
2197
+ const chunkData = await GET_CANDLES_FN({ symbol, interval, limit: chunkLimit }, currentSince, this);
2198
+ allData.push(...chunkData);
2199
+ remaining -= chunkLimit;
2200
+ if (remaining > 0) {
2201
+ currentSince = new Date(currentSince.getTime() + chunkLimit * stepMs);
2202
+ }
2203
+ }
2204
+ }
2205
+ else {
2206
+ allData = await GET_CANDLES_FN({ symbol, interval, limit: candleLimit }, since, this);
2207
+ }
2208
+ // Filter candles to strictly match the requested range
2209
+ const filteredData = allData.filter((candle) => candle.timestamp >= startTimestamp &&
2210
+ candle.timestamp < endTimestamp);
2211
+ // Apply distinct by timestamp to remove duplicates
2212
+ const uniqueData = Array.from(new Map(filteredData.map((candle) => [candle.timestamp, candle])).values());
2213
+ if (filteredData.length !== uniqueData.length) {
2214
+ const msg = `ClientExchange getRawCandles: Removed ${filteredData.length - uniqueData.length} duplicate candles by timestamp`;
2215
+ this.params.logger.warn(msg);
2216
+ console.warn(msg);
2217
+ }
2218
+ if (uniqueData.length < candleLimit) {
2219
+ const msg = `ClientExchange getRawCandles: Expected ${candleLimit} candles, got ${uniqueData.length}`;
2220
+ this.params.logger.warn(msg);
2221
+ console.warn(msg);
2222
+ }
2223
+ await CALL_CANDLE_DATA_CALLBACKS_FN(this, symbol, interval, since, candleLimit, uniqueData);
2224
+ return uniqueData;
2070
2225
  }
2071
2226
  /**
2072
- * Switches to a dummy persist adapter that discards all writes.
2073
- * All future persistence writes will be no-ops.
2227
+ * Fetches order book for a trading pair.
2228
+ *
2229
+ * Calculates time range based on execution context time (when) and
2230
+ * CC_ORDER_BOOK_TIME_OFFSET_MINUTES, then delegates to the exchange
2231
+ * schema implementation which may use or ignore the time range.
2232
+ *
2233
+ * @param symbol - Trading pair symbol
2234
+ * @param depth - Maximum depth levels (default: CC_ORDER_BOOK_MAX_DEPTH_LEVELS)
2235
+ * @returns Promise resolving to order book data
2236
+ * @throws Error if getOrderBook is not implemented
2074
2237
  */
2075
- useDummy() {
2076
- bt.loggerService.log(PERSIST_PARTIAL_UTILS_METHOD_NAME_USE_DUMMY);
2077
- this.usePersistPartialAdapter(PersistDummy);
2238
+ async getOrderBook(symbol, depth = GLOBAL_CONFIG.CC_ORDER_BOOK_MAX_DEPTH_LEVELS) {
2239
+ this.params.logger.debug("ClientExchange getOrderBook", {
2240
+ symbol,
2241
+ depth,
2242
+ });
2243
+ const to = new Date(this.params.execution.context.when.getTime());
2244
+ const from = new Date(to.getTime() -
2245
+ GLOBAL_CONFIG.CC_ORDER_BOOK_TIME_OFFSET_MINUTES * 60 * 1000);
2246
+ return await this.params.getOrderBook(symbol, depth, from, to, this.params.execution.context.backtest);
2078
2247
  }
2079
2248
  }
2249
+
2080
2250
  /**
2081
- * Global singleton instance of PersistPartialUtils.
2082
- * Used by ClientPartial for partial profit/loss levels persistence.
2083
- *
2084
- * @example
2085
- * ```typescript
2086
- * // Custom adapter
2087
- * PersistPartialAdapter.usePersistPartialAdapter(RedisPersist);
2088
- *
2089
- * // Read partial data
2090
- * const partialData = await PersistPartialAdapter.readPartialData("BTCUSDT", "my-strategy");
2091
- *
2092
- * // Write partial data
2093
- * await PersistPartialAdapter.writePartialData(partialData, "BTCUSDT", "my-strategy");
2094
- * ```
2251
+ * Default implementation for getCandles.
2252
+ * Throws an error indicating the method is not implemented.
2095
2253
  */
2096
- const PersistPartialAdapter = new PersistPartialUtils();
2254
+ const DEFAULT_GET_CANDLES_FN$1 = async (_symbol, _interval, _since, _limit, _backtest) => {
2255
+ throw new Error(`getCandles is not implemented for this exchange`);
2256
+ };
2097
2257
  /**
2098
- * Persistence utility class for breakeven state management.
2258
+ * Default implementation for formatQuantity.
2259
+ * Returns Bitcoin precision on Binance (8 decimal places).
2260
+ */
2261
+ const DEFAULT_FORMAT_QUANTITY_FN$1 = async (_symbol, quantity, _backtest) => {
2262
+ return quantity.toFixed(8);
2263
+ };
2264
+ /**
2265
+ * Default implementation for formatPrice.
2266
+ * Returns Bitcoin precision on Binance (2 decimal places).
2267
+ */
2268
+ const DEFAULT_FORMAT_PRICE_FN$1 = async (_symbol, price, _backtest) => {
2269
+ return price.toFixed(2);
2270
+ };
2271
+ /**
2272
+ * Default implementation for getOrderBook.
2273
+ * Throws an error indicating the method is not implemented.
2099
2274
  *
2100
- * Handles reading and writing breakeven state to disk.
2101
- * Uses memoized PersistBase instances per symbol-strategy pair.
2275
+ * @param _symbol - Trading pair symbol (unused)
2276
+ * @param _depth - Maximum depth levels (unused)
2277
+ * @param _from - Start of time range (unused - can be ignored in live implementations)
2278
+ * @param _to - End of time range (unused - can be ignored in live implementations)
2279
+ * @param _backtest - Whether running in backtest mode (unused)
2280
+ */
2281
+ const DEFAULT_GET_ORDER_BOOK_FN$1 = async (_symbol, _depth, _from, _to, _backtest) => {
2282
+ throw new Error(`getOrderBook is not implemented for this exchange`);
2283
+ };
2284
+ /**
2285
+ * Connection service routing exchange operations to correct ClientExchange instance.
2102
2286
  *
2103
- * Features:
2104
- * - Atomic file writes via PersistBase.writeValue()
2105
- * - Lazy initialization on first access
2106
- * - Singleton pattern for global access
2107
- * - Custom adapter support via usePersistBreakevenAdapter()
2287
+ * Routes all IExchange method calls to the appropriate exchange implementation
2288
+ * based on methodContextService.context.exchangeName. Uses memoization to cache
2289
+ * ClientExchange instances for performance.
2108
2290
  *
2109
- * File structure:
2110
- * ```
2111
- * ./dump/data/breakeven/
2112
- * ├── BTCUSDT_my-strategy/
2113
- * │ └── state.json // { "signal-id-1": { reached: true }, ... }
2114
- * └── ETHUSDT_other-strategy/
2115
- * └── state.json
2116
- * ```
2291
+ * Key features:
2292
+ * - Automatic exchange routing via method context
2293
+ * - Memoized ClientExchange instances by exchangeName
2294
+ * - Implements full IExchange interface
2295
+ * - Logging for all operations
2117
2296
  *
2118
2297
  * @example
2119
2298
  * ```typescript
2120
- * // Read breakeven data
2121
- * const breakevenData = await PersistBreakevenAdapter.readBreakevenData("BTCUSDT", "my-strategy");
2122
- * // Returns: { "signal-id": { reached: true }, ... }
2123
- *
2124
- * // Write breakeven data
2125
- * await PersistBreakevenAdapter.writeBreakevenData(breakevenData, "BTCUSDT", "my-strategy");
2299
+ * // Used internally by framework
2300
+ * const candles = await exchangeConnectionService.getCandles(
2301
+ * "BTCUSDT", "1h", 100
2302
+ * );
2303
+ * // Automatically routes to correct exchange based on methodContext
2126
2304
  * ```
2127
2305
  */
2128
- class PersistBreakevenUtils {
2306
+ class ExchangeConnectionService {
2129
2307
  constructor() {
2308
+ this.loggerService = inject(TYPES.loggerService);
2309
+ this.executionContextService = inject(TYPES.executionContextService);
2310
+ this.exchangeSchemaService = inject(TYPES.exchangeSchemaService);
2311
+ this.methodContextService = inject(TYPES.methodContextService);
2130
2312
  /**
2131
- * Factory for creating PersistBase instances.
2132
- * Can be replaced via usePersistBreakevenAdapter().
2313
+ * Retrieves memoized ClientExchange instance for given exchange name.
2314
+ *
2315
+ * Creates ClientExchange on first call, returns cached instance on subsequent calls.
2316
+ * Cache key is exchangeName string.
2317
+ *
2318
+ * @param exchangeName - Name of registered exchange schema
2319
+ * @returns Configured ClientExchange instance
2133
2320
  */
2134
- this.PersistBreakevenFactory = PersistBase;
2321
+ this.getExchange = functoolsKit.memoize(([exchangeName]) => `${exchangeName}`, (exchangeName) => {
2322
+ const { getCandles = DEFAULT_GET_CANDLES_FN$1, formatPrice = DEFAULT_FORMAT_PRICE_FN$1, formatQuantity = DEFAULT_FORMAT_QUANTITY_FN$1, getOrderBook = DEFAULT_GET_ORDER_BOOK_FN$1, callbacks } = this.exchangeSchemaService.get(exchangeName);
2323
+ return new ClientExchange({
2324
+ execution: this.executionContextService,
2325
+ logger: this.loggerService,
2326
+ exchangeName,
2327
+ getCandles,
2328
+ formatPrice,
2329
+ formatQuantity,
2330
+ getOrderBook,
2331
+ callbacks,
2332
+ });
2333
+ });
2135
2334
  /**
2136
- * Memoized storage factory for breakeven data.
2137
- * Creates one PersistBase instance per symbol-strategy-exchange combination.
2138
- * Key format: "symbol:strategyName:exchangeName"
2335
+ * Fetches historical candles for symbol using configured exchange.
2139
2336
  *
2140
- * @param symbol - Trading pair symbol
2141
- * @param strategyName - Strategy identifier
2142
- * @param exchangeName - Exchange identifier
2143
- * @returns PersistBase instance for this symbol-strategy-exchange combination
2337
+ * Routes to exchange determined by methodContextService.context.exchangeName.
2338
+ *
2339
+ * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
2340
+ * @param interval - Candle interval (e.g., "1h", "1d")
2341
+ * @param limit - Maximum number of candles to fetch
2342
+ * @returns Promise resolving to array of candle data
2144
2343
  */
2145
- this.getBreakevenStorage = functoolsKit.memoize(([symbol, strategyName, exchangeName]) => `${symbol}:${strategyName}:${exchangeName}`, (symbol, strategyName, exchangeName) => Reflect.construct(this.PersistBreakevenFactory, [
2146
- `${symbol}_${strategyName}_${exchangeName}`,
2147
- `./dump/data/breakeven/`,
2148
- ]));
2344
+ this.getCandles = async (symbol, interval, limit) => {
2345
+ this.loggerService.log("exchangeConnectionService getCandles", {
2346
+ symbol,
2347
+ interval,
2348
+ limit,
2349
+ });
2350
+ return await this.getExchange(this.methodContextService.context.exchangeName).getCandles(symbol, interval, limit);
2351
+ };
2149
2352
  /**
2150
- * Reads persisted breakeven data for a symbol and strategy.
2353
+ * Fetches next batch of candles relative to executionContext.when.
2151
2354
  *
2152
- * Called by ClientBreakeven.waitForInit() to restore state.
2153
- * Returns empty object if no breakeven data exists.
2355
+ * Returns candles that come after the current execution timestamp.
2356
+ * Used for backtest progression and live trading updates.
2154
2357
  *
2155
- * @param symbol - Trading pair symbol
2156
- * @param strategyName - Strategy identifier
2157
- * @param signalId - Signal identifier
2158
- * @param exchangeName - Exchange identifier
2159
- * @returns Promise resolving to breakeven data record
2358
+ * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
2359
+ * @param interval - Candle interval (e.g., "1h", "1d")
2360
+ * @param limit - Maximum number of candles to fetch
2361
+ * @returns Promise resolving to array of candle data
2160
2362
  */
2161
- this.readBreakevenData = async (symbol, strategyName, signalId, exchangeName) => {
2162
- bt.loggerService.info(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_READ_DATA);
2163
- const key = `${symbol}:${strategyName}:${exchangeName}`;
2164
- const isInitial = !this.getBreakevenStorage.has(key);
2165
- const stateStorage = this.getBreakevenStorage(symbol, strategyName, exchangeName);
2166
- await stateStorage.waitForInit(isInitial);
2167
- if (await stateStorage.hasValue(signalId)) {
2168
- return await stateStorage.readValue(signalId);
2169
- }
2170
- return {};
2363
+ this.getNextCandles = async (symbol, interval, limit) => {
2364
+ this.loggerService.log("exchangeConnectionService getNextCandles", {
2365
+ symbol,
2366
+ interval,
2367
+ limit,
2368
+ });
2369
+ return await this.getExchange(this.methodContextService.context.exchangeName).getNextCandles(symbol, interval, limit);
2171
2370
  };
2172
2371
  /**
2173
- * Writes breakeven data to disk.
2372
+ * Retrieves current average price for symbol.
2174
2373
  *
2175
- * Called by ClientBreakeven._persistState() after state changes.
2176
- * Creates directory and file if they don't exist.
2177
- * Uses atomic writes to prevent data corruption.
2374
+ * In live mode: fetches real-time average price from exchange API.
2375
+ * In backtest mode: calculates VWAP from candles in current timeframe.
2376
+ *
2377
+ * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
2378
+ * @returns Promise resolving to average price
2379
+ */
2380
+ this.getAveragePrice = async (symbol) => {
2381
+ this.loggerService.log("exchangeConnectionService getAveragePrice", {
2382
+ symbol,
2383
+ });
2384
+ return await this.getExchange(this.methodContextService.context.exchangeName).getAveragePrice(symbol);
2385
+ };
2386
+ /**
2387
+ * Formats price according to exchange-specific precision rules.
2388
+ *
2389
+ * Ensures price meets exchange requirements for decimal places and tick size.
2390
+ *
2391
+ * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
2392
+ * @param price - Raw price value to format
2393
+ * @returns Promise resolving to formatted price string
2394
+ */
2395
+ this.formatPrice = async (symbol, price) => {
2396
+ this.loggerService.log("exchangeConnectionService getAveragePrice", {
2397
+ symbol,
2398
+ price,
2399
+ });
2400
+ return await this.getExchange(this.methodContextService.context.exchangeName).formatPrice(symbol, price);
2401
+ };
2402
+ /**
2403
+ * Formats quantity according to exchange-specific precision rules.
2404
+ *
2405
+ * Ensures quantity meets exchange requirements for decimal places and lot size.
2406
+ *
2407
+ * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
2408
+ * @param quantity - Raw quantity value to format
2409
+ * @returns Promise resolving to formatted quantity string
2410
+ */
2411
+ this.formatQuantity = async (symbol, quantity) => {
2412
+ this.loggerService.log("exchangeConnectionService getAveragePrice", {
2413
+ symbol,
2414
+ quantity,
2415
+ });
2416
+ return await this.getExchange(this.methodContextService.context.exchangeName).formatQuantity(symbol, quantity);
2417
+ };
2418
+ /**
2419
+ * Fetches order book for a trading pair using configured exchange.
2420
+ *
2421
+ * Routes to exchange determined by methodContextService.context.exchangeName.
2422
+ * The ClientExchange will calculate time range and pass it to the schema
2423
+ * implementation, which may use (backtest) or ignore (live) the parameters.
2178
2424
  *
2179
- * @param breakevenData - Breakeven data record to persist
2180
- * @param symbol - Trading pair symbol
2181
- * @param strategyName - Strategy identifier
2182
- * @param signalId - Signal identifier
2183
- * @param exchangeName - Exchange identifier
2184
- * @returns Promise that resolves when write is complete
2425
+ * @param symbol - Trading pair symbol (e.g., "BTCUSDT")
2426
+ * @param depth - Maximum depth levels (default: CC_ORDER_BOOK_MAX_DEPTH_LEVELS)
2427
+ * @returns Promise resolving to order book data
2185
2428
  */
2186
- this.writeBreakevenData = async (breakevenData, symbol, strategyName, signalId, exchangeName) => {
2187
- bt.loggerService.info(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_WRITE_DATA);
2188
- const key = `${symbol}:${strategyName}:${exchangeName}`;
2189
- const isInitial = !this.getBreakevenStorage.has(key);
2190
- const stateStorage = this.getBreakevenStorage(symbol, strategyName, exchangeName);
2191
- await stateStorage.waitForInit(isInitial);
2192
- await stateStorage.writeValue(signalId, breakevenData);
2429
+ this.getOrderBook = async (symbol, depth) => {
2430
+ this.loggerService.log("exchangeConnectionService getOrderBook", {
2431
+ symbol,
2432
+ depth,
2433
+ });
2434
+ return await this.getExchange(this.methodContextService.context.exchangeName).getOrderBook(symbol, depth);
2193
2435
  };
2194
2436
  }
2195
- /**
2196
- * Registers a custom persistence adapter.
2197
- *
2198
- * @param Ctor - Custom PersistBase constructor
2199
- *
2200
- * @example
2201
- * ```typescript
2202
- * class RedisPersist extends PersistBase {
2203
- * async readValue(id) { return JSON.parse(await redis.get(id)); }
2204
- * async writeValue(id, entity) { await redis.set(id, JSON.stringify(entity)); }
2205
- * }
2206
- * PersistBreakevenAdapter.usePersistBreakevenAdapter(RedisPersist);
2207
- * ```
2208
- */
2209
- usePersistBreakevenAdapter(Ctor) {
2210
- bt.loggerService.info(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_PERSIST_BREAKEVEN_ADAPTER);
2211
- this.PersistBreakevenFactory = Ctor;
2212
- }
2213
- /**
2214
- * Switches to the default JSON persist adapter.
2215
- * All future persistence writes will use JSON storage.
2216
- */
2217
- useJson() {
2218
- bt.loggerService.log(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_JSON);
2219
- this.usePersistBreakevenAdapter(PersistBase);
2220
- }
2221
- /**
2222
- * Switches to a dummy persist adapter that discards all writes.
2223
- * All future persistence writes will be no-ops.
2224
- */
2225
- useDummy() {
2226
- bt.loggerService.log(PERSIST_BREAKEVEN_UTILS_METHOD_NAME_USE_DUMMY);
2227
- this.usePersistBreakevenAdapter(PersistDummy);
2228
- }
2229
2437
  }
2438
+
2230
2439
  /**
2231
- * Global singleton instance of PersistBreakevenUtils.
2232
- * Used by ClientBreakeven for breakeven state persistence.
2440
+ * Calculates profit/loss for a closed signal with slippage and fees.
2441
+ *
2442
+ * For signals with partial closes:
2443
+ * - Calculates weighted PNL: Σ(percent_i × pnl_i) for each partial + (remaining% × final_pnl)
2444
+ * - Each partial close has its own fees and slippage
2445
+ * - Total fees = 2 × (number of partial closes + 1 final close) × CC_PERCENT_FEE
2446
+ *
2447
+ * Formula breakdown:
2448
+ * 1. Apply slippage to open/close prices (worse execution)
2449
+ * - LONG: buy higher (+slippage), sell lower (-slippage)
2450
+ * - SHORT: sell lower (-slippage), buy higher (+slippage)
2451
+ * 2. Calculate raw PNL percentage
2452
+ * - LONG: ((closePrice - openPrice) / openPrice) * 100
2453
+ * - SHORT: ((openPrice - closePrice) / openPrice) * 100
2454
+ * 3. Subtract total fees (0.1% * 2 = 0.2% per transaction)
2455
+ *
2456
+ * @param signal - Closed signal with position details and optional partial history
2457
+ * @param priceClose - Actual close price at final exit
2458
+ * @returns PNL data with percentage and prices
2233
2459
  *
2234
2460
  * @example
2235
2461
  * ```typescript
2236
- * // Custom adapter
2237
- * PersistBreakevenAdapter.usePersistBreakevenAdapter(RedisPersist);
2238
- *
2239
- * // Read breakeven data
2240
- * const breakevenData = await PersistBreakevenAdapter.readBreakevenData("BTCUSDT", "my-strategy");
2462
+ * // Signal without partial closes
2463
+ * const pnl = toProfitLossDto(
2464
+ * {
2465
+ * position: "long",
2466
+ * priceOpen: 100,
2467
+ * },
2468
+ * 110 // close at +10%
2469
+ * );
2470
+ * console.log(pnl.pnlPercentage); // ~9.6% (after slippage and fees)
2241
2471
  *
2242
- * // Write breakeven data
2243
- * await PersistBreakevenAdapter.writeBreakevenData(breakevenData, "BTCUSDT", "my-strategy");
2472
+ * // Signal with partial closes
2473
+ * const pnlPartial = toProfitLossDto(
2474
+ * {
2475
+ * position: "long",
2476
+ * priceOpen: 100,
2477
+ * _partial: [
2478
+ * { type: "profit", percent: 30, price: 120 }, // +20% on 30%
2479
+ * { type: "profit", percent: 40, price: 115 }, // +15% on 40%
2480
+ * ],
2481
+ * },
2482
+ * 105 // final close at +5% for remaining 30%
2483
+ * );
2484
+ * // Weighted PNL = 30% × 20% + 40% × 15% + 30% × 5% = 6% + 6% + 1.5% = 13.5% (before fees)
2244
2485
  * ```
2245
2486
  */
2246
- const PersistBreakevenAdapter = new PersistBreakevenUtils();
2487
+ const toProfitLossDto = (signal, priceClose) => {
2488
+ const priceOpen = signal.priceOpen;
2489
+ // Calculate weighted PNL with partial closes
2490
+ if (signal._partial && signal._partial.length > 0) {
2491
+ let totalWeightedPnl = 0;
2492
+ let totalFees = 0;
2493
+ // Calculate PNL for each partial close
2494
+ for (const partial of signal._partial) {
2495
+ const partialPercent = partial.percent;
2496
+ const partialPrice = partial.price;
2497
+ // Apply slippage to prices
2498
+ let priceOpenWithSlippage;
2499
+ let priceCloseWithSlippage;
2500
+ if (signal.position === "long") {
2501
+ priceOpenWithSlippage = priceOpen * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2502
+ priceCloseWithSlippage = partialPrice * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2503
+ }
2504
+ else {
2505
+ priceOpenWithSlippage = priceOpen * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2506
+ priceCloseWithSlippage = partialPrice * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2507
+ }
2508
+ // Calculate PNL for this partial
2509
+ let partialPnl;
2510
+ if (signal.position === "long") {
2511
+ partialPnl = ((priceCloseWithSlippage - priceOpenWithSlippage) / priceOpenWithSlippage) * 100;
2512
+ }
2513
+ else {
2514
+ partialPnl = ((priceOpenWithSlippage - priceCloseWithSlippage) / priceOpenWithSlippage) * 100;
2515
+ }
2516
+ // Weight by percentage of position closed
2517
+ const weightedPnl = (partialPercent / 100) * partialPnl;
2518
+ totalWeightedPnl += weightedPnl;
2519
+ // Each partial has fees for open + close (2 transactions)
2520
+ totalFees += GLOBAL_CONFIG.CC_PERCENT_FEE * 2;
2521
+ }
2522
+ // Calculate PNL for remaining position (if any)
2523
+ // Compute totalClosed from _partial array
2524
+ const totalClosed = signal._partial.reduce((sum, p) => sum + p.percent, 0);
2525
+ const remainingPercent = 100 - totalClosed;
2526
+ if (remainingPercent > 0) {
2527
+ // Apply slippage
2528
+ let priceOpenWithSlippage;
2529
+ let priceCloseWithSlippage;
2530
+ if (signal.position === "long") {
2531
+ priceOpenWithSlippage = priceOpen * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2532
+ priceCloseWithSlippage = priceClose * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2533
+ }
2534
+ else {
2535
+ priceOpenWithSlippage = priceOpen * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2536
+ priceCloseWithSlippage = priceClose * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2537
+ }
2538
+ // Calculate PNL for remaining
2539
+ let remainingPnl;
2540
+ if (signal.position === "long") {
2541
+ remainingPnl = ((priceCloseWithSlippage - priceOpenWithSlippage) / priceOpenWithSlippage) * 100;
2542
+ }
2543
+ else {
2544
+ remainingPnl = ((priceOpenWithSlippage - priceCloseWithSlippage) / priceOpenWithSlippage) * 100;
2545
+ }
2546
+ // Weight by remaining percentage
2547
+ const weightedRemainingPnl = (remainingPercent / 100) * remainingPnl;
2548
+ totalWeightedPnl += weightedRemainingPnl;
2549
+ // Final close also has fees
2550
+ totalFees += GLOBAL_CONFIG.CC_PERCENT_FEE * 2;
2551
+ }
2552
+ // Subtract total fees from weighted PNL
2553
+ const pnlPercentage = totalWeightedPnl - totalFees;
2554
+ return {
2555
+ pnlPercentage,
2556
+ priceOpen,
2557
+ priceClose,
2558
+ };
2559
+ }
2560
+ // Original logic for signals without partial closes
2561
+ let priceOpenWithSlippage;
2562
+ let priceCloseWithSlippage;
2563
+ if (signal.position === "long") {
2564
+ // LONG: покупаем дороже, продаем дешевле
2565
+ priceOpenWithSlippage = priceOpen * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2566
+ priceCloseWithSlippage = priceClose * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2567
+ }
2568
+ else {
2569
+ // SHORT: продаем дешевле, покупаем дороже
2570
+ priceOpenWithSlippage = priceOpen * (1 - GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2571
+ priceCloseWithSlippage = priceClose * (1 + GLOBAL_CONFIG.CC_PERCENT_SLIPPAGE / 100);
2572
+ }
2573
+ // Применяем комиссию дважды (при открытии и закрытии)
2574
+ const totalFee = GLOBAL_CONFIG.CC_PERCENT_FEE * 2;
2575
+ let pnlPercentage;
2576
+ if (signal.position === "long") {
2577
+ // LONG: прибыль при росте цены
2578
+ pnlPercentage =
2579
+ ((priceCloseWithSlippage - priceOpenWithSlippage) /
2580
+ priceOpenWithSlippage) *
2581
+ 100;
2582
+ }
2583
+ else {
2584
+ // SHORT: прибыль при падении цены
2585
+ pnlPercentage =
2586
+ ((priceOpenWithSlippage - priceCloseWithSlippage) /
2587
+ priceOpenWithSlippage) *
2588
+ 100;
2589
+ }
2590
+ // Вычитаем комиссии
2591
+ pnlPercentage -= totalFee;
2592
+ return {
2593
+ pnlPercentage,
2594
+ priceOpen,
2595
+ priceClose,
2596
+ };
2597
+ };
2247
2598
 
2248
2599
  /**
2249
2600
  * Converts markdown content to plain text with minimal formatting
@@ -31531,6 +31882,61 @@ const CREATE_EXCHANGE_INSTANCE_FN = (schema) => {
31531
31882
  getOrderBook,
31532
31883
  };
31533
31884
  };
31885
+ /**
31886
+ * Attempts to read candles from cache.
31887
+ * Validates cache consistency (no gaps in timestamps) before returning.
31888
+ *
31889
+ * @param dto - Data transfer object containing symbol, interval, and limit
31890
+ * @param sinceTimestamp - Start timestamp in milliseconds
31891
+ * @param untilTimestamp - End timestamp in milliseconds
31892
+ * @param exchangeName - Exchange name
31893
+ * @returns Cached candles array or null if cache miss or inconsistent
31894
+ */
31895
+ const READ_CANDLES_CACHE_FN = functoolsKit.trycatch(async (dto, sinceTimestamp, untilTimestamp, exchangeName) => {
31896
+ const cachedCandles = await PersistCandleAdapter.readCandlesData(dto.symbol, dto.interval, exchangeName, dto.limit, sinceTimestamp, untilTimestamp);
31897
+ // Return cached data only if we have exactly the requested limit
31898
+ if (cachedCandles.length === dto.limit) {
31899
+ bt.loggerService.debug(`ExchangeInstance READ_CANDLES_CACHE_FN: cache hit for exchangeName=${exchangeName}, symbol=${dto.symbol}, interval=${dto.interval}, limit=${dto.limit}`);
31900
+ return cachedCandles;
31901
+ }
31902
+ bt.loggerService.warn(`ExchangeInstance READ_CANDLES_CACHE_FN: cache inconsistent (count or range mismatch) for exchangeName=${exchangeName}, symbol=${dto.symbol}, interval=${dto.interval}, limit=${dto.limit}`);
31903
+ return null;
31904
+ }, {
31905
+ fallback: async (error) => {
31906
+ const message = `ExchangeInstance READ_CANDLES_CACHE_FN: cache read failed`;
31907
+ const payload = {
31908
+ error: functoolsKit.errorData(error),
31909
+ message: functoolsKit.getErrorMessage(error),
31910
+ };
31911
+ bt.loggerService.warn(message, payload);
31912
+ console.warn(message, payload);
31913
+ errorEmitter.next(error);
31914
+ },
31915
+ defaultValue: null,
31916
+ });
31917
+ /**
31918
+ * Writes candles to cache with error handling.
31919
+ *
31920
+ * @param candles - Array of candle data to cache
31921
+ * @param dto - Data transfer object containing symbol, interval, and limit
31922
+ * @param exchangeName - Exchange name
31923
+ */
31924
+ const WRITE_CANDLES_CACHE_FN = functoolsKit.trycatch(functoolsKit.queued(async (candles, dto, exchangeName) => {
31925
+ await PersistCandleAdapter.writeCandlesData(candles, dto.symbol, dto.interval, exchangeName);
31926
+ bt.loggerService.debug(`ExchangeInstance WRITE_CANDLES_CACHE_FN: cache updated for exchangeName=${exchangeName}, symbol=${dto.symbol}, interval=${dto.interval}, count=${candles.length}`);
31927
+ }), {
31928
+ fallback: async (error) => {
31929
+ const message = `ExchangeInstance WRITE_CANDLES_CACHE_FN: cache write failed`;
31930
+ const payload = {
31931
+ error: functoolsKit.errorData(error),
31932
+ message: functoolsKit.getErrorMessage(error),
31933
+ };
31934
+ bt.loggerService.warn(message, payload);
31935
+ console.warn(message, payload);
31936
+ errorEmitter.next(error);
31937
+ },
31938
+ defaultValue: null,
31939
+ });
31534
31940
  /**
31535
31941
  * Instance class for exchange operations on a specific exchange.
31536
31942
  *
@@ -31588,6 +31994,13 @@ class ExchangeInstance {
31588
31994
  }
31589
31995
  const when = new Date(Date.now());
31590
31996
  const since = new Date(when.getTime() - adjust * 60 * 1000);
31997
+ const sinceTimestamp = since.getTime();
31998
+ const untilTimestamp = sinceTimestamp + limit * step * 60 * 1000;
31999
+ // Try to read from cache first
32000
+ const cachedCandles = await READ_CANDLES_CACHE_FN({ symbol, interval, limit }, sinceTimestamp, untilTimestamp, this.exchangeName);
32001
+ if (cachedCandles !== null) {
32002
+ return cachedCandles;
32003
+ }
31591
32004
  let allData = [];
31592
32005
  // If limit exceeds CC_MAX_CANDLES_PER_REQUEST, fetch data in chunks
31593
32006
  if (limit > GLOBAL_CONFIG.CC_MAX_CANDLES_PER_REQUEST) {
@@ -31610,7 +32023,6 @@ class ExchangeInstance {
31610
32023
  allData = await getCandles(symbol, interval, since, limit, isBacktest);
31611
32024
  }
31612
32025
  // Filter candles to strictly match the requested range
31613
- const sinceTimestamp = since.getTime();
31614
32026
  const whenTimestamp = when.getTime();
31615
32027
  const stepMs = step * 60 * 1000;
31616
32028
  const filteredData = allData.filter((candle) => candle.timestamp >= sinceTimestamp && candle.timestamp < whenTimestamp + stepMs);
@@ -31622,6 +32034,8 @@ class ExchangeInstance {
31622
32034
  if (uniqueData.length < limit) {
31623
32035
  bt.loggerService.warn(`ExchangeInstance Expected ${limit} candles, got ${uniqueData.length}`);
31624
32036
  }
32037
+ // Write to cache after successful fetch
32038
+ await WRITE_CANDLES_CACHE_FN(uniqueData, { symbol, interval, limit }, this.exchangeName);
31625
32039
  return uniqueData;
31626
32040
  };
31627
32041
  /**
@@ -33000,6 +33414,7 @@ exports.Partial = Partial;
33000
33414
  exports.Performance = Performance;
33001
33415
  exports.PersistBase = PersistBase;
33002
33416
  exports.PersistBreakevenAdapter = PersistBreakevenAdapter;
33417
+ exports.PersistCandleAdapter = PersistCandleAdapter;
33003
33418
  exports.PersistPartialAdapter = PersistPartialAdapter;
33004
33419
  exports.PersistRiskAdapter = PersistRiskAdapter;
33005
33420
  exports.PersistScheduleAdapter = PersistScheduleAdapter;