Ejemplo n.º 1
0
    def iyo_result_to_mongo_db(coin_name: str, start_time: int, end_time: int):
        Global.configure_default_root_logging(should_log_to_file=False,
                                              log_level=logging.CRITICAL)
        SharedMongoClient.initialize(should_use_localhost_db=True)
        db_client = SharedMongoClient.instance()

        # convert epoch time to local_time and log
        local_st = Global.convert_epoch_to_local_datetime(start_time,
                                                          timezone="kr")
        local_et = Global.convert_epoch_to_local_datetime(end_time,
                                                          timezone="kr")

        # create combination of coin that is injected by validating if the exchange has that coin
        rfab_combi_list = Global.get_rfab_combination_tuples(coin_name)

        for _combi in rfab_combi_list:
            logging.critical(
                "[%s-%s-%s] IYO conducting -> start_time: %s, end_time: %s" %
                (coin_name.upper(), str(_combi[0]).upper(), str(
                    _combi[1]).upper(), local_st, local_et))

            # draw iyo_config for bal & factor_setting
            iyo_config = Global.read_iyo_setting_config(coin_name)

            settings = TradeSettingConfig.get_settings(
                mm1_name=_combi[0],
                mm2_name=_combi[1],
                target_currency=coin_name,
                start_time=start_time,
                end_time=end_time,
                division=iyo_config["division"],
                depth=iyo_config["depth"],
                consecution_time=iyo_config["consecution_time"],
                is_virtual_mm=True)
            # todo
            bal_factor_settings = TradeSettingConfig.get_bal_fact_settings(
                iyo_config["krw_seq_end"], iyo_config["coin_seq_end"])

            factor_settings = TradeSettingConfig.get_factor_settings(
                _combi[0], _combi[1], coin_name,
                iyo_config["max_trade_coin_end"], iyo_config["threshold_end"],
                iyo_config["appx_unit_coin_price"])

            try:
                iyo_result = IntegratedYieldOptimizer.run(
                    settings, bal_factor_settings, factor_settings)

                # finally save to mongoDB
                if len(iyo_result) > 0:
                    db_client["statistics"]["iyo"].insert_many(iyo_result)
                else:
                    logging.critical(
                        "There was no oppty!! Skipping to next combination!")
                    continue

            except TypeError as e:
                Global.send_to_slack_channel(
                    Global.SLACK_BOT_STATUS_URL,
                    "Something went wrong in IYO Schduler! >> %s" % e)
                pass
    def run_iyo(cls, settings: dict, bal_factor_settings: dict,
                factor_settings: dict, oppty_dur_dict: dict):
        db_result = []
        for trade_type in ["new", "rev"]:
            for time in oppty_dur_dict[trade_type]:
                try:
                    # clone settings, balance factor settings, factor settings with original one
                    settings_clone = copy.deepcopy(settings)
                    bal_fact_set_clone = copy.deepcopy(bal_factor_settings)
                    fact_set_clone = copy.deepcopy(factor_settings)

                    # apply each oppty duration
                    settings_clone["start_time"] = time[0]
                    settings_clone["end_time"] = time[1]

                    # convert to local time
                    st_local = Global.convert_epoch_to_local_datetime(
                        time[0], timezone="kr")
                    et_local = Global.convert_epoch_to_local_datetime(
                        time[1], timezone="kr")
                    logging.error("Now in: [%s] start_time: %s, end_time: %s" %
                                  (trade_type.upper(), st_local, et_local))

                    # initial dry run -> get new, rev oppty count
                    new_oppty_count, rev_oppty_count = super().count_oppty_num(
                        settings_clone, cls.default_initial_setting_dict)

                    # opt initial settings by oppty
                    fact_set_clone = cls.opt_factor_settings_by_oppty(
                        fact_set_clone, new_oppty_count, rev_oppty_count)
                    # opt balance_settings by oppty
                    bal_fact_set_clone = cls.opt_balance_settings_by_oppty(
                        bal_fact_set_clone, new_oppty_count, rev_oppty_count)

                    # create coin balance proportionate current exchange rate
                    bal_fact_set_clone = IBO.create_coin_bal_from_krw_bal_by_exchange_rate(
                        settings_clone, bal_fact_set_clone)

                    # add init step for balance settings and initial settings
                    cls.init_initial_step(settings_clone, bal_fact_set_clone,
                                          fact_set_clone)

                    # run recursive
                    iyo_opt_result = cls.opt_by_bal_and_init_settings_recursive(
                        settings_clone, bal_fact_set_clone, fact_set_clone,
                        settings_clone["depth"])

                    # append original oppty count to final result
                    iyo_opt_result["new_oppty_count"] = new_oppty_count
                    iyo_opt_result["rev_oppty_count"] = rev_oppty_count

                    db_result.append(iyo_opt_result)

                except Exception as e:
                    logging.error(
                        "Something went wrong while executing IYO loop!", time,
                        e)

        # finally run IYO Stat appender and return final result
        return db_result
    def log_init_mode_mctu_info(self):
        local_anal_st = Global.convert_epoch_to_local_datetime(
            self.ocat_rewind_time, timezone="kr")
        local_anal_et = Global.convert_epoch_to_local_datetime(
            self.streamer_start_time, timezone="kr")

        logging.warning("=========== [MCTU INFO] ==========")
        logging.warning("[Anal Duration]: %s - %s" %
                        (local_anal_st, local_anal_et))

        target_dict = self.rec_instance.spread_dict["init"]
        for trade_type in target_dict.keys():
            logging.warning(
                "['%s' SPREAD RECORDER]:\n%s" %
                (trade_type.upper(),
                 self.get_mctu_spread_and_frequency(target_dict[trade_type])))

        self.th_instance.NEW["normal"] = float(
            input("Decide [NEW] MCTU spread threshold: "))
        self.th_instance.NEW["royal"] = float(
            input("Decide [NEW] MCTU Royal spread: "))
        self.th_instance.REV["normal"] = float(
            input("Decide [REV] MCTU spread threshold: "))
        self.th_instance.REV["royal"] = float(
            input("Decide [REV] MCTU Royal spread: "))
    def update_trade_condition_by_mtcu_analyzer(self):
        """mtcu: Min Tradable Coin Unit
        """

        mm1_rq = Global.convert_epoch_to_local_datetime(self.mm1_ob["requestTime"], timezone="kr")
        mm2_rq = Global.convert_epoch_to_local_datetime(self.mm2_ob["requestTime"], timezone="kr")

        logging.warning("[REQUEST TIME] -- mm1: %s, mm2: %s\n" % (mm1_rq, mm2_rq))

        # analyze by MCTS
        spread_info_dict = MCTSAnalyzer.min_coin_tradable_spread_strategy(self.mm1_ob, self.mm2_ob,
                                                                          self.mm1.taker_fee,
                                                                          self.mm2.taker_fee,
                                                                          self.streamer_min_trading_coin)

        new_cond = spread_info_dict["new"].able_to_trade
        rev_cond = spread_info_dict["rev"].able_to_trade

        logging.warning("========= [OPPTY NOTIFIER] ========")
        # if there is no Oppty,
        if new_cond is False and rev_cond is False:
            self.cond_instance.NEW["is_oppty"] = False
            self.cond_instance.NEW["is_royal"] = False
            self.cond_instance.REV["is_oppty"] = False
            self.cond_instance.REV["is_royal"] = False

            logging.error("[WARNING] There is no oppty.. Waiting")
            logging.error("=> [NEW] Fail reason: %s" % spread_info_dict["new"].fail_reason)
            logging.error("=> [REV] Fail reason: %s\n" % spread_info_dict["rev"].fail_reason)
            return

        # if oppty (NEW or REV)
        for trade_type in spread_info_dict.keys():
            if not spread_info_dict[trade_type].able_to_trade:
                getattr(self.cond_instance, trade_type.upper())["is_oppty"] = False
                getattr(self.cond_instance, trade_type.upper())["is_royal"] = False
                continue

            logging.critical("[HOORAY] [%s] Oppty detected!!! now evaluating spread infos.." % trade_type.upper())
            logging.critical("[SPREAD TO TRADE]: %.4f\n" % spread_info_dict[trade_type].spread_to_trade)

            getattr(self.cond_instance, trade_type.upper())["is_oppty"] = True

            # if gte royal spread,
            if spread_info_dict[trade_type].spread_to_trade >= getattr(self.th_instance, trade_type.upper())["royal"]:
                getattr(self.cond_instance, trade_type.upper())["is_royal"] = True
                logging.critical("[!CONGRAT!] THIS WAS ROYAL SPREAD!! Now command to trade no matter what!! :D")
            else:
                getattr(self.cond_instance, trade_type.upper())["is_royal"] = False

            # get spread_to_trade list from min_trdble_coin_sprd_list
            self.rec_instance.spread_dict["trade"][trade_type].extend(
                [{"spread_to_trade": spread_info_dict[trade_type].spread_to_trade,
                  "sell_amt": spread_info_dict[trade_type].sell_order_amt,
                  "buy_amt": spread_info_dict[trade_type].buy_order_amt}])
