@tradejs/types 1.0.0
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 +36 -0
- package/dist/index.d.mts +842 -0
- package/dist/index.d.ts +842 -0
- package/dist/index.js +18 -0
- package/dist/index.mjs +0 -0
- package/package.json +28 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
import { KlineIntervalV3 } from 'bybit-api';
|
|
2
|
+
|
|
3
|
+
interface Metrics {
|
|
4
|
+
periodDays: number;
|
|
5
|
+
periodMonths: number;
|
|
6
|
+
orders: number;
|
|
7
|
+
ordersPerMonth: number;
|
|
8
|
+
wins: number;
|
|
9
|
+
losses: number;
|
|
10
|
+
exposure: number;
|
|
11
|
+
amount: number;
|
|
12
|
+
maxAmount: number;
|
|
13
|
+
minAmount: number;
|
|
14
|
+
netProfit: number;
|
|
15
|
+
totalReturn: number;
|
|
16
|
+
cagr: number;
|
|
17
|
+
maxDrawdown: number;
|
|
18
|
+
calmar: number | null;
|
|
19
|
+
winRate: number;
|
|
20
|
+
riskRewardRatio: number | null;
|
|
21
|
+
expectancy: number;
|
|
22
|
+
maxConsecutiveWins: number;
|
|
23
|
+
maxConsecutiveLosses: number;
|
|
24
|
+
sharpeRatio: number | null;
|
|
25
|
+
}
|
|
26
|
+
interface EOMPoint {
|
|
27
|
+
month: string;
|
|
28
|
+
ts: number;
|
|
29
|
+
amount: number;
|
|
30
|
+
}
|
|
31
|
+
interface MonthlyEquityStats {
|
|
32
|
+
eomSeries: EOMPoint[];
|
|
33
|
+
monthlyReturns: number[];
|
|
34
|
+
monthlyMean: number;
|
|
35
|
+
monthlyStd: number;
|
|
36
|
+
monthlyDownsideStd: number;
|
|
37
|
+
sharpeMonthly: number | null;
|
|
38
|
+
sharpeMonthlyAnnualized: number | null;
|
|
39
|
+
sortinoMonthly: number | null;
|
|
40
|
+
sortinoMonthlyAnnualized: number | null;
|
|
41
|
+
positiveMonths: number;
|
|
42
|
+
maxMonthlyGain: number;
|
|
43
|
+
maxMonthlyDrop: number;
|
|
44
|
+
}
|
|
45
|
+
type ThresholdLevel = 'error' | 'warning' | 'success';
|
|
46
|
+
interface MetricThreshold {
|
|
47
|
+
thresholds: [number, number];
|
|
48
|
+
direction: 'higher' | 'lower';
|
|
49
|
+
isPercent?: boolean;
|
|
50
|
+
isAmount?: boolean;
|
|
51
|
+
precision: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type Interval = KlineIntervalV3;
|
|
55
|
+
type Provider = 'bybit' | 'binance' | 'coinbase';
|
|
56
|
+
interface Candle {
|
|
57
|
+
open: number;
|
|
58
|
+
high: number;
|
|
59
|
+
low: number;
|
|
60
|
+
close: number;
|
|
61
|
+
volume: number;
|
|
62
|
+
timestamp: number;
|
|
63
|
+
turnover: number;
|
|
64
|
+
}
|
|
65
|
+
interface KlineChartItem extends Candle {
|
|
66
|
+
dt: string;
|
|
67
|
+
[key: string]: unknown;
|
|
68
|
+
}
|
|
69
|
+
type KlineChartData = Array<KlineChartItem>;
|
|
70
|
+
interface KlineRequest {
|
|
71
|
+
symbol: string;
|
|
72
|
+
interval: Interval;
|
|
73
|
+
start?: number;
|
|
74
|
+
end: number;
|
|
75
|
+
silent?: boolean;
|
|
76
|
+
cacheOnly?: boolean;
|
|
77
|
+
}
|
|
78
|
+
interface Tp {
|
|
79
|
+
price: number;
|
|
80
|
+
rate: number;
|
|
81
|
+
done?: boolean;
|
|
82
|
+
}
|
|
83
|
+
type Sl = number | null;
|
|
84
|
+
type Direction = 'LONG' | 'SHORT';
|
|
85
|
+
type Trend = 'BULL' | 'BEAR';
|
|
86
|
+
type Order = {
|
|
87
|
+
symbol: string;
|
|
88
|
+
isLimit?: boolean;
|
|
89
|
+
qty: number;
|
|
90
|
+
price: number;
|
|
91
|
+
timestamp: number;
|
|
92
|
+
direction: Direction;
|
|
93
|
+
signal?: Signal;
|
|
94
|
+
};
|
|
95
|
+
type Position = {
|
|
96
|
+
symbol: string;
|
|
97
|
+
qty: number;
|
|
98
|
+
price: number;
|
|
99
|
+
direction: Direction;
|
|
100
|
+
};
|
|
101
|
+
type OrderType = 'OPEN_LONG' | 'OPEN_SHORT' | 'CLOSE_LONG' | 'CLOSE_SHORT' | 'TAKE_PROFIT_LONG' | 'TAKE_PROFIT_SHORT' | 'STOP_LOSS_LONG' | 'STOP_LOSS_SHORT';
|
|
102
|
+
type ConnectorCreator = (config: ConnectorConfig) => Promise<Connector>;
|
|
103
|
+
interface ConnectorConfig {
|
|
104
|
+
userName: string;
|
|
105
|
+
}
|
|
106
|
+
interface ConnectorRegistryEntry {
|
|
107
|
+
name: string;
|
|
108
|
+
creator: ConnectorCreator;
|
|
109
|
+
providers?: string[];
|
|
110
|
+
}
|
|
111
|
+
interface ConnectorPluginDefinition {
|
|
112
|
+
connectorEntries: ConnectorRegistryEntry[];
|
|
113
|
+
}
|
|
114
|
+
type GetPosition = (symbol: string) => Promise<Position | null>;
|
|
115
|
+
type GetPositions = () => Promise<Position[]>;
|
|
116
|
+
type PlaceOrder = (order: Order, tp?: Tp[], slPrice?: Sl) => Promise<boolean>;
|
|
117
|
+
type ClosePosition = (order: Omit<Order, 'qty'>) => Promise<boolean>;
|
|
118
|
+
type Kline = (options: KlineRequest) => Promise<KlineChartData>;
|
|
119
|
+
type GetTickers = () => Promise<Ticker[]>;
|
|
120
|
+
interface Connector {
|
|
121
|
+
kline: Kline;
|
|
122
|
+
getState: () => Promise<object>;
|
|
123
|
+
setState: (state: object) => Promise<void>;
|
|
124
|
+
getPosition: GetPosition;
|
|
125
|
+
getPositions: GetPositions;
|
|
126
|
+
placeOrder: PlaceOrder;
|
|
127
|
+
closePosition: ClosePosition;
|
|
128
|
+
getTickers: GetTickers;
|
|
129
|
+
}
|
|
130
|
+
interface Indicator {
|
|
131
|
+
id: string;
|
|
132
|
+
label: string;
|
|
133
|
+
enabled: boolean;
|
|
134
|
+
periods?: Array<number>;
|
|
135
|
+
}
|
|
136
|
+
type Indicators = Indicator[];
|
|
137
|
+
interface Filters {
|
|
138
|
+
provider?: Provider;
|
|
139
|
+
symbol: string;
|
|
140
|
+
interval: Interval;
|
|
141
|
+
start: number;
|
|
142
|
+
end: number;
|
|
143
|
+
}
|
|
144
|
+
interface Ticker {
|
|
145
|
+
symbol: string;
|
|
146
|
+
lastPrice: number;
|
|
147
|
+
indexPrice: number;
|
|
148
|
+
markPrice: number;
|
|
149
|
+
prevPrice24h: number;
|
|
150
|
+
price24hPcnt: number;
|
|
151
|
+
highPrice24h: number;
|
|
152
|
+
lowPrice24h: number;
|
|
153
|
+
prevPrice1h: number;
|
|
154
|
+
openInterest: number;
|
|
155
|
+
openInterestValue: number;
|
|
156
|
+
turnover24h: number;
|
|
157
|
+
volume24h: number;
|
|
158
|
+
fundingRate: number;
|
|
159
|
+
nextFundingTime: number;
|
|
160
|
+
predictedDeliveryPrice: string;
|
|
161
|
+
basisRate: string;
|
|
162
|
+
deliveryFeeRate: string;
|
|
163
|
+
deliveryTime: number;
|
|
164
|
+
ask1Size: number;
|
|
165
|
+
bid1Price: number;
|
|
166
|
+
ask1Price: number;
|
|
167
|
+
bid1Size: number;
|
|
168
|
+
basis: string;
|
|
169
|
+
preOpenPrice: string;
|
|
170
|
+
preQty: string;
|
|
171
|
+
}
|
|
172
|
+
type TrendLineMode = 'lows' | 'highs';
|
|
173
|
+
type TrendLine = {
|
|
174
|
+
id: string;
|
|
175
|
+
mode: TrendLineMode;
|
|
176
|
+
distance: number;
|
|
177
|
+
touches: {
|
|
178
|
+
timestamp: number;
|
|
179
|
+
value: number;
|
|
180
|
+
}[];
|
|
181
|
+
points: {
|
|
182
|
+
timestamp: number;
|
|
183
|
+
value: number;
|
|
184
|
+
}[];
|
|
185
|
+
alpha?: number[];
|
|
186
|
+
};
|
|
187
|
+
interface StrategyFigurePoint {
|
|
188
|
+
timestamp: number;
|
|
189
|
+
value: number;
|
|
190
|
+
}
|
|
191
|
+
interface StrategyFigureLine {
|
|
192
|
+
id?: string;
|
|
193
|
+
kind?: string;
|
|
194
|
+
points: StrategyFigurePoint[];
|
|
195
|
+
color?: string;
|
|
196
|
+
width?: number;
|
|
197
|
+
style?: 'solid' | 'dashed';
|
|
198
|
+
}
|
|
199
|
+
interface StrategyFigurePoints {
|
|
200
|
+
id?: string;
|
|
201
|
+
kind?: string;
|
|
202
|
+
points: StrategyFigurePoint[];
|
|
203
|
+
color?: string;
|
|
204
|
+
radius?: number;
|
|
205
|
+
}
|
|
206
|
+
interface StrategyFigureZone {
|
|
207
|
+
id?: string;
|
|
208
|
+
kind?: string;
|
|
209
|
+
start: StrategyFigurePoint;
|
|
210
|
+
end: StrategyFigurePoint;
|
|
211
|
+
color?: string;
|
|
212
|
+
borderColor?: string;
|
|
213
|
+
}
|
|
214
|
+
interface StrategyEntryModelFigures {
|
|
215
|
+
lines?: StrategyFigureLine[];
|
|
216
|
+
points?: StrategyFigurePoints[];
|
|
217
|
+
zones?: StrategyFigureZone[];
|
|
218
|
+
}
|
|
219
|
+
interface TrendLineOptions {
|
|
220
|
+
mode: TrendLineMode;
|
|
221
|
+
maxLines?: number;
|
|
222
|
+
range?: number;
|
|
223
|
+
epsilon?: number;
|
|
224
|
+
epsilonOffset?: number;
|
|
225
|
+
minTouches?: number;
|
|
226
|
+
minDistance?: number;
|
|
227
|
+
firstRange?: number;
|
|
228
|
+
offset?: number;
|
|
229
|
+
minTouchGap?: number;
|
|
230
|
+
maxTouchGap?: number;
|
|
231
|
+
capture?: boolean;
|
|
232
|
+
bestLines?: number;
|
|
233
|
+
maxDistance?: number;
|
|
234
|
+
}
|
|
235
|
+
interface Signal {
|
|
236
|
+
signalId: string;
|
|
237
|
+
symbol: string;
|
|
238
|
+
interval: Interval;
|
|
239
|
+
strategy: string;
|
|
240
|
+
direction: Direction;
|
|
241
|
+
timestamp: number;
|
|
242
|
+
orderStatus?: 'completed' | 'failed' | 'skipped' | 'canceled';
|
|
243
|
+
orderSkipReason?: string;
|
|
244
|
+
isConfigFromBacktest?: boolean;
|
|
245
|
+
ml?: {
|
|
246
|
+
probability: number;
|
|
247
|
+
threshold: number;
|
|
248
|
+
passed: boolean;
|
|
249
|
+
};
|
|
250
|
+
figures: {
|
|
251
|
+
trendLine?: TrendLine;
|
|
252
|
+
lines?: StrategyFigureLine[];
|
|
253
|
+
points?: StrategyFigurePoints[];
|
|
254
|
+
zones?: StrategyFigureZone[];
|
|
255
|
+
[key: string]: any;
|
|
256
|
+
};
|
|
257
|
+
prices: {
|
|
258
|
+
currentPrice: number;
|
|
259
|
+
takeProfitPrice: number;
|
|
260
|
+
stopLossPrice: number;
|
|
261
|
+
riskRatio: number;
|
|
262
|
+
};
|
|
263
|
+
indicators: Record<string, any>;
|
|
264
|
+
additionalIndicators?: Record<string, any>;
|
|
265
|
+
}
|
|
266
|
+
interface SignalAnalysis {
|
|
267
|
+
direction: Direction | null;
|
|
268
|
+
quality: 1 | 2 | 3 | 4 | 5 | number;
|
|
269
|
+
needRetest: boolean;
|
|
270
|
+
retestPrice: number | null;
|
|
271
|
+
takeProfitPrice: number | null;
|
|
272
|
+
stopLossPrice: number | null;
|
|
273
|
+
setup?: string;
|
|
274
|
+
confirmations?: string;
|
|
275
|
+
btcContext?: string;
|
|
276
|
+
retestPlan?: string;
|
|
277
|
+
riskLevels?: string;
|
|
278
|
+
qualityReason?: string;
|
|
279
|
+
triggerInvalidation?: string;
|
|
280
|
+
comment: string;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
type Strategy = (candle: KlineChartItem, btcCandle: KlineChartItem) => Promise<string | Signal>;
|
|
284
|
+
type BacktestPriceMode = 'mid' | 'close' | 'open' | 'rand';
|
|
285
|
+
interface StrategyConfig {
|
|
286
|
+
BACKTEST_PRICE_MODE?: BacktestPriceMode;
|
|
287
|
+
ML_ENABLED?: boolean;
|
|
288
|
+
[key: string]: any;
|
|
289
|
+
}
|
|
290
|
+
type StrategyResultConfig = StrategyConfig;
|
|
291
|
+
type StrategyConfigGrid = Record<string, unknown[]>;
|
|
292
|
+
interface StrategyCreatorParams {
|
|
293
|
+
userName: string;
|
|
294
|
+
symbol: string;
|
|
295
|
+
config: StrategyConfig;
|
|
296
|
+
connector: Connector;
|
|
297
|
+
data: KlineChartData;
|
|
298
|
+
btcData: KlineChartData;
|
|
299
|
+
btcBinanceData?: KlineChartData;
|
|
300
|
+
btcCoinbaseData?: KlineChartData;
|
|
301
|
+
}
|
|
302
|
+
type StrategyCreator = (params: StrategyCreatorParams) => Promise<Strategy>;
|
|
303
|
+
type TestingOptions = Pick<KlineRequest, 'start' | 'end'>;
|
|
304
|
+
interface BacktestRunConfig {
|
|
305
|
+
strategyName: string;
|
|
306
|
+
strategyConfig: StrategyConfig;
|
|
307
|
+
connectorName: string;
|
|
308
|
+
}
|
|
309
|
+
interface Test extends BacktestRunConfig {
|
|
310
|
+
userName: string;
|
|
311
|
+
name: string;
|
|
312
|
+
testId: string;
|
|
313
|
+
testSuiteId: string;
|
|
314
|
+
symbol: string;
|
|
315
|
+
options: TestingOptions;
|
|
316
|
+
ml?: boolean;
|
|
317
|
+
chunkId?: string;
|
|
318
|
+
}
|
|
319
|
+
type TestSuite = Test[];
|
|
320
|
+
interface TestStat extends Metrics {
|
|
321
|
+
score?: number;
|
|
322
|
+
}
|
|
323
|
+
interface StrategyResultEntry {
|
|
324
|
+
config: StrategyResultConfig;
|
|
325
|
+
stats: TestStat;
|
|
326
|
+
}
|
|
327
|
+
type StrategyResults = Record<string, StrategyResultEntry>;
|
|
328
|
+
interface MinimalStat {
|
|
329
|
+
amount: number;
|
|
330
|
+
profit: number;
|
|
331
|
+
orders: number;
|
|
332
|
+
}
|
|
333
|
+
interface TestingBoxResult {
|
|
334
|
+
orderLogId: string;
|
|
335
|
+
stat: MinimalStat;
|
|
336
|
+
}
|
|
337
|
+
type TestingBox = (test: Test) => Promise<TestingBoxResult | null>;
|
|
338
|
+
interface TestWorkerResult extends TestingBoxResult {
|
|
339
|
+
test: Test;
|
|
340
|
+
}
|
|
341
|
+
interface CompletedTest extends Omit<TestWorkerResult, 'stat'> {
|
|
342
|
+
stat: TestStat;
|
|
343
|
+
}
|
|
344
|
+
type OrderLog = Order & {
|
|
345
|
+
type: OrderType;
|
|
346
|
+
profit: number;
|
|
347
|
+
amount: number;
|
|
348
|
+
fee?: number;
|
|
349
|
+
index: number;
|
|
350
|
+
};
|
|
351
|
+
type OrderLogData = OrderLog[];
|
|
352
|
+
type SimpleOrderLogData = [number, number][];
|
|
353
|
+
interface TestResult extends Omit<CompletedTest, 'orderLogId'> {
|
|
354
|
+
orderLog: SimpleOrderLogData;
|
|
355
|
+
}
|
|
356
|
+
interface PositionLog {
|
|
357
|
+
direction: Direction;
|
|
358
|
+
open: {
|
|
359
|
+
amount: number;
|
|
360
|
+
timestamp: number;
|
|
361
|
+
};
|
|
362
|
+
close: {
|
|
363
|
+
amount: number;
|
|
364
|
+
timestamp: number;
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
type PositionLogData = PositionLog[];
|
|
368
|
+
type TestThresholds = Record<keyof TestStat, MetricThreshold>;
|
|
369
|
+
type TestThresholdsKey = keyof TestThresholds;
|
|
370
|
+
interface TestConnector extends Connector {
|
|
371
|
+
getResult: () => Promise<TestingBoxResult>;
|
|
372
|
+
checkTp: (candle: Candle) => Promise<void>;
|
|
373
|
+
checkSl: (candle: Candle) => Promise<void>;
|
|
374
|
+
drainMlResultsBatch: () => Promise<Array<{
|
|
375
|
+
signalId: string;
|
|
376
|
+
profit: number;
|
|
377
|
+
}>>;
|
|
378
|
+
}
|
|
379
|
+
interface TestConnectorContext {
|
|
380
|
+
userName?: string;
|
|
381
|
+
mlEnabled?: boolean;
|
|
382
|
+
}
|
|
383
|
+
type TestConnectorCreator = (connector: Connector, context?: TestConnectorContext) => TestConnector;
|
|
384
|
+
type ChartColor = string;
|
|
385
|
+
interface TestCompare {
|
|
386
|
+
testResult: TestResult;
|
|
387
|
+
color: ChartColor;
|
|
388
|
+
}
|
|
389
|
+
type TestCompareList = TestCompare[];
|
|
390
|
+
type OnChangeCompare = (testId: string) => void;
|
|
391
|
+
|
|
392
|
+
interface Bot {
|
|
393
|
+
symbol: string;
|
|
394
|
+
disabled?: boolean;
|
|
395
|
+
strategyName: string;
|
|
396
|
+
strategyConfig: StrategyConfig;
|
|
397
|
+
connectorName: string;
|
|
398
|
+
}
|
|
399
|
+
interface BotStatus {
|
|
400
|
+
symbol: string;
|
|
401
|
+
status: string;
|
|
402
|
+
}
|
|
403
|
+
type BotResults = BotStatus[];
|
|
404
|
+
type BotConfig = Bot[];
|
|
405
|
+
|
|
406
|
+
interface Item<T = Record<string, string | number | boolean>> {
|
|
407
|
+
label: string;
|
|
408
|
+
value: string;
|
|
409
|
+
description?: string;
|
|
410
|
+
data?: T;
|
|
411
|
+
}
|
|
412
|
+
type Items = Item[];
|
|
413
|
+
interface Figure {
|
|
414
|
+
ctx: CanvasRenderingContext2D;
|
|
415
|
+
x: number;
|
|
416
|
+
y: number;
|
|
417
|
+
color: string;
|
|
418
|
+
width: number;
|
|
419
|
+
height: number;
|
|
420
|
+
text?: string;
|
|
421
|
+
}
|
|
422
|
+
interface UIFilters extends Filters {
|
|
423
|
+
backtestId: string | null;
|
|
424
|
+
backtestStrategy: string | null;
|
|
425
|
+
}
|
|
426
|
+
type OnChangeFilters = (filters: Partial<UIFilters>) => void;
|
|
427
|
+
|
|
428
|
+
interface AIChatMessage {
|
|
429
|
+
from: 'user' | 'ai';
|
|
430
|
+
text: string;
|
|
431
|
+
command?: string;
|
|
432
|
+
}
|
|
433
|
+
type AIChatHistory = AIChatMessage[];
|
|
434
|
+
|
|
435
|
+
interface AiPayload {
|
|
436
|
+
signal: {
|
|
437
|
+
symbol: Signal['symbol'];
|
|
438
|
+
signalId: Signal['signalId'];
|
|
439
|
+
interval: Signal['interval'];
|
|
440
|
+
direction: Signal['direction'];
|
|
441
|
+
timestamp: Signal['timestamp'];
|
|
442
|
+
strategy: Signal['strategy'];
|
|
443
|
+
prices: {
|
|
444
|
+
currentPrice: Signal['prices']['currentPrice'];
|
|
445
|
+
takeProfitPrice: Signal['prices']['takeProfitPrice'];
|
|
446
|
+
stopLossPrice: Signal['prices']['stopLossPrice'];
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
figures: Record<string, unknown>;
|
|
450
|
+
indicators: unknown;
|
|
451
|
+
additionalIndicators: unknown;
|
|
452
|
+
}
|
|
453
|
+
interface StrategyAiAdapter {
|
|
454
|
+
buildPayload?: (params: {
|
|
455
|
+
signal: Signal;
|
|
456
|
+
basePayload: AiPayload;
|
|
457
|
+
}) => AiPayload;
|
|
458
|
+
buildSystemPromptAddon?: (params: {
|
|
459
|
+
signal: Signal;
|
|
460
|
+
}) => string;
|
|
461
|
+
buildHumanPromptAddon?: (params: {
|
|
462
|
+
signal: Signal;
|
|
463
|
+
payload: AiPayload;
|
|
464
|
+
}) => string;
|
|
465
|
+
mapEntryRuntimeFromConfig?: (config: StrategyConfig) => StrategyRuntimeAiOptions | undefined;
|
|
466
|
+
}
|
|
467
|
+
interface StrategyMlAdapter {
|
|
468
|
+
normalizeSignal?: (signal: Signal) => Signal;
|
|
469
|
+
normalizeStrategyConfig?: (strategyConfig?: Record<string, any>) => Record<string, any> | undefined;
|
|
470
|
+
mapEntryRuntimeFromConfig?: (config: StrategyConfig) => StrategyRuntimeMlOptions | undefined;
|
|
471
|
+
}
|
|
472
|
+
interface StrategyHookGateResult {
|
|
473
|
+
allow?: boolean;
|
|
474
|
+
reason?: string;
|
|
475
|
+
}
|
|
476
|
+
interface StrategyHookBaseContext {
|
|
477
|
+
connector: Connector;
|
|
478
|
+
strategyName: string;
|
|
479
|
+
userName: string;
|
|
480
|
+
symbol: string;
|
|
481
|
+
config: StrategyConfig;
|
|
482
|
+
env: string;
|
|
483
|
+
isConfigFromBacktest: boolean;
|
|
484
|
+
}
|
|
485
|
+
interface StrategyHookErrorContext extends StrategyHookBaseContext {
|
|
486
|
+
stage: string;
|
|
487
|
+
error: unknown;
|
|
488
|
+
decision?: StrategyDecision;
|
|
489
|
+
signal?: Signal;
|
|
490
|
+
}
|
|
491
|
+
interface StrategyHookInitContext extends StrategyHookBaseContext {
|
|
492
|
+
data: KlineChartItem[];
|
|
493
|
+
btcData: KlineChartItem[];
|
|
494
|
+
}
|
|
495
|
+
interface StrategyHookAfterDecisionContext extends StrategyHookBaseContext {
|
|
496
|
+
decision: StrategyDecision;
|
|
497
|
+
candle: KlineChartItem;
|
|
498
|
+
btcCandle: KlineChartItem;
|
|
499
|
+
}
|
|
500
|
+
interface StrategyHookSkipContext extends StrategyHookAfterDecisionContext {
|
|
501
|
+
decision: Extract<StrategyDecision, {
|
|
502
|
+
kind: 'skip';
|
|
503
|
+
}>;
|
|
504
|
+
}
|
|
505
|
+
interface StrategyHookBeforeCloseContext extends StrategyHookBaseContext {
|
|
506
|
+
decision: Extract<StrategyDecision, {
|
|
507
|
+
kind: 'exit';
|
|
508
|
+
}>;
|
|
509
|
+
}
|
|
510
|
+
interface StrategyHookEnrichContext extends StrategyHookBaseContext {
|
|
511
|
+
decision: Extract<StrategyDecision, {
|
|
512
|
+
kind: 'entry';
|
|
513
|
+
}>;
|
|
514
|
+
runtime: StrategyEntryRuntimeOptions | undefined;
|
|
515
|
+
signal?: Signal;
|
|
516
|
+
}
|
|
517
|
+
interface StrategyHookAfterAiContext extends StrategyHookEnrichContext {
|
|
518
|
+
quality?: number;
|
|
519
|
+
}
|
|
520
|
+
interface StrategyHookBeforeEntryGateContext extends StrategyHookAfterAiContext {
|
|
521
|
+
makeOrdersEnabled: boolean;
|
|
522
|
+
minAiQuality: number;
|
|
523
|
+
}
|
|
524
|
+
interface StrategyHookBeforePlaceOrderContext extends StrategyHookBaseContext {
|
|
525
|
+
entryContext: StrategyEntrySignalContext;
|
|
526
|
+
runtime: StrategyEntryRuntimeOptions | undefined;
|
|
527
|
+
decision: Extract<StrategyDecision, {
|
|
528
|
+
kind: 'entry';
|
|
529
|
+
}>;
|
|
530
|
+
signal?: Signal;
|
|
531
|
+
}
|
|
532
|
+
interface StrategyHookAfterPlaceOrderContext extends StrategyHookEnrichContext {
|
|
533
|
+
orderResult: unknown;
|
|
534
|
+
}
|
|
535
|
+
interface StrategyManifest {
|
|
536
|
+
name: string;
|
|
537
|
+
entryRuntimeDefaults?: {
|
|
538
|
+
ai?: StrategyRuntimeAiOptions;
|
|
539
|
+
ml?: Pick<StrategyRuntimeMlOptions, 'enabled'>;
|
|
540
|
+
};
|
|
541
|
+
hooks?: {
|
|
542
|
+
onInit?: (params: StrategyHookInitContext) => Promise<void> | void;
|
|
543
|
+
afterCoreDecision?: (params: StrategyHookAfterDecisionContext) => Promise<void> | void;
|
|
544
|
+
onSkip?: (params: StrategyHookSkipContext) => Promise<void> | void;
|
|
545
|
+
beforeClosePosition?: (params: StrategyHookBeforeCloseContext) => Promise<StrategyHookGateResult | void> | StrategyHookGateResult | void;
|
|
546
|
+
afterEnrichMl?: (params: StrategyHookEnrichContext) => Promise<void> | void;
|
|
547
|
+
afterEnrichAi?: (params: StrategyHookAfterAiContext) => Promise<void> | void;
|
|
548
|
+
beforeEntryGate?: (params: StrategyHookBeforeEntryGateContext) => Promise<StrategyHookGateResult | void> | StrategyHookGateResult | void;
|
|
549
|
+
beforePlaceOrder?: (params: StrategyHookBeforePlaceOrderContext) => Promise<void> | void;
|
|
550
|
+
afterPlaceOrder?: (params: StrategyHookAfterPlaceOrderContext) => Promise<void> | void;
|
|
551
|
+
onRuntimeError?: (params: StrategyHookErrorContext) => Promise<void> | void;
|
|
552
|
+
};
|
|
553
|
+
aiAdapter?: StrategyAiAdapter;
|
|
554
|
+
mlAdapter?: StrategyMlAdapter;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
interface StrategySignalMetaParams {
|
|
558
|
+
symbol: string;
|
|
559
|
+
interval: Interval;
|
|
560
|
+
direction: Direction;
|
|
561
|
+
timestamp: number;
|
|
562
|
+
isConfigFromBacktest: boolean;
|
|
563
|
+
}
|
|
564
|
+
interface StrategySignalPriceParams {
|
|
565
|
+
currentPrice: number;
|
|
566
|
+
takeProfitPrice: number;
|
|
567
|
+
stopLossPrice: number;
|
|
568
|
+
riskRatio: number;
|
|
569
|
+
}
|
|
570
|
+
interface StrategyEntryBaseParams {
|
|
571
|
+
qty: number;
|
|
572
|
+
}
|
|
573
|
+
interface StrategyEntryTakeProfitsParams {
|
|
574
|
+
takeProfits: Tp[];
|
|
575
|
+
}
|
|
576
|
+
interface StrategyEntryRuntimeBaseParams {
|
|
577
|
+
symbol: string;
|
|
578
|
+
direction: Direction;
|
|
579
|
+
timestamp: number;
|
|
580
|
+
currentPrice: number;
|
|
581
|
+
}
|
|
582
|
+
type StrategyEntryRuntimeBuilderParams<TExtra extends object = {}> = StrategyEntryRuntimeBaseParams & TExtra;
|
|
583
|
+
type StrategyEntrySignalDecisionBuilderParams<TPriceFields extends object = StrategySignalPriceParams, TExtra extends object = {}> = StrategySignalMetaParams & StrategyEntryBaseParams & TPriceFields & TExtra;
|
|
584
|
+
type StrategyIndicatorsMap = Signal['indicators'];
|
|
585
|
+
type StrategyAdditionalIndicatorsMap = NonNullable<Signal['additionalIndicators']>;
|
|
586
|
+
interface BuildStrategySignalParams {
|
|
587
|
+
signalId: string;
|
|
588
|
+
strategy: Signal['strategy'];
|
|
589
|
+
symbol: string;
|
|
590
|
+
interval: Signal['interval'];
|
|
591
|
+
direction: Direction;
|
|
592
|
+
timestamp: number;
|
|
593
|
+
prices: Signal['prices'];
|
|
594
|
+
figures?: Signal['figures'];
|
|
595
|
+
indicators?: Signal['indicators'];
|
|
596
|
+
additionalIndicators?: NonNullable<Signal['additionalIndicators']>;
|
|
597
|
+
isConfigFromBacktest?: boolean;
|
|
598
|
+
}
|
|
599
|
+
type BuildStrategySignalDraft = Omit<BuildStrategySignalParams, 'signalId'> & {
|
|
600
|
+
signalId?: string;
|
|
601
|
+
};
|
|
602
|
+
interface StrategyEntrySignalContext {
|
|
603
|
+
strategy: Signal['strategy'];
|
|
604
|
+
symbol: Signal['symbol'];
|
|
605
|
+
interval: Signal['interval'];
|
|
606
|
+
direction: Signal['direction'];
|
|
607
|
+
timestamp: Signal['timestamp'];
|
|
608
|
+
prices: Signal['prices'];
|
|
609
|
+
isConfigFromBacktest?: Signal['isConfigFromBacktest'];
|
|
610
|
+
}
|
|
611
|
+
interface StrategyAPIEntryParams {
|
|
612
|
+
code?: string;
|
|
613
|
+
direction: Direction;
|
|
614
|
+
figures?: BuildStrategySignalDraft['figures'];
|
|
615
|
+
indicators?: BuildStrategySignalDraft['indicators'];
|
|
616
|
+
additionalIndicators?: BuildStrategySignalDraft['additionalIndicators'];
|
|
617
|
+
signalId?: BuildStrategySignalDraft['signalId'];
|
|
618
|
+
orderPlan: StrategyEntryOrderPlan;
|
|
619
|
+
runtime?: StrategyEntryRuntimeOptions;
|
|
620
|
+
}
|
|
621
|
+
interface StrategyAPIMarketDataParams {
|
|
622
|
+
preloadStart?: number;
|
|
623
|
+
backtestPriceMode?: BacktestPriceMode;
|
|
624
|
+
}
|
|
625
|
+
interface StrategyMarketSnapshot {
|
|
626
|
+
fullData: KlineChartData;
|
|
627
|
+
lastCandle: KlineChartItem;
|
|
628
|
+
timestamp: number;
|
|
629
|
+
currentPrice: number;
|
|
630
|
+
}
|
|
631
|
+
interface MlCandleIndicatorsSnapshot {
|
|
632
|
+
candles15m: Candle[];
|
|
633
|
+
candles1h: Candle[];
|
|
634
|
+
candles4h: Candle[];
|
|
635
|
+
candles1d: Candle[];
|
|
636
|
+
btcCandles15m: Candle[];
|
|
637
|
+
btcCandles1h: Candle[];
|
|
638
|
+
btcCandles4h: Candle[];
|
|
639
|
+
btcCandles1d: Candle[];
|
|
640
|
+
}
|
|
641
|
+
interface BaseIndicatorsHistorySnapshot {
|
|
642
|
+
maFast?: number[];
|
|
643
|
+
maMedium?: number[];
|
|
644
|
+
maSlow?: number[];
|
|
645
|
+
atr?: number[];
|
|
646
|
+
atrPct?: number[];
|
|
647
|
+
bbUpper?: number[];
|
|
648
|
+
bbMiddle?: number[];
|
|
649
|
+
bbLower?: number[];
|
|
650
|
+
obv?: number[];
|
|
651
|
+
smaObv?: number[];
|
|
652
|
+
macd?: number[];
|
|
653
|
+
macdSignal?: number[];
|
|
654
|
+
macdHistogram?: number[];
|
|
655
|
+
price24hPcnt?: number[];
|
|
656
|
+
price1hPcnt?: number[];
|
|
657
|
+
highPrice1h?: number[];
|
|
658
|
+
lowPrice1h?: number[];
|
|
659
|
+
volume1h?: number[];
|
|
660
|
+
highPrice24h?: number[];
|
|
661
|
+
lowPrice24h?: number[];
|
|
662
|
+
volume24h?: number[];
|
|
663
|
+
highLevel?: number[];
|
|
664
|
+
lowLevel?: number[];
|
|
665
|
+
prevClose?: number[];
|
|
666
|
+
correlation?: number[];
|
|
667
|
+
spread?: number[];
|
|
668
|
+
}
|
|
669
|
+
type IndicatorsHistorySnapshot = Record<string, number[] | Candle[]> & BaseIndicatorsHistorySnapshot & Partial<MlCandleIndicatorsSnapshot>;
|
|
670
|
+
interface IndicatorSnapshot {
|
|
671
|
+
maFast: number;
|
|
672
|
+
maMedium: number;
|
|
673
|
+
maSlow: number;
|
|
674
|
+
atr: number;
|
|
675
|
+
atrPct: number | null;
|
|
676
|
+
bbUpper: number;
|
|
677
|
+
bbMiddle: number;
|
|
678
|
+
bbLower: number;
|
|
679
|
+
obv: number;
|
|
680
|
+
smaObv: number;
|
|
681
|
+
macd: number | undefined;
|
|
682
|
+
macdSignal: number | undefined;
|
|
683
|
+
macdHistogram: number | undefined;
|
|
684
|
+
price24hPcnt: number;
|
|
685
|
+
price1hPcnt: number;
|
|
686
|
+
highPrice1h: number | null;
|
|
687
|
+
lowPrice1h: number | null;
|
|
688
|
+
volume1h: number | null;
|
|
689
|
+
highPrice24h: number | null;
|
|
690
|
+
lowPrice24h: number | null;
|
|
691
|
+
volume24h: number | null;
|
|
692
|
+
candle: Candle;
|
|
693
|
+
prevCandle: Candle | null;
|
|
694
|
+
highLevel: number | null;
|
|
695
|
+
lowLevel: number | null;
|
|
696
|
+
correlation: number;
|
|
697
|
+
spread: number | null;
|
|
698
|
+
}
|
|
699
|
+
interface StrategyDirectionalTpSlParams {
|
|
700
|
+
price: number;
|
|
701
|
+
direction: Direction;
|
|
702
|
+
takeProfitDelta: number;
|
|
703
|
+
stopLossDelta: number;
|
|
704
|
+
unit?: 'percent' | 'ratio';
|
|
705
|
+
maxLossValue?: number;
|
|
706
|
+
feePercent?: number;
|
|
707
|
+
}
|
|
708
|
+
interface StrategyDirectionalTpSlResult {
|
|
709
|
+
stopLossPrice: number;
|
|
710
|
+
takeProfitPrice: number;
|
|
711
|
+
riskRatio: number;
|
|
712
|
+
qty?: number;
|
|
713
|
+
}
|
|
714
|
+
interface StrategyLastTradeController {
|
|
715
|
+
isInCooldown: (timestamp: number) => boolean;
|
|
716
|
+
markTrade: (timestamp: number) => void;
|
|
717
|
+
getLastTradeTimestamp: () => number | null;
|
|
718
|
+
}
|
|
719
|
+
interface StrategyLastTradeControllerParams {
|
|
720
|
+
env?: string;
|
|
721
|
+
enabled?: boolean;
|
|
722
|
+
cooldownMs?: number;
|
|
723
|
+
}
|
|
724
|
+
interface StrategyAPI {
|
|
725
|
+
skip: (code: string) => Extract<StrategyDecision, {
|
|
726
|
+
kind: 'skip';
|
|
727
|
+
}>;
|
|
728
|
+
entry: (params: StrategyAPIEntryParams) => Promise<Extract<StrategyDecision, {
|
|
729
|
+
kind: 'entry';
|
|
730
|
+
}>>;
|
|
731
|
+
getMarketData: (params?: StrategyAPIMarketDataParams) => Promise<StrategyMarketSnapshot>;
|
|
732
|
+
nextIndicators: (candle: KlineChartData[number], btcCandle: KlineChartData[number]) => unknown;
|
|
733
|
+
getCurrentPosition: () => ReturnType<Connector['getPosition']>;
|
|
734
|
+
isCurrentPositionExists: () => Promise<boolean>;
|
|
735
|
+
getDirectionalTpSlPrices: (params: StrategyDirectionalTpSlParams) => StrategyDirectionalTpSlResult;
|
|
736
|
+
createLastTradeController: (params?: StrategyLastTradeControllerParams) => StrategyLastTradeController;
|
|
737
|
+
}
|
|
738
|
+
interface StrategyIndicatorsState<TNext = unknown, TSnapshot = Record<string, any> | undefined> {
|
|
739
|
+
isInitialized: () => boolean;
|
|
740
|
+
setCurrentBar: (candle: KlineChartData[number], btcCandle: KlineChartData[number]) => void;
|
|
741
|
+
onBar: (candle?: KlineChartData[number], btcCandle?: KlineChartData[number]) => void;
|
|
742
|
+
next: (candle: KlineChartData[number], btcCandle: KlineChartData[number]) => TNext;
|
|
743
|
+
ensureInitializedWithCurrentBar: () => {
|
|
744
|
+
snapshot: () => TSnapshot;
|
|
745
|
+
};
|
|
746
|
+
snapshot: () => TSnapshot;
|
|
747
|
+
latestNumber: <K extends Extract<keyof NonNullable<TSnapshot>, string>>(key: K) => number | undefined;
|
|
748
|
+
}
|
|
749
|
+
interface StrategyRuntimeMlOptions {
|
|
750
|
+
enabled?: boolean;
|
|
751
|
+
strategyConfig?: StrategyConfig;
|
|
752
|
+
mlThreshold?: number;
|
|
753
|
+
}
|
|
754
|
+
interface StrategyRuntimeAiOptions {
|
|
755
|
+
enabled?: boolean;
|
|
756
|
+
minQuality?: number;
|
|
757
|
+
}
|
|
758
|
+
interface StrategyEntryRuntimeOptions {
|
|
759
|
+
ml?: StrategyRuntimeMlOptions;
|
|
760
|
+
ai?: StrategyRuntimeAiOptions;
|
|
761
|
+
beforePlaceOrder?: () => Promise<void>;
|
|
762
|
+
}
|
|
763
|
+
interface StrategyEntryOrderPlan {
|
|
764
|
+
qty: number;
|
|
765
|
+
stopLossPrice: number;
|
|
766
|
+
takeProfits: Tp[];
|
|
767
|
+
}
|
|
768
|
+
interface StrategyClosePlan {
|
|
769
|
+
price: number;
|
|
770
|
+
timestamp: number;
|
|
771
|
+
direction: Direction;
|
|
772
|
+
}
|
|
773
|
+
type StrategyDecision = {
|
|
774
|
+
kind: 'skip';
|
|
775
|
+
code: string;
|
|
776
|
+
} | {
|
|
777
|
+
kind: 'entry';
|
|
778
|
+
code: string;
|
|
779
|
+
entryContext: StrategyEntrySignalContext;
|
|
780
|
+
orderPlan: StrategyEntryOrderPlan;
|
|
781
|
+
signal?: Signal;
|
|
782
|
+
runtime?: StrategyEntryRuntimeOptions;
|
|
783
|
+
} | {
|
|
784
|
+
kind: 'exit';
|
|
785
|
+
code: string;
|
|
786
|
+
closePlan: StrategyClosePlan;
|
|
787
|
+
};
|
|
788
|
+
interface CreateStrategyCoreParams<TConfig extends StrategyConfig, TIndicatorsState extends StrategyIndicatorsState = StrategyIndicatorsState> {
|
|
789
|
+
userName: string;
|
|
790
|
+
symbol: string;
|
|
791
|
+
config: TConfig;
|
|
792
|
+
isConfigFromBacktest: boolean;
|
|
793
|
+
connector: Connector;
|
|
794
|
+
data: KlineChartData;
|
|
795
|
+
btcData: KlineChartData;
|
|
796
|
+
loadPineScript: (fileNameOrPath: string, fallback?: string) => string;
|
|
797
|
+
strategyApi: StrategyAPI;
|
|
798
|
+
indicatorsState: TIndicatorsState;
|
|
799
|
+
}
|
|
800
|
+
type StrategyCoreRunner = (candle: KlineChartItem, btcCandle: KlineChartItem) => Promise<StrategyDecision> | StrategyDecision;
|
|
801
|
+
type CreateStrategyCore<TConfig extends StrategyConfig, TSnapshot extends Record<string, any> | undefined = Record<string, any> | undefined, TNext = unknown> = (params: CreateStrategyCoreParams<TConfig, StrategyIndicatorsState<TNext, TSnapshot>>) => Promise<StrategyCoreRunner> | StrategyCoreRunner;
|
|
802
|
+
interface StrategyRegistryEntry {
|
|
803
|
+
manifest: StrategyManifest;
|
|
804
|
+
creator: StrategyCreator;
|
|
805
|
+
}
|
|
806
|
+
interface StrategyPluginDefinition {
|
|
807
|
+
strategyEntries: StrategyRegistryEntry[];
|
|
808
|
+
}
|
|
809
|
+
interface IndicatorPluginComputeParams {
|
|
810
|
+
candle: Candle;
|
|
811
|
+
btcCandle?: Candle;
|
|
812
|
+
data: Candle[];
|
|
813
|
+
btcData: Candle[];
|
|
814
|
+
baseResult: Partial<IndicatorSnapshot>;
|
|
815
|
+
}
|
|
816
|
+
interface IndicatorPluginFigureRenderer {
|
|
817
|
+
key: string;
|
|
818
|
+
title?: string;
|
|
819
|
+
type?: 'line' | 'bar';
|
|
820
|
+
color?: string;
|
|
821
|
+
lineWidth?: number;
|
|
822
|
+
dashed?: boolean;
|
|
823
|
+
constant?: number;
|
|
824
|
+
}
|
|
825
|
+
interface IndicatorPluginRenderer {
|
|
826
|
+
indicatorName?: string;
|
|
827
|
+
shortName?: string;
|
|
828
|
+
paneId?: string;
|
|
829
|
+
minHeight?: number;
|
|
830
|
+
figures: IndicatorPluginFigureRenderer[];
|
|
831
|
+
}
|
|
832
|
+
interface IndicatorPluginEntry {
|
|
833
|
+
indicator: Indicator;
|
|
834
|
+
historyKey?: string;
|
|
835
|
+
compute?: (params: IndicatorPluginComputeParams) => number | null | undefined;
|
|
836
|
+
renderer?: IndicatorPluginRenderer;
|
|
837
|
+
}
|
|
838
|
+
interface IndicatorPluginDefinition {
|
|
839
|
+
indicatorEntries: IndicatorPluginEntry[];
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
export type { AIChatHistory, AIChatMessage, AiPayload, BacktestPriceMode, BacktestRunConfig, BaseIndicatorsHistorySnapshot, Bot, BotConfig, BotResults, BotStatus, BuildStrategySignalDraft, BuildStrategySignalParams, Candle, ChartColor, CompletedTest, Connector, ConnectorConfig, ConnectorCreator, ConnectorPluginDefinition, ConnectorRegistryEntry, CreateStrategyCore, CreateStrategyCoreParams, 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, 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, StrategyHookBaseContext, StrategyHookBeforeCloseContext, StrategyHookBeforeEntryGateContext, StrategyHookBeforePlaceOrderContext, StrategyHookEnrichContext, StrategyHookErrorContext, StrategyHookGateResult, StrategyHookInitContext, StrategyHookSkipContext, 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 };
|