-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfeatures.coffee
853 lines (604 loc) · 20.4 KB
/
features.coffee
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
global.MIN_HISTORY_INFLUENCE = .01
frames_for_weight = (weight) ->
Math.log(MIN_HISTORY_INFLUENCE) / Math.log(1 - weight) + 1
global.check_history_continuation = (engine, depth, weight) ->
frames_required = frames_for_weight(weight)
if engine.num_frames < frames_required
log_error true,
message: """HISTORY NOT ENOUGH FOR ALPHA"""
alpha: weight
frames_required: frames_required
frames: engine.num_frames
depth: depth
should_continue = MIN_HISTORY_INFLUENCE < Math.pow( (1 - weight), depth)
should_continue
module.exports = f = {}
INC = 250
f.volume = (engine, args) ->
t = args.t
t2 = args.t2 or t
weight = args.weight or 1
k = "#{engine.now - t * engine.resolution}-#{weight}-#{engine.now - t2 * engine.resolution}"
if k of engine.volume_cache
return engine.volume_cache[k]
else
v = 0
for i in [t..t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
boundary = fb[1]
while idx % INC > 0 && idx < boundary
trade = engine.trades[idx]
v += trade.amount
idx += 1
while idx < boundary - INC
if !feature_cache.volume[idx]
nv = 0
end_idx = idx + INC
while idx < end_idx
trade = engine.trades[idx]
nv += trade.amount
idx += 1
idx -= INC
feature_cache.volume[idx] = nv
v += feature_cache.volume[idx]
idx += INC
while idx < boundary
trade = engine.trades[idx]
v += trade.amount
idx += 1
# cut off recursing after the impact of calculating the previous time
# frame's velocity is negligible. Note that this method assumes the
# initial t = 0!
should_continue = weight < 1 && check_history_continuation(engine, t, weight)
if should_continue
v2 = engine.volume
t: t + 1
t2: t2 + 1
weight: weight
v = Math.weighted_average v, v2, weight
engine.volume_cache[k] = v
f.volume.frames = (args) ->
t = args.t or 0
t + frames_for_weight((args.weight) or 1) + (args.t2 or 0) + 1
f.price = (engine, args) ->
weight = args.weight or 1
t = args.t or 0
t2 = args.t2 or t
k = "#{engine.now - t * engine.resolution}-#{engine.now - t2 * engine.resolution}"
if !(k of engine.price_cache)
amount = 0
total = 0
for i in [t..t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
boundary = fb[1]
while idx % INC > 0 && idx < boundary
trade = engine.trades[idx]
amount += trade.amount
total += trade.amount * trade.rate
idx += 1
while idx < boundary - INC
if !feature_cache.price[idx]
namount = ntotal = 0
end_idx = idx + INC
while idx < end_idx
trade = engine.trades[idx]
namount += trade.amount
ntotal += trade.amount * trade.rate
idx += 1
idx -= INC
feature_cache.price[idx] = [namount, ntotal]
val = feature_cache.price[idx]
amount += val[0]
total += val[1]
idx += INC
while idx < boundary
trade = engine.trades[idx]
amount += trade.amount
total += trade.amount * trade.rate
idx += 1
engine.price_cache[k] = [amount, total]
else
[amount, total] = engine.price_cache[k]
if total > 0
p = total / amount
should_continue = weight < 1 && check_history_continuation(engine, t, weight)
if should_continue
p2 = engine.price
t: t + 1
t2: t2 + 1
weight: weight
p = Math.weighted_average p, p2, weight
else
p = engine.price
t: t + 1
t2: t2 + 1
weight: weight
p
f.price.frames = (args) ->
t = args.t or 0
t + frames_for_weight((args.weight) or 1) + (args.t2 or 0) + 1
f.min_price = (engine, args) ->
t = args.t
t2 = args.t2 or t
min = Infinity
for i in [t..t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
boundary = fb[1]
while idx % INC > 0 && idx < boundary
trade = engine.trades[idx]
if trade.rate < min
min = trade.rate
idx += 1
while idx < boundary - INC
if !feature_cache.min_price[idx]
nmin = Infinity
end_idx = idx + INC
while idx < end_idx
trade = engine.trades[idx]
if nmin < trade.rate
nmin = trade.rate
idx += 1
idx -= INC
feature_cache.min_price[idx] = nmin
val = feature_cache.min_price[idx]
if val < min
min = val
idx += INC
while idx < boundary
trade = engine.trades[idx]
if trade.rate < min
min = trade.rate
idx += 1
if min == Infinity
min = engine.last_price args
min
f.max_price = (engine, args) ->
t = args.t
t2 = args.t2 or t
max = 0
for i in [t..t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
boundary = fb[1]
while idx % INC > 0 && idx < boundary
trade = engine.trades[idx]
if trade.rate > max
max = trade.rate
idx += 1
while idx < boundary - INC
if !feature_cache.max_price[idx]
nmax = 0
end_idx = idx + INC
while idx < end_idx
trade = engine.trades[idx]
if nmax < trade.rate
nmax = trade.rate
idx += 1
idx -= INC
feature_cache.max_price[idx] = nmax
val = feature_cache.max_price[idx]
if val > max
max = val
idx += INC
while idx < boundary
trade = engine.trades[idx]
if trade.rate > max
max = trade.rate
idx += 1
if max == 0
max = engine.last_price args
max
f.min_price.frames = f.max_price.frames = (args) -> f.last_price.frames(args) + (args.t2 or args.t or 0)
f.last_price = (engine, args) ->
t = args.t or 0
weight = args.weight or 1
if engine.trades_in_frame(t) > 0
p = engine.latest_trade(t).rate
else
p = engine.last_price {t: args.t + 1, weight}
should_continue = weight < 1 && check_history_continuation(engine, t, weight)
if should_continue
p2 = engine.last_price {t: args.t + 1, weight}
p = Math.weighted_average p, p2, weight
p
f.first_price = (engine, args) ->
t = args.t or 0
if engine.trades_in_frame(t) > 0
engine.earliest_trade(t).rate
else
engine.last_price t: args.t + 1
f.last_price.frames = f.first_price.frames = (args) ->
frames_for_weight((args.weight or 1)) + (args.t or 0) + 3
f.price_stddev = (engine, args) ->
rates = []
for i in [args.t..args.t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
while idx < fb[1]
rates.push engine.trades[idx].rate
idx += 1
if rates.length > 0
Math.standard_dev rates
else
null
f.price_stddev.frames = (args) ->
(args.t or 0) + (args.t2 or 0) + 2
f.volume_adjusted_price_stddev = (engine, args) ->
# https://tabbforum.com/opinions/quantifying-intraday-volatility?print_preview=true&single=true
# http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weightsd.pdf
weighted_mean = engine.price {weight: 1, t: args.t, t2: args.t2}
weighted_dev = 0
observations = 0
total_volume = 0
for i in [args.t..args.t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
while idx < fb[1]
trade = engine.trades[idx]
idx += 1
observations += 1
weighted_dev += trade.amount * (trade.rate - weighted_mean) * (trade.rate - weighted_mean)
total_volume += trade.amount
if observations > 1
weighted_variance = weighted_dev / ( (observations - 1) * total_volume / observations )
weighted_dev = Math.sqrt weighted_variance
weighted_dev / weighted_mean
else
null
f.volume_adjusted_price_stddev.frames = (args) ->
f.price.frames args
f.upwards_vs_downwards_stddev = (engine, args) ->
up = engine.upwards_volume_adjusted_price_stddev args
down = engine.downwards_volume_adjusted_price_stddev args
p = up - down
t = args.t or 0
t2 = args.t2 or t
weight = args.weight or 1
should_continue = weight < 1 && check_history_continuation(engine, t, weight)
if should_continue
p2 = engine.upwards_vs_downwards_stddev
t: t + 1
t2: t2 + 1
weight: weight
p = Math.weighted_average p, p2, weight
p
f.upwards_vs_downwards_stddev.frames = (args) ->
t = args.t or 0
t2 = args.t2 or t
weight = args.weight or 1
f.upwards_volume_adjusted_price_stddev.frames
t: frames_for_weight(weight) + t2 + 1
f.upwards_volume_adjusted_price_stddev = (engine, args) ->
opening_price = engine.first_price args
amount = 0
total = 0
for i in [args.t..args.t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
while idx < fb[1]
trade = engine.trades[idx]
idx += 1
continue if trade.rate < opening_price
amount += trade.amount
total += trade.total
return 0 if amount == 0
weighted_mean = total / amount
weighted_dev = 0
observations = 0
total_volume = 0
for i in [args.t..args.t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
while idx < fb[1]
trade = engine.trades[idx]
idx += 1
continue if trade.rate < opening_price
observations += 1
weighted_dev += trade.amount * (trade.rate - weighted_mean) * (trade.rate - weighted_mean)
total_volume += trade.amount
if observations > 1
weighted_variance = weighted_dev / ( (observations - 1) * total_volume / observations )
weighted_dev = Math.sqrt weighted_variance
weighted_dev / weighted_mean
else
0
f.downwards_volume_adjusted_price_stddev = (engine, args) ->
opening_price = engine.first_price args
amount = 0
total = 0
for i in [args.t..args.t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
while idx < fb[1]
trade = engine.trades[idx]
idx += 1
continue if trade.rate > opening_price
amount += trade.amount
total += trade.total
return 0 if amount == 0
weighted_mean = total / amount
weighted_dev = 0
observations = 0
total_volume = 0
for i in [args.t..args.t2] when i < engine.num_frames
fb = engine.frame_boundary(i)
idx = fb[0]
while idx < fb[1]
trade = engine.trades[idx]
idx += 1
continue if trade.rate > opening_price
observations += 1
weighted_dev += trade.amount * (trade.rate - weighted_mean) * (trade.rate - weighted_mean)
total_volume += trade.amount
if observations > 1
weighted_variance = weighted_dev / ( (observations - 1) * total_volume / observations )
weighted_dev = Math.sqrt weighted_variance
weighted_dev / weighted_mean
else
0
f.upwards_volume_adjusted_price_stddev.frames = f.downwards_volume_adjusted_price_stddev.frames = (args) ->
2 + Math.max(engine.first_price.frames(args), (args.t2 or args.t or 0))
f.stddev_by_volume = (engine, args) ->
volume = engine.volume args
stddev = engine.volume_adjusted_price_stddev args
stddev / (volume + 1)
f.stddev_by_volume.frames = (args) ->
Math.max engine.volume.frames(args), engine.volume_adjusted_price_stddev(args)
# velocity is derivative of price
f.velocity = (engine, args) ->
weight = args.weight or 1
t = args.t or 0
k = "#{engine.now - t * engine.resolution}-#{weight}"
if k of engine.velocity_cache
return engine.velocity_cache[k]
else
p0 = engine.price t: t
p1 = engine.price t: t + 1
dy = if !p1? || p1 == null then 0 else p0 - p1
# cut off recursing after the impact of calculating the previous time
# frame's velocity is negligible. Note that this method assumes the
# initial t = 0!
should_continue = weight != 1 && check_history_continuation(engine, t, weight)
if should_continue
v2 = engine.velocity
t: t + 1
weight: weight
dy = Math.weighted_average dy, v2, weight
engine.velocity_cache[k] = dy
dy
f.velocity.frames = (args) ->
t = args.t or 0
t2 = args.t2 or t
weight = args.weight or 1
f.price.frames
t: frames_for_weight(weight) + t2 + 1
f.acceleration = (engine, args) ->
weight = args.weight or 1
vel_weight = args.vel_weight or 1
t = args.t or 0
k = "#{engine.now - t * engine.resolution}-#{weight}"
if k of engine.acceleration_cache
return engine.acceleration_cache[k]
else
v0 = engine.velocity t: t, weight: vel_weight
v1 = engine.velocity t: t + 1, weight: vel_weight
dy = if !v1? || v1 == null then 0 else v0 - v1
# cut off recursing after the impact of calculating the previous time
# frame's velocity is negligible. Note that this method assumes the
# initial t = 0!
should_continue = weight != 1 && check_history_continuation(engine, t, weight)
if should_continue
a2 = engine.acceleration
t: t + 1
weight: weight
vel_weight: vel_weight
dy = Math.weighted_average dy, a2, weight
engine.acceleration_cache[k] = dy
dy
f.acceleration.frames = (args) ->
t = args.t or 0
t2 = args.t2 or t
weight = args.weight or 1
vel_weight = args.vel_weight or 1
f.velocity.frames
t: t + frames_for_weight(args.weight or 1) + t2 + 1
weight: vel_weight
# relative strength index
f.RSI = (engine, args) ->
gain = 0
loss = 0
periods = Math.ceil(1 / args.weight)
for p in [args.t..args.t + periods - 1]
cur = engine.price({t: p})
prev = engine.price({t: p + 1})
if cur > prev
gain += cur - prev
else
loss += prev - cur
RS = gain / loss
RSI = 100 - 100 / (1 + RS)
# TODO: why the difference between alpha and weight??!?
alpha = 1
should_continue = alpha != 1 && check_history_continuation(engine, args.t, alpha)
if should_continue
rsi2 = engine.RSI
t: args.t + 1
weight: args.weight
RSI = Math.weighted_average RSI, rsi2, alpha
RSI
f.RSI.frames = (args) ->
t = args.t + Math.ceil(1 / args.weight)
alpha = 1
f.price.frames({t: t + frames_for_weight(alpha), weight: args.weight}) + 1
MACD_EMA = (engine, t, alpha, t_unit, depth, args) ->
next_time = t - t_unit
val = engine.MACD_signal.past_result(args, next_time)
return val if MIN_HISTORY_INFLUENCE > Math.pow( (1 - alpha), depth)
alpha * val + (1 - alpha) * MACD_EMA(engine, next_time, alpha, t_unit, depth + 1, args)
f.MACD_signal = (engine, args, all_engines) ->
feature = args.MACD_feature or 'price'
long_resolution = engine
short_resolution = all_engines[args.short_resolution]
weight = args.weight
day12 = short_resolution[feature] {t: args.t, weight}
day26 = long_resolution[feature] {t: args.t, weight}
if feature == 'volume'
day26 *= short_resolution.resolution / long_resolution.resolution
MACD = day12 - day26
MACD_weight = args.MACD_weight or 1
#should_continue = MACD_weight != 1 && check_history_continuation(engine, args.t, MACD_weight)
if MACD_weight < 1 #should_continue
EMAD = MACD_EMA(engine, engine.now, MACD_weight, 6 * (args.eval_entry_every_n_seconds or config.eval_entry_every_n_seconds), 1, args)
return MACD if !EMAD
MACD = MACD_weight * MACD + (1 - MACD_weight) * EMAD
MACD
f.MACD = (engine, args, all_engines) ->
# MACD_line: (12-day EMA - 26-day EMA)
# signal_line: 9-day EMA of MACD Line
# calculated from MACD function: MACD_histogram = MACD Line - Signal Line
feature = args.MACD_feature or 'price'
long_resolution = engine
short_resolution = all_engines[args.short_resolution]
weight = args.weight
day12 = short_resolution[feature] {t: args.t, weight}
day26 = long_resolution[feature] {t: args.t, weight}
if feature == 'volume'
day26 *= short_resolution.resolution / long_resolution.resolution
MACD = day12 - day26
signal = engine.MACD_signal args
MACD - signal
f.MACD.frames = f.MACD_signal.frames = (args) ->
weight = args.weight
MACD_weight = args.MACD_weight or 1 #Math.min .9, args.weight * 12/9
t = args.t or 0
feature = args.MACD_feature or 'price'
frames = f[feature].frames
#t: t + frames_for_weight(MACD_weight) + 1
t: t + 1
weight: weight
frames
f.DI_plus = (engine, args) ->
alpha = args.weight or 1
# t2t = Date.now() if config.log_level > 1
ATR = engine.ATR({t: args.t, weight: alpha})
v = 100 * engine.DM_plus({t: args.t})
# by_feature.DI_plus ?= 0
# by_feature.DI_plus -= Date.now() - t2t if t_?
if v / ATR == Infinity
v = 0
else
v /= ATR
should_continue = alpha != 1 && check_history_continuation(engine, args.t, alpha)
if !should_continue
v
else
alpha * v + (1 - alpha) * engine.DI_plus({t: args.t + 1, weight: alpha})
f.DI_minus = (engine, args) ->
alpha = args.weight or 1
# t2t = Date.now() if config.log_level > 1
ATR = engine.ATR({t: args.t, weight: alpha})
v = 100 * engine.DM_minus({t: args.t})
# by_feature.DI_minus ?= 0
# by_feature.DI_minus -= Date.now() - t2t if t_?
if v / ATR == Infinity
v = 0
else
v /= ATR
should_continue = alpha != 1 && check_history_continuation(engine, args.t, alpha)
if !should_continue
v
else
alpha * v + (1 - alpha) * engine.DI_minus({t: args.t + 1, weight: alpha})
f.DI_plus.frames = f.DI_minus.frames = (args) ->
alpha = args.weight or 1
t = (args.t or 0)
Math.max(f.DM_plus.frames({t: t + frames_for_weight(alpha)}), \
f.ATR.frames({t: t + frames_for_weight(alpha), weight: alpha})) + 1
f.DM_plus = (engine, args) ->
p = args.t
alpha = args.weight or 1
# t2t = Date.now() if config.log_level > 1
cur_high = engine.max_price({t: p})
prev_high = engine.max_price({t: p + 1})
cur_low = engine.min_price({t: p})
prev_low = engine.min_price({t: p + 1})
# by_feature.DM_plus ?= 0
# by_feature.DM_plus -= Date.now() - t2t if t_?
dir_high = cur_high - prev_high
dir_high = 0 if dir_high < 0
dir_low = prev_low - cur_low
dir_low = 0 if dir_low < 0
v = if dir_high > dir_low && dir_high > 0 then dir_high else 0
should_continue = alpha != 1 && check_history_continuation(engine, args.t, alpha)
if !should_continue
v
else
alpha * v + (1 - alpha) * engine.DM_plus({t: args.t + 1, weight: alpha})
f.DM_minus = (engine, args) ->
p = args.t
alpha = args.weight or 1
# t2t = Date.now() if config.log_level > 1
cur_high = engine.max_price({t: p})
prev_high = engine.max_price({t: p + 1})
cur_low = engine.min_price({t: p})
prev_low = engine.min_price({t: p + 1})
# by_feature.DM_minus ?= 0
# by_feature.DM_minus -= Date.now() - t2t if t_?
dir_high = cur_high - prev_high
dir_high = 0 if dir_high < 0
dir_low = prev_low - cur_low
dir_low = 0 if dir_low < 0
v = if dir_low > dir_high && dir_low > 0 then dir_low else 0
should_continue = alpha != 1 && check_history_continuation(engine, args.t, alpha)
if !should_continue
v
else
alpha * v + (1 - alpha) * engine.DM_minus({t: args.t + 1, weight: alpha})
f.DM_plus.frames = f.DM_minus.frames = (args) ->
f.max_price.frames
t: (args.t or 0) + 1 + frames_for_weight(args.weight or 1)
# average true range
f.ATR = (engine, args) ->
p = args.t
# t2t = Date.now() if config.log_level > 1
cur_high = engine.max_price({t: p})
cur_low = engine.min_price({t: p})
# by_feature.ATR ?= 0
# by_feature.ATR -= Date.now() - t2t if t_?
tr = Math.abs cur_high - cur_low
alpha = args.weight
should_continue = alpha != 1 && check_history_continuation(engine, args.t, alpha)
if should_continue
m2 = engine.ATR
t: args.t + 1
weight: args.weight
tr = Math.weighted_average tr, m2, alpha
tr
f.ATR.frames = (args) ->
f.max_price.frames
t: (args.t or 0) + frames_for_weight(args.weight or 1)
# average directional index
f.ADX = (engine, args) ->
dx = 0
periods = Math.ceil(1 / args.weight)
for p in [args.t..args.t + periods - 1]
plus = engine.DI_plus({weight: args.weight, t: p})
minus = engine.DI_minus({weight: args.weight, t: p})
if plus + minus > 0
dx += Math.abs(plus - minus) / (plus + minus)
ADX = 100 * dx / periods
alpha = 1
should_continue = alpha != 1 && check_history_continuation(engine, args.t, alpha)
if should_continue
adx2 = engine.ADX
t: args.t + 1
weight: args.weight
ADX = Math.weighted_average ADX, adx2, alpha
ADX
f.ADX.frames = (args) ->
alpha = args.weight
t = args.t or 0
f.DI_plus.frames({t: t + frames_for_weight(alpha)}) + 1