-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.coffee
494 lines (402 loc) · 16.5 KB
/
test.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
#######################
### TRENDATRON 5000 ###
#######################
### INTRO
https://cryptotrader.org/strategies/peKY35zY2Z2G56rLi
by aspiramedia (https://cryptotrader.org/aspiramedia)
Please PM me with any updates, feedback, bugs, suggestions, criticism etc.
Please leave this header intact, adding your own comments in EDITOR'S COMMENTS.
Edited bots are NOT for submission into the CryptoTrader.org Strategies section.
###
### EDITOR'S COMMENTS
Made any edits? Why not explain here.
###
### DONATIONS
I am releasing this as a donation based bot. I am releasing this in hope of obtaining some donations from users here.
Please donate BTC to: 1GGZU5mAUSLxVDegdxjakTLqZy7zizRH74
###
### DISCLAIMER
As usual with all trading, only trade with what you are able to lose.
Start small. I am NOT responsible for your losses if any occur.
###
### CREDITS
The VIX and Swing indicators used here were originally by Chris Moody at TradingView.
Trading logic is my own.
Thanks to all at Cryptotrader.org that helped me along the way.
###
### ADVICE
Rather than just trading with this, perhaps make this bot your own. Use it as a learning tool. Edit it to trade as you like to match your strategy.
View this as a template for a long term trend trader with added scalping.
Backtesting is your friend. Backtest over long periods to identify strengths and weaknesses.
###
############
### CODE ###
############
talib = require 'talib'
trading = require 'trading'
params = require 'params'
STOP_LOSS = params.add 'Use a Stop Loss?', false
STOP_LOSS_PERCENTAGE = params.add 'If so, Stop Loss Percentage?', 5
SCALP = params.add 'Use Scalping?', true
SPLIT = params.add 'Split orders up?', false
SPLIT_AMOUNT = params.add 'If so, split into how many?', 4
PERIOD = params.add 'Trend reaction time (Max = 250 | Min = 50 | Default = 250 )', 250
SNIPE = params.add 'Use Fat Finger Sniping?', false
CODE = params.add 'Code to remove donation requests', 0
class VIX
constructor: (@period) ->
@close = []
@wvf = []
@trade = []
@count = 0
# INITIALIZE ARRAYS
for [@close.length..22]
@close.push 0
for [@wvf.length..@period]
@wvf.push 0
for [@trade.length..10]
@trade.push 0
calculate: (instrument) ->
close = instrument.close[instrument.close.length-1]
high = instrument.high[instrument.high.length-1]
low = instrument.low[instrument.low.length-1]
# INCREASE DATA COUNT
@count++
# REMOVE OLD DATA
@close.pop()
@wvf.pop()
@trade.pop()
# ADD NEW DATA
@close.unshift(0)
@wvf.unshift(0)
@trade.unshift(0)
# CALCULATE
@close[0] = close
highest = (@close.reduce (a,b) -> Math.max a, b)
@wvf[0] = ((highest - low) / (highest)) * 100
sdev = talib.STDDEV
inReal: @wvf
startIdx: 0
endIdx: @wvf.length-1
optInTimePeriod: @period
optInNbDev: 1
sdev = sdev[sdev.length-1]
midline = talib.SMA
inReal: @wvf
startIdx: 0
endIdx: @wvf.length-1
optInTimePeriod: @period
midline = midline[midline.length-1]
lowerband = midline - sdev
upperband = midline + sdev
rangehigh = (@wvf.reduce (a,b) -> Math.max a, b) * 0.85
rangelow = (@wvf.reduce (a,b) -> Math.min a, b) * 1.01
if @wvf[0] >= upperband or @wvf[0] >= rangehigh
@trade[0] = 0
plotMark
"wvf1": @wvf[0]
else
@trade[0] = 1
plotMark
"wvf2": @wvf[0]
# RETURN DATA
result =
wvf: @wvf[0]
rangehigh: rangehigh
rangelow: rangelow
trade: @trade
return result
class GANNSWING
constructor: (@period) ->
@count = 0
@buycount = 0
@sellcount = 0
@lowma = []
@highma = []
@fastlowma = []
@fasthighma = []
# INITIALIZE ARRAYS
for [@lowma.length..5]
@lowma.push 0
for [@highma.length..5]
@highma.push 0
for [@fastlowma.length..5]
@fastlowma.push 0
for [@fasthighma.length..5]
@fasthighma.push 0
calculate: (instrument) ->
close = instrument.close[instrument.close.length-1]
high = instrument.high[instrument.high.length-1]
low = instrument.low[instrument.low.length-1]
# REMOVE OLD DATA
@lowma.pop()
@highma.pop()
@fastlowma.pop()
@fasthighma.pop()
# ADD NEW DATA
@lowma.unshift(0)
@highma.unshift(0)
@fastlowma.unshift(0)
@fasthighma.unshift(0)
# CALCULATE
highma = talib.SMA
inReal: instrument.high
startIdx: 0
endIdx: instrument.high.length-1
optInTimePeriod: @period
@highma[0] = highma[highma.length-1]
lowma = talib.SMA
inReal: instrument.low
startIdx: 0
endIdx: instrument.low.length-1
optInTimePeriod: @period
@lowma[0] = lowma[lowma.length-1]
fasthighma = talib.SMA
inReal: instrument.high
startIdx: 0
endIdx: instrument.high.length-1
optInTimePeriod: @period / 5
@fasthighma[0] = fasthighma[fasthighma.length-1]
fastlowma = talib.SMA
inReal: instrument.low
startIdx: 0
endIdx: instrument.low.length-1
optInTimePeriod: @period / 5
@fastlowma[0] = fastlowma[fastlowma.length-1]
if close > @highma[1] * 0.998 or (close > @fasthighma[1] and close > @fastlowma[1] * 1.01 and @fasthighma[1] > @highma[1])
hld = 1
else if close < @lowma[1] / 0.998 or (close < @fastlowma[1] and close < @fasthighma[1] / 1.01 and @fastlowma[1] < @lowma[1])
hld = -1
else
hld = 0
if hld != 0
@count++
if hld != 0 && @count == 1
hlv = hld
@count = 0
else
hlv = 0
if hlv == -1
hi = @highma[0]
plotMark
"hi": 50
@sellcount++
@buycount = 0
if hlv == 1
lo = @lowma[0]
plotMark
"lo": -5
@buycount++
@sellcount = 0
if @buycount == 3
tradebuy = true
@buycount = 0
else
tradebuy = false
if @sellcount == 3
tradesell = true
@sellcount = 0
else
tradesell = false
# RETURN DATA
result =
tradesell: tradesell
tradebuy: tradebuy
return result
class FUNCTIONS
@ROUND_DOWN: (value, places) ->
offset = Math.pow(10, places)
return Math.floor(value*offset)/offset
class TRADE
@BUY: (instrument, amount, split, timeout) ->
price = instrument.price
trading = require 'trading'
amount = FUNCTIONS.ROUND_DOWN((portfolio.positions[instrument.curr()].amount)/price, 8)
trading.buy instrument, 'limit', amount, price, timeout
@SELL: (instrument, amount, split, timeout) ->
price = instrument.price
trading = require 'trading'
amount = FUNCTIONS.ROUND_DOWN(portfolio.positions[instrument.asset()].amount, 8)
trading.sell instrument, 'limit', amount, price, timeout
init: ->
context.vix = new VIX(20) # Period of stddev and midline
context.swing = new GANNSWING(PERIOD) # Period of highma and lowma
# FOR FINALISE STATS
context.balance_curr = 0
context.balance_btc = 0
context.price = 0
# TRADING
if SPLIT
context.trade_split = SPLIT_AMOUNT
else
context.trade_split = 0
context.trade_timeout = 3000
handle: ->
instrument = data.instruments[0]
price = instrument.close[instrument.close.length - 1]
storage.lastBuyPrice ?= 0
storage.TICK ?= 0
storage.NAG ?= 1
storage.coolOff ?= 0
tradingblock = 0
# FOR FINALISE STATS
context.price = instrument.close[instrument.close.length - 1]
context.balance_curr = portfolio.positions[instrument.curr()].amount
context.balance_btc = portfolio.positions[instrument.asset()].amount
# CALLING INDICATORS
vix = context.vix.calculate(instrument)
wvf = vix.wvf
rangehigh = vix.rangehigh
rangelow = vix.rangelow
trade = vix.trade
swing = context.swing.calculate(instrument)
tradesell = swing.tradesell
tradebuy = swing.tradebuy
# LOGGING
today = new Date
day = today.getDate()
month = today.getMonth()
combo = day + month + 1
if storage.TICK == 0
storage.balance_curr_start = portfolio.positions[instrument.curr()].amount
storage.balance_btc_start = portfolio.positions[instrument.asset()].amount
storage.price_start = price
if combo == CODE
storage.NAG = 0
# WELCOME
info "###"
info "Welcome to the Trendatron Bot."
if storage.NAG == 1
info "Thanks for choosing this free bot. As many hours have gone into its creation, please consider a donation to:"
info "BTC: 1GGZU5mAUSLxVDegdxjakTLqZy7zizRH74"
info "(The bot carries on regardless of donations - don't worry.)"
info "If you have donated PM me for a code to make donation requests disappear."
else
info "Thanks for choosing to donate. Appreciated."
if STOP_LOSS == true
info "You chose to use a Stop Loss, with a cutoff of " + STOP_LOSS_PERCENTAGE + " percent."
if SCALP == true
info "You chose to use scalping (default bot behaviour)"
if SPLIT == true
info "You chose to split orders up into " + SPLIT_AMOUNT + " orders."
if SNIPE == true
info "You chose to enable sniping."
info "###"
starting_btc_equiv = storage.balance_btc_start + storage.balance_curr_start / storage.price_start
current_btc_equiv = context.balance_btc + context.balance_curr / price
starting_fiat_equiv = storage.balance_curr_start + storage.balance_btc_start * storage.price_start
current_fiat_equiv = context.balance_curr + context.balance_btc * price
efficiency = Math.round((current_btc_equiv / starting_btc_equiv) * 1000) / 1000
efficiency_percent = Math.round((((current_btc_equiv / starting_btc_equiv) - 1) * 100) * 10) / 10
market_efficiency = Math.round((((context.price / storage.price_start) - 1) * 100) * 10) / 10
bot_efficiency = Math.round((((current_fiat_equiv / starting_fiat_equiv) - 1) * 100) * 10) / 10
storage.TICK++
if Math.round(storage.TICK/24) == (storage.TICK/24)
warn "### Day " + storage.TICK/24 + " Log"
debug "Current Fiat (USD): " + Math.round(context.balance_curr*100)/100 + " | Current Crypto (BTC): " + Math.round(context.balance_btc*100)/100
debug "Starting Fiat (USD): " + Math.round(storage.balance_curr_start*100)/100 + " | Starting Crypto (BTC): " + Math.round(storage.balance_btc_start*100)/100
debug "Current Portfolio Worth: " + Math.round(((context.balance_btc * price) + context.balance_curr)*100)/100
debug "Starting Portfolio Worth: " + Math.round(((storage.balance_btc_start * storage.price_start) + storage.balance_curr_start)*100)/100
debug "Efficiency of Buy and Hold: " + market_efficiency + "%"
debug "Efficiency of Bot: " + bot_efficiency + "%"
debug "Efficiency Vs Buy and Hold: " + efficiency + " which equals " + efficiency_percent + "%"
warn "###"
if Math.round(storage.TICK/48) == (storage.TICK/48) && storage.NAG == 1
info "###"
info "Thanks for using this bot. Please consider a donation to:"
info "BTC: 1GGZU5mAUSLxVDegdxjakTLqZy7zizRH74"
info "(The bot carries on regardless of donations - don't worry.)"
info "If you have donated PM me for a code to make donation requests disappear."
info "###"
# TRADING
if context.balance_curr/price > 0.01
if tradebuy == true && storage.coolOff <= 0
if TRADE.BUY(instrument, null, context.trade_split, context.trade_timeout)
storage.lastBuyPrice = price
storage.stop = true
info "#########"
info "Trend Buy"
info "#########"
tradingblock = 1
sendEmail "Trendatron TREND BUY" + " - Current Fiat: " + Math.round(context.balance_curr*100)/100 + " | Current BTC: " + Math.round(context.balance_btc*100)/100
if context.balance_curr/price > 0.01 && SCALP == true && storage.coolOff <= 0
if trade[0] == 1 && trade[1] == 1 && trade[2] == 0 && trade[3] == 0 && trade[4] == 0 && trade[5] == 0 && wvf > 8.5
if TRADE.BUY(instrument, null, context.trade_split, context.trade_timeout)
storage.lastBuyPrice = price
storage.stop = true
info "#########"
info "Scalp Buy"
info "#########"
tradingblock = 1
sendEmail "Trendatron SCALP BUY" + " - Current Fiat: " + Math.round(context.balance_curr*100)/100 + " | Current BTC: " + Math.round(context.balance_btc*100)/100
if context.balance_btc > 0.01
if (tradesell == true && wvf < 2.85) or (tradebuy == true && wvf > 8.5 && trade[0] == 1 && trade[1] == 0)
if TRADE.SELL(instrument, null, context.trade_split, context.trade_timeout)
storage.lastBuyPrice = 0
storage.lastSellPrice = price
storage.stop = false
warn "##########"
warn "Trend Sell"
warn "##########"
tradingblock = 1
sendEmail "Trendatron TREND SELL" + " - Current Fiat: " + Math.round(context.balance_curr*100)/100 + " | Current BTC: " + Math.round(context.balance_btc*100)/100
if STOP_LOSS
if storage.stop == true && price < storage.lastBuyPrice * (1 - (STOP_LOSS_PERCENTAGE / 100))
if TRADE.SELL(instrument, null, context.trade_split, context.trade_timeout)
storage.lastBuyPrice = 0
storage.lastSellPrice = price
storage.stop = false
storage.coolOff = 30
warn "##############"
warn "Stop Loss Sell"
warn "##############"
tradingblock = 1
sendEmail "Trendatron STOP LOSS SELL" + " - Current Fiat: " + Math.round(context.balance_curr*100)/100 + " | Current BTC: " + Math.round(context.balance_btc*100)/100
if SNIPE && tradingblock == 0
if context.balance_btc > 0.05
trading = require 'trading'
if trading.sell instrument, 'limit', null, instrument.price * 1.1, 3530
warn "##########"
warn "Snipe Sell"
warn "##########"
if context.balance_curr > 10
trading = require 'trading'
if trading.buy instrument, 'limit', null, instrument.price * 0.9, 3530
info "#########"
info "Snipe Buy"
info "#########"
storage.coolOff--
# PLOTTING / DEBUG
plot
wvf: wvf
rangehigh: rangehigh
rangelow: rangelow
setPlotOptions
wvf:
secondary: true
rangehigh:
secondary: true
rangelow:
secondary: true
wvf1:
secondary: true
color: 'blue'
size: 1
wvf2:
secondary: true
color: 'black'
size: 1
lo:
color: 'green'
secondary: true
size: 4
hi:
color: 'red'
secondary: true
size: 4
onStop: ->
# DISPLAY FINALISE STATS
if context.balance_curr > 10
info "Final BTC Equiv: " + Math.round(context.balance_curr/context.price*100)/100
if context.balance_btc > 0.05
info "Final BTC Value: " + Math.round(context.balance_btc*100)/100