backtest-kit 1.5.20 → 1.5.22
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/README.md +15 -2
- package/build/index.cjs +409 -245
- package/build/index.mjs +409 -245
- package/package.json +1 -1
- package/types.d.ts +351 -345
package/types.d.ts
CHANGED
|
@@ -950,6 +950,8 @@ interface IStrategySchema {
|
|
|
950
950
|
callbacks?: Partial<IStrategyCallbacks>;
|
|
951
951
|
/** Optional risk profile identifier for risk management */
|
|
952
952
|
riskName?: RiskName;
|
|
953
|
+
/** Optional several risk profile list for risk management (if multiple required) */
|
|
954
|
+
riskList?: RiskName[];
|
|
953
955
|
}
|
|
954
956
|
/**
|
|
955
957
|
* Reason why signal was closed.
|
|
@@ -7355,6 +7357,307 @@ declare class ExchangeConnectionService implements IExchange {
|
|
|
7355
7357
|
formatQuantity: (symbol: string, quantity: number) => Promise<string>;
|
|
7356
7358
|
}
|
|
7357
7359
|
|
|
7360
|
+
/**
|
|
7361
|
+
* Service for managing strategy schema registry.
|
|
7362
|
+
*
|
|
7363
|
+
* Uses ToolRegistry from functools-kit for type-safe schema storage.
|
|
7364
|
+
* Strategies are registered via addStrategy() and retrieved by name.
|
|
7365
|
+
*/
|
|
7366
|
+
declare class StrategySchemaService {
|
|
7367
|
+
readonly loggerService: LoggerService;
|
|
7368
|
+
private _registry;
|
|
7369
|
+
/**
|
|
7370
|
+
* Registers a new strategy schema.
|
|
7371
|
+
*
|
|
7372
|
+
* @param key - Unique strategy name
|
|
7373
|
+
* @param value - Strategy schema configuration
|
|
7374
|
+
* @throws Error if strategy name already exists
|
|
7375
|
+
*/
|
|
7376
|
+
register: (key: StrategyName, value: IStrategySchema) => void;
|
|
7377
|
+
/**
|
|
7378
|
+
* Validates strategy schema structure for required properties.
|
|
7379
|
+
*
|
|
7380
|
+
* Performs shallow validation to ensure all required properties exist
|
|
7381
|
+
* and have correct types before registration in the registry.
|
|
7382
|
+
*
|
|
7383
|
+
* @param strategySchema - Strategy schema to validate
|
|
7384
|
+
* @throws Error if strategyName is missing or not a string
|
|
7385
|
+
* @throws Error if interval is missing or not a valid SignalInterval
|
|
7386
|
+
* @throws Error if getSignal is missing or not a function
|
|
7387
|
+
*/
|
|
7388
|
+
private validateShallow;
|
|
7389
|
+
/**
|
|
7390
|
+
* Overrides an existing strategy schema with partial updates.
|
|
7391
|
+
*
|
|
7392
|
+
* @param key - Strategy name to override
|
|
7393
|
+
* @param value - Partial schema updates
|
|
7394
|
+
* @returns Updated strategy schema
|
|
7395
|
+
* @throws Error if strategy name doesn't exist
|
|
7396
|
+
*/
|
|
7397
|
+
override: (key: StrategyName, value: Partial<IStrategySchema>) => IStrategySchema;
|
|
7398
|
+
/**
|
|
7399
|
+
* Retrieves a strategy schema by name.
|
|
7400
|
+
*
|
|
7401
|
+
* @param key - Strategy name
|
|
7402
|
+
* @returns Strategy schema configuration
|
|
7403
|
+
* @throws Error if strategy name doesn't exist
|
|
7404
|
+
*/
|
|
7405
|
+
get: (key: StrategyName) => IStrategySchema;
|
|
7406
|
+
}
|
|
7407
|
+
|
|
7408
|
+
/** Type for active position map */
|
|
7409
|
+
type RiskMap = Map<string, IRiskActivePosition>;
|
|
7410
|
+
/** Symbol indicating that positions need to be fetched from persistence */
|
|
7411
|
+
declare const POSITION_NEED_FETCH: unique symbol;
|
|
7412
|
+
/**
|
|
7413
|
+
* ClientRisk implementation for portfolio-level risk management.
|
|
7414
|
+
*
|
|
7415
|
+
* Provides risk checking logic to prevent signals that violate configured limits:
|
|
7416
|
+
* - Maximum concurrent positions (tracks across all strategies)
|
|
7417
|
+
* - Custom validations with access to all active positions
|
|
7418
|
+
*
|
|
7419
|
+
* Multiple ClientStrategy instances share the same ClientRisk instance,
|
|
7420
|
+
* allowing cross-strategy risk analysis.
|
|
7421
|
+
*
|
|
7422
|
+
* Used internally by strategy execution to validate signals before opening positions.
|
|
7423
|
+
*/
|
|
7424
|
+
declare class ClientRisk implements IRisk {
|
|
7425
|
+
readonly params: IRiskParams;
|
|
7426
|
+
/**
|
|
7427
|
+
* Map of active positions tracked across all strategies.
|
|
7428
|
+
* Key: `${strategyName}:${exchangeName}:${symbol}`
|
|
7429
|
+
* Starts as POSITION_NEED_FETCH symbol, gets initialized on first use.
|
|
7430
|
+
*/
|
|
7431
|
+
_activePositions: RiskMap | typeof POSITION_NEED_FETCH;
|
|
7432
|
+
constructor(params: IRiskParams);
|
|
7433
|
+
/**
|
|
7434
|
+
* Initializes active positions by loading from persistence.
|
|
7435
|
+
* Uses singleshot pattern to ensure initialization happens exactly once.
|
|
7436
|
+
* Skips persistence in backtest mode.
|
|
7437
|
+
*/
|
|
7438
|
+
private waitForInit;
|
|
7439
|
+
/**
|
|
7440
|
+
* Persists current active positions to disk.
|
|
7441
|
+
*/
|
|
7442
|
+
private _updatePositions;
|
|
7443
|
+
/**
|
|
7444
|
+
* Registers a new opened signal.
|
|
7445
|
+
* Called by StrategyConnectionService after signal is opened.
|
|
7446
|
+
*/
|
|
7447
|
+
addSignal(symbol: string, context: {
|
|
7448
|
+
strategyName: string;
|
|
7449
|
+
riskName: string;
|
|
7450
|
+
}): Promise<void>;
|
|
7451
|
+
/**
|
|
7452
|
+
* Removes a closed signal.
|
|
7453
|
+
* Called by StrategyConnectionService when signal is closed.
|
|
7454
|
+
*/
|
|
7455
|
+
removeSignal(symbol: string, context: {
|
|
7456
|
+
strategyName: string;
|
|
7457
|
+
riskName: string;
|
|
7458
|
+
}): Promise<void>;
|
|
7459
|
+
/**
|
|
7460
|
+
* Checks if a signal should be allowed based on risk limits.
|
|
7461
|
+
*
|
|
7462
|
+
* Executes custom validations with access to:
|
|
7463
|
+
* - Passthrough params from ClientStrategy (symbol, strategyName, exchangeName, currentPrice, timestamp)
|
|
7464
|
+
* - Active positions via this.activePositions getter
|
|
7465
|
+
*
|
|
7466
|
+
* Returns false immediately if any validation throws error.
|
|
7467
|
+
* Triggers callbacks (onRejected, onAllowed) based on result.
|
|
7468
|
+
*
|
|
7469
|
+
* @param params - Risk check arguments (passthrough from ClientStrategy)
|
|
7470
|
+
* @returns Promise resolving to true if allowed, false if rejected
|
|
7471
|
+
*/
|
|
7472
|
+
checkSignal: (params: IRiskCheckArgs) => Promise<boolean>;
|
|
7473
|
+
}
|
|
7474
|
+
|
|
7475
|
+
/**
|
|
7476
|
+
* Connection service routing risk operations to correct ClientRisk instance.
|
|
7477
|
+
*
|
|
7478
|
+
* Routes risk checking calls to the appropriate risk implementation
|
|
7479
|
+
* based on the provided riskName parameter. Uses memoization to cache
|
|
7480
|
+
* ClientRisk instances for performance.
|
|
7481
|
+
*
|
|
7482
|
+
* Key features:
|
|
7483
|
+
* - Explicit risk routing via riskName parameter
|
|
7484
|
+
* - Memoized ClientRisk instances by riskName
|
|
7485
|
+
* - Risk limit validation for signals
|
|
7486
|
+
*
|
|
7487
|
+
* Note: riskName is empty string for strategies without risk configuration.
|
|
7488
|
+
*
|
|
7489
|
+
* @example
|
|
7490
|
+
* ```typescript
|
|
7491
|
+
* // Used internally by framework
|
|
7492
|
+
* const result = await riskConnectionService.checkSignal(
|
|
7493
|
+
* {
|
|
7494
|
+
* symbol: "BTCUSDT",
|
|
7495
|
+
* positionSize: 0.5,
|
|
7496
|
+
* currentPrice: 50000,
|
|
7497
|
+
* portfolioBalance: 100000,
|
|
7498
|
+
* currentDrawdown: 5,
|
|
7499
|
+
* currentPositions: 3,
|
|
7500
|
+
* dailyPnl: -2,
|
|
7501
|
+
* currentSymbolExposure: 8
|
|
7502
|
+
* },
|
|
7503
|
+
* { riskName: "conservative" }
|
|
7504
|
+
* );
|
|
7505
|
+
* ```
|
|
7506
|
+
*/
|
|
7507
|
+
declare class RiskConnectionService {
|
|
7508
|
+
private readonly loggerService;
|
|
7509
|
+
private readonly riskSchemaService;
|
|
7510
|
+
/**
|
|
7511
|
+
* Retrieves memoized ClientRisk instance for given risk name.
|
|
7512
|
+
*
|
|
7513
|
+
* Creates ClientRisk on first call, returns cached instance on subsequent calls.
|
|
7514
|
+
* Cache key is riskName string.
|
|
7515
|
+
*
|
|
7516
|
+
* @param riskName - Name of registered risk schema
|
|
7517
|
+
* @returns Configured ClientRisk instance
|
|
7518
|
+
*/
|
|
7519
|
+
getRisk: ((riskName: RiskName) => ClientRisk) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientRisk>;
|
|
7520
|
+
/**
|
|
7521
|
+
* Checks if a signal should be allowed based on risk limits.
|
|
7522
|
+
*
|
|
7523
|
+
* Routes to appropriate ClientRisk instance based on provided context.
|
|
7524
|
+
* Validates portfolio drawdown, symbol exposure, position count, and daily loss limits.
|
|
7525
|
+
* ClientRisk will emit riskSubject event via onRejected callback when signal is rejected.
|
|
7526
|
+
*
|
|
7527
|
+
* @param params - Risk check arguments (portfolio state, position details)
|
|
7528
|
+
* @param context - Execution context with risk name
|
|
7529
|
+
* @returns Promise resolving to risk check result
|
|
7530
|
+
*/
|
|
7531
|
+
checkSignal: (params: IRiskCheckArgs, context: {
|
|
7532
|
+
riskName: RiskName;
|
|
7533
|
+
}) => Promise<boolean>;
|
|
7534
|
+
/**
|
|
7535
|
+
* Registers an opened signal with the risk management system.
|
|
7536
|
+
* Routes to appropriate ClientRisk instance.
|
|
7537
|
+
*
|
|
7538
|
+
* @param symbol - Trading pair symbol
|
|
7539
|
+
* @param context - Context information (strategyName, riskName)
|
|
7540
|
+
*/
|
|
7541
|
+
addSignal: (symbol: string, context: {
|
|
7542
|
+
strategyName: string;
|
|
7543
|
+
riskName: RiskName;
|
|
7544
|
+
}) => Promise<void>;
|
|
7545
|
+
/**
|
|
7546
|
+
* Removes a closed signal from the risk management system.
|
|
7547
|
+
* Routes to appropriate ClientRisk instance.
|
|
7548
|
+
*
|
|
7549
|
+
* @param symbol - Trading pair symbol
|
|
7550
|
+
* @param context - Context information (strategyName, riskName)
|
|
7551
|
+
*/
|
|
7552
|
+
removeSignal: (symbol: string, context: {
|
|
7553
|
+
strategyName: string;
|
|
7554
|
+
riskName: RiskName;
|
|
7555
|
+
}) => Promise<void>;
|
|
7556
|
+
/**
|
|
7557
|
+
* Clears the cached ClientRisk instance for the given risk name.
|
|
7558
|
+
*
|
|
7559
|
+
* @param riskName - Name of the risk schema to clear from cache
|
|
7560
|
+
*/
|
|
7561
|
+
clear: (riskName?: RiskName) => Promise<void>;
|
|
7562
|
+
}
|
|
7563
|
+
|
|
7564
|
+
/**
|
|
7565
|
+
* Connection service for partial profit/loss tracking.
|
|
7566
|
+
*
|
|
7567
|
+
* Provides memoized ClientPartial instances per signal ID.
|
|
7568
|
+
* Acts as factory and lifetime manager for ClientPartial objects.
|
|
7569
|
+
*
|
|
7570
|
+
* Features:
|
|
7571
|
+
* - Creates one ClientPartial instance per signal ID (memoized)
|
|
7572
|
+
* - Configures instances with logger and event emitter callbacks
|
|
7573
|
+
* - Delegates profit/loss/clear operations to appropriate ClientPartial
|
|
7574
|
+
* - Cleans up memoized instances when signals are cleared
|
|
7575
|
+
*
|
|
7576
|
+
* Architecture:
|
|
7577
|
+
* - Injected into ClientStrategy via PartialGlobalService
|
|
7578
|
+
* - Uses memoize from functools-kit for instance caching
|
|
7579
|
+
* - Emits events to partialProfitSubject/partialLossSubject
|
|
7580
|
+
*
|
|
7581
|
+
* @example
|
|
7582
|
+
* ```typescript
|
|
7583
|
+
* // Service injected via DI
|
|
7584
|
+
* const service = inject<PartialConnectionService>(TYPES.partialConnectionService);
|
|
7585
|
+
*
|
|
7586
|
+
* // Called by ClientStrategy during signal monitoring
|
|
7587
|
+
* await service.profit("BTCUSDT", signal, 55000, 10.0, false, new Date());
|
|
7588
|
+
* // Creates or reuses ClientPartial for signal.id
|
|
7589
|
+
* // Delegates to ClientPartial.profit()
|
|
7590
|
+
*
|
|
7591
|
+
* // When signal closes
|
|
7592
|
+
* await service.clear("BTCUSDT", signal, 52000);
|
|
7593
|
+
* // Clears signal state and removes memoized instance
|
|
7594
|
+
* ```
|
|
7595
|
+
*/
|
|
7596
|
+
declare class PartialConnectionService implements IPartial {
|
|
7597
|
+
/**
|
|
7598
|
+
* Logger service injected from DI container.
|
|
7599
|
+
*/
|
|
7600
|
+
private readonly loggerService;
|
|
7601
|
+
/**
|
|
7602
|
+
* Memoized factory function for ClientPartial instances.
|
|
7603
|
+
*
|
|
7604
|
+
* Creates one ClientPartial per signal ID with configured callbacks.
|
|
7605
|
+
* Instances are cached until clear() is called.
|
|
7606
|
+
*
|
|
7607
|
+
* Key format: signalId
|
|
7608
|
+
* Value: ClientPartial instance with logger and event emitters
|
|
7609
|
+
*/
|
|
7610
|
+
private getPartial;
|
|
7611
|
+
/**
|
|
7612
|
+
* Processes profit state and emits events for newly reached profit levels.
|
|
7613
|
+
*
|
|
7614
|
+
* Retrieves or creates ClientPartial for signal ID, initializes it if needed,
|
|
7615
|
+
* then delegates to ClientPartial.profit() method.
|
|
7616
|
+
*
|
|
7617
|
+
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
7618
|
+
* @param data - Signal row data
|
|
7619
|
+
* @param currentPrice - Current market price
|
|
7620
|
+
* @param revenuePercent - Current profit percentage (positive value)
|
|
7621
|
+
* @param backtest - True if backtest mode, false if live mode
|
|
7622
|
+
* @param when - Event timestamp (current time for live, candle time for backtest)
|
|
7623
|
+
* @returns Promise that resolves when profit processing is complete
|
|
7624
|
+
*/
|
|
7625
|
+
profit: (symbol: string, data: ISignalRow, currentPrice: number, revenuePercent: number, backtest: boolean, when: Date) => Promise<void>;
|
|
7626
|
+
/**
|
|
7627
|
+
* Processes loss state and emits events for newly reached loss levels.
|
|
7628
|
+
*
|
|
7629
|
+
* Retrieves or creates ClientPartial for signal ID, initializes it if needed,
|
|
7630
|
+
* then delegates to ClientPartial.loss() method.
|
|
7631
|
+
*
|
|
7632
|
+
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
7633
|
+
* @param data - Signal row data
|
|
7634
|
+
* @param currentPrice - Current market price
|
|
7635
|
+
* @param lossPercent - Current loss percentage (negative value)
|
|
7636
|
+
* @param backtest - True if backtest mode, false if live mode
|
|
7637
|
+
* @param when - Event timestamp (current time for live, candle time for backtest)
|
|
7638
|
+
* @returns Promise that resolves when loss processing is complete
|
|
7639
|
+
*/
|
|
7640
|
+
loss: (symbol: string, data: ISignalRow, currentPrice: number, lossPercent: number, backtest: boolean, when: Date) => Promise<void>;
|
|
7641
|
+
/**
|
|
7642
|
+
* Clears partial profit/loss state when signal closes.
|
|
7643
|
+
*
|
|
7644
|
+
* Retrieves ClientPartial for signal ID, initializes if needed,
|
|
7645
|
+
* delegates clear operation, then removes memoized instance.
|
|
7646
|
+
*
|
|
7647
|
+
* Sequence:
|
|
7648
|
+
* 1. Get ClientPartial from memoize cache
|
|
7649
|
+
* 2. Ensure initialization (waitForInit)
|
|
7650
|
+
* 3. Call ClientPartial.clear() - removes state, persists to disk
|
|
7651
|
+
* 4. Clear memoized instance - prevents memory leaks
|
|
7652
|
+
*
|
|
7653
|
+
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
7654
|
+
* @param data - Signal row data
|
|
7655
|
+
* @param priceClose - Final closing price
|
|
7656
|
+
* @returns Promise that resolves when clear is complete
|
|
7657
|
+
*/
|
|
7658
|
+
clear: (symbol: string, data: ISignalRow, priceClose: number, backtest: boolean) => Promise<void>;
|
|
7659
|
+
}
|
|
7660
|
+
|
|
7358
7661
|
/**
|
|
7359
7662
|
* Connection service routing strategy operations to correct ClientStrategy instance.
|
|
7360
7663
|
*
|
|
@@ -7376,13 +7679,17 @@ declare class ExchangeConnectionService implements IExchange {
|
|
|
7376
7679
|
* ```
|
|
7377
7680
|
*/
|
|
7378
7681
|
declare class StrategyConnectionService {
|
|
7379
|
-
|
|
7380
|
-
|
|
7381
|
-
|
|
7382
|
-
|
|
7383
|
-
|
|
7384
|
-
|
|
7385
|
-
|
|
7682
|
+
readonly loggerService: LoggerService;
|
|
7683
|
+
readonly executionContextService: {
|
|
7684
|
+
readonly context: IExecutionContext;
|
|
7685
|
+
};
|
|
7686
|
+
readonly strategySchemaService: StrategySchemaService;
|
|
7687
|
+
readonly riskConnectionService: RiskConnectionService;
|
|
7688
|
+
readonly exchangeConnectionService: ExchangeConnectionService;
|
|
7689
|
+
readonly methodContextService: {
|
|
7690
|
+
readonly context: IMethodContext;
|
|
7691
|
+
};
|
|
7692
|
+
readonly partialConnectionService: PartialConnectionService;
|
|
7386
7693
|
/**
|
|
7387
7694
|
* Retrieves memoized ClientStrategy instance for given symbol-strategy pair.
|
|
7388
7695
|
*
|
|
@@ -7553,228 +7860,72 @@ declare class FrameConnectionService implements IFrame {
|
|
|
7553
7860
|
*/
|
|
7554
7861
|
declare class ClientSizing implements ISizing {
|
|
7555
7862
|
readonly params: ISizingParams;
|
|
7556
|
-
constructor(params: ISizingParams);
|
|
7557
|
-
/**
|
|
7558
|
-
* Calculates position size based on configured method and constraints.
|
|
7559
|
-
*
|
|
7560
|
-
* @param params - Calculation parameters (symbol, balance, prices, etc.)
|
|
7561
|
-
* @returns Promise resolving to calculated position size
|
|
7562
|
-
* @throws Error if required parameters are missing or invalid
|
|
7563
|
-
*/
|
|
7564
|
-
calculate(params: ISizingCalculateParams): Promise<number>;
|
|
7565
|
-
}
|
|
7566
|
-
|
|
7567
|
-
/**
|
|
7568
|
-
* Connection service routing sizing operations to correct ClientSizing instance.
|
|
7569
|
-
*
|
|
7570
|
-
* Routes sizing method calls to the appropriate sizing implementation
|
|
7571
|
-
* based on the provided sizingName parameter. Uses memoization to cache
|
|
7572
|
-
* ClientSizing instances for performance.
|
|
7573
|
-
*
|
|
7574
|
-
* Key features:
|
|
7575
|
-
* - Explicit sizing routing via sizingName parameter
|
|
7576
|
-
* - Memoized ClientSizing instances by sizingName
|
|
7577
|
-
* - Position size calculation with risk management
|
|
7578
|
-
*
|
|
7579
|
-
* Note: sizingName is empty string for strategies without sizing configuration.
|
|
7580
|
-
*
|
|
7581
|
-
* @example
|
|
7582
|
-
* ```typescript
|
|
7583
|
-
* // Used internally by framework
|
|
7584
|
-
* const quantity = await sizingConnectionService.calculate(
|
|
7585
|
-
* {
|
|
7586
|
-
* symbol: "BTCUSDT",
|
|
7587
|
-
* accountBalance: 10000,
|
|
7588
|
-
* priceOpen: 50000,
|
|
7589
|
-
* priceStopLoss: 49000,
|
|
7590
|
-
* method: "fixed-percentage"
|
|
7591
|
-
* },
|
|
7592
|
-
* { sizingName: "conservative" }
|
|
7593
|
-
* );
|
|
7594
|
-
* ```
|
|
7595
|
-
*/
|
|
7596
|
-
declare class SizingConnectionService {
|
|
7597
|
-
private readonly loggerService;
|
|
7598
|
-
private readonly sizingSchemaService;
|
|
7599
|
-
/**
|
|
7600
|
-
* Retrieves memoized ClientSizing instance for given sizing name.
|
|
7601
|
-
*
|
|
7602
|
-
* Creates ClientSizing on first call, returns cached instance on subsequent calls.
|
|
7603
|
-
* Cache key is sizingName string.
|
|
7604
|
-
*
|
|
7605
|
-
* @param sizingName - Name of registered sizing schema
|
|
7606
|
-
* @returns Configured ClientSizing instance
|
|
7607
|
-
*/
|
|
7608
|
-
getSizing: ((sizingName: SizingName) => ClientSizing) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientSizing>;
|
|
7609
|
-
/**
|
|
7610
|
-
* Calculates position size based on risk parameters and configured method.
|
|
7611
|
-
*
|
|
7612
|
-
* Routes to appropriate ClientSizing instance based on provided context.
|
|
7613
|
-
* Supports multiple sizing methods: fixed-percentage, kelly-criterion, atr-based.
|
|
7614
|
-
*
|
|
7615
|
-
* @param params - Calculation parameters (symbol, balance, prices, method-specific data)
|
|
7616
|
-
* @param context - Execution context with sizing name
|
|
7617
|
-
* @returns Promise resolving to calculated position size
|
|
7618
|
-
*/
|
|
7619
|
-
calculate: (params: ISizingCalculateParams, context: {
|
|
7620
|
-
sizingName: SizingName;
|
|
7621
|
-
}) => Promise<number>;
|
|
7622
|
-
}
|
|
7623
|
-
|
|
7624
|
-
/** Type for active position map */
|
|
7625
|
-
type RiskMap = Map<string, IRiskActivePosition>;
|
|
7626
|
-
/** Symbol indicating that positions need to be fetched from persistence */
|
|
7627
|
-
declare const POSITION_NEED_FETCH: unique symbol;
|
|
7628
|
-
/**
|
|
7629
|
-
* ClientRisk implementation for portfolio-level risk management.
|
|
7630
|
-
*
|
|
7631
|
-
* Provides risk checking logic to prevent signals that violate configured limits:
|
|
7632
|
-
* - Maximum concurrent positions (tracks across all strategies)
|
|
7633
|
-
* - Custom validations with access to all active positions
|
|
7634
|
-
*
|
|
7635
|
-
* Multiple ClientStrategy instances share the same ClientRisk instance,
|
|
7636
|
-
* allowing cross-strategy risk analysis.
|
|
7637
|
-
*
|
|
7638
|
-
* Used internally by strategy execution to validate signals before opening positions.
|
|
7639
|
-
*/
|
|
7640
|
-
declare class ClientRisk implements IRisk {
|
|
7641
|
-
readonly params: IRiskParams;
|
|
7642
|
-
/**
|
|
7643
|
-
* Map of active positions tracked across all strategies.
|
|
7644
|
-
* Key: `${strategyName}:${exchangeName}:${symbol}`
|
|
7645
|
-
* Starts as POSITION_NEED_FETCH symbol, gets initialized on first use.
|
|
7646
|
-
*/
|
|
7647
|
-
_activePositions: RiskMap | typeof POSITION_NEED_FETCH;
|
|
7648
|
-
constructor(params: IRiskParams);
|
|
7649
|
-
/**
|
|
7650
|
-
* Initializes active positions by loading from persistence.
|
|
7651
|
-
* Uses singleshot pattern to ensure initialization happens exactly once.
|
|
7652
|
-
* Skips persistence in backtest mode.
|
|
7653
|
-
*/
|
|
7654
|
-
private waitForInit;
|
|
7655
|
-
/**
|
|
7656
|
-
* Persists current active positions to disk.
|
|
7657
|
-
*/
|
|
7658
|
-
private _updatePositions;
|
|
7659
|
-
/**
|
|
7660
|
-
* Registers a new opened signal.
|
|
7661
|
-
* Called by StrategyConnectionService after signal is opened.
|
|
7662
|
-
*/
|
|
7663
|
-
addSignal(symbol: string, context: {
|
|
7664
|
-
strategyName: string;
|
|
7665
|
-
riskName: string;
|
|
7666
|
-
}): Promise<void>;
|
|
7667
|
-
/**
|
|
7668
|
-
* Removes a closed signal.
|
|
7669
|
-
* Called by StrategyConnectionService when signal is closed.
|
|
7670
|
-
*/
|
|
7671
|
-
removeSignal(symbol: string, context: {
|
|
7672
|
-
strategyName: string;
|
|
7673
|
-
riskName: string;
|
|
7674
|
-
}): Promise<void>;
|
|
7863
|
+
constructor(params: ISizingParams);
|
|
7675
7864
|
/**
|
|
7676
|
-
*
|
|
7677
|
-
*
|
|
7678
|
-
* Executes custom validations with access to:
|
|
7679
|
-
* - Passthrough params from ClientStrategy (symbol, strategyName, exchangeName, currentPrice, timestamp)
|
|
7680
|
-
* - Active positions via this.activePositions getter
|
|
7681
|
-
*
|
|
7682
|
-
* Returns false immediately if any validation throws error.
|
|
7683
|
-
* Triggers callbacks (onRejected, onAllowed) based on result.
|
|
7865
|
+
* Calculates position size based on configured method and constraints.
|
|
7684
7866
|
*
|
|
7685
|
-
* @param params -
|
|
7686
|
-
* @returns Promise resolving to
|
|
7867
|
+
* @param params - Calculation parameters (symbol, balance, prices, etc.)
|
|
7868
|
+
* @returns Promise resolving to calculated position size
|
|
7869
|
+
* @throws Error if required parameters are missing or invalid
|
|
7687
7870
|
*/
|
|
7688
|
-
|
|
7871
|
+
calculate(params: ISizingCalculateParams): Promise<number>;
|
|
7689
7872
|
}
|
|
7690
7873
|
|
|
7691
7874
|
/**
|
|
7692
|
-
* Connection service routing
|
|
7875
|
+
* Connection service routing sizing operations to correct ClientSizing instance.
|
|
7693
7876
|
*
|
|
7694
|
-
* Routes
|
|
7695
|
-
* based on the provided
|
|
7696
|
-
*
|
|
7877
|
+
* Routes sizing method calls to the appropriate sizing implementation
|
|
7878
|
+
* based on the provided sizingName parameter. Uses memoization to cache
|
|
7879
|
+
* ClientSizing instances for performance.
|
|
7697
7880
|
*
|
|
7698
7881
|
* Key features:
|
|
7699
|
-
* - Explicit
|
|
7700
|
-
* - Memoized
|
|
7701
|
-
* -
|
|
7882
|
+
* - Explicit sizing routing via sizingName parameter
|
|
7883
|
+
* - Memoized ClientSizing instances by sizingName
|
|
7884
|
+
* - Position size calculation with risk management
|
|
7702
7885
|
*
|
|
7703
|
-
* Note:
|
|
7886
|
+
* Note: sizingName is empty string for strategies without sizing configuration.
|
|
7704
7887
|
*
|
|
7705
7888
|
* @example
|
|
7706
7889
|
* ```typescript
|
|
7707
7890
|
* // Used internally by framework
|
|
7708
|
-
* const
|
|
7891
|
+
* const quantity = await sizingConnectionService.calculate(
|
|
7709
7892
|
* {
|
|
7710
7893
|
* symbol: "BTCUSDT",
|
|
7711
|
-
*
|
|
7712
|
-
*
|
|
7713
|
-
*
|
|
7714
|
-
*
|
|
7715
|
-
* currentPositions: 3,
|
|
7716
|
-
* dailyPnl: -2,
|
|
7717
|
-
* currentSymbolExposure: 8
|
|
7894
|
+
* accountBalance: 10000,
|
|
7895
|
+
* priceOpen: 50000,
|
|
7896
|
+
* priceStopLoss: 49000,
|
|
7897
|
+
* method: "fixed-percentage"
|
|
7718
7898
|
* },
|
|
7719
|
-
* {
|
|
7899
|
+
* { sizingName: "conservative" }
|
|
7720
7900
|
* );
|
|
7721
7901
|
* ```
|
|
7722
7902
|
*/
|
|
7723
|
-
declare class
|
|
7903
|
+
declare class SizingConnectionService {
|
|
7724
7904
|
private readonly loggerService;
|
|
7725
|
-
private readonly
|
|
7726
|
-
/**
|
|
7727
|
-
* Retrieves memoized ClientRisk instance for given risk name.
|
|
7728
|
-
*
|
|
7729
|
-
* Creates ClientRisk on first call, returns cached instance on subsequent calls.
|
|
7730
|
-
* Cache key is riskName string.
|
|
7731
|
-
*
|
|
7732
|
-
* @param riskName - Name of registered risk schema
|
|
7733
|
-
* @returns Configured ClientRisk instance
|
|
7734
|
-
*/
|
|
7735
|
-
getRisk: ((riskName: RiskName) => ClientRisk) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientRisk>;
|
|
7905
|
+
private readonly sizingSchemaService;
|
|
7736
7906
|
/**
|
|
7737
|
-
*
|
|
7738
|
-
*
|
|
7739
|
-
* Routes to appropriate ClientRisk instance based on provided context.
|
|
7740
|
-
* Validates portfolio drawdown, symbol exposure, position count, and daily loss limits.
|
|
7741
|
-
* ClientRisk will emit riskSubject event via onRejected callback when signal is rejected.
|
|
7907
|
+
* Retrieves memoized ClientSizing instance for given sizing name.
|
|
7742
7908
|
*
|
|
7743
|
-
*
|
|
7744
|
-
*
|
|
7745
|
-
* @returns Promise resolving to risk check result
|
|
7746
|
-
*/
|
|
7747
|
-
checkSignal: (params: IRiskCheckArgs, context: {
|
|
7748
|
-
riskName: RiskName;
|
|
7749
|
-
}) => Promise<boolean>;
|
|
7750
|
-
/**
|
|
7751
|
-
* Registers an opened signal with the risk management system.
|
|
7752
|
-
* Routes to appropriate ClientRisk instance.
|
|
7909
|
+
* Creates ClientSizing on first call, returns cached instance on subsequent calls.
|
|
7910
|
+
* Cache key is sizingName string.
|
|
7753
7911
|
*
|
|
7754
|
-
* @param
|
|
7755
|
-
* @
|
|
7912
|
+
* @param sizingName - Name of registered sizing schema
|
|
7913
|
+
* @returns Configured ClientSizing instance
|
|
7756
7914
|
*/
|
|
7757
|
-
|
|
7758
|
-
strategyName: string;
|
|
7759
|
-
riskName: RiskName;
|
|
7760
|
-
}) => Promise<void>;
|
|
7915
|
+
getSizing: ((sizingName: SizingName) => ClientSizing) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientSizing>;
|
|
7761
7916
|
/**
|
|
7762
|
-
*
|
|
7763
|
-
* Routes to appropriate ClientRisk instance.
|
|
7917
|
+
* Calculates position size based on risk parameters and configured method.
|
|
7764
7918
|
*
|
|
7765
|
-
*
|
|
7766
|
-
*
|
|
7767
|
-
*/
|
|
7768
|
-
removeSignal: (symbol: string, context: {
|
|
7769
|
-
strategyName: string;
|
|
7770
|
-
riskName: RiskName;
|
|
7771
|
-
}) => Promise<void>;
|
|
7772
|
-
/**
|
|
7773
|
-
* Clears the cached ClientRisk instance for the given risk name.
|
|
7919
|
+
* Routes to appropriate ClientSizing instance based on provided context.
|
|
7920
|
+
* Supports multiple sizing methods: fixed-percentage, kelly-criterion, atr-based.
|
|
7774
7921
|
*
|
|
7775
|
-
* @param
|
|
7922
|
+
* @param params - Calculation parameters (symbol, balance, prices, method-specific data)
|
|
7923
|
+
* @param context - Execution context with sizing name
|
|
7924
|
+
* @returns Promise resolving to calculated position size
|
|
7776
7925
|
*/
|
|
7777
|
-
|
|
7926
|
+
calculate: (params: ISizingCalculateParams, context: {
|
|
7927
|
+
sizingName: SizingName;
|
|
7928
|
+
}) => Promise<number>;
|
|
7778
7929
|
}
|
|
7779
7930
|
|
|
7780
7931
|
/**
|
|
@@ -8126,54 +8277,6 @@ declare class ExchangeSchemaService {
|
|
|
8126
8277
|
get: (key: ExchangeName) => IExchangeSchema;
|
|
8127
8278
|
}
|
|
8128
8279
|
|
|
8129
|
-
/**
|
|
8130
|
-
* Service for managing strategy schema registry.
|
|
8131
|
-
*
|
|
8132
|
-
* Uses ToolRegistry from functools-kit for type-safe schema storage.
|
|
8133
|
-
* Strategies are registered via addStrategy() and retrieved by name.
|
|
8134
|
-
*/
|
|
8135
|
-
declare class StrategySchemaService {
|
|
8136
|
-
readonly loggerService: LoggerService;
|
|
8137
|
-
private _registry;
|
|
8138
|
-
/**
|
|
8139
|
-
* Registers a new strategy schema.
|
|
8140
|
-
*
|
|
8141
|
-
* @param key - Unique strategy name
|
|
8142
|
-
* @param value - Strategy schema configuration
|
|
8143
|
-
* @throws Error if strategy name already exists
|
|
8144
|
-
*/
|
|
8145
|
-
register: (key: StrategyName, value: IStrategySchema) => void;
|
|
8146
|
-
/**
|
|
8147
|
-
* Validates strategy schema structure for required properties.
|
|
8148
|
-
*
|
|
8149
|
-
* Performs shallow validation to ensure all required properties exist
|
|
8150
|
-
* and have correct types before registration in the registry.
|
|
8151
|
-
*
|
|
8152
|
-
* @param strategySchema - Strategy schema to validate
|
|
8153
|
-
* @throws Error if strategyName is missing or not a string
|
|
8154
|
-
* @throws Error if interval is missing or not a valid SignalInterval
|
|
8155
|
-
* @throws Error if getSignal is missing or not a function
|
|
8156
|
-
*/
|
|
8157
|
-
private validateShallow;
|
|
8158
|
-
/**
|
|
8159
|
-
* Overrides an existing strategy schema with partial updates.
|
|
8160
|
-
*
|
|
8161
|
-
* @param key - Strategy name to override
|
|
8162
|
-
* @param value - Partial schema updates
|
|
8163
|
-
* @returns Updated strategy schema
|
|
8164
|
-
* @throws Error if strategy name doesn't exist
|
|
8165
|
-
*/
|
|
8166
|
-
override: (key: StrategyName, value: Partial<IStrategySchema>) => IStrategySchema;
|
|
8167
|
-
/**
|
|
8168
|
-
* Retrieves a strategy schema by name.
|
|
8169
|
-
*
|
|
8170
|
-
* @param key - Strategy name
|
|
8171
|
-
* @returns Strategy schema configuration
|
|
8172
|
-
* @throws Error if strategy name doesn't exist
|
|
8173
|
-
*/
|
|
8174
|
-
get: (key: StrategyName) => IStrategySchema;
|
|
8175
|
-
}
|
|
8176
|
-
|
|
8177
8280
|
/**
|
|
8178
8281
|
* Service for managing frame schema registry.
|
|
8179
8282
|
*
|
|
@@ -9526,103 +9629,6 @@ declare class OptimizerGlobalService {
|
|
|
9526
9629
|
dump: (symbol: string, optimizerName: string, path?: string) => Promise<void>;
|
|
9527
9630
|
}
|
|
9528
9631
|
|
|
9529
|
-
/**
|
|
9530
|
-
* Connection service for partial profit/loss tracking.
|
|
9531
|
-
*
|
|
9532
|
-
* Provides memoized ClientPartial instances per signal ID.
|
|
9533
|
-
* Acts as factory and lifetime manager for ClientPartial objects.
|
|
9534
|
-
*
|
|
9535
|
-
* Features:
|
|
9536
|
-
* - Creates one ClientPartial instance per signal ID (memoized)
|
|
9537
|
-
* - Configures instances with logger and event emitter callbacks
|
|
9538
|
-
* - Delegates profit/loss/clear operations to appropriate ClientPartial
|
|
9539
|
-
* - Cleans up memoized instances when signals are cleared
|
|
9540
|
-
*
|
|
9541
|
-
* Architecture:
|
|
9542
|
-
* - Injected into ClientStrategy via PartialGlobalService
|
|
9543
|
-
* - Uses memoize from functools-kit for instance caching
|
|
9544
|
-
* - Emits events to partialProfitSubject/partialLossSubject
|
|
9545
|
-
*
|
|
9546
|
-
* @example
|
|
9547
|
-
* ```typescript
|
|
9548
|
-
* // Service injected via DI
|
|
9549
|
-
* const service = inject<PartialConnectionService>(TYPES.partialConnectionService);
|
|
9550
|
-
*
|
|
9551
|
-
* // Called by ClientStrategy during signal monitoring
|
|
9552
|
-
* await service.profit("BTCUSDT", signal, 55000, 10.0, false, new Date());
|
|
9553
|
-
* // Creates or reuses ClientPartial for signal.id
|
|
9554
|
-
* // Delegates to ClientPartial.profit()
|
|
9555
|
-
*
|
|
9556
|
-
* // When signal closes
|
|
9557
|
-
* await service.clear("BTCUSDT", signal, 52000);
|
|
9558
|
-
* // Clears signal state and removes memoized instance
|
|
9559
|
-
* ```
|
|
9560
|
-
*/
|
|
9561
|
-
declare class PartialConnectionService implements IPartial {
|
|
9562
|
-
/**
|
|
9563
|
-
* Logger service injected from DI container.
|
|
9564
|
-
*/
|
|
9565
|
-
private readonly loggerService;
|
|
9566
|
-
/**
|
|
9567
|
-
* Memoized factory function for ClientPartial instances.
|
|
9568
|
-
*
|
|
9569
|
-
* Creates one ClientPartial per signal ID with configured callbacks.
|
|
9570
|
-
* Instances are cached until clear() is called.
|
|
9571
|
-
*
|
|
9572
|
-
* Key format: signalId
|
|
9573
|
-
* Value: ClientPartial instance with logger and event emitters
|
|
9574
|
-
*/
|
|
9575
|
-
private getPartial;
|
|
9576
|
-
/**
|
|
9577
|
-
* Processes profit state and emits events for newly reached profit levels.
|
|
9578
|
-
*
|
|
9579
|
-
* Retrieves or creates ClientPartial for signal ID, initializes it if needed,
|
|
9580
|
-
* then delegates to ClientPartial.profit() method.
|
|
9581
|
-
*
|
|
9582
|
-
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
9583
|
-
* @param data - Signal row data
|
|
9584
|
-
* @param currentPrice - Current market price
|
|
9585
|
-
* @param revenuePercent - Current profit percentage (positive value)
|
|
9586
|
-
* @param backtest - True if backtest mode, false if live mode
|
|
9587
|
-
* @param when - Event timestamp (current time for live, candle time for backtest)
|
|
9588
|
-
* @returns Promise that resolves when profit processing is complete
|
|
9589
|
-
*/
|
|
9590
|
-
profit: (symbol: string, data: ISignalRow, currentPrice: number, revenuePercent: number, backtest: boolean, when: Date) => Promise<void>;
|
|
9591
|
-
/**
|
|
9592
|
-
* Processes loss state and emits events for newly reached loss levels.
|
|
9593
|
-
*
|
|
9594
|
-
* Retrieves or creates ClientPartial for signal ID, initializes it if needed,
|
|
9595
|
-
* then delegates to ClientPartial.loss() method.
|
|
9596
|
-
*
|
|
9597
|
-
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
9598
|
-
* @param data - Signal row data
|
|
9599
|
-
* @param currentPrice - Current market price
|
|
9600
|
-
* @param lossPercent - Current loss percentage (negative value)
|
|
9601
|
-
* @param backtest - True if backtest mode, false if live mode
|
|
9602
|
-
* @param when - Event timestamp (current time for live, candle time for backtest)
|
|
9603
|
-
* @returns Promise that resolves when loss processing is complete
|
|
9604
|
-
*/
|
|
9605
|
-
loss: (symbol: string, data: ISignalRow, currentPrice: number, lossPercent: number, backtest: boolean, when: Date) => Promise<void>;
|
|
9606
|
-
/**
|
|
9607
|
-
* Clears partial profit/loss state when signal closes.
|
|
9608
|
-
*
|
|
9609
|
-
* Retrieves ClientPartial for signal ID, initializes if needed,
|
|
9610
|
-
* delegates clear operation, then removes memoized instance.
|
|
9611
|
-
*
|
|
9612
|
-
* Sequence:
|
|
9613
|
-
* 1. Get ClientPartial from memoize cache
|
|
9614
|
-
* 2. Ensure initialization (waitForInit)
|
|
9615
|
-
* 3. Call ClientPartial.clear() - removes state, persists to disk
|
|
9616
|
-
* 4. Clear memoized instance - prevents memory leaks
|
|
9617
|
-
*
|
|
9618
|
-
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
9619
|
-
* @param data - Signal row data
|
|
9620
|
-
* @param priceClose - Final closing price
|
|
9621
|
-
* @returns Promise that resolves when clear is complete
|
|
9622
|
-
*/
|
|
9623
|
-
clear: (symbol: string, data: ISignalRow, priceClose: number, backtest: boolean) => Promise<void>;
|
|
9624
|
-
}
|
|
9625
|
-
|
|
9626
9632
|
/**
|
|
9627
9633
|
* Global service for partial profit/loss tracking.
|
|
9628
9634
|
*
|