关于前端:量化现货合约跟单软件开发源代码可二开

35次阅读

共计 6290 个字符,预计需要花费 16 分钟才能阅读完成。

量化交易机器人是什么?本质上,交易机器人是一个软件程序,能够间接与金融 (通常应用 API 来获取和解释相干信息) 进行交互,并且能够依据对市场数据的解释来公布交易订单。他们通过监控市场上的价格走势,并依据一套当时设置好的规定作出反应来做出这些决策。个别状况下,交易机器人会剖析市场上的交易数量、订单、价格和工夫等行为,并依据你的爱好来布局它们。

在策略设定好之后,机器人智能调配每一次进单条件,严格执行交易策略,交易策略,依据以后行情,开发对接 v +hkkf5566 实时进行云大数据调整。同时反对百种交易同时执行交易策略,每一个种类立线程,主动治理报价深度,策略计算,实时查看交易状况,实时执行。

// main.py
 
import datetime
import os
import re
import shutil
 
from constant import Exchange
 
import pandas as pd
# pd.set_option('display.max_columns', None)
# pd.set_option('display.max_rows', None)
 
from base_dict import dateToContract, clear_off_trading
 
 
class HftDataPath:
 
    @classmethod
    def data_path(cls, date_str: str, exchange: Exchange) -> str:
        path_str: str = ''
        if exchange == Exchange.DCE:
            path_str = f'{cls.source_data_dir()}/ht_dce_L2-{date_str}.log'
        elif exchange == Exchange.SHFE:
            pass
        elif exchange == Exchange.CZCE:
            pass
        elif exchange == Exchange.CFFEX:
            pass
        else:
            pass
 
        return path_str
 
 
    @classmethod
    def source_data_dir(cls) -> str:
        # return "C:/Users/tqz_trader/Desktop/hft_data_parser"
        return "E:\ 海通期货 \ 大连 L2\ITfuwu_dce_l2\ITfuwu_dce_l2_2021\ht_dce_L2-2021_05"
 
    @classmethod
    def output_data_dir(cls) -> str:
        # return "C:/Users/tqz_trader/Desktop/hft_data_parser/output_data"
        return f'{cls.source_data_dir()}/output_data'
 
    @classmethod
    def output_main_data_dir(cls) -> str:
        # return "C:/Users/tqz_trader/Desktop/hft_data_parser/output_main_data"
        return f'{cls.source_data_dir()}/output_main_data'
 
    def __init__(self, date_str: str, exchange: Exchange, filter_option: bool = True):
        path = HftDataPath.data_path(date_str=date_str, exchange=exchange)
        # path = f'ht_dce_L2-{date_str}.log'
        # path = f'test_{path}'
        assert os.path.exists(path), f'{path} not exists.'
 
    def split_by_symbol(self) -> {str: pd.DataFrame()}:
        assert self.__data is not None, f'__data is None'
 
        self.__rets_map: dict = {}
        for symbol in list(set(self.__data.Symbol.tolist())):
            sub_data = self.__data[self.__data['Symbol'] == symbol]
            sub_data.reset_index(inplace=True)
            del sub_data['index']
 
            self.__rets_map[symbol] = sub_data
 
        return self
  
    def dump_to_csv(self):
        assert self.__rets_map is not None, f'__rets_map is None'
 
        # check output 目标目录 是否存在
        target_dir = HftDataPath.output_data_dir()
        if os.path.exists(path=target_dir) is False:
            os.mkdir(target_dir)
 
        # check 种类 & 对应日期 目标目录 是否存在
        symbol_dir_map: {str: str} = {}
        date_str = self.__date_str.replace("_", "")
        for symbol in self.rets_map().keys():
            symbol_dir = f'{target_dir}/data{re.match(r"^[a-zA-Z]{1,3}", symbol).group()}00'
            if os.path.exists(path=symbol_dir) is False:
                os.mkdir(symbol_dir)
 
            symbol_date_dir = f'{symbol_dir}/{date_str}'
            if os.path.exists(path=symbol_date_dir) is False:
                os.mkdir(symbol_date_dir)
 
            symbol_dir_map[symbol] = symbol_date_dir
 
        # write to csv
        for symbol, symbol_df in self.rets_map().items():
            target_path = f'{symbol_dir_map[symbol]}/{symbol}_{date_str}.csv'
            symbol_df.to_csv(target_path, index=False)
 
 
    def data(self) -> pd.DataFrame():
        return self.__data
 
 
    def rets_map(self) -> {str: pd.DataFrame()}:
        return self.__rets_map
 
 
    def __reset_format(self):
        assert self.__data is not None, f'__data is None.'
 
        # Date
        self.__data['Date'] = self.__date_str.replace('_', '')
 
        # Time, Symbol
        self.__data.rename(columns={'UpdateTime': 'Time'}, inplace=True)
        self.__data.rename(columns={'ContractID': 'Symbol'}, inplace=True)
 
        # HighLimit, LowLimit
        self.__data['HighLimit'] = 1000000
        self.__data['LowLimit'] = 0
 
        # LocalTime, timestamp
        self.__data['LocalTime'] = self.__data.Time
        self.__data['LocalTime'] = self.__data.LocalTime.str.replace(':', '', regex=True)
        self.__data['LocalTime'] = self.__data.LocalTime.str.replace('.', '', regex=True)
        self.__data['timestamp'] = self.__data.LocalTime
 
        # LocalNS
        self.__data["LocalNS"] = pd.to_datetime(self.__date_str.replace('_', '-') + '' + self.__data['Time']).apply(lambda x: x.value)
  
