forked from Reidmcc/Binance-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle_trade.py
437 lines (395 loc) · 17.8 KB
/
turtle_trade.py
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
###### Inspired by the Turtle Traders from the 1980s
###### More information at http://www.tradingblox.com/originalturtles/
###### Testing suggests this strategy does NOT work well with crypto
###### This is in the middle of a refactor
from api import BinanceAPI
client = BinanceAPI('[API key]','[API secret]')
# import time
# from data_checks import Data_checks
# import pandas
# from datetime import datetime
# import matplotlib.pyplot as plt
import datetime
from excepts import MalformedRequest, StatusUnknown, InternalError
from requests.exceptions import ConnectionError
from http.client import RemoteDisconnected
from http.client import HTTPException
from urllib3.exceptions import ProtocolError
class turtle:
def __init__(self):
self.pair = ""
self.pip_sats = None
self.price_mult = 1
self.interval = None
self.capital = None
self.limit = None
self.exit_trigger = None
self.enter_trigger = None
self.red = '\u001b[31;1m'
self.green = '\033[92m'
self.yellow = '\033[93m'
self.blue = '\u001b[34m'
self.mag = '\u001b[35;1m'
self.stop_col = '\033[0m'
def set_params(self, pair, interval, pip_sats, capital, limit, exit_trigger = 2, enter_trigger = 20, price_mult = 1):
self.pair = pair
self.pip_sats = pip_sats
self.interval = interval
self.capital = capital
self.limit = limit
self.exit_trigger = exit_trigger
self.enter_trigger = enter_trigger
self.price_mult = price_mult
def find_trades(self):
open_long = False
open_short = False
whip_retry_long = False
whip_retry_short = False
long_stop_trig = None
long_enter_price = None
long_size = None
# long_exit_price = None
short_stop_trig = None
short_enter_price = None
short_size = None
# short_exit_price = None
lost_longs = 0
won_longs = 0
lost_shorts = 0
won_shorts = 0
high_test = "high_20"
low_test = "low_20"
last_win = False
worst_recorded = False
worst_loss = None
worst_string = ''
best_recorded = False
best_gain = None
best_string = ''
stats = self.calc_stats()
for s in stats:
# print('')
# ticker = str(s["time"] + " Open: " + "{:.8f}".format(s["open"]) + " High: " + "{:.8f}".format(s["high"]) + " Low: " + "{:.8f}".format(s["low"]) + " Close: " "{:.8f}".format(s["close"]) + " N = " + "{:.8f}".format(s["N"])
# + " High 20: " + "{:.8f}".format(s["high_20"]) + " Low 20: " + "{:.8f}".format(s["low_20"]))
print(self.pair + s["time"] + " Open: " + "{:.8f}".format(s["open"]) + " High: " + "{:.8f}".format(s["high"]) + " Low: " + "{:.8f}".format(s["low"]) + " Close: " "{:.8f}".format(s["close"]) + " N = " + "{:.8f}".format(s["N"])
+ " High 20: " + "{:.8f}".format(s["high_20"]) + " Low 20: " + "{:.8f}".format(s["low_20"]))
if s["count"] >= self.enter_trigger + 3 \
and s["high_10"] != None and s["low_10"] != None \
and s[high_test] != None and s[low_test] != None:
if not open_long:
if s["high"] > s[high_test]:# and s["close"] > s["open"]:
long_enter_price = s[high_test]
# if s["unit"] < self.capital / long_enter_price:
# long_size = s["unit"]
# else:
# long_size = self.capital / long_enter_price
long_size = s["unit"]
long_stop_trig = long_enter_price - (1 * s["N"])
# long_stop_trig = long_enter_price * .99
open_long = True
print(self.blue + "Fast Long Breakout:" + " Entering at " + "{:.8f}".format(long_enter_price) + " Postion size = " + "{:.2f}".format(long_size)
+ " with stop at " + "{:.8f}".format(long_stop_trig) + self.stop_col)
print('')
elif s["low"] <= long_stop_trig and not whip_retry_long:
loss = (long_size * .9975 * long_stop_trig * .9975) - (long_enter_price * long_size)
# loss = (long_size * long_stop_trig) - (long_enter_price * long_size)
print(self.red + "Long stop loss triggered at " + "{:.8f}".format(long_stop_trig) + " for loss of " + "{:.8f}".format(loss) + self.stop_col)
open_long = False
# if not last_win:
self.capital += loss
lost_longs += 1
print("New total capital = " + "{:.8f}".format(self.capital))
print('')
if not worst_recorded:
worst_loss = loss
worst_string = str(s["time"] + ' ' + self.red + "Long stop loss triggered at " + "{:.8f}".format(long_stop_trig) + " for loss of " + "{:.8f}".format(loss) + self.stop_col)
worst_recorded = True
elif loss < worst_loss:
worst_loss = loss
worst_string = str(s["time"] + ' ' + self.red + "Long stop loss triggered at " + "{:.8f}".format(long_stop_trig) + " for loss of " + "{:.8f}".format(loss) + self.stop_col)
# last_win = False
# elif s["low"] <= s["low_10"]:
elif s["low"] <= s["low_10"] and s["low_10"] > long_enter_price * 1.005:
long_exit_price = s["low_10"]
prof_or_loss = (long_size * .9975 * long_exit_price * .9975) - (long_enter_price * long_size)
# prof_or_loss = (long_size * long_exit_price ) - (long_enter_price * long_size)
open_long = False
# if not last_win:
# self.capital += prof_or_loss
# print("New total capital = " + "{:.8f}".format(self.capital))
# print('')
if prof_or_loss > 0:
color = self.green
won_longs += 1
last_win = True
self.capital += prof_or_loss
else:
color = self.red
lost_longs += 1
last_win = False
self.capital += prof_or_loss
print(color + "Closing Long Postion:" " Exiting at " + "{:.8f}".format(long_exit_price) + " for profit/loss of " + "{:.8f}".format(prof_or_loss) + self.stop_col)
print("New total capital = " + "{:.8f}".format(self.capital))
print('')
if prof_or_loss > 0:
if not best_recorded:
best_gain = prof_or_loss
best_string = str(s["time"] + ' ' + color + "Closing Long Postion:" " Exiting at " + "{:.8f}".format(long_exit_price) + " for profit/loss of " + "{:.8f}".format(prof_or_loss) + self.stop_col)
best_recorded = True
elif prof_or_loss > best_gain:
best_gain = prof_or_loss
best_string = str(s["time"] + ' ' + color + "Closing Long Postion:" " Exiting at " + "{:.8f}".format(long_exit_price) + " for profit/loss of " + "{:.8f}".format(prof_or_loss) + self.stop_col)
return (self.capital, lost_longs, won_longs, lost_shorts, won_shorts, best_string, worst_string)
def calc_stats(self):
hist = []
dat = self.get_klines(self.pair, self.interval, self.limit)
count = 0
init_range_tot = 0
for k in dat:
count += 1
t_str = str(k[0])
k_time = int(t_str[:10])
k_t_stamp = str(datetime.datetime.fromtimestamp(k_time))
k_open = float(k[1]) * self.price_mult
k_high = float(k[2]) * self.price_mult
k_low = float(k[3]) * self.price_mult
k_close = float(k[4]) * self.price_mult
if count == 1:
hist.append(
{
"count":count,
"time":k_t_stamp,
"open":k_open,
"high":k_high,
"low":k_low,
"close":k_close,
"true_range":0,
"N":0,
"unit":0,
"high_10": 0,
"low_10": 0,
"high_20": 0,
"low_20": 0,
"high_55": 0,
"low_55": 0
}
)
elif count <= self.enter_trigger + 1:
prev_c = hist[-1]["close"]
high_min_low = k_high - k_low
high_min_prev = k_high - prev_c
prev_min_low = prev_c - k_low
diff_list = (high_min_low, high_min_prev, prev_min_low)
max_diff = max(diff_list)
hist.append(
{
"count": count,
"time": k_t_stamp,
"open": k_open,
"high": k_high,
"low": k_low,
"close": k_close,
"true_range": max_diff,
"N": 0,
"unit":0,
"high_10":0,
"low_10":0,
"high_20":0,
"low_20":0,
"high_55":0,
"low_55":0
}
)
init_range_tot += max_diff
elif count == self.enter_trigger + 2:
init_avg = init_range_tot / 20
prev_c = hist[-1]["close"]
high_min_low = k_high - k_low
high_min_prev = k_high - prev_c
prev_min_low = prev_c - k_low
diff_list = (high_min_low, high_min_prev, prev_min_low)
max_diff = max(diff_list)
k_N = (19 * init_avg + max_diff) / 20
k_unit = (self.capital * .01) / k_N
set_10 = False
high_10 = False
low_10 = False
for i in range(1, self.exit_trigger):
if not set_10:
high_10 = hist[-i]["high"]
low_10 = hist[-i]["low"]
set_10 = True
else:
if hist[-i]["high"] > high_10:
high_10 = hist[-i]["high"]
if hist[-i]["low"] < low_10:
low_10 = hist[-i]["low"]
set_20 = False
high_20 = False
low_20 = False
for i in range(1, self.enter_trigger):
if not set_20:
high_20 = hist[-i]["high"]
low_20 = hist[-i]["low"]
set_20 = True
else:
if hist[-i]["high"] > high_20:
high_20 = hist[-i]["high"]
if hist[-i]["low"] < low_20 or low_20 == None:
low_20 = hist[-i]["low"]
hist.append(
{
"count": count,
"time": k_t_stamp,
"open": k_open,
"high": k_high,
"low": k_low,
"close": k_close,
"true_range": max_diff,
"N": k_N,
"unit":k_unit,
"high_10":high_10,
"low_10":low_10,
"high_20": high_20,
"low_20": low_20,
"high_55": 0,
"low_55": 0
}
)
elif count < 56:
prev_N = hist[-1]["N"]
prev_c = hist[-1]["close"]
high_min_low = k_high - k_low
high_min_prev = k_high - prev_c
prev_min_low = prev_c - k_low
diff_list = (high_min_low, high_min_prev, prev_min_low)
max_diff = max(diff_list)
k_N = (19 * prev_N + max_diff) / 20
k_unit = (self.capital * .01) / k_N
set_10 = False
high_10 = False
low_10 = False
for i in range(1, self.exit_trigger):
if not set_10:
high_10 = hist[-i]["high"]
low_10 = hist[-i]["low"]
set_10 = True
else:
if hist[-i]["high"] > high_10:
high_10 = hist[-i]["high"]
if hist[-i]["low"] < low_10:
low_10 = hist[-i]["low"]
set_20 = False
high_20 = False
low_20 = False
for i in range(1, self.enter_trigger):
if not set_20:
high_20 = hist[-i]["high"]
low_20 = hist[-i]["low"]
set_20 = True
else:
if hist[-i]["high"] > high_20:
high_20 = hist[-i]["high"]
if hist[-i]["low"] < low_20 or low_20 == None:
low_20 = hist[-i]["low"]
hist.append(
{
"count": count,
"time": k_t_stamp,
"open": k_open,
"high": k_high,
"low": k_low,
"close": k_close,
"true_range": max_diff,
"N": k_N,
"unit": k_unit,
"high_10": high_10,
"low_10": low_10,
"high_20": high_20,
"low_20": low_20,
"high_55": 0,
"low_55": 0
}
)
else:
prev_N = hist[-1]["N"]
prev_c = hist[-1]["close"]
high_min_low = k_high - k_low
high_min_prev = k_high - prev_c
prev_min_low = prev_c - k_low
diff_list = (high_min_low, high_min_prev, prev_min_low)
max_diff = max(diff_list)
k_N = (19 * prev_N + max_diff) / 20
k_unit = (self.capital * .01) / k_N
set_10 = False
high_10 = False
low_10 = False
for i in range(1, self.exit_trigger):
if not set_10:
high_10 = hist[-i]["high"]
low_10 = hist[-i]["low"]
set_10 = True
else:
if hist[-i]["high"] > high_10:
high_10 = hist[-i]["high"]
if hist[-i]["low"] < low_10:
low_10 = hist[-i]["low"]
set_20 = False
high_20 = False
low_20 = False
for i in range(1, self.enter_trigger):
if not set_20:
high_20 = hist[-i]["high"]
low_20 = hist[-i]["low"]
set_20 = True
else:
if hist[-i]["high"] > high_20:
high_20 = hist[-i]["high"]
if hist[-i]["low"] < low_20 or low_20 == None:
low_20 = hist[-i]["low"]
set_55 = False
high_55 = False
low_55 = False
for i in range(1, 55):
if not set_55:
high_55 = hist[-i]["high"]
low_55 = hist[-i]["low"]
set_55 = True
else:
if hist[-i]["high"] > high_55 or high_55 == None:
high_55 = hist[-i]["high"]
if hist[-i]["low"] < low_55 or low_55 == None:
low_55 = hist[-i]["low"]
hist.append(
{
"count": count,
"time": k_t_stamp,
"open": k_open,
"high": k_high,
"low": k_low,
"close": k_close,
"true_range": max_diff,
"N": k_N,
"unit": k_unit,
"high_10": high_10,
"low_10": low_10,
"high_20": high_20,
"low_20": low_20,
"high_55": high_55,
"low_55": low_55
}
)
return hist
def get_klines(self, pair, interval, limit):
klines = []
got_klines = False
while not got_klines:
try:
klines = client.klines(pair, interval=interval, limit=limit)
except (MalformedRequest, InternalError, StatusUnknown, ConnectionError, RemoteDisconnected, ProtocolError,
HTTPException) as e:
print(str(e) + ' ' + str(e.__traceback__))
client.set_offset()
else:
got_klines = True
return klines