@tradejs/types 1.0.5 → 1.0.8

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.
Files changed (3) hide show
  1. package/dist/index.d.mts +381 -195
  2. package/dist/index.d.ts +381 -195
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -74,6 +74,7 @@ interface KlineRequest {
74
74
  end: number;
75
75
  silent?: boolean;
76
76
  cacheOnly?: boolean;
77
+ warmOnly?: boolean;
77
78
  }
78
79
  type DerivativesInterval = '15m' | '1h';
79
80
  type DerivativesRow = {
@@ -87,6 +88,42 @@ type DerivativesRow = {
87
88
  liqTotal?: number | null;
88
89
  source?: string | null;
89
90
  };
91
+ type DerivativesPressure = 'neutral' | 'crowded_long' | 'crowded_short' | 'long_flush' | 'short_flush';
92
+ type DerivativesContextRiskFlag = 'missing_derivatives' | 'stale_derivatives' | 'crowded_long' | 'crowded_short' | 'oi_falling' | 'oi_not_confirming' | 'long_liquidation_spike' | 'short_liquidation_spike';
93
+ interface DerivativesIntervalContext {
94
+ interval: DerivativesInterval;
95
+ asOfTs: number | null;
96
+ stale: boolean;
97
+ points: number;
98
+ openInterest: number | null;
99
+ oiChangePct1h: number | null;
100
+ oiChangePct4h: number | null;
101
+ oiChangePct24h: number | null;
102
+ fundingRate: number | null;
103
+ fundingZScore: number | null;
104
+ liqLong: number | null;
105
+ liqShort: number | null;
106
+ liqTotal: number | null;
107
+ liqImbalance: number | null;
108
+ liqSpikeRatio: number | null;
109
+ }
110
+ interface DerivativesSymbolContext {
111
+ source: 'coinalyze';
112
+ symbol: string;
113
+ timestamp: number;
114
+ intervals: Partial<Record<DerivativesInterval, DerivativesIntervalContext>>;
115
+ summary: {
116
+ pressure: DerivativesPressure;
117
+ directionAligned: boolean | null;
118
+ riskFlags: DerivativesContextRiskFlag[];
119
+ };
120
+ }
121
+ interface DerivativesContext extends DerivativesSymbolContext {
122
+ targetSymbol?: string;
123
+ primaryReferenceSymbol?: string;
124
+ referenceSymbols?: string[];
125
+ referenceContexts?: Record<string, DerivativesSymbolContext>;
126
+ }
90
127
  type SpreadRow = {
91
128
  symbol: string;
92
129
  interval: DerivativesInterval;
@@ -111,6 +148,7 @@ type Order = {
111
148
  price: number;
112
149
  timestamp: number;
113
150
  direction: Direction;
151
+ orderId?: string;
114
152
  signal?: Signal;
115
153
  };
116
154
  type Position = {
@@ -119,6 +157,10 @@ type Position = {
119
157
  price: number;
120
158
  direction: Direction;
121
159
  };
160
+ interface PositionPnlSnapshot extends Position {
161
+ currentPrice: number;
162
+ unrealizedPnl: number;
163
+ }
122
164
  type OrderType = 'OPEN_LONG' | 'OPEN_SHORT' | 'CLOSE_LONG' | 'CLOSE_SHORT' | 'TAKE_PROFIT_LONG' | 'TAKE_PROFIT_SHORT' | 'STOP_LOSS_LONG' | 'STOP_LOSS_SHORT';
123
165
  type ConnectorCreator = (config: ConnectorConfig) => Promise<Connector>;
124
166
  interface ConnectorConfig {
@@ -134,8 +176,36 @@ interface ConnectorPluginDefinition {
134
176
  }
135
177
  type GetPosition = (symbol: string) => Promise<Position | null>;
136
178
  type GetPositions = () => Promise<Position[]>;
137
- type PlaceOrder = (order: Order, tp?: Tp[], slPrice?: Sl) => Promise<boolean>;
179
+ type GetOpenPositionPnl = () => Promise<PositionPnlSnapshot[]>;
180
+ interface ClosedPnlRecord {
181
+ symbol: string;
182
+ qty: number;
183
+ entryPrice: number | null;
184
+ exitPrice: number | null;
185
+ closedPnl: number;
186
+ closedAt: number;
187
+ orderId?: string;
188
+ }
189
+ interface GetClosedPnlParams {
190
+ startTime: number;
191
+ endTime: number;
192
+ symbol?: string;
193
+ limit?: number;
194
+ }
195
+ type GetClosedPnl = (params: GetClosedPnlParams) => Promise<ClosedPnlRecord[]>;
196
+ type PlaceOrder = (order: Order) => Promise<boolean>;
138
197
  type ClosePosition = (order: Omit<Order, 'qty'>) => Promise<boolean>;
198
+ type SetTakeProfits = (params: {
199
+ symbol: string;
200
+ direction: Direction;
201
+ qty?: number;
202
+ takeProfits: Tp[];
203
+ }) => Promise<boolean>;
204
+ type SetStopLoss = (params: {
205
+ symbol: string;
206
+ direction: Direction;
207
+ stopLossPrice: Sl;
208
+ }) => Promise<boolean>;
139
209
  type Kline = (options: KlineRequest) => Promise<KlineChartData>;
140
210
  type GetTickers = () => Promise<Ticker[]>;
141
211
  interface Connector {
@@ -144,7 +214,11 @@ interface Connector {
144
214
  setState: (state: object) => Promise<void>;
145
215
  getPosition: GetPosition;
146
216
  getPositions: GetPositions;
217
+ getOpenPositionPnl?: GetOpenPositionPnl;
218
+ getClosedPnl?: GetClosedPnl;
147
219
  placeOrder: PlaceOrder;
220
+ setTakeProfits: SetTakeProfits;
221
+ setStopLoss: SetStopLoss;
148
222
  closePosition: ClosePosition;
149
223
  getTickers: GetTickers;
150
224
  }
@@ -255,14 +329,16 @@ interface TrendLineOptions {
255
329
  }
256
330
  interface Signal {
257
331
  signalId: string;
332
+ orderId?: string;
258
333
  symbol: string;
259
334
  interval: Interval;
260
335
  strategy: string;
261
336
  direction: Direction;
262
337
  timestamp: number;
263
- orderStatus?: 'completed' | 'failed' | 'skipped' | 'canceled';
338
+ orderStatus?: SignalOrderStatus;
264
339
  orderSkipReason?: string;
265
340
  isConfigFromBacktest?: boolean;
341
+ aiAnalysis?: Partial<SignalAnalysis>;
266
342
  ml?: {
267
343
  probability: number;
268
344
  threshold: number;
@@ -284,6 +360,24 @@ interface Signal {
284
360
  indicators: Record<string, any>;
285
361
  additionalIndicators?: Record<string, any>;
286
362
  }
363
+ type RuntimeSignalEvaluationStatus = 'signal' | 'skip' | 'error';
364
+ interface RuntimeSignalEvaluationRecord {
365
+ evaluationId: string;
366
+ userName: string;
367
+ strategy: string;
368
+ symbol: string;
369
+ interval: Interval;
370
+ timestamp: number;
371
+ evaluatedAt: number;
372
+ status: RuntimeSignalEvaluationStatus;
373
+ reason?: string;
374
+ signalId?: string;
375
+ direction?: Direction;
376
+ orderStatus?: SignalOrderStatus;
377
+ orderSkipReason?: string;
378
+ aiAnalysis?: Partial<SignalAnalysis> | null;
379
+ ml?: Signal['ml'];
380
+ }
287
381
  interface SignalAnalysis {
288
382
  direction: Direction | null;
289
383
  quality: 1 | 2 | 3 | 4 | 5 | number;
@@ -298,8 +392,40 @@ interface SignalAnalysis {
298
392
  riskLevels?: string;
299
393
  qualityReason?: string;
300
394
  triggerInvalidation?: string;
395
+ gateAnalysis?: Partial<SignalAnalysis>;
396
+ gateContradictsLlm?: boolean;
397
+ gateDecision?: 'approved' | 'rejected';
398
+ llmDecision?: 'approved' | 'rejected';
301
399
  comment: string;
302
400
  }
401
+ interface RuntimeAiAnalysisSnapshot {
402
+ strategy?: string;
403
+ symbol: string;
404
+ direction: Direction;
405
+ timestamp: number;
406
+ toleranceMs?: number;
407
+ analysis: Partial<SignalAnalysis>;
408
+ }
409
+ type SignalOrderStatus = 'completed' | 'failed' | 'skipped' | 'canceled';
410
+ type RuntimeTradeStatus = 'active' | 'closed';
411
+ interface RuntimeTradeRecord {
412
+ orderId: string;
413
+ signalId?: string;
414
+ strategy: string;
415
+ symbol: string;
416
+ direction: Direction;
417
+ qty: number;
418
+ entryPrice: number;
419
+ entryTimestamp: number;
420
+ status: RuntimeTradeStatus;
421
+ currentPrice?: number | null;
422
+ currentPnl?: number | null;
423
+ closedPnl?: number | null;
424
+ exitPrice?: number | null;
425
+ exitTimestamp?: number | null;
426
+ aiAnalysis?: Partial<SignalAnalysis> | null;
427
+ lastSyncedAt?: number;
428
+ }
303
429
 
304
430
  type Strategy = (candle: KlineChartItem, btcCandle: KlineChartItem) => Promise<string | Signal>;
305
431
  type BacktestPriceMode = 'mid' | 'close' | 'open' | 'rand';
@@ -335,7 +461,9 @@ interface Test extends BacktestRunConfig {
335
461
  symbol: string;
336
462
  options: TestingOptions;
337
463
  ml?: boolean;
464
+ ai?: boolean;
338
465
  chunkId?: string;
466
+ timeoutMs?: number;
339
467
  }
340
468
  type TestSuite = Test[];
341
469
  interface TestStat extends Metrics {
@@ -354,6 +482,9 @@ interface MinimalStat {
354
482
  interface TestingBoxResult {
355
483
  orderLogId: string;
356
484
  stat: MinimalStat;
485
+ inlineOrderLog?: OrderLogData;
486
+ inlinePositionLog?: PositionLogData;
487
+ inlineReplaySignalEvaluations?: RuntimeSignalEvaluationRecord[];
357
488
  }
358
489
  type TestingBox = (test: Test) => Promise<TestingBoxResult | null>;
359
490
  interface TestWorkerResult extends TestingBoxResult {
@@ -400,6 +531,7 @@ interface TestConnector extends Connector {
400
531
  interface TestConnectorContext {
401
532
  userName?: string;
402
533
  mlEnabled?: boolean;
534
+ aiEnabled?: boolean;
403
535
  }
404
536
  type TestConnectorCreator = (connector: Connector, context?: TestConnectorContext) => TestConnector;
405
537
  type ChartColor = string;
@@ -446,197 +578,6 @@ interface UIFilters extends Filters {
446
578
  }
447
579
  type OnChangeFilters = (filters: Partial<UIFilters>) => void;
448
580
 
449
- interface AIChatMessage {
450
- from: 'user' | 'ai';
451
- text: string;
452
- command?: string;
453
- }
454
- type AIChatHistory = AIChatMessage[];
455
-
456
- interface AiPayload {
457
- signal: {
458
- symbol: Signal['symbol'];
459
- signalId: Signal['signalId'];
460
- interval: Signal['interval'];
461
- direction: Signal['direction'];
462
- timestamp: Signal['timestamp'];
463
- strategy: Signal['strategy'];
464
- prices: {
465
- currentPrice: Signal['prices']['currentPrice'];
466
- takeProfitPrice: Signal['prices']['takeProfitPrice'];
467
- stopLossPrice: Signal['prices']['stopLossPrice'];
468
- };
469
- };
470
- figures: Record<string, unknown>;
471
- indicators: unknown;
472
- additionalIndicators: unknown;
473
- }
474
- interface StrategyAiAdapter {
475
- buildPayload?: (params: {
476
- signal: Signal;
477
- basePayload: AiPayload;
478
- }) => AiPayload;
479
- buildSystemPromptAddon?: (params: {
480
- signal: Signal;
481
- }) => string;
482
- buildHumanPromptAddon?: (params: {
483
- signal: Signal;
484
- payload: AiPayload;
485
- }) => string;
486
- mapEntryRuntimeFromConfig?: (config: StrategyConfig) => StrategyRuntimeAiOptions | undefined;
487
- }
488
- interface StrategyMlAdapter {
489
- normalizeSignal?: (signal: Signal) => Signal;
490
- normalizeStrategyConfig?: (strategyConfig?: Record<string, any>) => Record<string, any> | undefined;
491
- mapEntryRuntimeFromConfig?: (config: StrategyConfig) => StrategyRuntimeMlOptions | undefined;
492
- }
493
- interface StrategyHookGateResult {
494
- allow?: boolean;
495
- reason?: string;
496
- }
497
- type EntryDecision = Extract<StrategyDecision, {
498
- kind: 'entry';
499
- }>;
500
- type ExitDecision = Extract<StrategyDecision, {
501
- kind: 'exit';
502
- }>;
503
- interface StrategyHookCtx {
504
- connector: Connector;
505
- strategyName: string;
506
- userName: string;
507
- symbol: string;
508
- strategyConfig: StrategyConfig;
509
- env: string;
510
- isConfigFromBacktest: boolean;
511
- }
512
- interface StrategyHookMarketContext {
513
- candle?: KlineChartItem;
514
- btcCandle?: KlineChartItem;
515
- data?: KlineChartItem[];
516
- btcData?: KlineChartItem[];
517
- }
518
- interface StrategyHookEntryContext {
519
- context: StrategyEntrySignalContext;
520
- orderPlan: StrategyEntryOrderPlan;
521
- signal?: Signal;
522
- runtime: {
523
- raw?: StrategyEntryRuntimeOptions;
524
- resolved: StrategyEntryRuntimeOptions;
525
- };
526
- }
527
- type StrategyHookMlSkippedReason = 'BACKTEST' | 'DISABLED' | 'NO_RUNTIME' | 'NO_STRATEGY_CONFIG' | 'NO_THRESHOLD' | 'NO_RESULT';
528
- interface StrategyHookMlContext {
529
- config?: StrategyRuntimeMlOptions;
530
- attempted: boolean;
531
- applied: boolean;
532
- result?: Signal['ml'];
533
- skippedReason?: StrategyHookMlSkippedReason;
534
- }
535
- type StrategyHookAiSkippedReason = 'BACKTEST' | 'DISABLED' | 'NO_RUNTIME' | 'NO_QUALITY';
536
- interface StrategyHookAiContext {
537
- config?: StrategyRuntimeAiOptions;
538
- attempted: boolean;
539
- applied: boolean;
540
- quality?: number;
541
- skippedReason?: StrategyHookAiSkippedReason;
542
- }
543
- type StrategyHookStage = 'onInit' | 'afterCoreDecision' | 'onSkip' | 'beforeClosePosition' | 'afterEnrichMl' | 'afterEnrichAi' | 'beforeEntryGate' | 'beforePlaceOrder' | 'afterPlaceOrder' | 'runtime.beforePlaceOrder' | 'enrichSignalWithMl' | 'enrichSignalWithAi' | 'closePosition' | 'placeOrder';
544
- interface StrategyHookErrorPayload {
545
- stage: StrategyHookStage;
546
- cause: unknown;
547
- }
548
- interface StrategyHookErrorContext {
549
- ctx: StrategyHookCtx;
550
- market?: StrategyHookMarketContext;
551
- decision?: StrategyDecision;
552
- entry?: StrategyHookEntryContext;
553
- error: StrategyHookErrorPayload;
554
- }
555
- interface StrategyHookInitContext {
556
- ctx: StrategyHookCtx;
557
- market: Required<Pick<StrategyHookMarketContext, 'data' | 'btcData'>>;
558
- }
559
- interface StrategyHookAfterDecisionContext {
560
- ctx: StrategyHookCtx;
561
- market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
562
- decision: StrategyDecision;
563
- }
564
- interface StrategyHookSkipContext extends StrategyHookAfterDecisionContext {
565
- decision: Extract<StrategyDecision, {
566
- kind: 'skip';
567
- }>;
568
- }
569
- interface StrategyHookBeforeCloseContext extends StrategyHookAfterDecisionContext {
570
- decision: ExitDecision;
571
- }
572
- interface StrategyHookEnrichContext {
573
- ctx: StrategyHookCtx;
574
- market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
575
- decision: EntryDecision;
576
- entry: StrategyHookEntryContext;
577
- ml: StrategyHookMlContext;
578
- }
579
- interface StrategyHookAfterAiContext extends StrategyHookEnrichContext {
580
- ai: StrategyHookAiContext;
581
- }
582
- interface StrategyHookPolicyContext {
583
- aiQuality?: number;
584
- makeOrdersEnabled: boolean;
585
- minAiQuality: number;
586
- }
587
- interface StrategyHookBeforeEntryGateContext {
588
- ctx: StrategyHookCtx;
589
- market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
590
- decision: EntryDecision;
591
- entry: StrategyHookEntryContext;
592
- policy: StrategyHookPolicyContext;
593
- ml?: StrategyHookMlContext;
594
- ai?: StrategyHookAiContext;
595
- }
596
- interface StrategyHookBeforePlaceOrderContext {
597
- ctx: StrategyHookCtx;
598
- market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
599
- decision: EntryDecision;
600
- entry: StrategyHookEntryContext;
601
- policy: StrategyHookPolicyContext;
602
- ml?: StrategyHookMlContext;
603
- ai?: StrategyHookAiContext;
604
- }
605
- interface StrategyHookOrderContext {
606
- result: Signal | string;
607
- }
608
- interface StrategyHookAfterPlaceOrderContext {
609
- ctx: StrategyHookCtx;
610
- market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
611
- decision: EntryDecision;
612
- entry: StrategyHookEntryContext;
613
- policy: StrategyHookPolicyContext;
614
- ml?: StrategyHookMlContext;
615
- ai?: StrategyHookAiContext;
616
- order: StrategyHookOrderContext;
617
- }
618
- interface StrategyManifest {
619
- name: string;
620
- entryRuntimeDefaults?: {
621
- ai?: StrategyRuntimeAiOptions;
622
- ml?: Pick<StrategyRuntimeMlOptions, 'enabled'>;
623
- };
624
- hooks?: {
625
- onInit?: (params: StrategyHookInitContext) => Promise<void> | void;
626
- afterCoreDecision?: (params: StrategyHookAfterDecisionContext) => Promise<void> | void;
627
- onSkip?: (params: StrategyHookSkipContext) => Promise<void> | void;
628
- beforeClosePosition?: (params: StrategyHookBeforeCloseContext) => Promise<StrategyHookGateResult | void> | StrategyHookGateResult | void;
629
- afterEnrichMl?: (params: StrategyHookEnrichContext) => Promise<void> | void;
630
- afterEnrichAi?: (params: StrategyHookAfterAiContext) => Promise<void> | void;
631
- beforeEntryGate?: (params: StrategyHookBeforeEntryGateContext) => Promise<StrategyHookGateResult | void> | StrategyHookGateResult | void;
632
- beforePlaceOrder?: (params: StrategyHookBeforePlaceOrderContext) => Promise<void> | void;
633
- afterPlaceOrder?: (params: StrategyHookAfterPlaceOrderContext) => Promise<void> | void;
634
- onRuntimeError?: (params: StrategyHookErrorContext) => Promise<void> | void;
635
- };
636
- aiAdapter?: StrategyAiAdapter;
637
- mlAdapter?: StrategyMlAdapter;
638
- }
639
-
640
581
  interface StrategySignalMetaParams {
641
582
  symbol: string;
642
583
  interval: Interval;
@@ -701,6 +642,21 @@ interface StrategyAPIEntryParams {
701
642
  orderPlan: StrategyEntryOrderPlan;
702
643
  runtime?: StrategyEntryRuntimeOptions;
703
644
  }
645
+ interface StrategyAPIExitParams {
646
+ code?: string;
647
+ direction: Direction;
648
+ price?: number;
649
+ timestamp?: number;
650
+ }
651
+ interface StrategyProtectPlan {
652
+ direction: Direction;
653
+ stopLossPrice?: number | null;
654
+ takeProfits?: Tp[];
655
+ }
656
+ interface StrategyAPIProtectParams {
657
+ code?: string;
658
+ protectPlan: StrategyProtectPlan;
659
+ }
704
660
  interface StrategyAPIMarketDataParams {
705
661
  preloadStart?: number;
706
662
  backtestPriceMode?: BacktestPriceMode;
@@ -811,6 +767,12 @@ interface StrategyAPI {
811
767
  entry: (params: StrategyAPIEntryParams) => Promise<Extract<StrategyDecision, {
812
768
  kind: 'entry';
813
769
  }>>;
770
+ exit: (params: StrategyAPIExitParams) => Promise<Extract<StrategyDecision, {
771
+ kind: 'exit';
772
+ }>>;
773
+ protect: (params: StrategyAPIProtectParams) => Extract<StrategyDecision, {
774
+ kind: 'protect';
775
+ }>;
814
776
  getMarketData: (params?: StrategyAPIMarketDataParams) => Promise<StrategyMarketSnapshot>;
815
777
  nextIndicators: (candle: KlineChartData[number], btcCandle: KlineChartData[number]) => unknown;
816
778
  getCurrentPosition: () => ReturnType<Connector['getPosition']>;
@@ -834,9 +796,12 @@ interface StrategyRuntimeMlOptions {
834
796
  strategyConfig?: StrategyConfig;
835
797
  mlThreshold?: number;
836
798
  }
799
+ type StrategyAiMode = 'gate' | 'llm';
837
800
  interface StrategyRuntimeAiOptions {
838
801
  enabled?: boolean;
802
+ mode?: StrategyAiMode;
839
803
  minQuality?: number;
804
+ replayAnalyses?: RuntimeAiAnalysisSnapshot[];
840
805
  }
841
806
  interface StrategyEntryRuntimeOptions {
842
807
  ml?: StrategyRuntimeMlOptions;
@@ -867,6 +832,10 @@ type StrategyDecision = {
867
832
  kind: 'exit';
868
833
  code: string;
869
834
  closePlan: StrategyClosePlan;
835
+ } | {
836
+ kind: 'protect';
837
+ code: string;
838
+ protectPlan: StrategyProtectPlan;
870
839
  };
871
840
  interface CreateStrategyCoreParams<TConfig extends StrategyConfig, TIndicatorsState extends StrategyIndicatorsState = StrategyIndicatorsState> {
872
841
  userName: string;
@@ -876,7 +845,7 @@ interface CreateStrategyCoreParams<TConfig extends StrategyConfig, TIndicatorsSt
876
845
  connector: Connector;
877
846
  data: KlineChartData;
878
847
  btcData: KlineChartData;
879
- loadPineScript: (fileNameOrPath: string, fallback?: string) => string;
848
+ loadPineScriptFile: (fileNameOrPath: string, fallback?: string) => string;
880
849
  strategyApi: StrategyAPI;
881
850
  indicatorsState: TIndicatorsState;
882
851
  }
@@ -922,4 +891,221 @@ interface IndicatorPluginDefinition {
922
891
  indicatorEntries: IndicatorPluginEntry[];
923
892
  }
924
893
 
925
- export type { AIChatHistory, AIChatMessage, AiPayload, BacktestPriceMode, BacktestRunConfig, BaseIndicatorsHistorySnapshot, Bot, BotConfig, BotResults, BotStatus, BuildStrategySignalDraft, BuildStrategySignalParams, Candle, ChartColor, CompletedTest, Connector, ConnectorConfig, ConnectorCreator, ConnectorPluginDefinition, ConnectorRegistryEntry, CreateStrategyCore, CreateStrategyCoreParams, DerivativesInterval, DerivativesRow, Direction, EOMPoint, Figure, Filters, GetTickers, Indicator, IndicatorPluginComputeParams, IndicatorPluginDefinition, IndicatorPluginEntry, IndicatorPluginFigureRenderer, IndicatorPluginRenderer, IndicatorSnapshot, Indicators, IndicatorsHistorySnapshot, Interval, Item, Items, Kline, KlineChartData, KlineChartItem, KlineRequest, MetricThreshold, Metrics, MinimalStat, MlCandleIndicatorsSnapshot, MonthlyEquityStats, OnChangeCompare, OnChangeFilters, Order, OrderLog, OrderLogData, OrderType, Position, PositionLog, PositionLogData, Provider, Signal, SignalAnalysis, SimpleOrderLogData, Sl, SpreadRow, Strategy, StrategyAPI, StrategyAPIEntryParams, StrategyAPIMarketDataParams, StrategyAdditionalIndicatorsMap, StrategyAiAdapter, StrategyClosePlan, StrategyConfig, StrategyConfigGrid, StrategyCoreRunner, StrategyCreator, StrategyCreatorParams, StrategyDecision, StrategyDirectionalTpSlParams, StrategyDirectionalTpSlResult, StrategyEntryBaseParams, StrategyEntryModelFigures, StrategyEntryOrderPlan, StrategyEntryRuntimeBaseParams, StrategyEntryRuntimeBuilderParams, StrategyEntryRuntimeOptions, StrategyEntrySignalContext, StrategyEntrySignalDecisionBuilderParams, StrategyEntryTakeProfitsParams, StrategyFigureLine, StrategyFigurePoint, StrategyFigurePoints, StrategyFigureZone, StrategyHookAfterAiContext, StrategyHookAfterDecisionContext, StrategyHookAfterPlaceOrderContext, StrategyHookAiContext, StrategyHookAiSkippedReason, StrategyHookBeforeCloseContext, StrategyHookBeforeEntryGateContext, StrategyHookBeforePlaceOrderContext, StrategyHookCtx, StrategyHookEnrichContext, StrategyHookEntryContext, StrategyHookErrorContext, StrategyHookErrorPayload, StrategyHookGateResult, StrategyHookInitContext, StrategyHookMarketContext, StrategyHookMlContext, StrategyHookMlSkippedReason, StrategyHookOrderContext, StrategyHookPolicyContext, StrategyHookSkipContext, StrategyHookStage, StrategyIndicatorsMap, StrategyIndicatorsState, StrategyLastTradeController, StrategyLastTradeControllerParams, StrategyManifest, StrategyMarketSnapshot, StrategyMlAdapter, StrategyPluginDefinition, StrategyRegistryEntry, StrategyResultConfig, StrategyResultEntry, StrategyResults, StrategyRuntimeAiOptions, StrategyRuntimeMlOptions, StrategySignalMetaParams, StrategySignalPriceParams, Test, TestCompare, TestCompareList, TestConnector, TestConnectorContext, TestConnectorCreator, TestResult, TestStat, TestSuite, TestThresholds, TestThresholdsKey, TestWorkerResult, TestingBox, TestingBoxResult, TestingOptions, ThresholdLevel, Ticker, Tp, Trend, TrendLine, TrendLineMode, TrendLineOptions, UIFilters };
894
+ interface AiPayload {
895
+ signal: {
896
+ symbol: Signal['symbol'];
897
+ signalId: Signal['signalId'];
898
+ interval: Signal['interval'];
899
+ direction: Signal['direction'];
900
+ timestamp: Signal['timestamp'];
901
+ strategy: Signal['strategy'];
902
+ prices: {
903
+ currentPrice: Signal['prices']['currentPrice'];
904
+ takeProfitPrice: Signal['prices']['takeProfitPrice'];
905
+ stopLossPrice: Signal['prices']['stopLossPrice'];
906
+ };
907
+ };
908
+ figures: Record<string, unknown>;
909
+ indicators: unknown;
910
+ additionalIndicators: unknown;
911
+ }
912
+ interface StrategyAiAdapter {
913
+ buildPayload?: (params: {
914
+ signal: Signal;
915
+ basePayload: AiPayload;
916
+ }) => AiPayload;
917
+ postProcessAnalysis?: (params: {
918
+ signal: Signal;
919
+ payload: AiPayload;
920
+ analysis: Partial<SignalAnalysis>;
921
+ }) => Partial<SignalAnalysis>;
922
+ buildSystemPromptAddon?: (params: {
923
+ signal: Signal;
924
+ }) => string;
925
+ buildHumanPromptAddon?: (params: {
926
+ signal: Signal;
927
+ payload: AiPayload;
928
+ }) => string;
929
+ mapEntryRuntimeFromConfig?: (config: StrategyConfig) => StrategyRuntimeAiOptions | undefined;
930
+ }
931
+ interface StrategyMlAdapter {
932
+ normalizeSignal?: (signal: Signal) => Signal;
933
+ normalizeStrategyConfig?: (strategyConfig?: Record<string, any>) => Record<string, any> | undefined;
934
+ mapEntryRuntimeFromConfig?: (config: StrategyConfig) => StrategyRuntimeMlOptions | undefined;
935
+ }
936
+ interface StrategyHookGateResult {
937
+ allow?: boolean;
938
+ reason?: string;
939
+ }
940
+ type EntryDecision = Extract<StrategyDecision, {
941
+ kind: 'entry';
942
+ }>;
943
+ type ExitDecision = Extract<StrategyDecision, {
944
+ kind: 'exit';
945
+ }>;
946
+ interface StrategyHookCtx {
947
+ connector: Connector;
948
+ strategyName: string;
949
+ userName: string;
950
+ symbol: string;
951
+ strategyConfig: StrategyConfig;
952
+ env: string;
953
+ isConfigFromBacktest: boolean;
954
+ }
955
+ interface StrategyHookMarketContext {
956
+ candle?: KlineChartItem;
957
+ btcCandle?: KlineChartItem;
958
+ data?: KlineChartItem[];
959
+ btcData?: KlineChartItem[];
960
+ }
961
+ interface StrategyHookEntryContext {
962
+ context: StrategyEntrySignalContext;
963
+ orderPlan: StrategyEntryOrderPlan;
964
+ signal?: Signal;
965
+ runtime: {
966
+ raw?: StrategyEntryRuntimeOptions;
967
+ resolved: StrategyEntryRuntimeOptions;
968
+ };
969
+ }
970
+ type StrategyHookMlSkippedReason = 'BACKTEST' | 'DISABLED' | 'NO_RUNTIME' | 'NO_STRATEGY_CONFIG' | 'NO_THRESHOLD' | 'NO_RESULT';
971
+ interface StrategyHookMlContext {
972
+ config?: StrategyRuntimeMlOptions;
973
+ attempted: boolean;
974
+ applied: boolean;
975
+ result?: Signal['ml'];
976
+ skippedReason?: StrategyHookMlSkippedReason;
977
+ }
978
+ type StrategyHookAiSkippedReason = 'BACKTEST' | 'DISABLED' | 'NO_RUNTIME' | 'NO_QUALITY';
979
+ interface StrategyHookAiContext {
980
+ config?: StrategyRuntimeAiOptions;
981
+ attempted: boolean;
982
+ applied: boolean;
983
+ quality?: number;
984
+ skippedReason?: StrategyHookAiSkippedReason;
985
+ }
986
+ type StrategyHookStage = 'onInit' | 'onBar' | 'afterCoreDecision' | 'afterBarDecision' | 'onSkip' | 'beforeClosePosition' | 'afterEnrichMl' | 'afterEnrichAi' | 'beforeEntryGate' | 'beforePlaceOrder' | 'afterPlaceOrder' | 'runtime.beforePlaceOrder' | 'enrichSignalWithMl' | 'enrichSignalWithAi' | 'setTakeProfits' | 'setStopLoss' | 'protectPosition' | 'closePosition' | 'placeOrder';
987
+ interface StrategyHookErrorPayload {
988
+ stage: StrategyHookStage;
989
+ cause: unknown;
990
+ }
991
+ interface StrategyHookErrorContext {
992
+ ctx: StrategyHookCtx;
993
+ market?: StrategyHookMarketContext;
994
+ decision?: StrategyDecision;
995
+ entry?: StrategyHookEntryContext;
996
+ error: StrategyHookErrorPayload;
997
+ }
998
+ interface StrategyHookInitContext {
999
+ ctx: StrategyHookCtx;
1000
+ market: Required<Pick<StrategyHookMarketContext, 'data' | 'btcData'>>;
1001
+ }
1002
+ interface StrategyHookBarContext {
1003
+ ctx: StrategyHookCtx;
1004
+ market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
1005
+ }
1006
+ interface StrategyHookAfterDecisionContext extends StrategyHookBarContext {
1007
+ decision: StrategyDecision;
1008
+ }
1009
+ interface StrategyHookSkipContext extends StrategyHookAfterDecisionContext {
1010
+ decision: Extract<StrategyDecision, {
1011
+ kind: 'skip';
1012
+ }>;
1013
+ }
1014
+ interface StrategyHookBeforeCloseContext extends StrategyHookAfterDecisionContext {
1015
+ decision: ExitDecision;
1016
+ }
1017
+ interface StrategyHookEnrichContext {
1018
+ ctx: StrategyHookCtx;
1019
+ market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
1020
+ decision: EntryDecision;
1021
+ entry: StrategyHookEntryContext;
1022
+ ml: StrategyHookMlContext;
1023
+ }
1024
+ interface StrategyHookAfterAiContext extends StrategyHookEnrichContext {
1025
+ ai: StrategyHookAiContext;
1026
+ }
1027
+ interface StrategyHookPolicyContext {
1028
+ aiQuality?: number;
1029
+ makeOrdersEnabled: boolean;
1030
+ minAiQuality: number;
1031
+ }
1032
+ interface StrategyHookBeforeEntryGateContext {
1033
+ ctx: StrategyHookCtx;
1034
+ market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
1035
+ decision: EntryDecision;
1036
+ entry: StrategyHookEntryContext;
1037
+ policy: StrategyHookPolicyContext;
1038
+ ml?: StrategyHookMlContext;
1039
+ ai?: StrategyHookAiContext;
1040
+ }
1041
+ interface StrategyHookBeforePlaceOrderContext {
1042
+ ctx: StrategyHookCtx;
1043
+ market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
1044
+ decision: EntryDecision;
1045
+ entry: StrategyHookEntryContext;
1046
+ policy: StrategyHookPolicyContext;
1047
+ ml?: StrategyHookMlContext;
1048
+ ai?: StrategyHookAiContext;
1049
+ }
1050
+ interface StrategyHookOrderContext {
1051
+ result: Signal | string;
1052
+ }
1053
+ interface StrategyHookAfterPlaceOrderContext {
1054
+ ctx: StrategyHookCtx;
1055
+ market: Required<Pick<StrategyHookMarketContext, 'candle' | 'btcCandle'>>;
1056
+ decision: EntryDecision;
1057
+ entry: StrategyHookEntryContext;
1058
+ policy: StrategyHookPolicyContext;
1059
+ ml?: StrategyHookMlContext;
1060
+ ai?: StrategyHookAiContext;
1061
+ order: StrategyHookOrderContext;
1062
+ }
1063
+ interface StrategyManifest {
1064
+ name: string;
1065
+ entryRuntimeDefaults?: {
1066
+ ai?: StrategyRuntimeAiOptions;
1067
+ ml?: Pick<StrategyRuntimeMlOptions, 'enabled'>;
1068
+ };
1069
+ hooks?: {
1070
+ onInit?: (params: StrategyHookInitContext) => Promise<void> | void;
1071
+ onBar?: (params: StrategyHookBarContext) => Promise<StrategyDecision | void> | StrategyDecision | void;
1072
+ afterCoreDecision?: (params: StrategyHookAfterDecisionContext) => Promise<void> | void;
1073
+ afterBarDecision?: (params: StrategyHookAfterDecisionContext) => Promise<void> | void;
1074
+ onSkip?: (params: StrategyHookSkipContext) => Promise<void> | void;
1075
+ beforeClosePosition?: (params: StrategyHookBeforeCloseContext) => Promise<StrategyHookGateResult | void> | StrategyHookGateResult | void;
1076
+ afterEnrichMl?: (params: StrategyHookEnrichContext) => Promise<void> | void;
1077
+ afterEnrichAi?: (params: StrategyHookAfterAiContext) => Promise<void> | void;
1078
+ beforeEntryGate?: (params: StrategyHookBeforeEntryGateContext) => Promise<StrategyHookGateResult | void> | StrategyHookGateResult | void;
1079
+ beforePlaceOrder?: (params: StrategyHookBeforePlaceOrderContext) => Promise<void> | void;
1080
+ afterPlaceOrder?: (params: StrategyHookAfterPlaceOrderContext) => Promise<void> | void;
1081
+ onRuntimeError?: (params: StrategyHookErrorContext) => Promise<void> | void;
1082
+ };
1083
+ aiAdapter?: StrategyAiAdapter;
1084
+ mlAdapter?: StrategyMlAdapter;
1085
+ }
1086
+
1087
+ interface AIChatMessage {
1088
+ from: 'user' | 'ai';
1089
+ text: string;
1090
+ command?: string;
1091
+ }
1092
+ type AIChatHistory = AIChatMessage[];
1093
+ interface AiPromptPair {
1094
+ systemPrompt: string;
1095
+ humanPrompt: string;
1096
+ }
1097
+ interface AiDatasetRow {
1098
+ signalId: string;
1099
+ strategyName: string;
1100
+ symbol: string;
1101
+ direction: Direction;
1102
+ timestamp: number;
1103
+ profit: number;
1104
+ payload: AiPayload;
1105
+ testId?: string;
1106
+ testSuiteId?: string;
1107
+ testName?: string;
1108
+ connectorName?: string;
1109
+ }
1110
+
1111
+ export type { AIChatHistory, AIChatMessage, AiDatasetRow, AiPayload, AiPromptPair, BacktestPriceMode, BacktestRunConfig, BaseIndicatorsHistorySnapshot, Bot, BotConfig, BotResults, BotStatus, BuildStrategySignalDraft, BuildStrategySignalParams, Candle, ChartColor, ClosedPnlRecord, CompletedTest, Connector, ConnectorConfig, ConnectorCreator, ConnectorPluginDefinition, ConnectorRegistryEntry, CreateStrategyCore, CreateStrategyCoreParams, DerivativesContext, DerivativesContextRiskFlag, DerivativesInterval, DerivativesIntervalContext, DerivativesPressure, DerivativesRow, DerivativesSymbolContext, Direction, EOMPoint, Figure, Filters, GetClosedPnlParams, GetTickers, Indicator, IndicatorPluginComputeParams, IndicatorPluginDefinition, IndicatorPluginEntry, IndicatorPluginFigureRenderer, IndicatorPluginRenderer, IndicatorSnapshot, Indicators, IndicatorsHistorySnapshot, Interval, Item, Items, Kline, KlineChartData, KlineChartItem, KlineRequest, MetricThreshold, Metrics, MinimalStat, MlCandleIndicatorsSnapshot, MonthlyEquityStats, OnChangeCompare, OnChangeFilters, Order, OrderLog, OrderLogData, OrderType, Position, PositionLog, PositionLogData, PositionPnlSnapshot, Provider, RuntimeAiAnalysisSnapshot, RuntimeSignalEvaluationRecord, RuntimeSignalEvaluationStatus, RuntimeTradeRecord, RuntimeTradeStatus, Signal, SignalAnalysis, SignalOrderStatus, SimpleOrderLogData, Sl, SpreadRow, Strategy, StrategyAPI, StrategyAPIEntryParams, StrategyAPIExitParams, StrategyAPIMarketDataParams, StrategyAPIProtectParams, StrategyAdditionalIndicatorsMap, StrategyAiAdapter, StrategyAiMode, StrategyClosePlan, StrategyConfig, StrategyConfigGrid, StrategyCoreRunner, StrategyCreator, StrategyCreatorParams, StrategyDecision, StrategyDirectionalTpSlParams, StrategyDirectionalTpSlResult, StrategyEntryBaseParams, StrategyEntryModelFigures, StrategyEntryOrderPlan, StrategyEntryRuntimeBaseParams, StrategyEntryRuntimeBuilderParams, StrategyEntryRuntimeOptions, StrategyEntrySignalContext, StrategyEntrySignalDecisionBuilderParams, StrategyEntryTakeProfitsParams, StrategyFigureLine, StrategyFigurePoint, StrategyFigurePoints, StrategyFigureZone, StrategyHookAfterAiContext, StrategyHookAfterDecisionContext, StrategyHookAfterPlaceOrderContext, StrategyHookAiContext, StrategyHookAiSkippedReason, StrategyHookBarContext, StrategyHookBeforeCloseContext, StrategyHookBeforeEntryGateContext, StrategyHookBeforePlaceOrderContext, StrategyHookCtx, StrategyHookEnrichContext, StrategyHookEntryContext, StrategyHookErrorContext, StrategyHookErrorPayload, StrategyHookGateResult, StrategyHookInitContext, StrategyHookMarketContext, StrategyHookMlContext, StrategyHookMlSkippedReason, StrategyHookOrderContext, StrategyHookPolicyContext, StrategyHookSkipContext, StrategyHookStage, StrategyIndicatorsMap, StrategyIndicatorsState, StrategyLastTradeController, StrategyLastTradeControllerParams, StrategyManifest, StrategyMarketSnapshot, StrategyMlAdapter, StrategyPluginDefinition, StrategyProtectPlan, StrategyRegistryEntry, StrategyResultConfig, StrategyResultEntry, StrategyResults, StrategyRuntimeAiOptions, StrategyRuntimeMlOptions, StrategySignalMetaParams, StrategySignalPriceParams, Test, TestCompare, TestCompareList, TestConnector, TestConnectorContext, TestConnectorCreator, TestResult, TestStat, TestSuite, TestThresholds, TestThresholdsKey, TestWorkerResult, TestingBox, TestingBoxResult, TestingOptions, ThresholdLevel, Ticker, Tp, Trend, TrendLine, TrendLineMode, TrendLineOptions, UIFilters };