-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.pm
277 lines (213 loc) · 7.37 KB
/
conf.pm
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
package conf;
use Cache::Memcached::Fast;
require Exporter;
use Getopt::Long;
use vars qw/@ISA @EXPORT $VERSION/;
@ISA = qw/Exporter/;
@EXPORT = qw/process_commandline/;
$VERSION = 1.0;
my $start_time;
my $progress;
#my $ROW_SIZE = 470; #for sql rows
my $ROW_SIZE = 420; #for tokyo cabinet rows
$~ = 'HELPTEXT';
sub process_commandline {
if(@_ == 0) {
write;
exit();
}
$start_time = time();
GetOptions('date=s' => \$date, 'screen=s' => \$screenfile, 'list=s' => \$tickers, 'entry=s' => \$entryfile,
'exit=s' => \$exitfile, 'start=s' => \$startdate, 'finish=s' => \$enddate, 'replay=s' => \$replay_list,
'short-entry=s' => \$short_entry, 'short-exit=s' => \$short_exit, 'showreward' => \$showreward,
'init-margin=s' => \$initial_margin, 'maint-margin' => \$maint_margin, 'portfolio=s' => \$portfolio,
'benchmark=s' => \$benchmark, 'start-with=s' => \$startwith, 'risk=s' => \$risk, 'curve' => \$curve,
'connect-string=s' => \$connect_string, 'connect-user=s' => \$connect_user,
'skip-progress' => \$skip_progress, 'nocache' => \$disable_cache, 'skip-trades' => \$skip_trades,
'tickers=s' => \$tickerlist, 'cgi-handle=s' => \$cgi_handle, 'timer' => \$use_timer,
'filter=s' => \$long_filter, 'short-filter=s' => \$short_filter, 'stop=s' => \$stopfile, 'trail=s' => \$trailfile,
'short-stop=s' => \$short_stopfile, 'short-trail=s' => \$short_trailfile, 'periods=s' => \$period_count,
'slip=s' => \$slip_file, 'stop-equity' => \$stop_equity, 'cachemax=s' => \$cache_max, 'randomize' => \$randomize,
'blacklist=s' => \$blacklist);
die "Couldn't open $tickers" if (! $tickerlist && ! -e $tickers);
die "Couldn't open $screenfile" if $screenfile && ! -e $screenfile;
die "Couldn't open $short_entry" if $short_entry && ! -e $short_entry;
die "Couldn't open $replay_list" if $replay_list && ! -e $replay_list;
die "Coudln't open $short_exit" if $short_exit && ! -e $short_exit;
die "Couldn't open $entryfile" if $entryfile && ! -e $entryfile;
die "Couldn't open $exitfile" if $exitfile && ! -e $exitfile;
die "Couldn't open $long_filter" if $long_filter && ! -e $long_filter;
die "Couldn't open $short_filter" if $short_filter && ! -e $short_filter;
die "Couldn't open $stopfile" if $stopfile && ! -e $stopfile;
die "Couldn't open $trailfile" if $trailfile && ! -e $trailfile;
die "Couldn't open $short_stopfile" if $short_stopfile && ! -e $short_stopfile;
die "Couldn't open $short_trailfile" if $short_trailfile && ! -e $short_trailfile;
die "Couldn't open $slip_file" if $slip_file && ! -e $slip_file;
process_period_count();
init_cgi() if $cgi_handle;
compute_cache_size() if not $disable_cache;
}
sub date { return $date; }
sub screen { return $screenfile; }
sub list { return $tickers; }
sub enter_sig { return $entryfile; }
sub exit_sig { return $exitfile; }
sub start { return $startdate; }
sub finish { return $enddate; }
sub replay_list { return $replay_list; }
sub short_enter_sig { return $short_entry; }
sub short_exit_sig { return $short_exit; }
sub long_positions { return $entryfile || $exitfile; }
sub short_positions { return $short_entry || $short_exit; }
sub long_filter { return $long_filter; }
sub short_filter { return $short_filter; }
sub show_reward_ratio { return $showreward; }
sub noprogress { return $skip_progress; }
sub draw_curve { return $curve; }
sub usecache { return not $disable_cache; }
sub show_trades { return not $skip_trades; }
sub ticker_list { return $tickerlist; }
sub short_trail { return $short_trailfile; }
sub long_trail { return $trailfile; }
sub timer { return $use_timer; }
sub stop_equity { return $stop_equity; }
sub randomize_list { return $randomize; }
sub blacklist { return $blacklist; }
sub cache_size {
return $row_count;
}
sub slippage {
return $slip_file if $slip_file;
}
sub short_stop {
return $short_stopfile if $short_stopfile;
return "strategies/default_short_stop";
}
sub long_stop {
return $stopfile if $stopfile;
return "strategies/default_long_stop";
}
sub connect_string {
return $connect_string if $connect_string;
return "DBI:mysql:finance";
}
sub connect_user {
return $connect_user if $connect_user;
return "perldb";
}
sub initial_margin {
return (1 + $initial_margin) if $initial_margin;
return 1.5;
}
sub maint_margin {
return (1 + $maint_margin) if $maint_margin;
return 1.25;
}
sub portfolio {
return $portfolio if $portfolio;
return "portfolio";
}
sub benchmark {
return $benchmark if $benchmark;
return "QQQQ";
}
sub startwith {
return $startwith if $startwith;
return 5000;
}
sub risk_percent {
return ($risk / 100) if $risk;
return 0.01;
}
sub check_backtest_args {
die "You are using the same entry and exit" if $exitfile eq $entryfile && $exitfile;
die "missing -list (ticker list file)" if not $tickers and not $tickerlist;
die "missing -start (start date)" if not $startdate;
die "missing -entry (entry signal)" if not $entryfile and long_positions();
die "missing -exit (exit signal)" if not $exitfile and long_positions();
die "missing -short-exit (exit signal)" if not $short_exit and $short_entry and short_positions();
die "missing -short-entry (entry signal)" if not $short_entry and $short_exit and short_positions();
die "Please specify -periods and either -finish or -start, not both" if $period_count and $enddate and $startdate;
}
sub override_date_range {
$startdate = shift;
$enddate = shift;
}
sub elapsed_time {
return time() - $start_time;
}
sub process_period_count {
if($period_count) {
if($startdate) {
process_from_start();
} else {
process_from_end();
}
}
}
sub process_from_start {
my $d = $startdate;
$d =~ s/-//g;
my $end = new Date::Business(DATE => $d);
$end->addb($period_count);
my $rval = $end->image();
substr $rval, 4, 0, "-";
substr $rval, 7, 0, "-";
$enddate = $rval;
}
sub process_from_end {
my $d = $enddate;
$d =~ s/-//g;
my $start = new Date::Business(DATE => $d);
$start->subb($period_count);
my $rval = $start->image();
substr $rval, 4, 0, "-";
substr $rval, 7, 0, "-";
$startdate = $rval;
}
sub init_cgi {
$cache_handle = new Cache::Memcached::Fast({servers => [{address => '/tmp/mcd.sock', noreply => 1}]});
}
sub output {
my $data = shift;
my $dieflag = shift;
if($cgi_handle) {
if(length $data > 10 && ! $dieflag) {
$data = "###$data###";
} else {
$data = "$data===";
}
$cache_handle->set($cgi_handle, $data);
} else {
my $slen = rindex($data, "^^^^^");
$data = substr $data, 0, $slen;
$data =~ tr/^/ /;
print "$data";
}
die if $dieflag;
}
sub compute_cache_size {
my $msize = $cache_max ? $cache_max : 40;
$msize *= 1000000;
$row_count = int($msize / $ROW_SIZE);
}
format HELPTEXT =
OPTIONS FOR BACKTESTER:
-list <ticker list file>
-start <starting date in YYYY-MM-DD>
-finish <end date in YYYY-MM-DD>
-entry <rule file>
-exit <rule file>
-short-exit <rule file>
-short-entry <rule file>
OPTIONS FOR SCREENER:
-list <ticker list file>
-screen <rule file>
-date <screen date in YYYY-MM-DD>
AVAILABLE INDICATORS:
Relative Strength Index RSI<period>
Bollinger Bands BOLLINGER_UPPER | BOLLINGER_LOWER<period>,<deviations>
Average True Range ATR<period>
Williams %R WILLIAMS_R<period>
.
1;