Ejemplo n.º 5
0
    def get_oppty_dur_human_time(oppty_dur_dict: dict, timezone: str):
        final_dict = dict()
        for key in ["new", "rev"]:
            result_list = []
            for time_dur in oppty_dur_dict[key]:
                human_st = Global.convert_epoch_to_local_datetime(time_dur[0], timezone=timezone)
                human_et = Global.convert_epoch_to_local_datetime(time_dur[1], timezone=timezone)
                result_list.append([human_st, human_et])
            final_dict[key] = result_list

        return final_dict
    def log_trading_mode_mtcu_info(self, anal_start_time: int, anal_end_time: int):
        local_anal_st = Global.convert_epoch_to_local_datetime(anal_start_time, timezone="kr")
        local_anal_et = Global.convert_epoch_to_local_datetime(anal_end_time, timezone="kr")

        logging.warning("=========== [MCTU INFO] ==========")
        logging.warning("[Anal Duration]: %s - %s" % (local_anal_st, local_anal_et))

        target_dict = self.rec_instance.spread_dict["trade"]
        for trade_type in target_dict.keys():
            logging.warning("\n\n[ '%s' SPREAD RECORDER]:\n%s"
                            % (trade_type.upper(),
                               self.get_mtcu_spread_and_frequency(target_dict[trade_type])))
