-
Notifications
You must be signed in to change notification settings - Fork 2
/
crunch.coffee
694 lines (496 loc) · 19.9 KB
/
crunch.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
if !module?
module = {}
deposits =
all:
c1: 0
c2: 0
cached_positions = {}
indicator_cache = {}
add_to_cache = (dealer_or_dealers, name) ->
balances = from_cache '/balances'
if !Array.isArray(dealer_or_dealers)
name = dealer_or_dealers
cached_positions[name] = fetch(name).positions
deposits[name] =
c1: balances[deslash(name)]?.deposits.c1 or 0
c2: balances[deslash(name)]?.deposits.c2 or 0
else
# console.time(name)
dealers = dealer_or_dealers
deposits[name] =
c1: 0
c2: 0
positions = []
for dealer in dealers
if !(dealer of cached_positions)
add_to_cache(dealer)
deposits[name].c1 += deposits[dealer].c1
deposits[name].c2 += deposits[dealer].c2
positions.push cached_positions[dealer]
cached_positions[name] = [].concat positions... #cached_positions[name].concat cached_positions[dealer]
# console.timeEnd(name)
compute_KPI = (dealer_or_dealers, name) ->
if name && bus?.cache.stats?[name] && config?.simulation
return bus.cache.stats[name]
price_data = from_cache '/price_data'
balances = from_cache '/balances'
if !config?
config = from_cache '/config'
if Array.isArray(dealer_or_dealers)
dealers = dealer_or_dealers
console.assert name, {message: 'KPI group of dealers must be named'}
if !cached_positions[name] || !deposits[name]
add_to_cache dealer_or_dealers, name
else
name ?= dealer_or_dealers
dealers = [dealer_or_dealers]
if !cached_positions[name] || !deposits[name]
add_to_cache name
positions = cached_positions[name]
if name && bus?.cache.stats?[name] && config?.simulation
return bus.cache.stats[name]
# console.time(name)
ts = KPI.ts
period_length = KPI.period_length
dates = KPI.dates
more_than_day = 0
more_than_hour = 0
open = 0
completed = 0
gains = 0
for p in positions
more_than_day += 1 if (!p.closed && ts - p.created > 24 * 60 * 60) || ( p.closed && p.closed - p.created > 24 * 60 * 60)
more_than_hour += 1 if (!p.closed && ts - p.created > 60 * 60) || ( p.closed && p.closed - p.created > 60 * 60)
if !p.closed
open += 1
else
completed += 1
gains += 1 if p.closed && p.profit > 0
stats =
dealers: dealer_or_dealers
more_than_day: more_than_day / (positions.length or 1)
more_than_hour: more_than_hour / (positions.length or 1)
status:
open: open
completed: completed
gains: gains
fills = []
for pos in positions
for fill in (pos.entry?.fills or [])
fills.push fill
for fill in (pos.exit?.fills or [])
fills.push fill
# fills = (pos.entry.fills for pos in positions when pos.entry?.fills?.length > 0)
# fills = fills.concat (pos.exit.fills for pos in positions when pos.exit?.fills?.length > 0)
# fills = [].concat fills... # flatten
fills.sort (a,b) -> b.date - a.date
positions_by_closed = (p for p in positions when p.closed)
positions_by_closed.sort (a,b) -> b.closed - a.closed
positions_by_created = positions_by_closed.slice() #positions.slice()
positions_by_created.sort (a,b) -> b.created - a.created
active_positions = []
baseline = deposits[name]
cur_balance =
c2: deposits[name].c2
c1: deposits[name].c1
prev_balance =
c2: deposits[name].c2
c1: deposits[name].c1
profits_from_trades = 0
series =
profit_index: []
unit_profits: []
fees: []
volume: []
fee_rate: []
c1_fees: []
c2_fees: []
trade_profit: []
trade_profit_difference: []
ratio_compared_to_deposit: []
returns: []
open: []
start: dates[0] / 1000
end: dates[dates.length - 1] / 1000
start_idx = price_data.c1xc2.length - dates.length
for period,idx in dates
last_period = start_idx + idx
$c1 = $c2 = BTC_2_ETH = null
while !$c1 || !$c2 || !BTC_2_ETH
if config.c1 == config.accounting_currency
$c1 = 1
if !$c2
$c2 = price_data.c1xc2[last_period]?.close
else
if !$c1
$c1 = price_data.c1[last_period]?.close
if !$c2
$c2 = price_data.c2[last_period]?.close
if !BTC_2_ETH
BTC_2_ETH = price_data.c1xc2[last_period].close
last_period -= 1
# trade-level metrics
c2_fees = c1_fees = volume = 0
while true
break if fills.length == 0 || fills[fills.length - 1].date > period / 1000 + period_length
fill = fills.pop()
volume += fill.amount
# console.log 'TRADE AMOUNT', trade.amount, trade.type
fee = fill.fee
if fill.type == 'buy'
cur_balance.c1 -= fill.total
cur_balance.c2 += fill.amount
if config.exchange == 'poloniex'
c2_fees += fee
cur_balance.c2 -= fee
else
c1_fees += fee
cur_balance.c1 -= fee
else
cur_balance.c1 += fill.total
cur_balance.c2 -= fill.amount
c1_fees += fee
cur_balance.c1 -= fee
if !stats.$c1_start?
stats.$c1_start = $c1
stats.$c2_start = $c2
stats.$c1_end = $c1
stats.$c2_end = $c2
if (cur_balance.c2 < 0 || cur_balance.c1 < 0) && config.simulation
console.error 'negative balance!', cur_balance, name
console.log config.c1, config.c2 #, fill,
process.exit()
# position-level metrics
while true
break if positions_by_closed.length == 0 || positions_by_closed[positions_by_closed.length - 1].closed > period / 1000 + period_length
pos = positions_by_closed.pop()
profits_from_trades += pos.profit
added_active = false
while true
break if positions_by_created.length == 0 || positions_by_created[positions_by_created.length - 1].created > period / 1000 + period_length
active_positions.push positions_by_created.pop()
added_active = true
if added_active && active_positions.length > 1
active_positions.sort (a,b) -> b.closed - a.closed
while true
break if active_positions.length == 0 || active_positions[active_positions.length - 1].closed > period / 1000 + period_length
active_positions.pop()
series.trade_profit.push [period / 1000, profits_from_trades]
trade_profit_difference = if idx == 0 then 0 else profits_from_trades - series.trade_profit[idx - 1][1]
series.trade_profit_difference.push [period / 1000, trade_profit_difference]
baseline_adjustment = $c2 * baseline.c2 + $c1 * baseline.c1
profit = $c2 * cur_balance.c2 + $c1 * cur_balance.c1 - baseline_adjustment
series.profit_index.push [period / 1000, profit]
baseline_adjustment = baseline.c2 + baseline.c1 / BTC_2_ETH
unit_profits = cur_balance.c2 + cur_balance.c1 / BTC_2_ETH - baseline_adjustment
series.unit_profits.push [period / 1000, unit_profits]
prev_volume = if idx == 0 then 0 else series.volume[idx - 1][1]
series.volume.push [period / 1000, volume + prev_volume]
previous_c1 = if idx == 0 then 0 else series.c1_fees[idx - 1][1]
previous_c2 = if idx == 0 then 0 else series.c2_fees[idx - 1][1]
fees = (c2_fees + previous_c2) + (c1_fees + previous_c1) / BTC_2_ETH
series.fees.push [period / 1000, fees]
series.c1_fees.push [period / 1000, c1_fees + previous_c1]
series.c2_fees.push [period / 1000, c2_fees + previous_c2]
series.fee_rate.push [period / 1000, series.fees[series.fees.length - 1][1] / series.volume[series.volume.length - 1][1]]
# difference = if idx == 0 then 0 else profit - series.profit_index[idx - 1][1]
# series.difference.push [period / 1000, difference]
# use this for strategies that never exit
# val = $c2 * cur_balance.c2 + $c1 * cur_balance.c1
# prev_val = prev_balance.c2 * $c2 + prev_balance.c1 * $c1
# # console.assert prev_val >= 0, message: 'previous value is not greater than 0!', prev_balance: prev_balance
# ret = 100 * (val - prev_val) / baseline_adjustment
# series.returns.push [period / 1000,ret]
baseline_val_in_ETH = baseline.c2 + baseline.c1 / BTC_2_ETH
ret = trade_profit_difference / baseline_val_in_ETH
series.returns.push [period / 1000, 100 * ret ]
original_ratio = baseline.c2 / (baseline.c1 + baseline.c2)
cur_ratio = cur_balance.c2 / (cur_balance.c1 + cur_balance.c2)
ratio_cp_deposit = cur_ratio - original_ratio
series.ratio_compared_to_deposit.push [period / 1000, ratio_cp_deposit]
if cur_balance.c2 < 0 || cur_balance.c1 < 0
console.error 'negative balance!', cur_balance, name
series.open.push [period / 1000, active_positions.length]
prev_balance.c2 = cur_balance.c2
prev_balance.c1 = cur_balance.c1
stats.metrics = series
if bus?.cache && 'stats' of bus.cache
from_cache('stats')[name] = stats
# console.timeEnd(name)
stats
KPI = (callback) ->
price_data = from_cache '/price_data'
balances = from_cache '/balances'
if !KPI.initialized
KPI.initialized = true
cached_positions = {}
indicator_cache = {}
add_to_cache get_dealers(), 'all'
for name in get_series()
add_to_cache name
time = from_cache '/time'
if !time.earliest? || !time.latest?
mint = Infinity
for p in cached_positions.all
mint = p.created if p.created < mint
time.earliest = mint
time.latest ||= now()
save time
KPI.ts = time.earliest
dates = KPI.dates = (o.date * 1000 for o in price_data.c1xc2 when o.date >= time.earliest - price_data.granularity ) #&& o.date <= time.latest)
dates.sort()
KPI.period_length = (dates[1] - dates[0]) / 1000
console.log '\n\nComputing KPIs' if config?.log_level > 0
dealers = Object.keys(cached_positions)
series_data = get_series()
all_stats = {}
try
for dealer in dealers when dealer not in series_data
stats = compute_KPI dealer
all_stats[dealer] = stats
catch error
console.error error
callback all_stats
dealer_measures = (stats) ->
'CAGR': (s) ->
if s of stats
"#{indicators.return(s, stats).toFixed(2)}%"
'Sortino': (s) ->
if s of stats
"#{indicators.sortino(s,stats).toFixed(2)}"
'Profit*': (s) ->
if s of stats
"$#{(indicators.profit(s,stats) or 0).toFixed(0)}"
'Score*': (s) ->
if s of stats
(indicators.score(s,stats)).toFixed(3)
# 'Power*': (s) ->
# if s of stats
# (indicators.power(s,stats)).toFixed(2)
# 'Open*': (s) ->
# if s of stats
# (indicators.open(s,stats)).toFixed(2)
'Profit': (s) ->
if s of stats
series = stats[s].metrics.profit_index
"$#{series[series.length - 1]?[1].toFixed(2) or 0 }"
# 'Trade profit': (s) ->
# if s of stats
# series = stats[s].metrics.trade_profit
# "#{series[series.length - 1]?[1].toFixed(2) or 0 }"
'Completed': (s) ->
if s of stats
"#{indicators.completed(s,stats)}"
'Success': (s) ->
if s of stats
"#{(indicators.success(s,stats)).toFixed(1)}%"
'μ duration': (s) ->
if s of stats
"#{indicators.avg_duration(s,stats).toFixed(2)}"
'x͂ duration': (s) ->
if s of stats
"#{indicators.median_duration(s,stats).toFixed(2)}"
'Done in day': (s) ->
if s of stats
"#{( indicators.in_day(s,stats)).toFixed(1)}%"
'within hour': (s) ->
if s of stats
"#{( indicators.in_hour(s,stats)).toFixed(1) }%"
'μ return': (s) ->
if s of stats
"#{indicators.avg_return(s,stats).toFixed(2)}%"
'x͂ return': (s) ->
if s of stats
"#{indicators.median_return(s,stats).toFixed(2)}%"
# 'μ loss': (s) ->
# if s of stats
# "#{indicators.avg_loss(s,stats).toFixed(2)}%"
# 'x͂ loss': (s) ->
# if s of stats
# "#{indicators.median_loss(s,stats).toFixed(2)}%"
# 'μ gain': (s) ->
# if s of stats
# "#{indicators.avg_gain(s,stats).toFixed(2)}%"
# 'x͂ gain': (s) ->
# if s of stats
# "#{indicators.median_gain(s,stats).toFixed(2)}%"
indicators =
score: (s, stats) ->
indicator_cache.score ||= {}
return indicator_cache.score[s] if s of indicator_cache.score
success = indicators.success(s,stats) / 100
sortino = Math.max Math.abs(indicators.sortino(s,stats)), 0.01
completed = (indicators.completed(s,stats) or 1) / stats[s].dealers.length
avg_return = Math.average (r[1] for r in stats[s].metrics.returns when r != 0)
sc = 100 * Math.abs Math.log(completed + 1) * avg_return * success * Math.log(sortino + 1)
if avg_return < 0
sc *= -1
indicator_cache.score[s] = sc
sc
# assumes 1 day periods
sortino: (s, stats) ->
indicator_cache.sortino ||= {}
return indicator_cache.sortino[s] if s of indicator_cache.sortino
returns = (r[1] for r in stats[s].metrics.returns)
if returns.length == 0
return 0
granularity = from_cache('/price_data').granularity
periods_per_year = 365 * 24 * 60 * 60 / granularity
yearly_return_target = 0.01 # 1% minimum yearly return goal. Treats not trading as slight negative.
target_return = 100 * (Math.pow(1 + yearly_return_target, 1 / periods_per_year) - 1) # convert to per period return. Treats not trading as slight negative.
avg_return = Math.average returns
neg_diff = ( Math.pow(Math.min(0, (r - target_return)),2) for r in returns )
downside_dev = Math.sqrt(Math.average(neg_diff))
sortino = (avg_return - target_return) / downside_dev
sortino *= Math.sqrt(periods_per_year) # annualize
indicator_cache.sortino[s] = sortino
sortino
profit: (s, stats) ->
indicator_cache.profit ||= {}
return indicator_cache.profit[s] if s of indicator_cache.profit
profs = Math.quartiles (v[1] for v in stats[s].metrics.profit_index)
prof = (profs.q1 + profs.q2 + profs.q3) / 3 or 0
indicator_cache.profit[s] = prof
prof
unit_profits: (s, stats) ->
indicator_cache.unit_profits ||= {}
return indicator_cache.unit_profits[s] if s of indicator_cache.unit_profits
profs = Math.quartiles (v[1] for v in stats[s].metrics.unit_profits)
prof = (profs.q1 + profs.q2 + profs.q3) / 3 or 0
indicator_cache.unit_profits[s] = prof
prof
trade_profit: (s, stats) ->
indicator_cache.trade_profit ||= {}
return indicator_cache.trade_profit[s] if s of indicator_cache.trade_profit
nonzero = (v[1] for v in stats[s].metrics.trade_profit_difference when v[1] != 0 )
if nonzero.length > 0
prof = Math.average nonzero
else
prof = 0
indicator_cache.trade_profit[s] = prof
prof
fees: (s, stats) ->
indicator_cache.fees ||= {}
return indicator_cache.fees[s] if s of indicator_cache.fees
nonzero = (v[1] for v in stats[s].metrics.fees when v[1] != 0 )
if nonzero.length > 0
prof = Math.average nonzero
else
prof = 0
indicator_cache.fees[s] = prof
prof
open: (s, stats) ->
return 1
indicator_cache.open ||= {}
return indicator_cache.open[s] if s of indicator_cache.open
o = Math.quartiles (v[1] for v in stats[s].metrics.open)
#(o.q3 + o.max) / 2 or 0
open = o.max or 0
indicator_cache.open[s] = open
open
power: (s, stats) ->
indicator_cache.power ||= {}
return indicator_cache.power[s] if s of indicator_cache.power
o = indicators.open(s,stats) or 1
prof = indicators.profit(s,stats)
power = prof / o
indicator_cache.power[s] = power
power
return: (s, stats) ->
indicator_cache.return ||= {}
return indicator_cache.return[s] if s of indicator_cache.return
# TODO: update for live system
# U$D value of baseline
price_data = from_cache('/price_data')
$c2 = if !price_data.c2 then price_data.c1xc2[price_data.c1xc2.length - 1].close else price_data.c2[price_data.c2.length - 1].close
$c1 = if !price_data.c1 then 1 else price_data.c1[price_data.c1.length - 1].close
invested = deposits[s].c2 * $c2 + deposits[s].c1 * $c1
idx = stats[s].metrics.profit_index.length - 1
profit = stats[s].metrics.profit_index[idx][1]
#annualize
years = (stats[s].metrics.end - stats[s].metrics.start) / (365 * 24 * 60 * 60)
ret = Math.pow((1 + profit / invested), 1 / years) - 1
ret *= 100
indicator_cache.return[s] = ret
ret
avg_return: (s, stats) ->
indicator_cache.avg_return ||= {}
return indicator_cache.avg_return[s] if s of indicator_cache.avg_return
returns = (r[1] for r in stats[s].metrics.returns when r[1] != 0)
avg_return = if returns.length > 0 then Math.average returns else 0
indicator_cache.avg_return[s] = avg_return
avg_return
avg_loss: (s, stats) ->
indicator_cache.avg_loss ||= {}
return indicator_cache.avg_loss[s] if s of indicator_cache.avg_loss
returns = (r[1] for r in stats[s].metrics.returns when r[1] < 0)
avg_loss = if returns.length > 0 then Math.average returns else 0
indicator_cache.avg_loss[s] = avg_loss
avg_loss
avg_gain: (s, stats) ->
indicator_cache.avg_gain ||= {}
return indicator_cache.avg_gain[s] if s of indicator_cache.avg_gain
returns = (r[1] for r in stats[s].metrics.returns when r[1] > 0)
avg_gain = if returns.length > 0 then Math.average returns else 0
indicator_cache.avg_gain[s] = avg_gain
avg_gain
median_return: (s, stats) ->
indicator_cache.median_return ||= {}
return indicator_cache.median_return[s] if s of indicator_cache.median_return
returns = (r[1] for r in stats[s].metrics.returns when r[1] != 0)
median_return = if returns.length > 0 then Math.median returns else 0
indicator_cache.median_return[s] = median_return
median_return
median_loss: (s, stats) ->
indicator_cache.median_loss ||= {}
return indicator_cache.median_loss[s] if s of indicator_cache.median_loss
returns = (r[1] for r in stats[s].metrics.returns when r[1] < 0)
median_loss = if returns.length > 0 then Math.median returns else 0
indicator_cache.median_loss[s] = median_loss
median_loss
median_gain: (s, stats) ->
indicator_cache.median_gain ||= {}
return indicator_cache.median_gain[s] if s of indicator_cache.median_gain
returns = (r[1] for r in stats[s].metrics.returns when r[1] > 0)
median_gain = if returns.length > 0 then Math.median returns else 0
indicator_cache.median_gain[s] = median_gain
median_gain
completed: (s, stats) -> stats[s].status.completed
success: (s, stats) ->
if stats[s].status.completed > 0
100 * stats[s].status.gains / ((stats[s].status.completed + stats[s].status.open) or 1)
else
pos_return = 0
for r in stats[s].metrics.returns
if r[1] > 0
pos_return += 1
100 * pos_return / stats[s].metrics.returns.length
in_day: (s, stats) -> 100 * (1 - stats[s].more_than_day)
in_hour: (s, stats) -> 100 * (1 - stats[s].more_than_hour)
avg_duration: (s, stats) ->
indicator_cache.avg_duration ||= {}
return indicator_cache.avg_duration[s] if s of indicator_cache.avg_duration
durations = ( (p.closed - p.created) / 60 for p in cached_positions[s] when p.closed)
avg_duration = if durations.length > 0 then Math.average durations else 0
indicator_cache.avg_duration[s] = avg_duration
avg_duration
median_duration: (s, stats) ->
indicator_cache.median_duration ||= {}
return indicator_cache.median_duration[s] if s of indicator_cache.median_duration
durations = ( (p.closed - p.created) / 60 for p in cached_positions[s] when p.closed)
median_duration = if durations.length > 0 then Math.median durations else 0
indicator_cache.median_duration[s] = median_duration
median_duration
indicators.profit.additive = true
indicators.completed.additive = true
indicators.power.additive = true
indicators.trade_profit.additive = true
indicators.open.additive = true
indicators.fees.additive = true
crunch = module.exports =
cached_positions: cached_positions
compute_KPI: compute_KPI
KPI: KPI
dealer_measures: dealer_measures
indicators: indicators
make_global crunch