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