Ejemplo n.º 7
0
    def launch_oppty_sliced_iyo(self, anal_start_time: int,
                                rewinded_time: int):
        st_local = Global.convert_epoch_to_local_datetime(rewinded_time,
                                                          timezone="kr")
        et_local = Global.convert_epoch_to_local_datetime(anal_start_time,
                                                          timezone="kr")
        logging.critical(
            "[%s-%s-%s] Sliced IYO conducting -> , start_time: %s, end_time: %s"
            % (self.target_currency.upper(), self.mm1_name.upper(),
               self.mm2_name.upper(), st_local, et_local))

        # draw iyo_config for bal & factor_setting
        sliced_iyo_config = Global.read_sliced_iyo_setting_config(
            self.target_currency)
        # set settings, bal_fact_settings, factor_settings
        settings = TradeSettingConfig.get_settings(
            mm1_name=self.mm1_name,
            mm2_name=self.mm2_name,
            target_currency=self.target_currency,
            start_time=rewinded_time,
            end_time=anal_start_time,
            division=sliced_iyo_config["division"],
            depth=sliced_iyo_config["depth"],
            consecution_time=sliced_iyo_config["consecution_time"],
            is_virtual_mm=True)

        bal_factor_settings = TradeSettingConfig.get_bal_fact_settings(
            sliced_iyo_config["krw_seq_end"],
            sliced_iyo_config["coin_seq_end"])

        factor_settings = TradeSettingConfig.get_factor_settings(
            self.mm1_name, self.mm2_name, self.target_currency,
            sliced_iyo_config["max_trade_coin_end"],
            sliced_iyo_config["threshold_end"],
            sliced_iyo_config["appx_unit_coin_price"])
        try:
            slicied_iyo_result = IntegratedYieldOptimizer.run(
                settings,
                bal_factor_settings,
                factor_settings,
                is_stat_appender=False,
                is_slicing_dur=True,
                slicing_interval=sliced_iyo_config["slicing_interval"])
        # FIXME: type error는 커서 에러때문에...
        except IndexError or TypeError:
            slicied_iyo_result = None

        return slicied_iyo_result
