def connect(): logger = logging.getLogger('client') logger.setLevel('ERROR') print("Setup was successful.") e = Exchange() if not e.is_connected(): e.connect() print('connected') else: print('already connected') return e
logging.info(exchange.connect()) logging.info("Setup was successful.") try: # we can run multiple strategies at once, but really having separate bots to do this would be simpler # since currently we can't ignore our own orders in aggregate data such as the pricebook # strats = [dual_market_strat.DualMarketStrat(exchange, ["PHILIPS_A", "PHILIPS_B"])] # strats = [safe_dual_strat.SafeDualStrat(exchange, ["PHILIPS_A", "PHILIPS_B"])] # strats = [difference_strat.DifferenceStrat(exchange, ["PHILIPS_A", "PHILIPS_B"])] #strats = [dual_paper_strat.DualPaperStrat(exchange, ["PHILIPS_A", "PHILIPS_B"])] strats = [dual_hedge_strat.DualHedgeStrat(exchange, "PHILIPS_A", "PHILIPS_B")] while True: time.sleep(0.2) for s in strats: try: s.update(); except AssertionError as e: # if get disconnected than do something else # Try to reconnect while not exchange.is_connected(): logger.info("disconnected, trying to reconnect") time.sleep(15) exchange.connect() # Once reconnected, make strategy that experienced disconnection do cleanup s.get_out_of_positions(300) continue # logger.info(exchange.get_cash()) except KeyboardInterrupt as e: print(e) strats[0].get_out_of_positions() #raise #sys.exit(0)
trades_sum[t.instrument_id][0] += sgn_cash trades_sum[t.instrument_id][1] += sgn_vol for instr, net in trades_sum.items(): if (net != [0,0]): print(timestamp() + f"{'BOUGHT' if net[1]>0 else 'SOLD '} {abs(net[1])} lots of {'PHA' if instr==pha else 'PHB'} for {-(net[0]/net[1]):8.2f}") # this function could also export statistics and data for further analysis if it were nexessary last_trades_update = time.time() last_position = (0, 0) done = False while not done: try: while not e.is_connected(): print(timestamp() + 'disconnected, trying to reconnect...') time.sleep(1) e.connect() positions = e.get_positions() position = (positions.get(pha), positions.get(phb)) if position != last_position: last_position = position trades = e.poll_new_trades(pha) + e.poll_new_trades(phb) if trades: summarize_trades(trades) print(timestamp() + f"==> POSITION: PHA: {position[0]}, PHB: {position[1]}") books = e.get_last_price_book(pha), e.get_last_price_book(phb)