-
Notifications
You must be signed in to change notification settings - Fork 276
/
main.py
219 lines (164 loc) · 8.06 KB
/
main.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
from trade_client import *
from store_order import *
from load_config import *
from collections import defaultdict
from datetime import datetime, time
import time
import json
import os.path
# loads local configuration
config = load_config('config.yml')
def get_all_coins():
"""
Returns all coins from Binance
"""
return client.get_all_tickers()
def generate_coin_seen_dict(all_coins):
"""
This method should be used once before starting the loop.
The value for every coin detected before the loop is set to True in the coin_seen_dict.
All the new coins detected during the loop will have a value of False.
"""
coin_seen_dict = defaultdict(bool)
for old_coin in all_coins:
coin_seen_dict[old_coin['symbol']] = True
return coin_seen_dict
def get_new_coins(coin_seen_dict):
"""
This method checks if there are new coins listed and returns them in a list.
The value of the new coins in coin_seen_dict will be set to True to make them not get detected again.
"""
result = []
all_coins_recheck = get_all_coins()
for new_coin in all_coins_recheck:
if not coin_seen_dict[new_coin['symbol']]:
result += [new_coin]
# this line ensures the new coin isn't detected again
coin_seen_dict[new_coin['symbol']] = True
return result
def get_price(coin, pairing):
"""
Get the latest price for a coin
"""
return client.get_ticker(symbol=coin+pairing)['lastPrice']
def main():
"""
Sells, adjusts TP and SL according to trailing values
and buys new coins
"""
# store config deets
tp = config['TRADE_OPTIONS']['TP']
sl = config['TRADE_OPTIONS']['SL']
enable_tsl = config['TRADE_OPTIONS']['ENABLE_TSL']
tsl = config['TRADE_OPTIONS']['TSL']
ttp = config['TRADE_OPTIONS']['TTP']
pairing = config['TRADE_OPTIONS']['PAIRING']
qty = config['TRADE_OPTIONS']['QUANTITY']
frequency = config['TRADE_OPTIONS']['RUN_EVERY']
test_mode = config['TRADE_OPTIONS']['TEST']
all_coins = get_all_coins()
coin_seen_dict = generate_coin_seen_dict(all_coins)
while True:
try:
# check if the order file exists and load the current orders
# basically the sell block and update TP and SL logic
if os.path.isfile('order.json'):
order = load_order('order.json')
for coin in list(order):
# store some necesarry trade info for a sell
stored_price = float(order[coin]['price'])
coin_tp = order[coin]['tp']
coin_sl = order[coin]['sl']
volume = order[coin]['volume']
symbol = coin.split(pairing)[0]
last_price = get_price(symbol, pairing)
# update stop loss and take profit values if threshold is reached
if float(last_price) > stored_price + (stored_price*coin_tp /100) and enable_tsl:
# increase as absolute value for TP
new_tp = float(last_price) + (float(last_price)*ttp /100)
# convert back into % difference from when the coin was bought
new_tp = float( (new_tp - stored_price) / stored_price*100)
# same deal as above, only applied to trailing SL
new_sl = float(last_price) - (float(last_price)*tsl /100)
new_sl = float((new_sl - stored_price) / stored_price*100)
# new values to be added to the json file
order[coin]['tp'] = new_tp
order[coin]['sl'] = new_sl
store_order('order.json', order)
print(f'updated tp: {round(new_tp, 3)} and sl: {round(new_sl, 3)}')
# close trade if tsl is reached or trail option is not enabled
elif float(last_price) < stored_price - (stored_price*sl /100) or float(last_price) > stored_price + (stored_price*tp /100) and not enable_tsl:
try:
# sell for real if test mode is set to false
if not test_mode:
sell = create_order(coin, coin['volume'], 'SELL')
print(f"sold {coin} at {(float(last_price) - stored_price) / float(stored_price)*100}")
# remove order from json file
order.pop(coin)
store_order('order.json', order)
except Exception as e:
print(e)
# store sold trades data
else:
if os.path.isfile('sold.json'):
sold_coins = load_order('sold.json')
else:
sold_coins = {}
if not test_mode:
sold_coins[coin] = sell
store_order('sold.json', sold_coins)
else:
sold_coins[coin] = {
'symbol':coin,
'price':last_price,
'volume':volume,
'time':datetime.timestamp(datetime.now()),
'profit': float(last_price) - stored_price,
'relative_profit': round((float(last_price) - stored_price) / stored_price*100, 3)
}
store_order('sold.json', sold_coins)
else:
order = {}
# check if new coins are listed
new_coins = get_new_coins(coin_seen_dict)
# the buy block and logic pass
if len(new_coins) > 0:
print(f'New coins detected: {new_coins}')
for coin in new_coins:
# buy if the coin hasn't already been bought
if coin['symbol'] not in order and pairing in coin['symbol']:
symbol_only = coin['symbol'].split(pairing)[0]
print(f"Preparing to buy {coin['symbol']}")
price = get_price(symbol_only, pairing)
volume = convert_volume(coin['symbol'], qty, price)
try:
# Run a test trade if true
if config['TRADE_OPTIONS']['TEST']:
order[coin['symbol']] = {
'symbol':symbol_only+pairing,
'price':price,
'volume':volume,
'time':datetime.timestamp(datetime.now()),
'tp': tp,
'sl': sl
}
print('PLACING TEST ORDER')
# place a live order if False
else:
order[coin['symbol']] = create_order(symbol_only+pairing, volume, 'BUY')
order[coin['symbol']]['tp'] = tp
order[coin['symbol']]['sl'] = sl
except Exception as e:
print(e)
else:
print(f"Order created with {volume} on {coin['symbol']}")
store_order('order.json', order)
else:
print(f"New coin detected, but {coin['symbol']} is currently in portfolio, or {pairing} does not match")
else:
pass
except Exception as e:
print(e)
if __name__ == '__main__':
print('working...')
main()