def main( pair: str, depth: int, theta: float, window_size: int, sleep_seconds: int, position_size: float, ): """ Load and log price information from kraken """ sobi_strategy = SobiStrategy(window_size=window_size, theta=theta, depth=depth, position_size=position_size) backtester = Backtest() data_center = DataCenter(pair=pair) while True: # query latest data from exchange market_state = data_center.get_market_data() # update indicators and signals sobi_strategy.update_market_state(current_state=market_state) desired_position = sobi_strategy.desired_position # create orders and calculate pnl backtester.update_market_state(market_state) backtester.rebalance_position(desired_position) pnl = backtester.get_current_profit() last_order = backtester.get_last_order() # log output to console log_info = dict( time_rfc=market_state["time"], midprice=market_state["order_book"].midprice, best_bid=market_state["order_book"].best_bid, best_ask=market_state["order_book"].best_ask, **sobi_strategy.indicators, **sobi_strategy.signals, last_order=last_order, pnl=pnl, ) log_msg = ut.get_log_msg(log_info) logging.info(log_msg) # conform to krakens call rate limit time.sleep(sleep_seconds)
def run_iteration(pair: str, strategy: Strategy, engine: Backtest, data_center: DataCenter): """ Run one round trip --> Gather and updat data --> Recalculate strategy indicators and signals --> rebalance position if necessary """ # query latest data from exchange market_state = data_center.get_market_data() # update indicators and signals strategy.update_market_state(current_state=market_state) desired_position = strategy.desired_position # create orders and calculate pnl engine.update_market_state(market_state) engine.rebalance_position(desired_position) pnl = engine.get_current_profit() last_order = engine.get_last_order() # log output to console log_info = dict( time_rfc=market_state["time"], midprice=market_state["order_book"].midprice, best_bid=market_state["order_book"].best_bid, best_ask=market_state["order_book"].best_ask, **strategy.indicators, current_signal=strategy.trade_signal, last_order=last_order, pnl=pnl, ) log_msg = ut.get_log_msg(log_info) logging.info(log_msg) # conform to krakens call rate limit time.sleep(strategy.sleep_seconds)
def main( payload: dict, window_size: int, adx_threshold: int, sleep_seconds: int, position_size: float, ): """ Load and log price information from kraken """ trend_strategy = TrendStrategy(window_size=window_size, adx_threshold=adx_threshold) backtester = Backtest() while True: # krakens server time server_time_rfc, server_time_unix = kraken.get_server_time() # last 720 open high low close periods ohlc: np.array = kraken.get_ohlc(payload, interval=1) _, bids, asks = kraken.get_orderbook(payload) best_bid, best_ask, midprice = ut.calc_midprice(bids, asks) market_state = dict( time=server_time_unix, ohlc=ohlc, best_bid=best_bid, best_ask=best_ask, midprice=midprice, ) # check if all data is available -> if not, continue iterations # do stuff trend_strategy.update_market_state(market_state) trend_strategy.update_indicators() trend_strategy.update_signals() indicators = trend_strategy.get_indicators() signals = trend_strategy.get_signals() # order routing desired_position = signals["current"] * position_size backtester.update_market_state(market_state) backtester.rebalance_position(desired_position) pnl = backtester.get_current_profit() last_order = backtester.get_last_order() current_state = dict( time_rfc=server_time_rfc, ohlc=market_state["ohlc"][-1], best_bid=best_bid, best_ask=best_ask, **indicators, **signals, last_order=last_order, pnl=pnl, ) # log output to console log_msg = ut.get_log_msg(current_state) logging.info(log_msg) # conform to krakens call rate limit time.sleep(sleep_seconds)