示例#1
0
def getResult(strategyName, strategy_class, symbolinfo, K_MIN, rawdataDic,
              para, result_para_dic, indexcols, timestart):
    time1 = time.time()
    setname = para['Setname']
    print("%s %s %d %s Enter %.3f" % (strategyName, symbolinfo.domain_symbol,
                                      K_MIN, setname, time1 - timestart))

    initialCash = result_para_dic['initialCash']
    positionRatio = result_para_dic['positionRatio']
    remove_polar_switch = result_para_dic['remove_polar_switch']
    remove_polar_rate = result_para_dic['remove_polar_rate']

    symbollist = symbolinfo.getSymbolList()
    symbolDomainDic = symbolinfo.getSymbolDomainDic()
    result = pd.DataFrame()
    last_domain_utc = None
    for symbol in symbollist:
        if last_domain_utc:
            # 如果上一个合约的最后一次平仓时间超过其主力合约结束时间,则要修改本次合约的开始时间为上一次平仓后
            symbol_domain_start = last_domain_utc
            symbolDomainDic[symbol][0] = last_domain_utc
        else:
            symbol_domain_start = symbolDomainDic[symbol][0]
        symbol_domain_end = symbolDomainDic[symbol][1]
        rawdata = rawdataDic[symbol]
        r = strategy_class.run_trade_logic(symbolinfo, rawdata, para)
        r['symbol'] = symbol  # 增加主力全约列
        r = r.loc[(r['openutc'] >= symbol_domain_start)
                  & (r['openutc'] <= symbol_domain_end)]
        last_domain_utc = None
        if r.shape[0] > 0:
            last_close_utc = r.iloc[-1]['closeutc']
            if last_close_utc > symbol_domain_end:
                # 如果本合约最后一次平仓时间超过其主力合约结束时间,则要修改本合约的主力结束时间为平仓后
                symbolDomainDic[symbol][1] = last_close_utc
                last_domain_utc = last_close_utc
            result = pd.concat([result, r])
    result.reset_index(drop=True, inplace=True)

    # 去极值操作
    if remove_polar_switch:
        result = RS.opr_result_remove_polar(result, remove_polar_rate)

    # 全部操作结束后,要根据修改完的主力时间重新接出一份主连来计算dailyK
    domain_bar = pd.DataFrame()
    for symbol in symbollist[:-1]:
        symbol_domain_start = symbolDomainDic[symbol][0]
        symbol_domain_end = symbolDomainDic[symbol][1]
        rbar = rawdataDic[symbol]
        bars = rbar.loc[(rbar['utc_time'] >= symbol_domain_start)
                        & (rbar['utc_endtime'] < symbol_domain_end)]
        domain_bar = pd.concat([domain_bar, bars])
    # 最后一个合约只截前不截后
    symbol = symbollist[-1]
    symbol_domain_start = symbolDomainDic[symbol][0]
    rbar = rawdataDic[symbol]
    bars = rbar.loc[rbar['utc_time'] >= symbol_domain_start]
    domain_bar = pd.concat([domain_bar, bars])

    dailyK = DI.generatDailyClose(domain_bar)
    result['commission_fee'], result['per earn'], result['own cash'], result[
        'hands'] = RS.calcResult(result, symbolinfo, initialCash,
                                 positionRatio)
    bt_folder = "%s %d backtesting\\" % (symbolinfo.domain_symbol, K_MIN)

    result.to_csv(bt_folder + strategyName + ' ' + symbolinfo.domain_symbol +
                  str(K_MIN) + ' ' + setname + ' result.csv',
                  index=False)
    dR = RS.dailyReturn(symbolinfo, result, dailyK, initialCash)  # 计算生成每日结果
    dR.calDailyResult()
    dR.dailyClose.to_csv(
        (bt_folder + strategyName + ' ' + symbolinfo.domain_symbol +
         str(K_MIN) + ' ' + setname + ' dailyresult.csv'))
    results = RS.getStatisticsResult(result, False, indexcols, dR.dailyClose)
    del result
    print results
    return [setname] + results  # 在这里附上setname
