def get_orderbook_cursor(cls, mm1: str, mm2: str, coin_name: str, start_time: int, end_time: int): db_client = SharedMongoClient.instance() mm1_col = db_client[mm1][coin_name + "_orderbook"] mm2_col = db_client[mm2][coin_name + "_orderbook"] return SharedMongoClient.get_data_from_db(mm1_col, mm2_col, start_time, end_time)
def get_past_mtcu_spread_info_init_mode(self, anal_start_time: int, anal_end_time: int): """mtcu: Min Tradable Coin Unit """ logging.warning("Now analyzing past spread infos..plz wait!!\n") # get OTC from determined combination otc_result_dict = self.get_otc_result_init_mode(anal_start_time, anal_end_time) # get mm1, mm2 collection by target_currency mm1_col = getattr(SharedMongoClient, "get_%s_db" % self.mm1_name)()[self.target_currency + "_orderbook"] mm2_col = getattr(SharedMongoClient, "get_%s_db" % self.mm2_name)()[self.target_currency + "_orderbook"] # loop through sliced_oppty_dur and launch backtesting for trade_type in ["new", "rev"]: for sliced_time_list in otc_result_dict[trade_type]: start_time = sliced_time_list[0] end_time = sliced_time_list[1] mm1_cursor, mm2_cursor = SharedMongoClient.get_data_from_db(mm1_col, mm2_col, start_time, end_time) for mm1_data, mm2_data in zip(mm1_cursor, mm2_cursor): spread_info_dict = MCTSAnalyzer.min_coin_tradable_spread_strategy( mm1_data, mm2_data, self.mm1.taker_fee, self.mm2.taker_fee, self.streamer_min_trading_coin) target_spread_info = spread_info_dict[trade_type] if (target_spread_info.able_to_trade is False) or (target_spread_info.spread_to_trade < 0): continue self.rec_instance.spread_dict["init"][trade_type].append({ "spread_to_trade": target_spread_info.spread_to_trade, "sell_amt": target_spread_info.sell_order_amt, "buy_amt": target_spread_info.buy_order_amt}) return
def main(target_currency: str, mm1_name: str, mm2_name: str, st_local: str, et_local: str): Global.configure_default_root_logging(should_log_to_file=False) SharedMongoClient.initialize(should_use_localhost_db=False) start_time = Global.convert_local_datetime_to_epoch(st_local, timezone="kr") end_time = Global.convert_local_datetime_to_epoch(et_local, timezone="kr") mm1_taker_fee = Global.read_market_fee(mm1_name, True) mm1_maker_fee = Global.read_market_fee(mm1_name, False) mm2_taker_fee = Global.read_market_fee(mm2_name, True) mm2_maker_fee = Global.read_market_fee(mm2_name, False) mm1_market = getattr(Market, "VIRTUAL_%s" % mm1_name.upper()) mm2_market = getattr(Market, "VIRTUAL_%s" % mm2_name.upper()) bal_setting = { 'mm1': { 'krw_balance': 0, 'coin_balance': 0.04475666666666667 }, 'mm2': { 'krw_balance': 333333.3333333333, 'coin_balance': 0.0 } } initial_setting_dict = { 'max_trading_coin': 0.005, 'min_trading_coin': 0, 'new': { 'threshold': 0, 'factor': 1 }, 'rev': { 'threshold': 0, 'factor': 1 } } mm1 = VirtualMarketManager(mm1_market, mm1_taker_fee, mm1_maker_fee, bal_setting["mm1"]["krw_balance"], bal_setting["mm1"]["coin_balance"], target_currency, True) mm2 = VirtualMarketManager(mm2_market, mm2_taker_fee, mm2_maker_fee, bal_setting["mm2"]["krw_balance"], bal_setting["mm2"]["coin_balance"], target_currency, True) mm1_col = SharedMongoClient.get_target_col(mm1_market, target_currency) mm2_col = SharedMongoClient.get_target_col(mm2_market, target_currency) mm1_data_cursor, mm2_data_cursor = SharedMongoClient.get_data_from_db( mm1_col, mm2_col, start_time, end_time) RfabBacktester(mm1, mm2, target_currency).run(mm1_data_cursor, mm2_data_cursor, initial_setting_dict, is_running_in_optimizer=False)
def get_history_data(settings: dict): target_currency = settings["target_currency"] mm1_col = SharedMongoClient.get_target_db( settings["mm1"]["market_tag"])[target_currency + "_orderbook"] mm2_col = SharedMongoClient.get_target_db( settings["mm2"]["market_tag"])[target_currency + "_orderbook"] return SharedMongoClient.get_data_from_db(mm1_col, mm2_col, settings["start_time"], settings["end_time"])
def initiate_market_mongo_settings(settings: dict): target_currency = settings["target_currency"] mm1 = VirtualMarketManager(settings["mm1"]["market_tag"], settings["mm1"]["min_trading_coin"], settings["mm1"]["krw_balance"], settings["mm1"]["coin_balance"], target_currency) mm2 = VirtualMarketManager(settings["mm2"]["market_tag"], settings["mm2"]["min_trading_coin"], settings["mm2"]["krw_balance"], settings["mm2"]["coin_balance"], target_currency) mm1_col = SharedMongoClient.get_target_col(settings["mm1"]["market_tag"], target_currency) mm2_col = SharedMongoClient.get_target_col(settings["mm2"]["market_tag"], target_currency) mm1_data_cursor, mm2_data_cursor = SharedMongoClient.get_data_from_db(mm1_col, mm2_col, settings["start_time"], settings["end_time"]) return mm1, mm2, mm1_data_cursor, mm2_data_cursor
def calc_krw_coin_exchange_ratio_during_oppty_dur(settings: dict): mm1_col = SharedMongoClient.get_target_col( settings["mm1"]["market_tag"], settings["target_currency"]) mm2_col = SharedMongoClient.get_target_col( settings["mm2"]["market_tag"], settings["target_currency"]) mm1_cursor, mm2_cursor = SharedMongoClient.get_data_from_db( mm1_col, mm2_col, settings["start_time"], settings["end_time"]) # get average mid exchange krw price in terms of unit coin during designated time_dur mid_price_list = [] for mm1_data, mm2_data in zip(mm1_cursor, mm2_cursor): mm1_mid_price, _, _, = BasicAnalyzer.get_orderbook_mid_price( mm1_data) mm2_mid_price, _, _, = BasicAnalyzer.get_orderbook_mid_price( mm2_data) combined_mid_price = (mm1_mid_price + mm2_mid_price) / 2 mid_price_list.append(combined_mid_price) return sum(mid_price_list) / len(mid_price_list)