def pack_to_traindatas2(data_path, train_path, zong_t, time_lidu, hc_zq=3):
    '''

    '''

    #  1.读取回测文件
    train_datas = {}
    with open(data_path, 'rb') as f0:
        resdf = pickle.load(f0)

    #  2.1读取训练数据地址,保存一个空文件。
    time0 = time.strftime("%m-%d", time.localtime())
    train_path = train_path.split(
        '.')[0] + f'_周期{time_lidu}_回测周期{hc_zq}_{time0}' + '.pickle'
    if os.path.exists(train_path):  # 读取保存地址
        print(f'地址存在{train_path}。')

    #  2.2读取训练数据地址,保存一个空文件。
    else:
        print(f'文件地址可用,新建一个空文件:{train_path}')
        with open(train_path, 'wb') as f_new:
            pickle.dump(train_datas, f_new)
            print(f'空文件保存在:{train_path}')
    # 3.0 获取历史信息。
    hf = HKFuture()  # 首次使用先配置..KRData
    h_data = hf.get_main_contract_bars('HSI',
                                       start=zong_t[0],
                                       end=(zong_t[-1] +
                                            datetime.timedelta(days=27)))
    #  3.按策略名字生成训练数据数据
    for k in resdf.keys():  # 遍历里面的策略名
        da = resdf[k]  #da是df数据,所有月份和参数。
        #   生成数据
        if len(da.keys()) > 0:
            print(f'{k}开始生成训练数据')
            # trainda 数据格式:{时间:数据,时间:数据。}
            trainda = generate_train_data2(da,
                                           zong_t=zong_t,
                                           hc_zq=hc_zq,
                                           h_data=h_data)
            train_datas[k] = trainda
            print(f'训练数据已经添加,当前:{k}')
            print('已存在》key:', train_datas.keys())

    with open(train_path, 'wb') as f1:
        pickle.dump(train_datas, f1)
        print('生成完毕:已存在key:')
        for i in train_datas.keys():
            print(i)
        print(f'\n保存在:{train_path}')
示例#2
0
文件: widget.py 项目: xyh888/KRData
    class DataFetcher(QThread):
        def __init__(self, parent=None):
            super().__init__(parent)
            from KRData.HKData import HKFuture
            self.hf = HKFuture()
            self.finished.connect(self.parent().show_data_fetch_finished)

        def run(self) -> None:
            parent = self.parent()
            if parent:
                parent.raw_data['HSI'] = self.hf.get_main_contract_bars('HSI', start='20190101')
示例#3
0
文件: widget.py 项目: xyh888/KRData
 def __init__(self, parent=None):
     super().__init__(parent)
     from KRData.HKData import HKFuture
     self.hf = HKFuture()
     self.finished.connect(self.parent().show_data_fetch_finished)
示例#4
0
def trade_review_in_notebook3(symbol,
                              executions: list = None,
                              pnlType='session'):
    import ipywidgets as widgets
    from IPython.display import display
    from .HKData import HKFuture

    if executions:
        executions.sort(key=lambda e: e['datetime'])  # 交易执行排序

    open_close_match = []
    if pnlType == 'session':
        from collections import deque
        pos_queue = deque()
        match = []
        for i, e in enumerate(executions):
            pos_queue.append((i, e['size']) if e['direction'] == 'long' else (
                i, -e['size']))
            match.append(i)
            if sum(p[1] for p in pos_queue) == 0:
                pos_queue.clear()
                open_close_match.append(match)
                match = []
        else:
            if match:
                open_close_match.append(match)

    ktype = widgets.RadioButtons(options=[('1 min', 1), ('5 min', 5),
                                          ('15 min', 15), ('30 min', 30),
                                          ('60 min', 30)],
                                 description='ktype')
    annotate = widgets.Checkbox(value=False, description='annotate')
    matchSelection = widgets.SelectMultiple(
        options=[(str(m), m) for m in open_close_match],
        rows=20,
        layout=widgets.Layout(width='100%', height='100px'),
        description='match',
        disabled=False)
    symbolWidget = widgets.Text(value=str(symbol),
                                description='symbol',
                                disable=True)
    figSuffixWidget = widgets.Text(value='', description='figSuffix')
    offsetWidget = widgets.IntSlider(min=0,
                                     max=500,
                                     step=10,
                                     value=60,
                                     continuous_update=False,
                                     description='expand_offset')

    fig = None

    def save_fig(b):
        if fig is not None:
            try:
                fig.savefig(f'{symbol}_{figSuffixWidget.value}.png')
            except Exception as e:
                print('errSaveFig:', e)

    saveFigButton = widgets.Button(description='保存图片')
    saveFigButton.on_click(save_fig)

    savfig_box = widgets.HBox([saveFigButton, figSuffixWidget])

    hf = HKFuture()

    def show_data(match, ktype, expand_offset, annotate):
        nonlocal fig
        es = []
        for m in match:
            for i in m:
                es.append(executions[i])
        if not es:
            print('请选择execution')
            return
        fig = hf.display_trades(symbol,
                                es,
                                period=ktype,
                                expand_offset=expand_offset,
                                annotate=annotate)

    params = {
        'match': matchSelection,
        'ktype': ktype,
        'expand_offset': offsetWidget,
        'annotate': annotate
    }

    out = widgets.interactive_output(show_data, params)
    display(symbolWidget, matchSelection, ktype, annotate, offsetWidget,
            savfig_box, out)
