Example #1
0
def init() -> None:
    """
    初始化全局变量
    """
    cta_data.update(load_json(CTA_DATA_FILENAME))
    cta_settings.update(load_json(CTA_SETTING_FILENAME))
    follow_data.update(load_json(FOLLOW_DATA_FILENAME))
    strategy_to_class_name_map.update(get_strategy_to_class_name())

    temp_map = {
        Content.CTA_SETTING: {
            "filename": CTA_SETTING_FILENAME,
            "create_func": get_cta_setting_fields,
            "to_server_func": cta_setting_to_server,
            "from_server_func": cta_setting_from_server
        },
        Content.CTA_DATA: {
            "filename": CTA_DATA_FILENAME,
            "create_func": get_cta_data_fields,
            "to_server_func": cta_data_to_server,
            "from_server_func": cta_data_from_server
        },
        Content.FOLLOW_DATA: {
            "filename": FOLLOW_DATA_FILENAME,
            "create_func": get_follow_data_fields,
            "to_server_func": follow_data_to_server,
            "from_server_func": follow_data_from_server
        }
    }
    content_map.update(temp_map)
    logger.info("数据初始化完成")
Example #2
0
 def connect(self, *args, **kwargs):
     try:
         self.db = pymysql.connect(*args, **kwargs)
         logger.info("Mysql连接成功")
     except:
         logger.info('Mysql连接失败')
         traceback.print_exc()
Example #3
0
def create_table(mysql_handler: MySqlHandler, content: Content) -> None:
    """
    通过前缀名创建数据库表格
    content: 'setting' or 'data' or 'follow_data'
    """
    create_func = content_map[content]['create_func']
    fields_list = create_func()
    for (table_name, field_dict) in fields_list:
        mysql_handler.create_table(table_name, field_dict)
    logger.info(f"{content.value}:数据表创建成功")
Example #4
0
def clear_server_data(mysql_handler: MySqlHandler, content: Content):
    """
    清除数据库数据
    """
    tables = get_table_list(mysql_handler, content)
    if tables:
        for table in tables:
            mysql_handler.drop_table(table)
        logger.info(f"{content.value}所有数据表删除成功")
    else:
        logger.info(f"{content.value}没有数据表")
Example #5
0
def follow_data_to_server(mysql_handler: MySqlHandler) -> None:
    """
    推送跟随数据到数据库
    """
    modified_time = get_file_modified_time(FOLLOW_DATA_FILENAME)

    # sync trade ids
    id_table_name = 'follow_data_trade_ids'
    mysql_handler.delete_all(id_table_name)
    trade_ids = follow_data['tradeid_orderids_dict']

    if not trade_ids:
        row = dict()
        row['follow_id'] = 'empty'
        row['order_id'] = 'empty'
        row['last_modified_time'] = modified_time
        mysql_handler.insert(id_table_name, row)
    else:
        for trade_id, order_list in trade_ids.items():
            row = dict()
            row['follow_id'] = trade_id
            row['last_modified_time'] = modified_time
            for order in order_list:
                row['order_id'] = order
                mysql_handler.insert(id_table_name, row)
    logger.info(f"跟随单ID:数据同步到远程成功")

    # sync positions
    pos_table_name = 'follow_data_positions'
    mysql_handler.delete_all(pos_table_name)
    positions = follow_data['positions']
    if not positions:
        row = dict()
        row['source_long'] = 0
        row['source_short'] = 0
        row['target_long'] = 0
        row['target_short'] = 0
        row['vt_symbol'] = 'empty'
        row['last_modified_time'] = modified_time
        mysql_handler.insert(pos_table_name, row)
    else:
        for vt_symbol, pos_dict in positions.items():
            row = dict()
            row['vt_symbol'] = vt_symbol
            row['last_modified_time'] = modified_time
            row.update(pos_dict)

            # insert
            mysql_handler.insert(pos_table_name, row)
    logger.info(f"跟随单仓位:数据同步到远程成功")
