forked from wolfinch/wolfinch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWolfinch.py
executable file
·386 lines (330 loc) · 12.3 KB
/
Wolfinch.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
#! /usr/bin/env python3
'''
# Wolfinch Auto trading Bot
# Desc: Main File implements Bot
# Copyright: (c) 2017-2020 Joshith Rayaroth Koderi
# This file is part of Wolfinch.
#
# Wolfinch is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Wolfinch is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Wolfinch. If not, see <https://www.gnu.org/licenses/>.
'''
import time
import sys
# import os
import traceback
import argparse
from decimal import getcontext
import random
# import logging
from utils import getLogger, get_product_config, load_config, get_config
import sims
import exchanges
from market import market_init, market_setup, get_market_list, \
feed_Q_process_msg, feed_deQ, get_market_by_product
import db
import stats
import ui
from ui import ui_conn_pipe
# mpl_logger = logging.getLogger('matplotlib')
# mpl_logger.setLevel(logging.WARNING)
log = getLogger('Wolfinch')
log.setLevel(log.INFO)
gRestart = False
# global Variables
MAIN_TICK_DELAY = 0.500 # 500 milli
def Wolfinch_init():
# seed random
random.seed()
# 1. Retrieve states back from Db
# db.init_order_db(Order)
# setup ui if required
if ui.integrated_ui:
ui.ui_conn_pipe = ui.ui_mp_init(ui.port)
if ui.ui_conn_pipe is None:
log.critical("unable to setup ui!! ")
print("unable to setup UI!!")
sys.exit(1)
# 2. Init Exchanges
exchanges.init_exchanges(get_config())
# 3. Init markets
market_init(exchanges.exchange_list, get_product_config)
# 4. Setup markets
market_setup(restart=gRestart)
# 5. start stats thread
stats.start()
def Wolfinch_end():
log.info("Finalizing Wolfinch")
exchanges.close_exchanges()
# stop stats thread
log.info("waiting to stop stats thread")
stats.stop()
ui.ui_mp_end()
log.info("all cleanup done.")
def wolfinch_main():
"""
Main Function for Wolfinch
"""
feed_deQ_fn = feed_deQ
feed_Q_process_msg_fn = feed_Q_process_msg
integrated_ui = ui.integrated_ui
ui_conn_pipe = ui.ui_conn_pipe
sleep_time = MAIN_TICK_DELAY
while True:
cur_time = time.time()
# log.critical("Current(%d)Sleep time left:%s"%(cur_time, str(sleep_time)))
# check for the msg in the feed Q and process, with timeout
msg = feed_deQ_fn(sleep_time)
# log.critical("Current(%d)"%(time.time()))
while msg is not None:
feed_Q_process_msg_fn(msg)
msg = feed_deQ_fn(0)
if integrated_ui == True:
process_ui_msgs(ui_conn_pipe)
for market in get_market_list():
process_market(market)
# '''Make sure each iteration take exactly LOOP_DELAY time'''
sleep_time = (MAIN_TICK_DELAY -(time.time()- cur_time))
# if sleep_time < 0 :
# log.critical("******* TIMING SKEWED(%f)******"%(sleep_time))
sleep_time = 0 if sleep_time < 0 else sleep_time
# end While(true)
def process_market(market):
# """
# processing routine for one exchange
# """
log.debug("processing Market: exchange(%s)product: %s" %(market.exchange_name, market.name))
# update various market states on tick
market.update_market_states()
# Trade only on primary markets
if market.new_candle is True:
if market.primary is True:
signal, sl, tp = market.generate_trade_signal()
market.consume_trade_signal(signal, sl, tp)
if sims.simulator_on:
sims.market_simulator_run(market, sims.backtesting_on)
stats.stats_update_order_bulk(market)
# check pending trades periodically and takes actions(this logic is rate-limited)
market.watch_pending_orders()
# commit market states to the db periodically(this logic is rate-limited)
market.lazy_commit_market_states()
def process_ui_trade_notif(msg):
exch = msg.get("exchange")
product = msg.get("product")
side = msg.get("side")
signal = msg.get("signal")
m = get_market_by_product(exch, product)
if not m:
log.error("Unknown exchange/product exch: %s prod: %s" %(exch, product))
else:
log.info("Manual Trade Req: exch: %s prod: %s side: %s signal: %s" %(
exch, product, side, str(signal)))
m.consume_trade_signal(signal)
def process_ui_pause_trading_notif(msg):
exch = msg.get("exchange")
product = msg.get("product")
buy_pause = msg.get("buy_pause")
sell_pause = msg.get("sell_pause")
m = get_market_by_product(exch, product)
if not m:
log.error("Unknown exchange/product exch: %s prod: %s" %(exch, product))
else:
log.info("pause trading on exch: %s prod: %s" %(exch, product))
m.pause_trading(buy_pause, sell_pause)
def process_ui_get_markets_rr(msg, ui_conn_pipe):
log.debug("enter")
m_dict = {}
for m in get_market_list():
p_list = m_dict.get(m.exchange_name)
if not p_list:
m_dict[m.exchange_name] = [{"product_id": m.product_id,
"buy_paused": m.trading_paused_buy,
"sell_paused": m.trading_paused_sell}]
else:
p_list.append({"product_id": m.product_id,
"buy_paused": m.trading_paused_buy,
"sell_paused": m.trading_paused_sell})
msg["type"] = "GET_MARKETS_RESP"
msg["data"] = m_dict
ui_conn_pipe.send(msg)
def process_ui_get_market_indicators_rr(msg, ui_conn_pipe):
log.debug("enter")
exch = msg.get("exchange")
product = msg.get("product")
num_periods = msg.get("periods", 0)
start_time = msg.get("start_time", 0)
market = get_market_by_product(exch, product)
ind_list = {}
if market:
ind_list = market.get_indicator_list(num_periods, start_time)
msg["type"] = "GET_MARKET_INDICATORS_RESP"
msg["data"] = ind_list
ui_conn_pipe.send(msg)
def process_ui_get_positions_rr(msg, ui_conn_pipe):
log.debug("enter")
exch = msg.get("exchange")
product = msg.get("product")
start_time = msg.get("start_time", 0)
end_time = msg.get("end_time", 0)
market = get_market_by_product(exch, product)
pos_list = {}
if market:
log.info ("get positions ")
pos_list = market.get_positions_list(start_time, end_time)
msg["type"] = "GET_MARKET_POSITIONS_RESP"
msg["data"] = pos_list
ui_conn_pipe.send(msg)
def process_ui_msgs(ui_conn_pipe):
try:
while ui_conn_pipe.poll():
msg = ui_conn_pipe.recv()
err = msg.get("error", None)
if err is not None:
log.error("error in the pipe, ui finished: msg:%s" %(err))
raise Exception("UI error - %s" %(err))
else:
log.info ("ui_msg: %s"%(msg))
msg_type = msg.get("type")
if msg_type == "TRADE":
process_ui_trade_notif(msg)
elif msg_type == "GET_MARKETS":
process_ui_get_markets_rr(msg, ui_conn_pipe)
elif msg_type == "GET_MARKET_INDICATORS":
process_ui_get_market_indicators_rr(msg, ui_conn_pipe)
elif msg_type == "GET_MARKET_POSITIONS":
process_ui_get_positions_rr(msg, ui_conn_pipe)
elif msg_type == "PAUSE_TRADING":
process_ui_pause_trading_notif(msg)
else:
log.error("Unknown ui msg type: %s", msg_type)
except Exception as e:
log.critical("exception %s on ui" %(str(e)))
raise e
def clean_states():
'''
clean states
'''
log.info("Clearing Db")
db.clear_db()
stats.clear_stats()
def arg_parse():
'''
arg parse
'''
global gRestart
parser = argparse.ArgumentParser(description='Wolfinch Auto Trading Bot')
parser.add_argument('--version', action='version', version='%(prog)s 1.0.1')
parser.add_argument("--clean",
help='Clean states,dbs and exit. Clear all the existing states',
action='store_true')
parser.add_argument("--config", help='Wolfinch Global config file')
parser.add_argument("--backtesting", help='do backtesting', action='store_true')
parser.add_argument("--import_only", help='do import only and exit ', action='store_true')
parser.add_argument("--restart", help='restart from the previous state', action='store_true')
parser.add_argument("--ga_restart", help='restart genetic analysis from previous state',
action='store_true')
args = parser.parse_args()
if args.clean:
clean_states()
exit(0)
if args.config:
log.debug("config file: %s" % (str(args.config)))
if False == load_config(args.config):
log.critical("Config parse error!!")
parser.print_help()
exit(1)
else:
log.debug("config loaded successfully!")
# exit(0)
else:
parser.print_help()
exit(1)
if args.import_only:
log.debug("import_only enabled")
sims.import_only = True
sims.genetic_optimizer_on = False
sims.backtesting_on = False
else:
log.debug("import_only disabled")
sims.import_only = False
if args.restart:
log.debug("restart enabled")
print("Restarting from previous state")
gRestart = True
else:
log.debug("restart disabled")
gRestart = False
if args.ga_restart:
log.debug("ga_restart enabled")
sims.ga_restart = True
else:
log.debug("import_only disabled")
sims.ga_restart = False
if args.backtesting:
log.debug("backtesting enabled")
sims.backtesting_on = True
# sims.simulator_on = True
# else:
# log.debug("backtesting disabled")
# sims.backtesting_on = False
# log.debug("sims.backtesting_on: %d"%(sims.backtesting_on))
# exit(1)
######### ******** MAIN ****** #########
if __name__ == '__main__':
'''
main entry point
'''
arg_parse()
getcontext().prec = 8 # decimal precision
print("Starting Wolfinch Trading Bot..")
try:
if sims.genetic_optimizer_on:
print("starting genetic backtesting optimizer")
sims.ga_sim_main(get_config(), get_product_config)
print("finished running genetic backtesting optimizer")
sys.exit()
Wolfinch_init()
if sims.import_only:
log.info("import only")
raise SystemExit
if sims.backtesting_on:
sims.market_backtesting_run(sims.simulator_on)
if ui.integrated_ui:
wolfinch_main()
else:
raise SystemExit
else:
#slow down a little bit. wait to get to a whole minute boundary, we might get some initial trades wrong here. that's ok
# this initial delay will help us to get cleaner candles when we are operational
# log.debug("waiting to start wolfinch main")
# wait = int(60 - time.time()%60)
# while (wait):
# print ("starting main in %s seconds.."%(str(wait)))
# # log.info ("starting main in %d seconds.."%(wait))
# wait -= 1
# time.sleep(1)
log.info("Starting Main forever loop")
print("Starting Main forever loop")
wolfinch_main()
except(KeyboardInterrupt, SystemExit):
Wolfinch_end()
sys.exit()
except Exception as e:
log.critical("Unexpected error: exception: %s" %(traceback.format_exc()))
print("Unexpected error: exception: %s" %(traceback.format_exc()))
Wolfinch_end()
raise
# traceback.print_exc()
# os.abort()
# '''Not supposed to reach here'''
print("\nWolfinch end")
# EOF