示例#5
0
def trade_review_in_notebook2(symbol=None,
                              mkdata=None,
                              period=1,
                              executions: list = None,
                              account=None,
                              date_from=None,
                              date_to=None,
                              expand_offset=120,
                              source='IB'):
    import ipywidgets as widgets
    from IPython.display import display
    from dateutil import parser
    import datetime as dt
    import talib
    import pandas as pd
    import mpl_finance as mpf

    if isinstance(expand_offset, tuple):
        s_offset = expand_offset[0]
        e_offset = expand_offset[1]
    else:
        s_offset = e_offset = expand_offset

    if account and date_from and symbol:
        # 在只填写accout与date_from的情况下,找出date_from当天,account中的所有交易记录
        from KRData.IBData import IBTrade
        ibt = IBTrade(account)
        date_from = parser.parse(date_from) if isinstance(date_from,
                                                          str) else date_from
        date_to = date_from + dt.timedelta(days=1) if date_to is None else (
            parser.parse(date_to) if isinstance(date_to, str) else date_to)
        fills = ibt[date_from:date_to:symbol]
        if fills.count() < 1:
            raise Exception(f'账户:{account}在{date_from}-{date_to}不存在交易记录')
        conId = fills[0].contract.conId
        executions = [{
            'datetime':
            f.time.replace(second=0) + dt.timedelta(hours=8),
            'price':
            f.execution.price,
            'size':
            f.execution.shares,
            'direction':
            'long' if f.execution.side == 'BOT' else 'short'
        } for f in fills]
        executions.sort(key=lambda e: e['datetime'])
        start = executions[0]['datetime'] - dt.timedelta(minutes=s_offset)
        end = executions[-1]['datetime'] + dt.timedelta(minutes=e_offset)
        if source == 'IB':
            barTypeMap = {
                1: '1 min',
                5: '5 mins',
                15: '15 mins',
                30: '30 mins',
                60: '1 hour'
            }
            mkdata = ibt._ib_market.get_bars_from_ib(conId,
                                                     barType=barTypeMap.get(
                                                         period, '1 min'),
                                                     start=start,
                                                     end=end)
        elif source == 'HK':
            from KRData.HKData import HKFuture
            hf = HKFuture()
            mkdata = hf.get_bars(symbol,
                                 start=start,
                                 end=end,
                                 ktype=f'{period}min',
                                 queryByDate=False)
            del hf
        del ibt
    elif mkdata is None and symbol:
        # 在只填写execution的情况下,自动根据source获取数据

        start = executions[0]['datetime'] - dt.timedelta(minutes=s_offset)
        end = executions[-1]['datetime'] + dt.timedelta(minutes=e_offset)

        if source == 'IB':
            from KRData.IBData import IBMarket
            ibm = IBMarket()
            barTypeMap = {
                1: '1 min',
                5: '5 mins',
                15: '15 mins',
                30: '30 mins',
                60: '1 hour'
            }
            mkdata = ibm.get_bars_from_ib(symbol,
                                          barType=barTypeMap.get(
                                              period, '1 min'),
                                          start=start,
                                          end=end)
            del ibm
        elif source == 'HK':
            from KRData.HKData import HKFuture
            hf = HKFuture()
            mkdata = hf.get_bars(symbol,
                                 start=start,
                                 end=end,
                                 ktype=f'{period}min',
                                 queryByDate=False)
            del hf

    mkdata = _concat_executions(mkdata, executions)
    mkdata['ma5'] = talib.MA(mkdata['close'].values, timeperiod=5)
    mkdata['ma10'] = talib.MA(mkdata['close'].values, timeperiod=10)
    mkdata['ma30'] = talib.MA(mkdata['close'].values, timeperiod=30)
    mkdata['ma60'] = talib.MA(mkdata['close'].values, timeperiod=60)

    symbolWidget = widgets.Text(value=str(symbol),
                                description='symbol',
                                disable=True)

    fromWidget = widgets.Text(value='',
                              placeholder='yyyymmdd HH:MM:SS',
                              description='From:',
                              disabled=True)
    toWidget = widgets.Text(value='',
                            placeholder='yyyymmdd HH:MM:SS',
                            description='To:',
                            disabled=True)
    time_range_box = widgets.HBox([fromWidget, toWidget])
    annotate = widgets.Checkbox(value=False, description='annotate')

    offsetWidget = widgets.IntSlider(min=0,
                                     max=500,
                                     step=10,
                                     value=60,
                                     description='expand_offset')

    executionSelection = widgets.SelectionRangeSlider(
        options=[(str(_e['datetime']), _e['datetime']) for _e in executions],
        index=(0, len(executions) - 1),
        description='execution',
        disabled=False,
        continuous_update=False,
    )

    fig = None

    def save_fig(b):
        if fig is not None:
            try:
                fig.savefig('fig.png')
            except Exception as e:
                print('errSaveFig:', e)

    saveFigButton = widgets.Button(description='保存图片')
    saveFigButton.on_click(save_fig)

    def show_data(e_select, offset, annotate):
        nonlocal fig
        s = e_select[0] - dt.timedelta(minutes=offset)
        e = e_select[-1] + dt.timedelta(minutes=offset)

        fromWidget.value = str(s)
        toWidget.value = str(e)

        temp_mkdata = mkdata[s:e]

        fig = draw_klines(temp_mkdata, annotate=annotate)

    params = {
        'e_select': executionSelection,
        'offset': offsetWidget,
        'annotate': annotate
    }

    out = widgets.interactive_output(show_data, params)
    display(symbolWidget, time_range_box, annotate, executionSelection,
            offsetWidget, saveFigButton, out)
示例#6
0
 def __init__(self, hf: HKFuture, index='HSI', **arg):
     self.hf = hf
     self._DateList = hf.get_main_contract_trade_dates(index)
     self.Test_Trade = True
     self.std_base = 2.5
     self.cls()
示例#7
0
 def __init__(self, hf: HKFuture, index='HSI'):
     self.hf = hf
     self._DateList = hf.get_main_contract_trade_dates(index)
     self.trade_para()
     self.Test_Trade = True
     self.cls()