def _scanner(): scanner.update(25, idx_filter='BTC') scan = Timer(name='scanner', expire='every 30 clock min utc') while True: if scan.remain() == 0: scanner.update(25, idx_filter='BTC') scan.reset() time.sleep(1800)
def _data(now=False): """ """ cmc = Timer(name='cmc', expire='every 5 clock min utc') if now == True: tickers.update(limit=500) while True: if cmc.remain() == 0: tickers.update(limit=500) print('Updated CMC tickers') cmc.reset() print("cmc: {:} sec remain".format(cmc.remain(unit='s'))) time.sleep(cmc.remain()/1000)
def _trading(): """Main trade cycle loop. """ print('Preloading historic data....') trade.init() #timer_1m = Timer(name='trade_1m', expire='every 1 clock min utc') timer_5m = Timer(name='trade_5m', expire='every 5 clock min utc') while True: if timer_5m.remain() == 0: time.sleep(10) trade.update('5m') timer_5m.reset() """if timer_1m.remain() == 0: time.sleep(8) trade.update('1m') timer_1m.reset() """ time.sleep(5)
def run(e_pairs, e_kill): """Main scanner thread loop. """ tmr = Timer(expire='every 20 clock minutes utc', quiet=True) sma_med_trend_filter() while True: if e_kill.isSet(): break if tmr.remain() == 0: # Edit conf w/o having to restart bot. importlib.reload(docs.botconf) lock.acquire() print("{} pairs enabled pre-scan.".format(len(get_pairs()))) lock.release() # Reset enabled pairs to only open trades. set_pairs([],'ENABLED', exclusively=True) # Scan and enable any additional filtered pairs. sma_med_trend_filter() tmr.reset() time.sleep(3) print("Scanner thread: terminating...")
def run(e_pairs, e_kill): global storedata, connkeys, ws client = app.bot.client #print("Connecting to websocket...") ws = BinanceSocketManager(client) pairs = get_pairs() connkeys += [ws.start_kline_socket(pair, recv_kline, interval=n) \ for n in TRD_FREQS for pair in pairs] lock.acquire() print("Subscribed to {} kline sockets.".format(len(connkeys))) lock.release() ws.start() #print('Connected. Press Ctrl+C to quit') tmr = Timer(name='pairs', expire='every 5 clock min utc', quiet=True) while True: if e_kill.isSet(): break if e_pairs.isSet(): update_sockets() e_pairs.clear() if tmr.remain() == 0: tmr.reset() if len(storedata) > 0: #print("websock_thread: saving new candles...") candles.bulk_save(storedata) storedata = [] time.sleep(1) close_all() print("Websock thread: Terminating...")
#--------------------------------------------------------------------------- if __name__ == '__main__': db = set_db('localhost') cred = list(db.api_keys.find())[0] killer = GracefulKiller() print("Connecting to Binance websocket client...") client = Client(cred['key'], cred['secret']) bnc_wss = BinanceSocketManager(client) connect_klines(bnc_wss, pairs) print("{} connections created.".format(len(conn_keys))) bnc_wss.start() print('Connected.') print('Press Ctrl+C to quit') timer_1m = Timer(name='pairs', expire='every 1 clock min utc') while True: if timer_1m.remain(quiet=True) == 0: pairs = detect_pair_change() timer_1m.reset(quiet=True) if killer.kill_now: print('Caught SIGINT command. Shutting down...') break update_spinner() time.sleep(0.1) close_all()
def run(e_pairs, e_kill): """Main trading loop thread. Consumes candle data from queue and manages/executes trades. TODO: add in code for tracking unclosed candle wicks prices: # Clear all partial candle data dfW = dfW.drop([(c['pair'], strtofreq(c['freqstr']))]) """ from main import q db = app.get_db() t1 = Timer() tmr1 = Timer(name='pos', expire='every 1 clock min utc', quiet=True) tmr10 = Timer(name='earn', expire='every 10 clock min utc', quiet=True) reports.positions() reports.earnings() n = 0 while True: if e_kill.isSet(): break ent_ids, ex_ids = [], [] # Trading algo inner loop. while q.empty() == False: c = q.get() candles.modify_dfc(c) ss = snapshot(c) query = { 'pair': c['pair'], 'freqstr': c['freqstr'], 'status': 'open' } # Eval position entries/exits for trade in db.trades.find(query): update_stats(trade, ss) ex_ids += eval_exit(trade, c, ss) if c['closed'] and c['pair'] in get_pairs(): ent_ids += eval_entry(c, ss) n += 1 # Reporting outer loop. if tmr1.remain() == 0: reports.positions() tmr1.reset() if tmr10.remain() == 0: reports.earnings() tmr10.reset() if len(ent_ids) + len(ex_ids) > 0: reports.trades(ent_ids + ex_ids) if n > 75: lock.acquire() print('{} queue items processed. [{:,.0f} ms/item]'\ .format(n, t1.elapsed()/n)) lock.release() t1.reset() n = 0 # Outer loop tail if len(ex_ids) > 0: if c['pair'] not in get_pairs(): # TODO: check no other open positions hold this pair, safe # for disabling. set_pairs([c['pair']], 'DISABLED') update_spinner() time.sleep(0.1) print('Trade thread: Terminating...')