-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
237 lines (200 loc) · 10.1 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from trade_client import *
from store_order import *
from logger import logger
from load_config import *
from new_listings_scraper import *
from collections import defaultdict
from datetime import datetime, time
import time
import threading
import json
import os.path
old_coins = ["CHESS","OTHERCRAP"]
# loads local configuration
config = load_config('config.yml')
# load necessary files
if os.path.isfile('sold.json'):
sold_coins = load_order('sold.json')
else:
sold_coins = {}
if os.path.isfile('order.json'):
order = load_order('order.json')
else:
order = {}
if os.path.isfile('new_listing.json'):
announcement_coin = load_order('new_listing.json')
else:
announcement_coin = False
# Keep the supported currencies loaded in RAM so no time is wasted fetching
# currencies.json from disk when an announcement is made
global supported_currencies
logger.debug("Starting get_all_currencies")
supported_currencies = get_all_currencies(single=True)
logger.debug("Finished get_all_currencies")
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']
t = threading.Thread(target=search_and_update)
t.start()
t2 = threading.Thread(target=get_all_currencies)
t2.start()
while True:
# check if the order file exists and load the current orders
# basically the sell block and update TP and SL logic
if len(order) > 0:
for coin in list(order):
# store some necessary 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 = order[coin]['symbol']
logger.debug('Data for sell:\r\n' + 'Coin Info: \r\n' + coin +
'\r\nStored price: ' + stored_price + "\r\nCoin TP: " +
coin_tp + '\r\nCoin SL: ' + coin_sl + '\r\nVolume: ' +
volume + '\r\nSymbol: ' + symbol)
logger.info("get_last_price existing coin: ", coin)
last_price = get_last_price(symbol, pairing)
logger.info("Finished get_last_price")
logger.info(f'{last_price=}')
logger.info(f'{stored_price + (stored_price*sl /100)=}')
# 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)
logger.info(f'Updated TP: {round(new_tp, 3)} and SL:'
f' {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 * coin_tp / 100) and not enable_tsl:
try:
# sell for real if test mode is set to false
if not test_mode:
logger.info("starting sell place_order with : ",symbol,
pairing, volume*99.5/100, 'sell', last_price)
sell = place_order(symbol, pairing, volume*99.5/100, 'sell', last_price)
logger.info("Finish sell place_order")
logger.info(f"sold {coin} with {(float(last_price) - stored_price) / float(stored_price)*100}% PNL")
# remove order from json file
order.pop(coin)
store_order('order.json', order)
logger.debug('Order saved in order.json')
except Exception as e:
logger.error(e)
# store sold trades data
else:
if not test_mode:
sold_coins[coin] = sell
store_order('sold.json', sold_coins)
logger.info('Order saved in sold.json')
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),
'id': 'test-order',
'text': 'test-order',
'create_time': datetime.timestamp(datetime.now()),
'update_time': datetime.timestamp(datetime.now()),
'currency_pair': f'{symbol}_{pairing}',
'status': 'closed',
'type': 'limit',
'account': 'spot',
'side': 'sell',
'iceberg': '0',
'price': last_price}
logger.info('Sold coins:\r\n' + sold_coins[coin])
store_order('sold.json', sold_coins)
# the buy block and logic pass
# announcement_coin = load_order('new_listing.json')
if os.path.isfile('new_listing.json'):
announcement_coin = load_order('new_listing.json')
else:
announcement_coin = False
global supported_currencies
if announcement_coin and announcement_coin not in order and announcement_coin not in sold_coins and announcement_coin not in old_coins:
logger.info(f'New annoucement detected: {announcement_coin}')
if supported_currencies is not False:
if announcement_coin in supported_currencies:
logger.debug("Starting get_last_price")
price = get_last_price(announcement_coin, pairing)
logger.debug('Coin price: ' + price)
logger.debug('Finished get_last_price')
try:
# Run a test trade if true
if config['TRADE_OPTIONS']['TEST']:
order[announcement_coin] = {
'symbol': announcement_coin,
'price': price,
'volume': qty,
'time': datetime.timestamp(datetime.now()),
'tp': tp,
'sl': sl,
'id': 'test-order',
'text': 'test-order',
'create_time': datetime.timestamp(datetime.now()),
'update_time': datetime.timestamp(datetime.now()),
'currency_pair': f'{announcement_coin}_{pairing}',
'status': 'filled',
'type': 'limit',
'account': 'spot',
'side': 'buy',
'iceberg': '0'
}
logger.info('PLACING TEST ORDER')
logger.debug(order[announcement_coin])
# place a live order if False
else:
logger.info("starting buy place_order with : ",announcement_coin, pairing, qty,'buy', price)
order[announcement_coin] = place_order(announcement_coin, pairing, qty,'buy', price)
order[announcement_coin]['tp'] = tp
order[announcement_coin]['sl'] = sl
logger.info("Finished buy place_order")
except Exception as e:
logger.error(e)
else:
logger.info(f"Order created with {qty} on {announcement_coin}")
store_order('order.json', order)
else:
logger.warning(f"Coin " + announcement_coin + " is not supported on gate io")
os.remove("new_listing.json")
logger.debug('Removed new_listing.json due to coin not being '
'listed on gate io')
else:
get_all_currencies()
else:
logger.info(
"No coins announced, or coin has already been bought/sold. Checking more frequently in case TP and SL need updating")
time.sleep(3)
# except Exception as e:
# print(e)
if __name__ == '__main__':
logger.info('working...')
main()