-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuBacktestEngine.pas
More file actions
405 lines (351 loc) · 11.8 KB
/
uBacktestEngine.pas
File metadata and controls
405 lines (351 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
unit uBacktestEngine;
interface
uses
System.SysUtils, System.Classes, System.Math, System.Generics.Collections, System.DateUtils,
System.JSON, Data.DB, FireDAC.Comp.Client,
uHyperSniper, uDataManager;
type
TTradeSignal = record
Timestamp: TDateTime;
Decision: string; // 'LONG', 'SHORT'
Entry: Double;
ExitPrice: Double;
PnLPct: Double;
IsWin: Boolean;
Score: Integer;
end;
TBacktestResult = record
Symbol: string;
TotalSignals: Integer;
Wins: Integer;
Losses: Integer;
HitRate: Double;
AvgPnL: Double;
TotalPnL: Double;
LongN: Integer;
ShortN: Integer;
LongHR: Double;
ShortHR: Double;
MaxWin: Double;
MaxLoss: Double;
Signals: TList<TTradeSignal>;
Errors: TStringList;
end;
TBacktestEngine = class
public
class function ResampleTF(ASource: TFDMemTable; const ARule: string): TFDMemTable;
class procedure MemTableToArray(AMemTable: TFDMemTable;
var Closes, Highs, Lows, Volumes, Opens: TArray<Double>;
var Timestamps: TArray<TDateTime>;
var OI: TArray<Double>;
var Funding: TArray<Double>);
class function ExecuteBacktest(
const ASymbol: string;
ADataFull: TFDMemTable;
APeriodStart, APeriodEnd: TDateTime;
AWarmupN: Integer;
AConfig: TJSONObject
): TBacktestResult;
end;
implementation
class function TBacktestEngine.ResampleTF(ASource: TFDMemTable; const ARule: string): TFDMemTable;
var
Interval: Integer;
CurrentPeriod: TDateTime;
H, L, O, C, V, OI, F: Double;
First: Boolean;
function GetPeriodStart(ADT: TDateTime; const ARule: string): TDateTime;
begin
Result := RecodeTime(ADT, HourOf(ADT), 0, 0, 0);
if ARule = '4h' then
begin
Result := RecodeTime(ADT, 0, 0, 0, 0);
Result := IncHour(Result, (HourOf(ADT) div 4) * 4);
end
else if ARule = 'D' then
Result := RecodeTime(ADT, 0, 0, 0, 0);
end;
begin
Result := TFDMemTable.Create(nil);
Result.FieldDefs.Add('timestamp', ftDateTime);
Result.FieldDefs.Add('open', ftFloat);
Result.FieldDefs.Add('high', ftFloat);
Result.FieldDefs.Add('low', ftFloat);
Result.FieldDefs.Add('close', ftFloat);
Result.FieldDefs.Add('volume', ftFloat);
Result.FieldDefs.Add('oi', ftFloat);
Result.FieldDefs.Add('funding', ftFloat);
Result.CreateDataSet;
Result.IndexFieldNames := 'timestamp';
if ASource.IsEmpty then Exit;
ASource.First;
CurrentPeriod := GetPeriodStart(ASource.FieldByName('timestamp').AsDateTime, ARule);
H := -1e10; L := 1e10; O := ASource.FieldByName('open').AsFloat; V := 0; OI := 0; F := 0;
First := True;
while not ASource.Eof do
begin
var TS := ASource.FieldByName('timestamp').AsDateTime;
var PeriodStart := GetPeriodStart(TS, ARule);
if PeriodStart <> CurrentPeriod then
begin
Result.Append;
Result.FieldByName('timestamp').AsDateTime := CurrentPeriod;
Result.FieldByName('open').AsFloat := O;
Result.FieldByName('high').AsFloat := H;
Result.FieldByName('low').AsFloat := L;
Result.FieldByName('close').AsFloat := C;
Result.FieldByName('volume').AsFloat := V;
Result.FieldByName('oi').AsFloat := OI;
Result.FieldByName('funding').AsFloat := F;
Result.Post;
CurrentPeriod := PeriodStart;
O := ASource.FieldByName('open').AsFloat;
H := ASource.FieldByName('high').AsFloat;
L := ASource.FieldByName('low').AsFloat;
V := 0;
end;
C := ASource.FieldByName('close').AsFloat;
V := V + ASource.FieldByName('volume').AsFloat;
H := Max(H, ASource.FieldByName('high').AsFloat);
L := Min(L, ASource.FieldByName('low').AsFloat);
OI := ASource.FieldByName('oi').AsFloat;
F := ASource.FieldByName('funding').AsFloat;
ASource.Next;
end;
Result.Append;
Result.FieldByName('timestamp').AsDateTime := CurrentPeriod;
Result.FieldByName('open').AsFloat := O;
Result.FieldByName('high').AsFloat := H;
Result.FieldByName('low').AsFloat := L;
Result.FieldByName('close').AsFloat := C;
Result.FieldByName('volume').AsFloat := V;
Result.FieldByName('oi').AsFloat := OI;
Result.FieldByName('funding').AsFloat := F;
Result.Post;
end;
class procedure TBacktestEngine.MemTableToArray(AMemTable: TFDMemTable;
var Closes, Highs, Lows, Volumes, Opens: TArray<Double>;
var Timestamps: TArray<TDateTime>;
var OI: TArray<Double>;
var Funding: TArray<Double>);
var
I: Integer;
begin
AMemTable.First;
SetLength(Closes, AMemTable.RecordCount);
SetLength(Highs, AMemTable.RecordCount);
SetLength(Lows, AMemTable.RecordCount);
SetLength(Volumes, AMemTable.RecordCount);
SetLength(Opens, AMemTable.RecordCount);
SetLength(Timestamps, AMemTable.RecordCount);
SetLength(OI, AMemTable.RecordCount);
SetLength(Funding, AMemTable.RecordCount);
I := 0;
while not AMemTable.Eof do
begin
Closes[I] := AMemTable.FieldByName('close').AsFloat;
Highs[I] := AMemTable.FieldByName('high').AsFloat;
Lows[I] := AMemTable.FieldByName('low').AsFloat;
Volumes[I] := AMemTable.FieldByName('volume').AsFloat;
Opens[I] := AMemTable.FieldByName('open').AsFloat;
Timestamps[I] := AMemTable.FieldByName('timestamp').AsDateTime;
OI[I] := AMemTable.FieldByName('oi').AsFloat;
Funding[I] := AMemTable.FieldByName('funding').AsFloat;
Inc(I);
AMemTable.Next;
end;
end;
class function TBacktestEngine.ExecuteBacktest(const ASymbol: string;
ADataFull: TFDMemTable; APeriodStart, APeriodEnd: TDateTime;
AWarmupN: Integer; AConfig: TJSONObject): TBacktestResult;
var
Table4H, TableD: TFDMemTable;
C1H, H1H, L1H, V1H, OI1H, F1H: TArray<Double>;
T1H: TArray<TDateTime>;
C4H, H4H, L4H, V4H, OI4H, F4H: TArray<Double>;
T4H: TArray<TDateTime>;
CD, HD, LD, VD, OID, FD: TArray<Double>;
TD: TArray<TDateTime>;
I, Idx4H, IdxD: Integer;
Details: TDecisionDetails;
Decision: Integer;
Signal: TTradeSignal;
Raw: Integer;
M4H: Integer;
EntryPrice, ExitPrice, PnL: Double;
PnLs: TList<Double>;
dummy1, dummy2, dummy3, dummy4, dummy5: TArray<Double>;
dummyT: TArray<TDateTime>;
O1H: TArray<Double>;
begin
Result.Symbol := ASymbol;
Result.TotalSignals := 0;
Result.Wins := 0;
Result.Losses := 0;
Result.HitRate := 0;
Result.AvgPnL := 0;
Result.TotalPnL := 0;
Result.LongN := 0;
Result.ShortN := 0;
Result.LongHR := 0;
Result.ShortHR := 0;
Result.MaxWin := 0;
Result.MaxLoss := 0;
Result.Signals := TList<TTradeSignal>.Create;
Result.Errors := TStringList.Create;
PnLs := TList<Double>.Create;
Table4H := ResampleTF(ADataFull, '4h');
TableD := ResampleTF(ADataFull, 'D');
try
MemTableToArray(ADataFull, C1H, H1H, L1H, V1H, O1H, T1H, OI1H, F1H);
MemTableToArray(Table4H, C4H, H4H, L4H, dummy1, dummy2, T4H, dummy4, dummy5);
MemTableToArray(TableD, CD, HD, LD, dummy1, dummy2, TD, dummy4, dummy5);
Idx4H := 0;
IdxD := 0;
var v_climax: Double := 3.0;
var s_trend: Integer := 6;
var s_range: Integer := 7;
var s_start: Integer := 8;
var s_end: Integer := 20;
var e_score: Integer := 10;
if Assigned(AConfig) and Assigned(AConfig.GetValue(ASymbol)) then
begin
var Obj := AConfig.GetValue(ASymbol) as TJSONObject;
v_climax := StrToFloatDef(Obj.GetValue('vol_climax_mult').Value, 3.0, TFormatSettings.Invariant);
s_trend := StrToIntDef(Obj.GetValue('score_threshold_trend').Value, 6);
s_range := StrToIntDef(Obj.GetValue('score_threshold_range').Value, 7);
s_start := StrToIntDef(Obj.GetValue('session_start').Value, 8);
s_end := StrToIntDef(Obj.GetValue('session_end').Value, 20);
e_score := StrToIntDef(Obj.GetValue('extreme_score').Value, 10);
end;
for I := 0 to High(T1H) do
begin
if I < AWarmupN then
Continue;
var TS := T1H[I];
if (TS < APeriodStart) or (TS > APeriodEnd) then
Continue;
if I + 1 > High(T1H) then
Break;
while (Idx4H < High(T4H)) and (T4H[Idx4H+1] <= TS) do
Inc(Idx4H);
while (IdxD < High(TD)) and (TD[IdxD+1] <= TS) do
Inc(IdxD);
var s1h := Max(0, I + 1 - 210);
var s4h := Max(0, Idx4H + 1 - 60);
var sd := Max(0, IdxD + 1 - 60);
var wC1H := Copy(C1H, s1h, I - s1h + 1);
var wH1H := Copy(H1H, s1h, I - s1h + 1);
var wL1H := Copy(L1H, s1h, I - s1h + 1);
var wV1H := Copy(V1H, s1h, I - s1h + 1);
var wOI1H := Copy(OI1H, s1h, I - s1h + 1);
var wC4H := Copy(C4H, s4h, Idx4H - s4h + 1);
var wH4H := Copy(H4H, s4h, Idx4H - s4h + 1);
var wL4H := Copy(L4H, s4h, Idx4H - s4h + 1);
var wCD := Copy(CD, sd, IdxD - sd + 1);
var wHD := Copy(HD, sd, IdxD - sd + 1);
var wLD := Copy(LD, sd, IdxD - sd + 1);
if (Length(wC4H) < 20) or (Length(wCD) < 10) then Continue;
Raw := (HourOf(TS) * 60 + MinuteOf(TS)) mod 240;
if Raw = 0 then
M4H := 0
else
M4H := 240 - Raw;
try
Decision := TradingDecision(
wC1H, wH1H, wL1H, wV1H,
wC4H, wH4H, wL4H,
wCD, wHD, wLD,
Details,
M4H, True, HourOf(TS),
1.0, 1.0, wOI1H, F1H[I],
'NEUTRAL',
v_climax, s_trend, s_range, s_start, s_end, e_score
);
if Decision <> NO_OPEN then
begin
EntryPrice := O1H[I + 1];
ExitPrice := C1H[I + 1];
if EntryPrice > 0 then
begin
if Decision = LONG_POS then
begin
PnL := (ExitPrice - EntryPrice) / EntryPrice * 100.0;
Signal.Decision := 'LONG';
end
else
begin
PnL := (EntryPrice - ExitPrice) / EntryPrice * 100.0;
Signal.Decision := 'SHORT';
end;
if IsNaN(PnL) or IsInfinite(PnL) then
PnL := 0;
Signal.Timestamp := TS;
Signal.Entry := EntryPrice;
Signal.ExitPrice := ExitPrice;
Signal.PnLPct := PnL;
Signal.IsWin := PnL > 0;
Signal.Score := Details.Score;
Result.Signals.Add(Signal);
PnLs.Add(PnL);
Inc(Result.TotalSignals);
if Signal.IsWin then
Inc(Result.Wins)
else
Inc(Result.Losses);
if Signal.Decision = 'LONG' then
Inc(Result.LongN) else Inc(Result.ShortN);
Result.MaxWin := Max(Result.MaxWin, PnL);
Result.MaxLoss := Min(Result.MaxLoss, PnL);
end;
end;
except
on E: Exception do
Result.Errors.Add(Format('%s: %s', [DateTimeToStr(TS), E.Message]));
end;
end;
Result.HitRate := 0;
Result.AvgPnL := 0;
Result.TotalPnL := 0;
Result.LongHR := 0;
Result.ShortHR := 0;
if Result.TotalSignals > 0 then
begin
Result.HitRate := (Result.Wins / Result.TotalSignals) * 100.0;
for var PValue in PnLs do
Result.TotalPnL := Result.TotalPnL + PValue;
Result.AvgPnL := Result.TotalPnL / Result.TotalSignals;
var LWins := 0;
var SWins := 0;
for var SigObj in Result.Signals do
begin
if SigObj.IsWin then
begin
if SigObj.Decision = 'LONG' then
Inc(LWins)
else
Inc(SWins);
end;
end;
if Result.LongN > 0 then
Result.LongHR := (LWins / Result.LongN) * 100.0;
if Result.ShortN > 0 then
Result.ShortHR := (SWins / Result.ShortN) * 100.0;
end;
if IsNaN(Result.HitRate) or IsInfinite(Result.HitRate) then
Result.HitRate := 0;
if IsNaN(Result.AvgPnL) or IsInfinite(Result.AvgPnL) then
Result.AvgPnL := 0;
if IsNaN(Result.TotalPnL) or IsInfinite(Result.TotalPnL) then
Result.TotalPnL := 0;
if IsNaN(Result.LongHR) or IsInfinite(Result.LongHR) then
Result.LongHR := 0;
if IsNaN(Result.ShortHR) or IsInfinite(Result.ShortHR) then
Result.ShortHR := 0;
finally
PnLs.Free;
Table4H.Free;
TableD.Free;
end;
end;
end.