Strategy interface implemented by ClientStrategy. Defines core strategy execution methods.
tick: (symbol: string, strategyName: string) => Promise<IStrategyTickResult>
Single tick of strategy execution with VWAP monitoring. Checks for signal generation (throttled) and TP/SL conditions.
getPendingSignal: (symbol: string) => Promise<IPublicSignalRow>
Retrieves the currently active pending signal for the symbol. If no active signal exists, returns null. Used internally for monitoring TP/SL and time expiration.
getScheduledSignal: (symbol: string) => Promise<IPublicSignalRow>
Retrieves the currently active scheduled signal for the symbol. If no scheduled signal exists, returns null. Used internally for monitoring scheduled signal activation.
getBreakeven: (symbol: string, currentPrice: number) => Promise<boolean>
Checks if breakeven threshold has been reached for the current pending signal.
Uses the same formula as BREAKEVEN_FN to determine if price has moved far enough to cover transaction costs (slippage + fees) and allow breakeven to be set. Threshold: (CC_PERCENT_SLIPPAGE + CC_PERCENT_FEE) * 2 transactions
For LONG position:
For SHORT position:
Special cases:
getStopped: (symbol: string) => Promise<boolean>
Checks if the strategy has been stopped.
Returns the stopped state indicating whether the strategy should cease processing new ticks or signals.
backtest: (symbol: string, strategyName: string, candles: ICandleData[]) => Promise<IStrategyBacktestResult>
Fast backtest using historical candles. Iterates through candles, calculates VWAP, checks TP/SL on each candle.
For scheduled signals: first monitors activation/cancellation, then if activated continues with TP/SL monitoring.
stop: (symbol: string, backtest: boolean) => Promise<void>
Stops the strategy from generating new signals.
Sets internal flag to prevent getSignal from being called on subsequent ticks. Does NOT force-close active pending signals - they continue monitoring until natural closure (TP/SL/time_expired).
Use case: Graceful shutdown in live trading mode without abandoning open positions.
cancel: (symbol: string, backtest: boolean, cancelId?: string) => Promise<void>
Cancels the scheduled signal without stopping the strategy.
Clears the scheduled signal (waiting for priceOpen activation). Does NOT affect active pending signals or strategy operation. Does NOT set stop flag - strategy can continue generating new signals.
Use case: Cancel a scheduled entry that is no longer desired without stopping the entire strategy.
partialProfit: (symbol: string, percentToClose: number, currentPrice: number, backtest: boolean) => Promise<boolean>
Executes partial close at profit level (moving toward TP).
Closes specified percentage of position at current price. Updates _tpClosed, _totalClosed, and _partialHistory state. Persists updated signal state for crash recovery.
Validations:
Use case: User-controlled partial close triggered from onPartialProfit callback.
partialLoss: (symbol: string, percentToClose: number, currentPrice: number, backtest: boolean) => Promise<boolean>
Executes partial close at loss level (moving toward SL).
Closes specified percentage of position at current price. Updates _slClosed, _totalClosed, and _partialHistory state. Persists updated signal state for crash recovery.
Validations:
Use case: User-controlled partial close triggered from onPartialLoss callback.
trailingStop: (symbol: string, percentShift: number, currentPrice: number, backtest: boolean) => Promise<boolean>
Adjusts trailing stop-loss by shifting distance between entry and original SL.
CRITICAL: Always calculates from ORIGINAL SL, not from current trailing SL. This prevents error accumulation on repeated calls. Larger percentShift ABSORBS smaller one (updates only towards better protection).
Calculates new SL based on percentage shift of the ORIGINAL distance (entry - originalSL):
For LONG position (entry=100, originalSL=90, distance=10%):
For SHORT position (entry=100, originalSL=110, distance=10%):
Absorption behavior:
Validations:
Use case: User-controlled trailing stop triggered from onPartialProfit callback.
trailingTake: (symbol: string, percentShift: number, currentPrice: number, backtest: boolean) => Promise<boolean>
Adjusts the trailing take-profit distance for an active pending signal.
CRITICAL: Always calculates from ORIGINAL TP, not from current trailing TP. This prevents error accumulation on repeated calls. Larger percentShift ABSORBS smaller one (updates only towards more conservative TP).
Updates the take-profit distance by a percentage adjustment relative to the ORIGINAL TP distance. Negative percentShift brings TP closer to entry (more conservative). Positive percentShift moves TP further from entry (more aggressive).
Absorption behavior:
Price intrusion protection: If current price has already crossed the new TP level, the update is skipped to prevent immediate TP triggering.
breakeven: (symbol: string, currentPrice: number, backtest: boolean) => Promise<boolean>
Moves stop-loss to breakeven (entry price) when price reaches threshold.
Moves SL to entry price (zero-risk position) when current price has moved far enough in profit direction to cover transaction costs (slippage + fees). Threshold is calculated as: (CC_PERCENT_SLIPPAGE + CC_PERCENT_FEE) * 2
Behavior:
For LONG position (entry=100, slippage=0.1%, fee=0.1%):
For SHORT position (entry=100, slippage=0.1%, fee=0.1%):
Validations:
Use case: User-controlled breakeven protection triggered from onPartialProfit callback.