Ejemplo n.º 8
0
    def _actual_run_in_loop(self):
        now_date = int(time.time())

        publish_epoch_date_set = self.get_publish_epoch_date_set()

        for publish_epoch_date in publish_epoch_date_set:
            if (now_date >= publish_epoch_date) \
                    and (now_date <= publish_epoch_date + (self.interval_time_sec * 2)):

                start_time = publish_epoch_date - self.time_dur_to_anal
                end_time = publish_epoch_date

                logging.critical("OTC activated start_time: %d" % now_date)
                # loop through all possible coins and run
                final_result = []
                for target_currency in list(Global.get_avail_coin_in_list()):
                    logging.critical("Now conducting %s" %
                                     target_currency.upper())
                    result_by_one_coin = self.otc_all_mm_comb_by_one_coin(
                        target_currency, start_time, end_time)
                    final_result.extend(result_by_one_coin)

                # sort by highest to lowest oppty duration
                descending_order_result = self.sort_by_logest_oppty_time_to_lowest(
                    final_result)
                top_twenty_descend_order_result = descending_order_result[:20]

                # send this final result to slack in form of str
                start_local_date = Global.convert_epoch_to_local_datetime(
                    start_time)
                publish_local_date = Global.convert_epoch_to_local_datetime(
                    publish_epoch_date, timezone="kr")
                self.send_result_nicely_to_slack(
                    top_twenty_descend_order_result, start_local_date,
                    publish_local_date)
            else:
                continue
        pass
    def write_ledger_balance_in_excel(self, target_row: int,
                                      ledger_data: dict):

        # write time
        self.rfab_ws[
            self.LEDGER_COLUMN["time"] +
            str(target_row)].value = Global.convert_epoch_to_local_datetime(
                ledger_data["time"], timezone="kr")

        # write mode_status
        self.rfab_ws[self.LEDGER_COLUMN["mode_status"] +
                     str(target_row)].value = ledger_data["mode_status"]

        # write balance
        for key1 in ["mm1", "mm2", "total"]:
            for key2 in ["krw", "coin"]:
                self.rfab_ws[self.LEDGER_COLUMN[key1][key2] +
                             str(target_row)].value = ledger_data[key1][key2]
import logging
from config.global_conf import Global
from config.shared_mongo_client import SharedMongoClient
from optimizer.integrated_yield_optimizer import IYOStatAppender

Global.configure_default_root_logging(should_log_to_file=False, log_level=logging.INFO)
SharedMongoClient.initialize(should_use_localhost_db=True)

start_time = Global.convert_local_datetime_to_epoch("2018.07.24 11:00:00", timezone="kr")
end_time = Global.convert_local_datetime_to_epoch("2018.07.24 12:00:00", timezone="kr")

db_client = SharedMongoClient.instance()
iyo_col = db_client["statistics"]["iyo"]
iyo_cur = iyo_col.find({
    "settings.start_time": {
        "$gte": start_time,
        "$lte": end_time}}).sort([("start_time", 1)])

for iyo_data in iyo_cur:
    logging.critical("Now starting: %s" % Global.convert_epoch_to_local_datetime(iyo_data["settings"]["start_time"]))
    stat_result = IYOStatAppender.run(iyo_data=iyo_data)

    # append this stat dict to the current IYO_data
    db_client["statistics"]["iyo"].update(
        {'_id': iyo_data['_id']}, {
            '$set': {
                'stat': stat_result
            }}, upsert=False, multi=False)

    logging.critical("Appended Stat result to target IYO_data at MongoDB")