示例#2
0
def single_sl(strategy_name, symbol_info, bar_type, setname, bar1m_dic,
              barxm_dic, stop_loss_class_list, result_para_dic, indexcols,
              timestart):
    print "%s %s %d %s" % (strategy_name, symbol_info.domain_symbol, bar_type,
                           setname)
    symbol = symbol_info.domain_symbol
    bt_folder = "%s %d backtesting\\" % (symbol, bar_type)
    oprdf = pd.read_csv(bt_folder + strategy_name + ' ' + symbol +
                        str(bar_type) + ' ' + setname + ' result.csv')

    close_type_list = []
    all_final_result_dic = {}  # 这个是用来保存每个文件的RS结果,返回给外部调用的
    all_stop_loss_opr_result_dic = {}  # 这个是用来保存每个参数每次操作的止损结果
    for stop_loss_class in stop_loss_class_list:
        sl_name = stop_loss_class.get_sl_name()
        close_type_list.append(sl_name)
        final_result_dic = {}
        stop_loss_opr_result_dic = {}
        for para in stop_loss_class.get_para_dic_list():
            final_result_dic[para['para_name']] = []
            stop_loss_opr_result_dic[para['para_name']] = []
        all_stop_loss_opr_result_dic[sl_name] = stop_loss_opr_result_dic
        all_final_result_dic[sl_name] = final_result_dic

    for stop_loss_class in stop_loss_class_list:
        if stop_loss_class.need_data_process_before_domain:
            bar1m_dic, barxm_dic = stop_loss_class.data_process_before_domain(
                bar1m_dic, barxm_dic)

    symbolDomainDic = symbol_info.amendSymbolDomainDicByOpr(oprdf)
    bar1m = DI.getDomainbarByDomainSymbol(symbol_info.getSymbolList(),
                                          bar1m_dic, symbolDomainDic)
    bar1m = bar1m_prepare(bar1m)
    barxm = DI.getDomainbarByDomainSymbol(symbol_info.getSymbolList(),
                                          barxm_dic, symbolDomainDic)

    barxm.set_index('utc_time', drop=False, inplace=True)  # 开始时间对齐
    bar1m.set_index('utc_time', drop=False, inplace=True)

    for stop_loss_class in stop_loss_class_list:
        if stop_loss_class.need_data_process_after_domain:
            bar1m, barxm = stop_loss_class.data_process_after_domain(
                bar1m, barxm)

    positionRatio = result_para_dic['positionRatio']
    initialCash = result_para_dic['initialCash']

    oprnum = oprdf.shape[0]
    worknum = 0

    for i in range(oprnum):
        opr = oprdf.iloc[i]
        #startutc = barxm.loc[opr['openutc'], 'utc_endtime']  # 从开仓的10m线结束后开始
        #endutc = barxm.loc[opr['closeutc'], 'utc_endtime'] - 60  # 一直到平仓的10m线结束
        startutc = opr['openutc']
        endutc = opr['closeutc']
        data_1m = bar1m.loc[startutc:endutc]
        data1m = data_1m.drop(data_1m.index[-1])  # 因为loc取数是含头含尾的,所以要去掉最后一行
        for stop_loss_class in stop_loss_class_list:
            sl_name = stop_loss_class.get_sl_name()
            stop_loss_opr_result_dic = all_stop_loss_opr_result_dic[sl_name]
            opr_result_dic = stop_loss_class.get_opr_sl_result(opr, data1m)
            for para in stop_loss_class.get_para_dic_list():
                stop_loss_opr_result_dic[para['para_name']].append(
                    opr_result_dic[para['para_name']])

    slip = symbol_info.getSlip()

    olddailydf = pd.read_csv(bt_folder + strategy_name + ' ' + symbol +
                             str(bar_type) + ' ' + setname +
                             ' dailyresult.csv',
                             index_col='date')
    oldr = RS.getStatisticsResult(oprdf, False, indexcols, olddailydf)
    dailyK = DI.generatDailyClose(barxm)

    for stop_loss_class in stop_loss_class_list:
        sl_name = stop_loss_class.get_sl_name()
        stop_loss_opr_result_dic = all_stop_loss_opr_result_dic[sl_name]
        final_result_dic = all_final_result_dic[sl_name]
        folder_prefix = stop_loss_class.get_folder_prefix()
        file_suffix = stop_loss_class.get_file_suffix()
        for para_name, opr_result_dic_list in stop_loss_opr_result_dic.items():
            result_df = pd.DataFrame(opr_result_dic_list)
            oprdf_temp = pd.concat([oprdf, result_df], axis=1)
            oprdf_temp['new_ret'] = (
                (oprdf_temp['new_closeprice'] - oprdf_temp['openprice']) *
                oprdf_temp['tradetype']) - slip
            oprdf_temp[
                'new_ret_r'] = oprdf_temp['new_ret'] / oprdf_temp['openprice']
            oprdf_temp['new_commission_fee'], oprdf_temp['new_per earn'], oprdf_temp['new_own cash'], oprdf_temp['new_hands'] = \
                RS.calcResult(oprdf_temp, symbol_info, initialCash, positionRatio, ret_col='new_ret')
            # 保存新的result文档
            tofolder = "%s%s\\" % (folder_prefix, para_name)
            oprdf_temp.to_csv(tofolder + strategy_name + ' ' + symbol +
                              str(bar_type) + ' ' + setname + ' ' +
                              file_suffix + para_name + '.csv',
                              index=False)
            dR = RS.dailyReturn(symbol_info, oprdf_temp, dailyK,
                                initialCash)  # 计算生成每日结果
            dR.calDailyResult()
            dR.dailyClose.to_csv(tofolder + strategy_name + ' ' + symbol +
                                 str(bar_type) + ' ' + setname + ' daily' +
                                 file_suffix + para_name + '.csv')
            newr = RS.getStatisticsResult(oprdf_temp, True, indexcols,
                                          dR.dailyClose)
            worknum = oprdf_temp.loc[oprdf_temp['new_closeindex'] !=
                                     oprdf_temp['closeindex']].shape[0]
            final_result_dic[para_name] = [setname, para_name, worknum
                                           ] + oldr + newr

    return all_final_result_dic
