-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathkantu_multithreading.pas
More file actions
149 lines (110 loc) · 4.85 KB
/
kantu_multithreading.pas
File metadata and controls
149 lines (110 loc) · 4.85 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
unit kantu_multithreading;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Dialogs, Forms,kantu_definitions, LCLProc, dateutils, kantu_simulation, math;
Type
TMyIndiThread = class(TThread)
private
protected
simulationResults : array of TIndicatorSimulationResults;
entryPattern, closePattern: TIndicatorPattern;
maxRulesPerCandle: integer;
slippagePercent: double;
i, j: integer;
maxCandleShift: integer;
singleCycleSystems: double;
resultQuota: integer;
useSL, useTP, useClose: Boolean;
startDate, endDate, endOutOfSample: TDateTime;
generationPeriod, walkForwardPeriod, strategiesRequested: integer;
testedPatterns: TIndicatorPatternGroup;
procedure Execute; override;
procedure syncSimulation;
public
isRandomPeriodSelection : boolean;
isMultipleSymbols : boolean;
isSingleCycle : boolean;
Constructor Create(CreateSuspended : boolean);
end;
implementation
uses kantu_regular_simulation, kantu_main, kantu_indicators ;
constructor TMyIndiThread.Create(CreateSuspended : boolean);
begin
maxRulesPerCandle := StrToInt(SimulationForm.OptionsGrid.Cells[1, IDX_OPT_MAX_RULES_PER_CANDLE]);
maxCandleShift := StrToInt(SimulationForm.OptionsGrid.Cells[1, IDX_OPT_MAX_CANDLE_SHIFT]);
resultQuota := StrToInt(SimulationForm.OptionsGrid.Cells[1, IDX_OPT_REQUESTED_SYSTEMS]);
useSL := SimulationForm.useSLCheck.Checked;
useTP := SimulationForm.useTPCheck.Checked;
startDate := SimulationForm.BeginInSampleCalendar.Date;
endDate := SimulationForm.EndInSampleCalendar.Date ;
endOutOfSample := SimulationForm.EndOutOfSampleCalendar.Date;
SetLength(simulationResults, 1);
SetLength(testedPatterns, 0);
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TMyIndiThread.syncSimulation;
var
k: integer;
begin
SetLength(indicatorEntryPatterns, Length(indicatorEntryPatterns)+1);
SetLength(indicatorClosePatterns, Length(indicatorClosePatterns)+1);
indicatorEntryPatterns[ Length(indicatorEntryPatterns)-1] := entryPattern;
indicatorClosePatterns[ Length(indicatorClosePatterns)-1] := closePattern;
for k := 0 to strategiesRequested-1 do
addIndicatorResultToGrid(simulationResults[k], Length(indicatorEntryPatterns), endOutOfSample );
validResults := validResults + 1;
MainForm.StatusLabel.Caption := 'Simulation progress: ' + FloatToStr(simulationsRan) + ' runs, valid ' + IntToStr(MainForm.ResultsGrid.RowCount-1) + '/' + IntToStr(strategiesRequested) + ' Avg time/sim : ' + FloatToStr(MainForm.simulationTime/1000);
MainForm.ProgressBar1.Position := MainForm.ResultsGrid.RowCount-1 ;
j := 0;
i := 1 + i;
end;
procedure TMyIndiThread.Execute;
var
isPositiveCount: integer;
k: integer;
begin
i := 0;
j := 0;
strategiesRequested := 1;
while (((validResults < resultQuota) and (isSingleCycle = False)) or
((simulationsRan < singleCycleSystems) and (isSingleCycle = True)))
and (MainForm.isCancel = false) do
begin
if isMultipleSymbols then
begin
strategiesRequested := Length(LoadedIndiHistoryData);
SetLength(simulationResults, strategiesRequested);
end;
if isRandomPeriodSelection then
begin
startDate := Now;
while IncDay(startDate, walkForwardPeriod+generationPeriod) > LoadedIndiHistoryData[0].time[Length(LoadedIndiHistoryData[0].time)-1] do
startDate := LoadedIndiHistoryData[0].time[RandomRange(50, Length(LoadedIndiHistoryData[0].time)-1)] ;
endDate := IncDay(startDate, generationPeriod);
endOutOfSample := IncDay(endDate, walkForwardPeriod);
end;
entryPattern := generateRandomIndicatorPattern(maxRulesPerCandle, maxCandleShift);
closePattern := generateRandomIndicatorPattern(maxRulesPerCandle, maxCandleShift);
isPositiveCount := 0;
for k := 0 to strategiesRequested -1 do
begin
simulationResults[k].trades := nil;
simulationResults[k] := runIndicatorSimulation( entryPattern,
closePattern,
k,
useSL,
useTP,
startDate,
endDate,
true);
if ( isIndicatorPositiveResult(simulationResults[k]))then isPositiveCount := isPositiveCount + 1;
end;
if ( isPositiveCount = Length(LoadedIndiHistoryData))then
Synchronize(@syncSimulation);
j := j + 1;
simulationsRan := simulationsRan + 1;
end;
end;
end.