class HftDataManager:
 
    @classmethod
    def parser(cls, start_date_str: str, end_date_str: str, exchange: Exchange):
        """
        clear source hft data
        :param start_date_str: begin date of source file
        :param end_date_str: end date of source file
        :param exchange: enum of exchang
        """start_date, end_date = datetime.datetime.strptime(start_date_str,'%Y_%m_%d').date(), datetime.datetime.strptime(end_date_str,'%Y_%m_%d').date()
 
        while True:
            if start_date > end_date:
                break
 
            format_date_str = str(start_date).replace("-", "_")
            if os.path.exists(path=HftDataPath.data_path(date_str=format_date_str, exchange=exchange)):
                HftDataOperator(date_str=format_date_str, exchange=exchange).split_by_symbol().dump_to_csv()
 
            print(f'date({start_date}) parser over.')
            start_date += datetime.timedelta(days=1)
 
    @classmethod
    def refresh_main_contracts_dir(cls, target_contracts: list, start_date_str: str, end_date_str: str):
        """refresh main contract's dir
        :param target_contracts: target contracts which need refresh
        :param start_date_str: begin date of source file
        :param end_date_str: end date of source file
        """
 
        source_dir = HftDataPath.output_data_dir()
        assert os.path.exists(path=source_dir), f'{source_dir} not exist.'
 
        target_dir = HftDataPath.output_main_data_dir()
        if os.path.exists(path=target_dir) is True:
            shutil.rmtree(target_dir)
        os.mkdir(target_dir)
 
        for contract in target_contracts:
            start_date, end_date = datetime.datetime.strptime(start_date_str,'%Y_%m_%d').date(), datetime.datetime.strptime(end_date_str, '%Y_%m_%d').date()
 
            source_sub_dir = f'{source_dir}/data{contract}'
            target_sub_dir = f'{target_dir}/data{contract}'
            if os.path.exists(path=target_sub_dir) is False:
                os.mkdir(target_sub_dir)
 
            while True:
                if start_date > end_date:
                    break
 
                format_date_str = str(start_date).replace('-', '')
                main_contract = dateToContract(instrument=contract, date=format_date_str)
 
                source_main_contract_dir = f'{source_sub_dir}/{format_date_str}'
                target_main_contract_dir = f'{target_sub_dir}/{format_date_str}'
                if os.path.exists(path=source_main_contract_dir):
                    os.mkdir(target_main_contract_dir)
 
                source_main_contract_path = f'{source_main_contract_dir}/{main_contract}_{format_date_str}.csv'
                target_main_contract_path = f'{target_main_contract_dir}/{main_contract}_{format_date_str}.csv'
 
                if os.path.exists(path=source_main_contract_path):
                    shutil.copy(src=source_main_contract_path, dst=target_main_contract_path)
 
                    # clear main contract data.
                    target_df = pd.read_csv(target_main_contract_path)
 
                    clear_off_trading(target_df, "timestamp")
                    target_df = target_df.drop_duplicates(subset='timestamp', keep='first', inplace=False)
                    target_df = target_df.reset_index(drop=True)
 
                    target_df.to_csv(target_main_contract_path, index=False)
 
                start_date += datetime.timedelta(days=1)
 
 
if __name__ == '__main__':
    _start_date_str = "2021_05_14"
    _end_date_str = "2021_05_24"
 
    HftDataManager.parser(start_date_str=_start_date_str, end_date_str=_end_date_str, exchange=Exchange.DCE)
 
    # HftDataManager.refresh_main_contracts_dir(#     target_contracts=['v00', 'eg00'],
    #     start_date_str=_start_date_str,
    #     end_date_str=_end_date_str
    # )

正文完
 0