示例#3
0
def multi_stop_loss(strategyName, symbolInfo, K_MIN, setname,
                    stopLossTargetDictList, barxmdic, result_para_dic,
                    tofolder, indexcols):
    print 'setname:', setname
    symbol = symbolInfo.domain_symbol
    bt_folder = "%s %d backtesting\\" % (symbol, K_MIN)
    oprdf = pd.read_csv(bt_folder + strategyName + ' ' + symbol + str(K_MIN) +
                        ' ' + setname + ' result.csv')

    symbolDomainDic = symbolInfo.amendSymbolDomainDicByOpr(oprdf)
    barxm = DI.getDomainbarByDomainSymbol(symbolInfo.getSymbolList(), barxmdic,
                                          symbolDomainDic)
    dailyK = DI.generatDailyClose(barxm)

    positionRatio = result_para_dic['positionRatio']
    initialCash = result_para_dic['initialCash']

    oprlist = []
    sltnum = len(stopLossTargetDictList)
    for i in range(sltnum):
        slt = stopLossTargetDictList[i]
        # 遍历读取各止损目标的结果文件,按名称将结果写入oprdf中
        sltdf = pd.read_csv("%s%s %s%d %s %s" %
                            (slt['folder'], strategyName, symbol, K_MIN,
                             setname, slt['fileSuffix']))
        sltName = slt['name']
        oprdf[sltName + '_closeprice'] = sltdf['new_closeprice']
        oprdf[sltName + '_closetime'] = sltdf['new_closetime']
        oprdf[sltName + '_closeindex'] = sltdf['new_closeindex']
        oprdf[sltName + '_closeutc'] = sltdf['new_closeutc']
        oprdf[sltName + '_ret'] = sltdf['new_ret']
        oprdf[sltName + '_own cash'] = sltdf['new_own cash']
        oprlist.append(sltdf)
    # dsloprname=stratetyName+' '+symbol + str(K_MIN) + ' ' + setname + ' resultDSL_by_tick.csv'
    # ownloprname=stratetyName+' '+symbol + str(K_MIN) + ' ' + setname + ' resultOWNL_by_tick.csv'
    # dsloprdf=pd.read_csv(dslFolder+dsloprname)
    # ownloprdf=pd.read_csv(ownlFolder+ownloprname)

    oprdf['new_closeprice'] = oprdf['closeprice']
    oprdf['new_closetime'] = oprdf['closetime']
    oprdf['new_closeindex'] = oprdf['closeindex']
    oprdf['new_closeutc'] = oprdf['closeutc']
    oprdf['min_closeutc'] = oprdf['closeutc']
    oprdf['max_closeutc'] = oprdf['closeutc']
    for i in range(sltnum):
        # 先取最早平仓的时间,再根据时间去匹配类型
        slt = stopLossTargetDictList[i]
        utcname = slt['name'] + '_closeutc'
        oprdf['min_closeutc'] = oprdf.loc[:, ['min_closeutc', utcname]].min(
            axis=1)
        oprdf['max_closeutc'] = oprdf.loc[:, ['max_closeutc', utcname]].max(
            axis=1)
    # 根据最早平仓时间的结果,匹配平仓类型,不处理时间相同的情况
    oprdf['closetype'] = 'Normal'
    oprdf.loc[oprdf['max_closeutc'] != oprdf['closeutc'],
              'min_closeutc'] = oprdf['max_closeutc']
    for i in range(sltnum):
        slt = stopLossTargetDictList[i]
        name = slt['name']
        utcname = name + '_closeutc'
        utcnamebuf = name + '_closeutc_buf'
        oprdf[utcnamebuf] = oprdf[utcname]
        oprdf.loc[(oprdf['max_closeutc'] != oprdf['closeutc']) &
                  (oprdf[utcname] == oprdf['closeutc']),
                  utcnamebuf] = oprdf['max_closeutc']
    for i in range(sltnum):
        # 先取最早平仓的时间,再根据时间去匹配类型
        slt = stopLossTargetDictList[i]
        name = slt['name']
        utcnamebuf = name + '_closeutc_buf'
        oprdf['min_closeutc'] = oprdf.loc[:, ['min_closeutc', utcnamebuf]].min(
            axis=1)
    for i in range(sltnum):
        # 先按与最小相同的标识名称,因为止损文件中没有生效的操作的值与原值相同
        # 所以标识完后剩下的Normal就是原时间比止损时间早的值(也就是使用最小值匹配不出来的值,需要特殊处理)
        slt = stopLossTargetDictList[i]
        name = slt['name']
        utcname = name + '_closeutc'
        oprdf.loc[oprdf['min_closeutc'] == oprdf[utcname],
                  'closetype'] = slt['name']
        oprdf.loc[oprdf['min_closeutc'] == oprdf[utcname],
                  'new_closeprice'] = oprdf[name + '_closeprice']
        oprdf.loc[oprdf['min_closeutc'] == oprdf[utcname],
                  'new_closetime'] = oprdf[name + '_closetime']
        oprdf.loc[oprdf['min_closeutc'] == oprdf[utcname],
                  'new_closeindex'] = oprdf[name + '_closeindex']
        oprdf.loc[oprdf['min_closeutc'] == oprdf[utcname],
                  'new_closeutc'] = oprdf[name + '_closeutc']

        oprdf.drop(name + '_closeutc_buf', axis=1, inplace=True)  # 删掉buf列
    # 标识正常止损
    oprdf.loc[oprdf['min_closeutc'] == oprdf['closeutc'],
              'closetype'] = 'Normal'
    oprdf.drop('min_closeutc', axis=1, inplace=True)
    oprdf.drop('max_closeutc', axis=1, inplace=True)
    slip = symbolInfo.getSlip()
    # 2017-12-08:加入滑点
    oprdf['new_ret'] = ((oprdf['new_closeprice'] - oprdf['openprice']) *
                        oprdf['tradetype']) - slip
    oprdf['new_ret_r'] = oprdf['new_ret'] / oprdf['openprice']
    oprdf['new_commission_fee'], oprdf['new_per earn'], oprdf[
        'new_own cash'], oprdf['new_hands'] = RS.calcResult(oprdf,
                                                            symbolInfo,
                                                            initialCash,
                                                            positionRatio,
                                                            ret_col='new_ret')
    oprdf.to_csv(tofolder + '\\' + strategyName + ' ' + symbol + str(K_MIN) +
                 ' ' + setname + ' result_multiSLT.csv',
                 index=False)

    # 计算统计结果
    slWorkNum = oprdf.loc[oprdf['closetype'] != 'Normal'].shape[0]
    olddailydf = pd.read_csv(bt_folder + strategyName + ' ' + symbol +
                             str(K_MIN) + ' ' + setname + ' dailyresult.csv',
                             index_col='date')
    oldr = RS.getStatisticsResult(oprdf, False, indexcols, olddailydf)

    dR = RS.dailyReturn(symbolInfo, oprdf, dailyK, initialCash)  # 计算生成每日结果
    dR.calDailyResult()
    dR.dailyClose.to_csv(tofolder + '\\' + strategyName + ' ' + symbol +
                         str(K_MIN) + ' ' + setname +
                         ' dailyresult_multiSLT.csv')
    newr = RS.getStatisticsResult(oprdf, True, indexcols, dR.dailyClose)
    print newr
    return [
        setname,
        tofolder,
        slWorkNum,
    ] + oldr + newr
