-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro_expander.pm
executable file
·435 lines (333 loc) · 13.6 KB
/
macro_expander.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use analysis::indicators;
use analysis::candlesticks;
use analysis::fundamentals;
use analysis::demark;
use analysis::rank;
my @tokens = qw(\+ - \* / <= >= < > ; = != [0-9]+\| RANK\| AND OR NOT [()] \.[\d]+ [\d]+[\.]{0,1}[\d]* , CURRENT_RATIO MIN[VOHLC] CDL_BULL_MARUBOZU CMO
MAX_UP_DAY MAX_DOWN_DAY COM_CHAN_INDEX CDL_BEAR_MARUBOZU CDL_BULL_SPINNING_TOP CDL_BEAR_SPINNING_TOP CDL_DOJI CDL_DRAGONFLY
CDL_GRAVESTONE OBV CDL_HAMMER CDL_HANGMAN CDL_INVERTED_HAMMER CDL_SHOOTING_STAR MAX[VOHLC] AVG[VOHLC] EMA[VOHLC] [VOHLC] EPS_TTM
REVENUE_TTM NET_INCOME_TTM ROE EPS SAR EARNINGS_GROWTH STRENGTH MCAP FLOAT BOLLINGER_UPPER BOLLINGER_LOWER RSI WILLIAMS_R ATR
MACDS MACDH MACD MOMENTUM ROC BOP ADXR ADX ACCELERATION_UPPER ACCELERATION_LOWER ULTOSC ADXR ADX STOCH_FAST_[D|K] AROON_UP AROON_DOWN
AROON_OSC EFFICIENCY_RATIO TD_COMBO_BUY TD_COMBO_SELL TD_SEQUENTIAL_BUY TD_SEQUENTIAL_SELL TD_SETUP_SELL TD_SETUP_BUY
PPO FOR_TICKER[\s]+[^;]{1,5} KELTNER_LOWER KELTNER_UPPER MFI WMA[VOHLC] STD_DEV ROA REV_PERSHARE PROFIT_MARGIN
BOOK_PERSHARE TOTAL_ASSETS CURRENT_ASSETS TOTAL_DEBT CURRENT_DEBT CASH EQUITY NET_INCOME REVENUE STRENGTH TRENDSCORE
RWI_LOW RWI_HIGH DIVIDEND_YIELD PRICE_EARNINGS DISCOUNTED_CASH_FLOW TREND_INTENSITY PAYOUT_RATIO ULCER_INDEX RAVI
POSITION_DAYS_HELD POSITION_RETURN_PERCENT POSITION_RETURN_R POSITION_SHARE_COUNT POSITION_BUY_PRICE POSITION_SELL_PRICE DAYCHANGE
SHARES_OUTSTANDING RAND DATE_MAX_C DATE_MIN_C
);
my %arg_macro_table = ( "V" => "fetch_volume_at", "L" => "fetch_low_at", "MAXO" => "max_open", "MAXV" => "max_volume",
"MAXC" => "max_close", "O" => "fetch_open_at", "MINO" => "min_open", "MINH" => "min_high",
"MINL" => "min_low", "MINC" => "min_close", "MINV" => "min_volume", "C" => "fetch_close_at",
"MAXH" => "max_high", "MAXL" => "max_low", "AVGV" => "avg_volume", "AVGO" => "avg_open",
"AVGH" => "avg_high", "AVGL" => "avg_low", "AVGC" => "avg_close", "H" => "fetch_high_at",
"MACD" => "compute_macd", "STRENGTH" => "fetch_strength", "WILLIAMS_R" => "compute_williams_r",
"BOLLINGER_UPPER" => "compute_upper_bollinger", "BOLLINGER_LOWER" => "compute_lower_bollinger",
"RSI" => "compute_rsi", "EMAC" => "exp_avg_close", "EMAO" => "exp_avg_open", "EMAH" => "exp_avg_high",
"EMAL" => "exp_avg_low", "EMAV" => "exp_avg_volume", "ATR" => "compute_atr", "MACD" => "compute_macd",
"MACDS" => "compute_macd_signal", "MACDH" => "compute_macd_hist", "MOMENTUM" => "compute_momentum",
"ROC" => "compute_roc", "OBV" => "compute_obv", "ADX" => "compute_adx", "ADXR" => "compute_adx_r",
"ACCELERATION_UPPER" => "compute_upper_accband", "ACCELERATION_LOWER" => "compute_lower_accband",
"SAR" => "compute_sar", "ULTOSC" => "compute_ultosc", "STOCH_FAST_D" => "compute_fast_stoch_d",
"STOCH_FAST_K" => "compute_fast_stoch_k", "AROON_UP" => "compute_aroon_up", "AROON_DOWN" =>"compute_aroon_down",
"AROON_OSC" => "compute_aroon_osc", "EFFICIENCY_RATIO" => "compute_efficiency_ratio", "PPO" => "compute_ppo",
"KELTNER_UPPER" => "compute_upper_keltner", "KELTNER_LOWER" => "compute_lower_keltner", "COM_CHAN_INDEX" => "compute_cci",
"MFI" => "compute_mfi", "CMO" => "compute_cmo", "WMAC" => "wma_close", "WMAO" => "wma_open",
"WMAH" => "wma_high", "WMAL" => "wma_low", "WMAV" => "wma_volume", "STD_DEV" => "compute_standard_dev",
"EPS" => "fundamental_eps", "FLOAT" => "fundamental_float", "CURRENT_RATIO" => "fundamental_current_ratio",
"ROE" => "fundamental_roe", "MCAP" => "fundamental_mcap", "EARNINGS_GROWTH" => "fundamental_egrowth",
"ROA" => "fundamental_roa", "REV_PERSHARE" => "fundamental_pershare_revenue",
"PROFIT_MARGIN" => "fundamental_profit_margin", "BOOK_PERSHARE" => "fundamental_pershare_book",
"TOTAL_ASSETS" => "fundamental_total_assets", "CURRENT_ASSETS" => "fundamental_current_assets",
"TOTAL_DEBT" => "fundamental_total_debt", "CURRENT_DEBT" => "fundamental_current_debt",
"CASH" => "fundamental_cash", "EQUITY" => "fundamental_equity", "NET_INCOME" => "fundamental_net_income",
"REVENUE" => "fundamental_revenue", "STRENGTH" => "relative_strength", "RWI_LOW" => "random_walk_low",
"RWI_HIGH" => "random_walk_high", "DISCOUNTED_CASH_FLOW" => "fundamental_dcf",
"TREND_INTENSITY" => "compute_trend_intensity", "ULCER_INDEX" => "compute_ulcer_index", "RAVI" => "compute_ravi",
"SHARES_OUTSTANDING" => "fundamental_shares_outstanding", "EPS_TTM" => "fundamental_eps_ttm",
"REVENUE_TTM" => "fundamental_revenue_ttm", "NET_INCOME_TTM" => "fundamental_net_income_ttm",
"MAX_UP_DAY" => "max_up_day", "MAX_DOWN_DAY" => "max_down_day", "RAND" => "random_value",
"DATE_MAX_C" => "max_close_date", "DATE_MIN_C" => "min_close_date"
);
my %noarg_macro_table = ( "=" => "==", "OR" => "||", "AND" => "&&", "BOP" => "compute_bop()", "OBV" => "compute_obv",
"TD_SEQUENTIAL_BUY" => "td_sequential_buy()", "TD_SEQUENTIAL_SELL" => "td_sequential_sell()",
"TD_COMBO_BUY" => "td_combo_buy()", "TD_COMBO_SELL" => "td_combo_sell()", "TD_SETUP_BUY" => "td_buy_setup()",
"TD_SETUP_SELL" => "td_sell_setup()", "CDL_BULL_MARUBOZU" => "candle_bullish_marubozu()",
"CDL_BEAR_MRUBOZU" => "candle_bearish_marubozu()", "CDL_BULL_SPINNING_TOP" => "candle_bullish_top()",
"CDL_BEAR_SPINNING_TOP" => "candle_bearish_top()", "CDL_DOJI" => "candle_doji()",
"CDL_DRAGONFLY" => "candle_dragonfly()", "CDL_GRAVESTONE" => "candle_gravestone()",
"CDL_HAMMER" => "candle_hammer()", "CDL_HANGMAN" => "candle_hanging_man()",
"CDL_INVERTED_HAMMER" => "candle_inverted_hammer", "CDL_SHOOTING_STAR" => "candle_shooting_star()",
"TRENDSCORE" => "compute_trend_score()", "COPPOCK" => "compute_coppock()",
"DIVIDEND_YIELD" => "fundamental_div_yield()", "PRICE_EARNINGS" => "fundamental_price_earnings()",
"PAYOUT_RATIO" => "fundamental_payout_ratio()", "POSITION_DAYS_HELD" => "position_time()",
"POSITION_RETURN_PERCENT" => "position_return_percent()", "POSITION_RETURN_R" => "position_return_r()",
"POSITION_SHARE_COUNT" => "position_shares()", "POSITION_BUY_PRICE" => "position_buy_price()",
"POSITION_SELL_PRICE" => "position_sell_price()", "DAYCHANGE" => "closing_price_change()"
);
my %lookback_table = ( "ACCELERATION_UPPER" => "TA_ACCBANDS", "ACCELERATION_LOWER" => "TA_ACCBANDS",
"TD_COMBO_BUY" => "DEMARK", "TD_COMBO_SELL" => "DEMARK", "TD_SEQUENTIAL_BUY" => "DEMARK",
"TD_SEQUENTIAL_SELL" => "DEMARK", "TD_SETUP_SELL" => "DEMARK_SETUP", "TD_SETUP_BUY" => "DEMARK_SETUP",
"CDL_BULL_MARUBOZU" => "MARUBOZU", "CDL_BEAR_MARUBOZU" => "MARUBOZU",
"CDL_BULL_SPINNING_TOP" => "TA_CDLSPINNINGTOP", "CDL_BEAR_SPINNING_TOP" => "TA_CDLSPINNINGTOP",
"CDL_DOJI" => "TA_CDLDOJI", "CDL_DRAGONFLY" => "TA_CDLDRAGONFLYDOJI",
"CDL_GRAVESTONE" => "TA_CDLGRAVESTONEDOJI", "CDL_HAMMER" => "TA_CDLHAMMER",
"CDL_HANGMAN" => "CDL_HANGINGMAN", "CDL_INVERTED_HAMMER" => "TA_CDLINVERTEDHAMMER",
"CDL_SHOOTING_STAR" => "TA_CDLSHOOTINGSTAR", "ULTOSC" => "TA_ULTOSC", "AROON_OSC" => "TA_AROONOSC",
);
my %transform_table = ( "FOR_TICKER[\\s]+[^;]{1,5}" => \&process_for_ticker, "[0-9]+\\|" => \&process_days_ago,
"RANK\\|" => \&process_rank);
my @token_list;
my $current_statement;
my $current_action;
my $current_limit;
my $complete_meta;
my $rank_pull;
my $rank_flag;
my %statement_map;
sub tokenize {
my $raw_screen = "";
my $file = shift;
open(INFILE, $file);
while(<INFILE>) {
chomp;
$raw_screen .= "$_ " if $_ !~ /#.+/;
}
while($raw_screen) {
$prev = $raw_screen;
foreach $token (@tokens) {
if($raw_screen =~ m/^[\s]*($token)(.*)/) {
$raw_screen = $2;
push @token_list, $1;
last;
}
$raw_screen = "" if $raw_screen =~ /^[\s]+$/;
}
conf::output("unrecognized token in $prev", 1) if $prev eq $raw_screen;
}
}
sub next_token {
my $token = shift @token_list;
$current_statement .= "$token ";
return $token;
}
sub parse_screen {
$t = screen_from_file(shift);
return @$t;
}
#do an end run around parse_scan to return
#just the expression without the if statement
sub parse_expression {
tokenize(shift);
my $act = parse_statement();
$current_action = "";
$statement_map{$act} = $current_statement;
return [$act, $current_limit];
}
sub screen_from_file {
my $f = shift;
tokenize($f);
my @rlist;
while(@token_list > 0) {
parse_scan(\@rlist);
}
conf::output("Empty file $f", 1) if scalar @rlist == 0;
@rlist = sort {$a->[1] <=> $b->[1]} @rlist;
return \@rlist;
}
sub parse_scan {
my $actions = shift;
parse_statement();
my $act = "if($current_action) {return 1} else {return 0}";
$statement_map{$act} = $current_statement;
push @$actions, [$act, $current_limit];
$current_action = "";
}
sub parse_statement {
$current_statement = "";
$token = next_token();
$current_limit = 1;
while($token ne "" && $token ne ";") {
if(exists $noarg_macro_table{$token}) {
$current_action .= "$noarg_macro_table{$token}";
if(exists $lookback_table{$token}) {
$lcall = $lookback_table{$token} . "_Lookback()";
set_pull(eval($lcall));
} else {
lookback_custom($token, "", 0);
}
} elsif(exists $arg_macro_table{$token}) {
$current_action .= "$arg_macro_table{$token}(";
capture_args($token);
} elsif( ! probe_transform_table($token)) {
handle_meta() if $token =~ /<|>|=|<=|>=|!=|AND|OR|\)/;
$current_action .= " $token";
}
$token = next_token();
}
handle_meta();
return $current_action;
}
sub handle_meta {
if($complete_meta) {
if($rank_flag) {
$current_action .= "', $rank_pull)";
$rank_pull = 0;
$rank_flag = 0;
$current_limit = 0;
} else {
$current_action .= "')";
}
$complete_meta = 0;
}
}
sub capture_args {
my $max = 0;
my $count = 0;
my $arg = next_token();
my $arglist;
while($arg =~ /[0-9]+(\.){0,1}[0-9]*/ || $arg eq ",") {
$max = $arg if $arg > $max;
$arglist .= "$arg";
$count++;
$arg = next_token();
}
$arglist .= ")" if $count > 0;
$arglist .= "0)" if $count == 0;
$current_action .= $arglist;
my $ltoken = shift;
if(exists $lookback_table{$ltoken}) {
$lcall = $lookback_table{$ltoken} . "_Lookback($arglist";
set_pull(eval($lcall) + $complete_meta);
} else {
lookback_custom($ltoken, $arglist, $max);
}
#re-insert previous token and remove
#it from statement to prevent duplicates
unshift @token_list, $arg;
$current_statement = substr $current_statement, 0, -(length $arg - 1);
}
sub lookback_custom {
my $ctoken = shift;
my $alist = shift;
my $maxval = shift;
my $pullval = $maxval;
if($ctoken =~/[OHLCV]/) {
$pullval++ if $pullval > 0;
}
#EMA and PPO both need similar lookback periods
#because PPO is using EMA as it's average function
if($ctoken =~ /EMA[VOHLC]/ && $alist =~ /([0-9]+)\)/) {
$pullval = 4 * ($1 + 1);
}
if($ctoken =~ /PPO/) {
if($alist =~ /[0-9]+,([0-9]+)\)/) {
$pullval = 4 * ($1 + 1);
}
}
if($ctoken =~ /KELTNER/) {
if($alist =~ /([0-9]+),.+/) {
$pullval = 4 * ($1 + 1);
}
}
#get around bugs in ta-lib's lookback functions
if($ctoken =~ /WILLIAMS_R/) {
$pullval = $maxval;
}
if($ctoken =~ /AROON_(UP|DOWN)/) {
$pullval = $maxval + 1;
}
if($ctoken =~ /RSI/ && $alist =~ /([0-9]+)\)/) {
$pullval = 4 * $1;
}
if($ctoken =~ /STOCH_FAST_[K|D]/ && $alist =~ /([0-9]+)\)/) {
$pullval = eval "TA_STOCHF_Lookback($1, $1, $TA_MAType_SMA)";
}
if($ctoken =~ /MACD[S]*/) {
$pullval = $maxval * 4;
}
if($ctoken =~ /ADX[R]?/ && $alist =~ /([0-9]+)\)/) {
$pullval = 5 * ($1 + 1);
}
if($ctoken =~ /ATR?/ && $alist =~ /([0-9]+)\)/) {
$pullval = $1 + 1;
}
if($ctoken =~ /COM_CHAN_INDEX/ && $alist =~ /([0-9]+)/) {
$pullval = $1;
}
if($ctoken =~ /MFI/ && $alist =~ /([0-9]+)/) {
$pullval = $1 + 1;
}
if($ctoken =~ /CMO/ && $alist =~ /([0-9]+)/) {
$pullval = $1 + 1;
}
if($ctoken =~ /STD_DEV/ && $alist =~ /([0-9]+),.+/) {
$pullval = $1 + 1;
}
if($ctoken =~ /RWI_LOW/ || $ctoken =~ /RWI_HIGH/) {
$pullval = $alist + 1;
}
if($ctoken =~ /TREND_INTENSITY/ && $alist =~ /([0-9]+),([0-9]+)/) {
$pullval = ($1 > $2 ? $1 : $2);
}
if($ctoken =~ /RAVI/ && $alist =~ /[0-9]+,([0-9]+)/) {
$pullval = $1;
}
if($ctoken =~ /DISCOUNTED_CASH_FLOW/) {
$pullval = 1;
}
if($ctoken =~ /STRENGTH/ || $ctoken =~ /PAYOUT_RATIO/) {
$pullval = 0;
}
if($ctoken =~ /TRENDSCORE/) {
$pullval = 21;
}
if($ctoken =~ /POSITION*/) {
$pullval = 0;
}
if($ctoken =~ /DAYCHANGE/) {
$pullval = 2;
}
if($ctoken =~ /RAND/) {
$pullval = 0;
}
set_pull($pullval + $complete_meta);
}
sub probe_transform_table {
my $token = shift;
foreach(keys %transform_table) {
if($token =~ /$_/) {
my $fcall = $transform_table{$_};
$fcall->($token);
return 1;
}
}
}
sub process_for_ticker {
my $insymbol = shift;
if($insymbol =~ /FOR_TICKER[\s]+(.*)/) {
$current_action .= "force_data_load('$1')";
$current_limit = 0;
}
}
sub process_days_ago {
my $daycount = shift;
if($daycount =~ /([0-9]+)\|/) {
$current_action .= "days_ago($1, '";
$complete_meta = $1;
}
}
sub process_rank {
$current_action .= "rank_by('";
$complete_meta = 1;
$rank_flag = 1;
}
sub set_pull {
my $t = shift;
$rank_pull = $t;
$current_limit = $t if $t > $current_limit;
}
sub statement_from_action {
my $statement = shift;
return $statement_map{$statement};
}
1;