from threading import Thread # Use README for more detail on strategy and functions e = Exchange() a = e.connect() TIME_PERIOD = 0.13 TIME_RUNNING = 1200 TRADES = TIME_RUNNING / TIME_PERIOD ORDER_VOLUME = 24 WEIGHTING_FACTOR = 0.1 / ORDER_VOLUME VOLUME_WEIGHTING = 1 / 2 INSTRUMENT = "PHILIPS_B" e.poll_new_trades(INSTRUMENT) diagonosticsOutput = "" firstTrader = Trader(exchange=e, instrument=INSTRUMENT, instrumentB="PHILIPS_A", orderVolume=ORDER_VOLUME, weightingFactor=WEIGHTING_FACTOR, volumeWeighting=VOLUME_WEIGHTING) executing = True count = 0 while executing: firstTrader.trade() firstTrader.hedge()
# import matplotlib as plt e = Exchange() a = e.connect() # pnl = [] TIME_PERIOD = 0.2 TIME_RUNNING = 300 TRADES = TIME_RUNNING / TIME_PERIOD ORDER_VOLUME = 5 WEIGHTING_FACTOR = 0.1 / ORDER_VOLUME #0.001 VOLUME_WEIGHTING = 1 / 2 INSTRUMENT = "PHILIPS_B" e.poll_new_trades(INSTRUMENT) diagonosticsOutput = "" firstTrader = Trader(exchange=e, instrument=INSTRUMENT, instrumentB="PHILIPS_A", orderVolume=ORDER_VOLUME, weightingFactor=WEIGHTING_FACTOR, volumeWeighting=VOLUME_WEIGHTING) executing = True count = 0 while executing: order_book = e.get_last_price_book(instrument_id="PHILIPS_A") # pnl.append(e.get_pnl) bidAskDict = firstTrader.trade()
a = e.connect() # you can also define host/user/pass yourself # when not defined, it is taken from ~/.optibook file if it exists # if that file does not exists, an error is thrown #e = Exchange(host='host-to-connect-to') #a = e.connect(username='******', password='******') # Returns all currently outstanding orders orders = e.get_outstanding_orders(instrument_id) for o in orders.values(): print('outstanding orders: ' + o) # Returns all trades you have done since the last time this function was called trades = e.poll_new_trades(instrument_id) for t in trades: print( f"[TRADED {t.instrument_id}] price({t.price}), volume({t.volume}), side({t.side})" ) # Returns all trades you have done since since the instantiation of the Exchange trades = e.get_trade_history(instrument_id) for t in trades: print( f"[TRADED {t.instrument_id}] price({t.price}), volume({t.volume}), side({t.side})" ) # Returns all current positions positions = e.get_positions() for p in positions:
# 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) try_to_trade(0, books, position) # buy PHA, sell PHB try_to_trade(1, books, position) # buy PHB, sell PHA except KeyboardInterrupt: done = True except Exception as exc: print(timestamp() + "error: " + str(exc))