-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-trader.py
360 lines (280 loc) · 13.6 KB
/
auto-trader.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
##
## auto-trader.py
## This file is contains the main functionality of the auto-trader.
## It contains the algorithmic trading methodology and generates all of the
## necessary objects.
## The main algorithmic trading functionality is under the large while loop,
## which loops through the trading conditions and sees which ones pass. If so,
## a trade is executed, otherwise it goes to the next trading condition.
##
## The auto-trader can only be in either buying mode or selling mode.
## If you wish to do more at once, run this as many times as you would like simultaneously
##
from time import sleep
import time
import condition
import exchange
##
## Disables the current condition and enables the next condition on the branch
##
## @param _condition
## The current condition that needs to not be run anymore
##
def next_link(_condition):
print("NEXT LINK from condition " + _condition.condition_id + " to condition " + _condition.next_link.condition_id)
_condition.run = False
_condition.next_link.run = True
# The time in seconds between price retrievals
sleep_time = 0.2
# Generate the crypto exchange
crypto_exchange = exchange.generate_exchange()
# The buy conditions
buy_conditions = condition.generate_and_validate_trade_conditions('buy')
# The sell conditions
sell_conditions = condition.generate_and_validate_trade_conditions('sell')
# The sell floor (sells everything and stops program)
sell_floor = condition.generate_sell_floor()
# Done generating everything, now the auto-trader will run
print('--------------------------------------')
print(' Done configuring')
print(' The auto-trader is now running')
print('--------------------------------------')
# Pair of price and time
prices_interval = []
# MODE:
# 0 - buy
# 1 - sell
mode = 0
# Changes to true after every trade and causes a reset to key variables
reset = True
# Get the max interval length
max_interval_length = 0
for i in buy_conditions:
if i.interval > max_interval_length:
max_interval_length = i.interval
for i in sell_conditions:
if i.interval > max_interval_length:
max_interval_length = i.interval
# Declare the variables max_price_since_trade and min_price_since_trade
max_price_since_trade = crypto_exchange.get_price()
min_price_since_trade = max_price_since_trade
#
# The main while loop that goes executes the algorithmic trading
#
while True:
# If a trade has just been executed
if reset:
# Ensure this section only runs right after a trade is executed
reset = False
# After every trade, the USD balance and crypto balance is printed to Account_Holdings.txt
account_holdings = open('Account-Holdings.txt', 'a')
account_holdings.truncate(0)
account_holdings.write('Current balance in USD: '+str(crypto_exchange.balance)+'\n')
account_holdings.write('Current balance in '+str(crypto_exchange.coin)+': '+str(crypto_exchange.crypto_balance))
account_holdings.close()
# Clear the list of prices in the interval
prices_interval.clear()
# Reset max_price_since_trade and min_price_since_trade
max_price_since_trade = crypto_exchange.get_price()
min_price_since_trade = max_price_since_trade
# Reset the availability of the buy conditions
for i in buy_conditions:
if not i.has_previous:
i.run = True
else:
i.run = False
# Reset the availability of the sell conditions
for i in sell_conditions:
if not i.has_previous:
i.run = True
else:
i.run = False
# Pause between getting the prices
sleep(sleep_time)
# Get the price of the coin
crypto_price = crypto_exchange.get_price()
# Link the price and time of the coin and append it to the interval prices list
current_time = time.time()
prices_interval.append((crypto_price, current_time))
# Remove the prices with a time that are greater than maximum interval seconds ago
for i in prices_interval:
if current_time - i[1] > max_interval_length:
prices_interval.pop(0)
else:
break
# If the current price is greater than the max price since the last trade
if crypto_price > max_price_since_trade:
max_price_since_trade = crypto_price
# If the current price is less than the min price since the last trade
if crypto_price < min_price_since_trade:
min_price_since_trade = crypto_price
# If the price is less than the sell floor, sell everything and end the program
if crypto_price < sell_floor:
print("PRICE IS BELOW THE SELL FLOOR")
if mode == 1:
crypto_exchange.sell()
quit()
#
# BUY MODE
#
if mode == 0:
# Loop through every buy condition
for i in buy_conditions:
# If the current buy condition isn't valid, move on to the next one
if not i.run:
continue
# Get the max and min prices in the interval of the buy condition
max_interval_price = crypto_price
min_interval_price = crypto_price
for p in reversed(prices_interval):
if current_time - p[1] > i.interval:
break
if p[0] > max_interval_price:
max_interval_price = p[0]
elif p[0] < min_interval_price:
min_interval_price = p[0]
# If the buy condition used PERCENT_UP
if i.percent_up > 1:
# If the condition uses INTERVAL_PRICE
if i.from_up == 0:
# If the current price >= minimum price in the interval * the percent up, BUY
if crypto_price >= (min_interval_price*i.percent_up):
print("BOUGHT AT "+str(crypto_price))
mode = 1
reset = True
crypto_exchange.buy()
# If the condition uses TRADE_PRICE
else:
# If the current price >= minimum price since the last trade * the percent up, BUY
if crypto_price >= (min_price_since_trade*i.percent_up):
print("BOUGHT AT "+str(crypto_price))
mode = 1
reset = True
crypto_exchange.buy()
# If the buy condition used PRICE_UP
else:
# If the condition uses INTERVAL_PRICE
if i.from_up == 0:
# If the current price >= minimum price in the interval + the price up, BUY
if crypto_price >= min_interval_price + i.price_up:
print("BOUGHT AT "+str(crypto_price))
mode = 1
reset = True
crypto_exchange.buy()
# If the condition uses TRADE_PRICE
else:
# If the current price >= minimum price since the last trade + the price up, BUY
if crypto_price >= min_price_since_trade + i.price_up:
print("BOUGHT AT "+str(crypto_price))
mode = 1
reset = True
crypto_exchange.buy()
# If there is no next link, continue to the next buy condition
if type(i.next_link).__name__ != 'TradeCondition':
continue
# If the buy condition uses PERCENT_DOWN
if i.percent_down < 1:
# If the condition uses INTERVAL_PRICE
if i.from_down == 0:
# If the current price <= maximum price in the interval * the percent down, NEXT LINK
if crypto_price <= (max_interval_price*i.percent_down):
next_link(i)
# If the condition uses TRADE_PRICE
else:
# If the current price <= maximum price since the last trade * the percent down, NEXT LINK
if crypto_price <= (max_price_since_trade*i.percent_down):
next_link(i)
# If the buy condition uses PRICE_DOWN
else:
# If the buy condition uses INTERVAL_PRICE
if i.from_down == 0:
# If the difference of the maximum price in the interval and the current price >= the price down, NEXT LINK
if (max_interval_price-crypto_price) >= i.price_down:
next_link(i)
# If the buy condition uses TRADE_PRICE
else:
# If the difference of the maximum price since the last trade and the current price >= the price down, NEXT LINK
if (max_price_since_trade-crypto_price) >= i.price_down:
next_link(i)
#
# SELL MODE
#
else:
# Loop through every sell condition
for i in sell_conditions:
# If the current sell condition isn't valid, move on to the next one
if not i.run:
continue
# Get the max and min prices in the interval of the sell condition
max_interval_price = crypto_price
min_interval_price = crypto_price
for p in reversed(prices_interval):
if current_time - p[1] > i.interval:
break
if p[0] > max_interval_price:
max_interval_price = p[0]
elif p[0] < min_interval_price:
min_interval_price = p[0]
# If the sell condition uses PERCENT_DOWN
if i.percent_down < 1:
# If the condition uses INTERVAL_PRICE
if i.from_down == 0:
# If the current price <= maximum price in the interval * the percent down, SELL
if crypto_price <= (max_interval_price*i.percent_down):
print("SOLD AT "+str(crypto_price))
mode = 0
reset = True
crypto_exchange.sell()
# If the condition uses TRADE_PRICE
else:
# If the current price <= maximum price since the last trade * the percent down, SELL
if crypto_price <= (max_price_since_trade*i.percent_down):
print("SOLD AT "+str(crypto_price))
mode = 0
reset = True
crypto_exchange.sell()
# If the sell condition uses PRICE_DOWN
else:
# If the condition uses INTERVAL_PRICE
if i.from_down == 0:
# If the difference of the maximum price in the interval and the current price >= the price down, SELL
if (max_interval_price-crypto_price) >= i.price_down:
print("SOLD AT "+str(crypto_price))
mode = 0
reset = True
crypto_exchange.sell()
# If the condition uses TRADE_PRICE
else:
# If the difference of the maximum price since the last trade and the current price >= the price down, SELL
if (max_price_since_trade-crypto_price) >= i.price_down:
print("SOLD AT "+str(i.price_down))
mode = 0
reset = True
crypto_exchange.sell()
# If there is no next link, continue to the next sell condition
if type(i.next_link).__name__ != 'TradeCondition':
continue
# If the sell condition uses PERCENT_UP
if i.percent_up > 1:
# If the condition uses INTERVAL_PRICE
if i.from_up == 0:
# If the current price >= minimum price in the interval * the percent up, NEXT LINK
if crypto_price >= (min_interval_price*i.percent_up):
next_link(i)
# If the condition uses TRADE_PRICE
else:
# If the current price >= minimum price since the last trade + the percent up, NEXT LINK
if crypto_price >= (min_price_since_trade*i.percent_up):
next_link(i)
# If the sell condition uses PRICE_UP
else:
# If the condition uses INTERVAL_PRICE
if i.from_up == 0:
# If the current price >= minimum price in the interval + the price up, NEXT LINK
if crypto_price >= min_interval_price + i.price_up:
next_link(i)
# If the condition uses TRADE_PRICE
else:
# If the current price >= minimum price since the last trade + the price up, NEXT LINK
if crypto_price >= min_price_since_trade + i.price_up:
next_link(i)