示例#4
0
def calOprResult(strategyName,
                 rawpath,
                 symbolinfo,
                 K_MIN,
                 nextmonth,
                 columns,
                 barxmdic,
                 positionRatio,
                 initialCash,
                 indexcols,
                 indexcolsFlag,
                 resultfilesuffix='result.csv'):
    '''
    根据灰区的取值,取出各灰区的操作列表,组成目标集组的操作表,并计算各个评价指标
    :return:
    '''
    symbol = symbolinfo.domain_symbol
    graydf = pd.read_csv(rawpath + 'ForwardOprAnalyze\\' + strategyName + ' ' +
                         symbol + str(K_MIN) + 'multiTargetForwardSetname.csv',
                         index_col='Group')

    cols = graydf.columns.tolist()[3:]
    cols.append(nextmonth)
    groupResult = []
    closetime_col = columns['closetime_col']
    closeindex_col = columns['closeindex_col']
    closeprice_col = columns['closeprice_col']
    closeutc_col = columns['closeutc_col']
    retr_col = columns['retr_col']
    ret_col = columns['ret_col']
    cash_col = columns['cash_col']
    hands_col = columns['hands_col']

    for i in range(graydf.shape[0]):
        gray = graydf.iloc[i]
        oprdf = pd.DataFrame(columns=[
            'opentime', 'openutc', 'openindex', 'openprice', closetime_col,
            closeutc_col, closeindex_col, closeprice_col, 'tradetype', ret_col,
            retr_col, 'symbol'
        ])
        print gray.name, gray.Target, gray.Windows
        for l in range(len(cols) - 1):
            startmonth = cols[l]
            endmonth = cols[l + 1]
            setname = gray[startmonth]
            oprdf = pd.concat([
                oprdf,
                getOprlistByMonth(strategyName, rawpath, symbol, K_MIN,
                                  setname, startmonth, endmonth, columns,
                                  resultfilesuffix)
            ])

        oprdf = oprdf.reset_index(drop=True)

        oprdf['commission_fee'], oprdf['per earn'], oprdf[cash_col], oprdf[
            hands_col] = RS.calcResult(oprdf, symbolinfo, initialCash,
                                       positionRatio, ret_col)
        tofilename = ('%s %s%d_%s_win%d_oprResult.csv' %
                      (strategyName, symbol, K_MIN, gray.Target, gray.Windows))
        oprdf.to_csv(rawpath + 'ForwardOprAnalyze\\' + tofilename)

        symbolDomainDic = symbolinfo.amendSymbolDomainDicByOpr(
            oprdf, closeutc_col=closeutc_col)
        barxm = DC.getDomainbarByDomainSymbol(symbolinfo.getSymbolList(),
                                              barxmdic, symbolDomainDic)
        dailyK = DC.generatDailyClose(barxm)  # 生成按日的K线

        dR = RS.dailyReturn(symbolinfo, oprdf, dailyK, initialCash)  # 计算生成每日结果
        dR.calDailyResult()
        tofilename = ('%s %s%d_%s_win%d_oprdailyResult.csv' %
                      (strategyName, symbol, K_MIN, gray.Target, gray.Windows))
        dR.dailyClose.to_csv(rawpath + 'ForwardOprAnalyze\\' + tofilename)

        r = RS.getStatisticsResult(oprdf, indexcolsFlag, indexcols,
                                   dR.dailyClose)

        groupResult.append([gray.name, gray.Target, gray.Windows] + r)

    groupResultDf = pd.DataFrame(groupResult,
                                 columns=['Group', 'Target', 'Windows'] +
                                 indexcols)
    groupResultDf.to_csv(rawpath + 'ForwardOprAnalyze\\' + strategyName + ' ' +
                         symbol + '_' + str(K_MIN) + '_groupOprResult.csv',
                         index=False)
    pass