Example #6
0
def cta_data_to_server(mysql_handler: MySqlHandler) -> None:
    """
    推送本地cta数据到数据库
    """
    modified_time = get_file_modified_time(CTA_DATA_FILENAME)
    for strategy_name, data in cta_data.items():
        row = dict()
        row['strategy_name'] = strategy_name
        row['last_modified_time'] = modified_time
        row.update(data)

        table_name = strategy_to_table_name(strategy_name, Content.CTA_DATA)

        # delete first.
        mysql_handler.delete(table_name, get_cond_dict(strategy_name))
        # insert
        mysql_handler.insert(table_name, row)
        logger.info(f"策略{strategy_name}:数据同步到远程成功")
Example #7
0
def cta_setting_to_server(mysql_handler: MySqlHandler) -> None:
    """
    推送本地cta配置到数据库
    """
    modified_time = get_file_modified_time(CTA_SETTING_FILENAME)
    for strategy_name, settings in cta_settings.items():
        row = dict()
        row['strategy_name'] = strategy_name
        row['class_name'] = settings['class_name']
        row['vt_symbol'] = settings['vt_symbol']
        row['last_modified_time'] = modified_time
        row.update(settings['setting'])

        table_name = strategy_to_table_name(strategy_name, Content.CTA_SETTING)
        # print(table_name)

        # delete first.
        mysql_handler.delete(table_name, get_cond_dict(strategy_name))
        # insert
        mysql_handler.insert(table_name, row)
        logger.info(f"策略{strategy_name}:配置同步到远程成功")
Example #8
0
def main():
    logger.info(f"连接数据库 {mysql_setting['host']}:{mysql_setting['port']}")
    mysql = MySqlHandler()
    mysql.connect(**mysql_setting)

    logger.info("数据库连接等待...")
    for i in reversed(range(5)):
        logger.info(f"等待倒计时:{i}")
        sleep(1)

    try:
        init()
        # for content in [Content.CTA_SETTING, Content.CTA_DATA, Content.FOLLOW_DATA]:
        #     sync(mysql, content)
        sync(mysql, Content.FOLLOW_DATA)
    except:
        traceback.print_exc()
        mysql.close_db()
Example #9
0
 def close_db(self):
     self.db.close()
     logger.info("Mysql连接关闭")
Example #10
0
def sync(mysql_handler: MySqlHandler, content: Content) -> None:
    """
    自动同步
    """
    logger.info(f"正在同步数据:{content.value}")
    filename = content_map[content]['filename']
    sync_to_server_func = content_map[content]['to_server_func']
    sync_from_server_func = content_map[content]['from_server_func']

    local_exist = False
    remote_exist = False
    local_time = None
    server_time = None

    if is_local_exist(content):
        local_exist = True
        local_time = get_local_modified_time(content)

    if is_remote_exist(mysql_handler, content):
        remote_exist = True
        server_time = get_server_modified_time(mysql_handler, content)

    if local_exist:
        logger.info("本地数据已存在")
        if remote_exist:
            logger.info("远程数据已存在")
            if local_time > server_time:
                logger.info("本地数据是最新的")
                # 删除数据库上所有数据和表格
                clear_server_data(mysql_handler, content)
                create_table(mysql_handler, content)
                # 重新建立表格和数据
                sync_to_server_func(mysql_handler)
                logger.info("上传数据成功")
            elif server_time > local_time:
                logger.info("本地数据不是最新的")
                data = sync_from_server_func(mysql_handler)
                save_json(filename, data)
                logger.info("数据获取成功")
            else:
                logger.info("本地数据与数据库时间戳一致,无需同步")
        else:
            logger.info("远程数据不存在")
            create_table(mysql_handler, content)
            sync_to_server_func(mysql_handler)
            logger.info("上传数据成功")
    else:
        logger.info("本地数据不存在")
        if remote_exist:
            logger.info("远程数据已存在")
            data = sync_from_server_func(mysql_handler)
            save_json(filename, data)
            logger.info("数据获取成功")
        else:
            logger.info("本地和远程数据不存在")