A New Day Has Come 全新的时代

In [1]:
# 基础库导入

from __future__ import print_function
from __future__ import division

import warnings
# warnings.filterwarnings('ignore')
# warnings.simplefilter('ignore')
import time
import moment  

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import ipywidgets
%matplotlib inline

import os
import sys
# 使用insert 0即只使用github,避免交叉使用了pip安装的abupy,导致的版本不一致问题
sys.path.insert(0, os.path.abspath('../'))
import abupy

from abupy import AbuFactorAtrNStop, AbuFactorPreAtrNStop, AbuFactorCloseAtrNStop, AbuFactorBuyBreak
from abupy import AbuFactorSellBreak
from abupy import abu, EMarketTargetType, AbuMetricsBase, ABuMarketDrawing, ABuProgress, ABuSymbolPd
from abupy import EMarketTargetType, EDataCacheType, EMarketSourceType, EMarketDataFetchMode, EStoreAbu, AbuUmpMainMul

from abupy import AbuUmpMainDeg, AbuUmpMainJump, AbuUmpMainPrice, AbuUmpMainWave, feature, AbuFeatureDegExtend
from abupy import AbuUmpEdgeDeg, AbuUmpEdgePrice, AbuUmpEdgeWave, AbuUmpEdgeFull, AbuUmpEdgeMul, AbuUmpEegeDegExtend
from abupy import AbuUmpMainDegExtend, ump, Parallel, delayed, AbuMulPidProgress


pd.options.display.max_rows = 1000
from IPython.display import display
from IPython.core.display import HTML
from pprint import pprint, pformat 

# 关闭沙盒数据
abupy.env.disable_example_env_ipython()
 
#os.path.join(ABuEnv.g_project_cache_dir, 'abu_socket_progress')
# print(os.environ.get(CACHE_ROOT, './cache'))
# for k, v in os.environ.items():
#     print(f'{k}={v}')
# print(os.environ.get('CACHE_ROOT', './cache'))
暂时支持windows和mac os,不是windows就是mac os(不使用Darwin做判断),linux下没有完整测试 
 g_is_mac_os: True /opt/notebooks/abupy/CoreBu/ABuEnv.py
/tmp/abu_socket_progress
NumExpr defaulting to 8 threads.
disable example env
In [2]:
from abupy import env
from abupy.CoreBu import  ABuEnv

# print(ABuEnv._p_dir)
In [3]:
from WzFeature.WzFeatureMaDeg import WzFeatureMaDeg
from WzFeature.WzFeatureIndexMaDeg import WzFeatureIndexMaDeg
from WzFeature.WzFeatureMaSpeed import WzFeatureMaSpeed
from WzFeature.WzFeatureIndexPoly import WzFeatureIndexPoly
from WzFeature.WzFeaturePoly import WzFeaturePoly
from WzFeature.WzFeatureIndexSimilarity import WzFeatureIndexSimilarity


from WzUmp.WzUmpMainMaDeg import WzUmpMainMaDeg
from WzUmp.WzUmpMainMaSpeed import WzUmpMainMaSpeed
from WzUmp.WzUmpMainIndexMaDeg import WzUmpMainIndexMaDeg
from WzUmp.WzUmpMainIndexPoly import WzUmpMainIndexPoly
from WzUmp.WzUmpMainPoly import WzUmpMainPoly

from WzUmp.WzUmpMainMaSpeedWithIndMaDegAndSmlr import WzUmpMainMaSpeedWithIndMaDegAndSmlr
from WzUmp.WzUmpMainIndexPolyAndMaDeg import  WzUmpMainIndexPolyAndMaDeg
from WzUmp.WzUmpMainPolyWithMaDeg import WzUmpMainPolyWithMaDeg
from WzUmp.WzUmpMainPriceWithMaDeg import WzUmpMainPriceWithMaDeg

# WzUmpMainMaSpeedWithIndMaDegAndSmlr.v()

from WzUmp.WzUmpEdgeIndexMaDeg import WzUmpEdgeIndexMaDeg
from WzUmp.WzUmpEdgeIndexPoly import WzUmpEdgeIndexPoly
from WzUmp.WzUmpEdgeMaDeg import WzUmpEdgeMaDeg
from WzUmp.WzUmpEdgeMaSpeed import WzUmpEdgeMaSpeed
from WzUmp.WzUmpEdgePoly import  WzUmpEdgePoly

from WzUmp.WzUmpEdgeMaSpeedWithIndMaDegAndSmlr import WzUmpEdgeMaSpeedWithIndMaDegAndSmlr
from WzUmp.WzUmpEdgeIndexPolyAndMaDeg import  WzUmpEdgeIndexPolyAndMaDeg
from WzUmp.WzUmpEdgePolyWithMaDeg import WzUmpEdgePolyWithMaDeg
from WzUmp.WzUmpEdgePriceWithMaDeg import WzUmpEdgePriceWithMaDeg

股池

股池为上证50 + 创业50 + 沪深300 + 中证500 几大指数的成分股

In [4]:
import numpy as np
import random
from  stock_pool import stock_pool

def custom_symbols():
    symbols = []    
    symbols.extend( random.sample(stock_pool.get('zh500'), 500))
    symbols.extend( random.sample(stock_pool.get('hs300'), 300))
    symbols.extend( random.sample(stock_pool.get('sh50'), 49))
    symbols.extend( random.sample(stock_pool.get('chuang50'), 50))
    
    
    print("股池总数:" , len(symbols))

    symbols = np.unique(np.array(symbols))
    print('unique后:', len(symbols))

    return symbols

symbols = custom_symbols()
print(symbols)
/opt/notebooks/abupy_lecture
股池总数: 899
unique后: 820
['000001' '000002' '000006' '000008' '000009' '000012' '000021' '000025'
 '000027' '000028' '000031' '000039' '000049' '000060' '000061' '000062'
 '000063' '000066' '000069' '000078' '000089' '000090' '000100' '000156'
 '000157' '000158' '000166' '000333' '000338' '000400' '000401' '000402'
 '000408' '000413' '000415' '000418' '000423' '000425' '000426' '000488'
 '000501' '000503' '000513' '000519' '000528' '000536' '000537' '000538'
 '000541' '000543' '000547' '000552' '000553' '000559' '000563' '000564'
 '000568' '000581' '000587' '000596' '000598' '000600' '000612' '000623'
 '000625' '000627' '000630' '000636' '000651' '000656' '000661' '000667'
 '000671' '000681' '000685' '000686' '000690' '000703' '000709' '000712'
 '000717' '000718' '000723' '000725' '000727' '000728' '000729' '000732'
 '000738' '000750' '000758' '000761' '000766' '000768' '000776' '000778'
 '000783' '000786' '000792' '000807' '000813' '000826' '000829' '000830'
 '000839' '000848' '000858' '000860' '000876' '000877' '000878' '000887'
 '000895' '000898' '000926' '000930' '000932' '000937' '000938' '000959'
 '000960' '000961' '000963' '000969' '000970' '000975' '000980' '000983'
 '000987' '000988' '000990' '000997' '000998' '000999' '001965' '001979'
 '002001' '002002' '002004' '002007' '002008' '002013' '002019' '002024'
 '002027' '002028' '002030' '002032' '002038' '002041' '002044' '002048'
 '002049' '002050' '002051' '002056' '002064' '002065' '002074' '002078'
 '002081' '002085' '002092' '002093' '002110' '002118' '002120' '002127'
 '002128' '002129' '002131' '002142' '002146' '002147' '002152' '002153'
 '002155' '002174' '002176' '002179' '002183' '002191' '002195' '002202'
 '002212' '002221' '002223' '002230' '002233' '002236' '002241' '002242'
 '002244' '002249' '002250' '002251' '002252' '002254' '002266' '002268'
 '002271' '002273' '002276' '002277' '002280' '002281' '002285' '002294'
 '002299' '002302' '002304' '002308' '002310' '002311' '002317' '002332'
 '002340' '002344' '002345' '002352' '002353' '002354' '002358' '002359'
 '002366' '002368' '002371' '002372' '002373' '002375' '002384' '002385'
 '002390' '002399' '002400' '002407' '002408' '002410' '002411' '002414'
 '002415' '002416' '002422' '002424' '002426' '002431' '002434' '002437'
 '002439' '002440' '002444' '002450' '002456' '002460' '002463' '002465'
 '002466' '002468' '002470' '002475' '002477' '002482' '002489' '002491'
 '002493' '002500' '002503' '002505' '002506' '002507' '002508' '002509'
 '002512' '002517' '002544' '002555' '002558' '002563' '002572' '002573'
 '002583' '002588' '002589' '002594' '002601' '002602' '002603' '002624'
 '002625' '002635' '002640' '002642' '002663' '002665' '002670' '002672'
 '002673' '002681' '002690' '002699' '002701' '002707' '002709' '002714'
 '002736' '002745' '002773' '002797' '002807' '002815' '002818' '002831'
 '002839' '002916' '002920' '002925' '300001' '300002' '300003' '300009'
 '300010' '300014' '300015' '300017' '300024' '300026' '300027' '300033'
 '300038' '300055' '300058' '300059' '300070' '300072' '300073' '300078'
 '300088' '300098' '300113' '300115' '300122' '300124' '300133' '300134'
 '300136' '300142' '300144' '300146' '300156' '300159' '300166' '300168'
 '300170' '300182' '300197' '300199' '300202' '300244' '300251' '300253'
 '300257' '300266' '300274' '300287' '300291' '300296' '300297' '300308'
 '300315' '300316' '300324' '300347' '300355' '300376' '300377' '300383'
 '300408' '300413' '300418' '300433' '300450' '300454' '300459' '300496'
 '300601' '300618' '300634' '300666' '300676' '300699' '300747' '300750'
 '300760' '600000' '600004' '600006' '600008' '600009' '600010' '600011'
 '600015' '600016' '600017' '600018' '600019' '600021' '600022' '600023'
 '600025' '600026' '600027' '600028' '600029' '600030' '600031' '600036'
 '600037' '600038' '600039' '600048' '600050' '600053' '600056' '600058'
 '600060' '600061' '600062' '600064' '600066' '600068' '600073' '600079'
 '600085' '600086' '600089' '600094' '600098' '600100' '600104' '600109'
 '600111' '600115' '600118' '600120' '600122' '600125' '600126' '600138'
 '600141' '600143' '600151' '600153' '600155' '600157' '600158' '600160'
 '600161' '600166' '600167' '600169' '600170' '600171' '600176' '600177'
 '600179' '600183' '600184' '600188' '600195' '600196' '600201' '600208'
 '600216' '600219' '600221' '600233' '600240' '600256' '600258' '600259'
 '600260' '600266' '600267' '600271' '600276' '600277' '600280' '600282'
 '600291' '600297' '600298' '600307' '600309' '600312' '600315' '600316'
 '600325' '600329' '600332' '600335' '600338' '600339' '600340' '600346'
 '600348' '600350' '600352' '600362' '600366' '600369' '600372' '600373'
 '600376' '600380' '600383' '600388' '600390' '600392' '600393' '600398'
 '600406' '600409' '600410' '600415' '600416' '600418' '600426' '600428'
 '600435' '600436' '600438' '600458' '600460' '600466' '600478' '600482'
 '600487' '600489' '600498' '600499' '600500' '600503' '600507' '600511'
 '600516' '600518' '600519' '600522' '600525' '600528' '600535' '600536'
 '600545' '600547' '600549' '600557' '600563' '600565' '600566' '600567'
 '600570' '600572' '600575' '600580' '600582' '600583' '600584' '600585'
 '600588' '600594' '600597' '600598' '600600' '600606' '600611' '600614'
 '600618' '600623' '600633' '600637' '600639' '600640' '600642' '600643'
 '600645' '600648' '600649' '600655' '600657' '600660' '600664' '600673'
 '600674' '600687' '600688' '600690' '600694' '600699' '600703' '600704'
 '600705' '600717' '600718' '600729' '600737' '600739' '600741' '600743'
 '600748' '600750' '600751' '600754' '600755' '600757' '600759' '600760'
 '600765' '600770' '600773' '600777' '600779' '600782' '600787' '600795'
 '600801' '600804' '600808' '600809' '600811' '600816' '600820' '600823'
 '600827' '600835' '600837' '600839' '600848' '600859' '600862' '600863'
 '600867' '600869' '600872' '600874' '600875' '600879' '600881' '600884'
 '600885' '600886' '600887' '600893' '600895' '600900' '600901' '600908'
 '600909' '600917' '600919' '600926' '600939' '600958' '600959' '600967'
 '600970' '600971' '600977' '600978' '600993' '600996' '600998' '600999'
 '601000' '601001' '601003' '601005' '601006' '601009' '601012' '601016'
 '601018' '601019' '601020' '601021' '601066' '601088' '601098' '601100'
 '601106' '601108' '601111' '601117' '601127' '601128' '601138' '601155'
 '601166' '601168' '601169' '601179' '601186' '601198' '601200' '601211'
 '601212' '601216' '601225' '601228' '601229' '601231' '601238' '601288'
 '601311' '601318' '601326' '601328' '601333' '601336' '601360' '601377'
 '601390' '601398' '601555' '601598' '601600' '601601' '601607' '601608'
 '601611' '601618' '601628' '601633' '601668' '601669' '601678' '601688'
 '601689' '601717' '601718' '601727' '601766' '601777' '601788' '601800'
 '601801' '601808' '601811' '601818' '601828' '601838' '601857' '601866'
 '601869' '601872' '601877' '601878' '601880' '601881' '601888' '601898'
 '601899' '601901' '601919' '601928' '601933' '601939' '601958' '601966'
 '601969' '601985' '601988' '601989' '601990' '601991' '601992' '601997'
 '601998' '603000' '603025' '603056' '603077' '603156' '603160' '603169'
 '603198' '603225' '603228' '603233' '603259' '603260' '603288' '603328'
 '603355' '603369' '603377' '603444' '603486' '603515' '603556' '603568'
 '603569' '603650' '603658' '603659' '603712' '603766' '603799' '603806'
 '603816' '603833' '603858' '603866' '603868' '603877' '603883' '603885'
 '603888' '603899' '603986' '603993']
/opt/conda/lib/python3.7/site-packages/openpyxl/worksheet/header_footer.py:48: UserWarning: Cannot parse header or footer so it will be ignored
  warn("""Cannot parse header or footer so it will be ignored""")

策略

策略本身优秀与否不是重点,本次主要是研究 弱人工智能如何帮我们提高收益,是观察没有使用AI和使用AI后 的对比。

In [5]:
from picker.WzPickNonNew import WzPickNonNew
from picker.WzPickMaAng import WzPickMaAng
from abupy import AbuPtPosition
from factor.WzFactorBuyRsiV8 import WzFactorBuyRsiV8
from factor.WzFactorSellRsiV8 import WzFactorSellRsiV8
from factor.WzFactorBuyMaV6 import WzFactorBuyMaV6
from factor.WzFactorSellMaV5 import WzFactorSellMaV5

from abupy import slippage 
# 开启针对非集合竞价阶段的涨停,滑点买入价格以高概率在接近涨停的价格买入 
slippage.sbb.g_enable_limit_up = True 
# 将集合竞价阶段的涨停买入成功概率设置为0,如果设置为0.2即20%概率成功买入 
slippage.sbb.g_pre_limit_up_rate = 0 
# 开启针对非集合竞价阶段的跌停,滑点卖出价格以高概率在接近跌停的价格卖出 
slippage.ssb.g_enable_limit_down = True 
# 将集合竞价阶段的跌停卖出成功概率设置为0, 如果设置为0.2即20%概率成功卖出 
slippage.ssb.g_pre_limit_down_rate = 0


abupy.env.g_data_cache_type = EDataCacheType.E_DATA_CACHE_CSV
abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN
abupy.env.g_data_fetch_mode = EMarketDataFetchMode.E_DATA_FETCH_FORCE_LOCAL


read_cash = 10000000 #资金
o_pos_base =0.1 #abupy.beta.atr.g_atr_pos_base
print('o_pos_base:', o_pos_base)
pt_pos_base =  o_pos_base / 12
print('pt_pos_base:' , pt_pos_base)


buy_factors=[{'buy_default': 'post=36.88',
  'class': WzFactorBuyRsiV8,
  'position': {'class': AbuPtPosition,
               'past_day_cnt': 68,
               'pos_base': pt_pos_base },
  'rsi_timeperiod': 6,
  'stock_pickers': [{'class': WzPickNonNew,
                     'pick_period': 'week',
                     'weeks': 24},
                  ]
             }]

# 卖出因子继续使用上一节使用的因子
sell_factors = [
#     {'stop_loss_n': 1.0, 'stop_win_n': 3.0,
#      'class': AbuFactorAtrNStop},
#     {'class': AbuFactorPreAtrNStop, 'pre_atr_n': 1.5},
    {'class': WzFactorSellMaV5 ,
      'ma_type': 1,
      'sell_x': 'rvv=5,6,0.22,yc,1.0'},
    {'class': WzFactorSellRsiV8,
      'rsi_timeperiod': 6,
      'sell_default':'[email protected]' }, #rvv=10,-,78,12,80,1.0  
    {'class': AbuFactorCloseAtrNStop, 'close_atr_n': 1.25},
    {'class': AbuFactorAtrNStop, 'stop_loss_n': 0.65},
#     {'class': WzFactorSellRsiV8,
#       'rsi_timeperiod': 6,
#       'sell_default': 'lt=2,29,1.0|lt=3,39,1.0'}, 
]


print("随便输出点什么表示已经执行过: ")
o_pos_base: 0.1
pt_pos_base: 0.008333333333333333
随便输出点什么表示已经执行过: 
In [6]:
print( read_cash *  0.0083333)
83333.0

策略回测 - 准备交割单数据用于机器人裁判训练

将股池所有股票分切成3块,拿2/3用于机器人裁判训练, 剩下1/3是测试集用途。

In [6]:
# import psutil


from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
simplefilter(action='ignore', category=ResourceWarning)

# g_cpu_cnt = psutil.cpu_count(logical=True) * 1
# print ("g_cpu_cnt:", g_cpu_cnt)

# 回测生成买入时刻特征
abupy.env.g_enable_ml_feature = True
# 回测开始时将symbols切割分为训练集数据和测试集两份,使用训练集进行回测
abupy.env.g_enable_train_test_split = True
# 训练:测试 = 1:1
abupy.env.g_split_tt_n_folds = 3

feature.clear_user_feature()

abupy.feature.g_price_rank_keys = [12, 25, 50, 100, 200]

feature.append_user_feature(AbuFeatureDegExtend)

feature.append_user_feature(WzFeatureMaDeg)
feature.append_user_feature(WzFeatureIndexMaDeg)
feature.append_user_feature(WzFeatureMaSpeed)
feature.append_user_feature(WzFeatureIndexPoly)
feature.append_user_feature(WzFeaturePoly)
feature.append_user_feature(WzFeatureIndexSimilarity)

abu_result_tuple = None

def run_loop_back():
    global abu_result_tuple
#     , symbols    
#     print(symbols)
    abu_result_tuple, _ = abu.run_loop_back(read_cash,
                                            buy_factors,
                                            sell_factors,
                                            choice_symbols = symbols,
#                                             choice_symbols=None,
                                            start='2016-02-02', end='2019-12-18',
#                                             stock_picks= stock_pickers,
#                                             n_process_kl = 8,
#                                             n_process_pick= 8,
                                           )

    # 把运行的结果保存在本地,以便之后分析回测使用,保存回测结果数据代码如下所示
    abu.store_abu_result_tuple(abu_result_tuple, n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                               custom_name='train_cn')
    ABuProgress.clear_output()
    AbuMetricsBase.show_general(*abu_result_tuple, only_show_returns=True).plot_max_draw_down()

def run_load_train():
    global abu_result_tuple
    abu_result_tuple = abu.load_abu_result_tuple(n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                                                 custom_name='train_cn')
    AbuMetricsBase.show_general(*abu_result_tuple,  only_show_returns=True)#.plot_max_draw_down()

def select(select):
    if select == 'run loop back':
        run_loop_back()
    else:
        run_load_train()

# _ = ipywidgets.interact_manual(select, select=[ 'load train data', 'run loop back'])
run_load_train()
买入后卖出的交易数量:28711
买入后尚未卖出的交易数量:604
胜率:40.6708%
平均获利期望:7.1467%
平均亏损期望:-5.0276%
盈亏比:0.9797
策略收益: -12.9717%
基准收益: 8.9269%
策略年化收益: -3.4591%
基准年化收益: 2.3805%
策略买入成交比例:29.0841%
策略资金利用率比例:84.8218%
策略共执行945个交易日

看,原始策略很差,亏!!

训练集和测试集回顾:

上面的abupy.env.g_enable_train_test_split = Trueabupy.env.g_split_tt_n_folds = 3配置自动把训练集和测试集分切好了,打印出来看看。

In [6]:
from abupy import ABuFileUtil
a = ['1', '2', '3', 'a', 'b', 'c']
b = ['0','2','b','f**k']
print('a&b交集为:', set(a).intersection(b) )


train = ABuFileUtil.load_pickle("/root/abu/data/cache/market_train_symbols_hs")
test = ABuFileUtil.load_pickle("/root/abu/data/cache/market_test_symbols_hs")

print('训练集和测试集交集为:', set(train).intersection(test) )

print( len(train), "个训练集股票:\r\n", train, "\r\n") 
print( len(test), "个测试集股票:\r\n", test) 
a&b交集为: {'b', '2'}
训练集和测试集交集为: set()
546 个训练集股票:
 ['000002', '000008', '000009', '000021', '000027', '000028', '000031', '000049', '000060', '000062', '000063', '000066', '000069', '000078', '000089', '000090', '000100', '000156', '000157', '000158', '000166', '000333', '000338', '000401', '000402', '000408', '000413', '000415', '000418', '000425', '000426', '000503', '000513', '000537', '000538', '000552', '000553', '000563', '000568', '000581', '000623', '000625', '000651', '000661', '000667', '000671', '000685', '000703', '000709', '000712', '000718', '000725', '000727', '000728', '000729', '000732', '000738', '000750', '000758', '000768', '000778', '000783', '000786', '000792', '000807', '000813', '000826', '000829', '000830', '000839', '000848', '000858', '000876', '000877', '000878', '000895', '000898', '000926', '000938', '000961', '000963', '000969', '000970', '000975', '000980', '000983', '000987', '000988', '000998', '000999', '001965', '001979', '002001', '002002', '002004', '002007', '002008', '002019', '002024', '002030', '002038', '002041', '002048', '002050', '002051', '002056', '002064', '002065', '002074', '002078', '002081', '002092', '002120', '002127', '002131', '002142', '002147', '002152', '002176', '002179', '002183', '002191', '002195', '002202', '002212', '002230', '002233', '002244', '002249', '002251', '002252', '002254', '002266', '002268', '002271', '002281', '002294', '002302', '002344', '002352', '002353', '002354', '002358', '002359', '002366', '002371', '002372', '002373', '002385', '002399', '002400', '002407', '002408', '002410', '002411', '002414', '002415', '002416', '002424', '002431', '002440', '002444', '002450', '002463', '002465', '002470', '002477', '002482', '002493', '002500', '002506', '002507', '002509', '002517', '002544', '002555', '002558', '002563', '002583', '002588', '002589', '002601', '002602', '002603', '002624', '002625', '002640', '002642', '002663', '002665', '002670', '002672', '002673', '002681', '002690', '002701', '002736', '002745', '002773', '002797', '002807', '002831', '002916', '002925', '300001', '300003', '300009', '300010', '300014', '300017', '300024', '300026', '300027', '300033', '300038', '300055', '300070', '300073', '300078', '300088', '300098', '300122', '300124', '300133', '300134', '300142', '300146', '300166', '300182', '300251', '300253', '300257', '300274', '300287', '300291', '300297', '300308', '300315', '300316', '300347', '300355', '300377', '300408', '300413', '300418', '300433', '300450', '300454', '300459', '300634', '300699', '300747', '300760', '600000', '600004', '600006', '600009', '600010', '600011', '600016', '600017', '600018', '600019', '600022', '600025', '600026', '600027', '600028', '600031', '600038', '600039', '600048', '600050', '600053', '600056', '600058', '600062', '600064', '600066', '600073', '600085', '600089', '600094', '600098', '600100', '600104', '600109', '600118', '600120', '600125', '600126', '600138', '600141', '600151', '600153', '600155', '600157', '600160', '600161', '600166', '600170', '600171', '600176', '600177', '600179', '600188', '600195', '600196', '600201', '600216', '600219', '600221', '600233', '600240', '600256', '600259', '600260', '600266', '600271', '600277', '600282', '600307', '600312', '600315', '600316', '600329', '600332', '600335', '600339', '600340', '600348', '600352', '600366', '600369', '600373', '600376', '600380', '600388', '600390', '600392', '600398', '600406', '600409', '600418', '600435', '600436', '600458', '600466', '600482', '600487', '600489', '600499', '600503', '600507', '600511', '600516', '600518', '600519', '600522', '600525', '600535', '600536', '600545', '600549', '600557', '600563', '600565', '600566', '600567', '600572', '600575', '600582', '600583', '600585', '600594', '600600', '600606', '600611', '600614', '600618', '600623', '600633', '600639', '600642', '600643', '600649', '600655', '600657', '600660', '600664', '600688', '600690', '600694', '600703', '600704', '600705', '600718', '600729', '600737', '600739', '600741', '600743', '600748', '600750', '600751', '600755', '600760', '600765', '600770', '600782', '600787', '600804', '600808', '600809', '600816', '600827', '600837', '600848', '600859', '600862', '600872', '600874', '600875', '600879', '600881', '600884', '600886', '600887', '600895', '600900', '600901', '600908', '600909', '600917', '600919', '600926', '600939', '600958', '600971', '600977', '600978', '600996', '600998', '601000', '601001', '601006', '601012', '601016', '601018', '601019', '601020', '601021', '601088', '601098', '601106', '601111', '601117', '601138', '601155', '601166', '601168', '601169', '601179', '601186', '601200', '601211', '601212', '601216', '601225', '601228', '601229', '601238', '601288', '601311', '601336', '601360', '601377', '601390', '601398', '601555', '601598', '601601', '601608', '601618', '601669', '601678', '601688', '601689', '601727', '601788', '601808', '601811', '601828', '601838', '601857', '601866', '601869', '601872', '601878', '601880', '601881', '601898', '601899', '601901', '601919', '601928', '601933', '601958', '601969', '601985', '601988', '601989', '601990', '601991', '601992', '603000', '603025', '603077', '603169', '603198', '603225', '603233', '603259', '603260', '603288', '603328', '603355', '603377', '603444', '603515', '603556', '603568', '603658', '603659', '603712', '603766', '603799', '603806', '603816', '603833', '603858', '603866', '603868', '603883', '603885', '603993'] 

274 个测试集股票:
 ['000001', '000006', '000012', '000025', '000039', '000061', '000400', '000423', '000488', '000501', '000519', '000528', '000536', '000541', '000543', '000547', '000559', '000564', '000587', '000596', '000598', '000600', '000612', '000627', '000630', '000636', '000656', '000681', '000686', '000690', '000717', '000723', '000761', '000766', '000776', '000860', '000887', '000930', '000932', '000937', '000959', '000960', '000990', '000997', '002013', '002027', '002028', '002032', '002044', '002049', '002085', '002093', '002110', '002118', '002128', '002129', '002146', '002153', '002155', '002174', '002221', '002223', '002236', '002241', '002242', '002250', '002273', '002276', '002277', '002280', '002285', '002299', '002304', '002308', '002310', '002311', '002317', '002332', '002340', '002345', '002368', '002375', '002384', '002390', '002422', '002426', '002434', '002437', '002439', '002456', '002460', '002466', '002468', '002475', '002489', '002491', '002503', '002505', '002508', '002512', '002572', '002573', '002594', '002635', '002699', '002707', '002709', '002714', '002815', '002818', '002839', '002920', '300002', '300015', '300058', '300059', '300072', '300113', '300115', '300136', '300144', '300156', '300159', '300168', '300170', '300197', '300199', '300202', '300244', '300266', '300296', '300324', '300376', '300383', '300496', '300601', '300618', '300666', '300676', '300750', '600008', '600015', '600021', '600023', '600029', '600030', '600036', '600037', '600060', '600061', '600068', '600079', '600086', '600111', '600115', '600122', '600143', '600158', '600167', '600169', '600183', '600184', '600208', '600258', '600267', '600276', '600280', '600291', '600297', '600298', '600309', '600325', '600338', '600346', '600350', '600362', '600372', '600383', '600393', '600410', '600415', '600416', '600426', '600428', '600438', '600460', '600478', '600498', '600500', '600528', '600547', '600570', '600580', '600584', '600588', '600597', '600598', '600637', '600640', '600645', '600648', '600673', '600674', '600687', '600699', '600717', '600754', '600757', '600759', '600773', '600777', '600779', '600795', '600801', '600811', '600820', '600823', '600835', '600839', '600863', '600867', '600869', '600885', '600893', '600959', '600967', '600970', '600993', '600999', '601003', '601005', '601009', '601066', '601100', '601108', '601127', '601128', '601198', '601231', '601318', '601326', '601328', '601333', '601600', '601607', '601611', '601628', '601633', '601668', '601717', '601718', '601766', '601777', '601800', '601801', '601818', '601877', '601888', '601939', '601966', '601997', '601998', '603056', '603156', '603160', '603228', '603369', '603486', '603569', '603650', '603877', '603888', '603899', '603986']

用上面交割单数据训练机器人裁判

-

主裁:

内置主裁

In [7]:
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
simplefilter(action='ignore', category=ResourceWarning)

# 需要全局设置为A股市场,在ump会根据市场类型保存读取对应的ump
abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN

ump_deg=None
ump_jump=None
ump_mul=None
ump_price=None
ump_main_deg_extend=None
ump_wave=None

# 使用训练集交易数据训练主裁
orders_pd_train_cn = abu_result_tuple.orders_pd

def train_main_ump():
    print('AbuUmpMainDeg begin...') #brust_min=False,      
    AbuUmpMainDeg.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False, show_order=False)
 
    print('AbuUmpMainPrice begin...')
    AbuUmpMainPrice.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False, show_order=False)   
    
    print('AbuUmpMainJump begin...')
    AbuUmpMainJump.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False, show_order=False)
    
    print('AbuUmpMainWave begin...')
    AbuUmpMainWave.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False, show_order=False)  
       
    print('AbuUmpMainDegExtend begin...')
    AbuUmpMainDegExtend.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False, show_order=False)
    
    print('AbuUmpMainMul begin...')
    AbuUmpMainMul.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False, show_order=False)
        
    # 依然使用load_main_ump,避免下面多进程内存拷贝过大
    load_main_ump()
    
def load_main_ump():
    global ump_deg, ump_jump, ump_mul, ump_price, ump_main_deg_extend, ump_wave
    ump_deg = AbuUmpMainDeg(predict=True)
    ump_jump = AbuUmpMainJump(predict=True)
    ump_mul = AbuUmpMainMul(predict=True)
    ump_price = AbuUmpMainPrice(predict=True)
    ump_main_deg_extend = AbuUmpMainDegExtend(predict=True)
    ump_wave =  AbuUmpMainWave(predict=True )
    print('load main ump complete!')

def select(select):
    if select == 'train main ump':
        train_main_ump()
    else:
        load_main_ump()

# _ = ipywidgets.interact_manual(select, select=['load main ump', 'train main ump'])
load_main_ump()
load main ump complete!

自定义主裁

In [8]:
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
simplefilter(action='ignore', category=ResourceWarning)

abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN

ump_price_with_ma_deg = None
ump_ma_speed_with_ind_ma_deg_and_smlr = None
ump_index_poly_and_ma_deg = None
ump_poly_with_ma_deg = None 
ump_index_ma_deg = None 
ump_index_poly = None 
ump_ma_deg = None
ump_ma_speed = None
ump_poly = None

# 使用训练集交易数据训练主裁
orders_pd_train_cn = abu_result_tuple.orders_pd



def load_main_ump_user():
    global ump_price_with_ma_deg, ump_ma_speed_with_ind_ma_deg_and_smlr, ump_index_poly_and_ma_deg, ump_poly_with_ma_deg,\
        ump_index_ma_deg, ump_index_poly,  ump_ma_deg, ump_ma_speed, ump_poly
    
    ump_price_with_ma_deg = WzUmpMainPriceWithMaDeg(predict=True)
    ump_ma_speed_with_ind_ma_deg_and_smlr = WzUmpMainMaSpeedWithIndMaDegAndSmlr(predict=True)
    ump_index_poly_and_ma_deg = WzUmpMainIndexPolyAndMaDeg(predict=True)
    ump_poly_with_ma_deg = WzUmpMainPolyWithMaDeg(predict=True)
    ump_index_ma_deg = WzUmpMainIndexMaDeg(predict=True)
    ump_index_poly = WzUmpMainIndexPoly(predict=True)
    ump_ma_deg = WzUmpMainMaDeg(predict=True)
    ump_ma_speed = WzUmpMainMaSpeed(predict=True)
    ump_poly = WzUmpMainPoly(predict=True)
    
    print('load main ump user complete!')

def train_main_ump_user():
    
    print('WzUmpMainPriceWithMaDeg  begin...')
    WzUmpMainPriceWithMaDeg.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)
        
    print('WzUmpMainMaSpeedWithIndMaDegAndSmlr begin...')
    WzUmpMainMaSpeedWithIndMaDegAndSmlr.ump_main_clf_dump(orders_pd_train_cn,show_info=True, save_order=False,show_order=False)        
    
    print('WzUmpMainIndexPolyAndMaDeg begin...')
    WzUmpMainIndexPolyAndMaDeg.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)
    
    print('WzUmpMainIndexMaDeg...')
    WzUmpMainIndexMaDeg.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)
#     ump_ma_deg = WzUmpMainMaDeg.ump_main_clf_dump(orders_pd_train, p_ncs=slice(20, 40, 1))
#     ump_ma_deg.fiter.df.head()

    print('WzUmpMainIndexPoly...')
    WzUmpMainIndexPoly.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)    
    
    print('WzUmpMainPolyWithMaDeg begin...')
    WzUmpMainPolyWithMaDeg.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)       
    
    print('WzUmpMainMaDeg begin...')
    WzUmpMainMaDeg.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)    
    
    print('WzUmpMainMaSpeed begin...')
    WzUmpMainMaSpeed.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)
    
    print('WzUmpMainPoly...')
    WzUmpMainPoly.ump_main_clf_dump(orders_pd_train_cn, show_info=True, save_order=False,show_order=False)

    load_main_ump_user()
    print('train complete!')    

def select(select):
    if select == 'train main ump user':
        train_main_ump_user()
    else:
        load_main_ump_user()

# _ = ipywidgets.interact_manual(select, select=['load main ump', 'train main ump'])
load_main_ump_user()
load main ump user complete!

边裁

In [9]:
# from abupy import AbuUmpEdgeDeg, AbuUmpEdgePrice, AbuUmpEdgeWave, AbuUmpEdgeFull, AbuUmpEdgeMul, AbuUmpEegeDegExtend
# AbuUmpEdgeWave, AbuUmpEdgeFull, AbuUmpEdgeMul
# 需要全局设置为A股市场,在ump会根据市场类型保存读取对应的ump
abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN

# 使用训练集交易数据训练
orders_pd_train_cn = abu_result_tuple.orders_pd

print('WzUmpEdgePriceWithMaDeg begin...')
WzUmpEdgePriceWithMaDeg.ump_edge_clf_dump(orders_pd_train_cn)
edge_price_ma_deg = WzUmpEdgePriceWithMaDeg(predict=True)

print('WzUmpEdgeMaSpeedWithIndMaDegAndSmlr begin...')
WzUmpEdgeMaSpeedWithIndMaDegAndSmlr.ump_edge_clf_dump(orders_pd_train_cn)
edge_ma_speed_with_ind_ma_deg_and_smlr = WzUmpEdgeMaSpeedWithIndMaDegAndSmlr(predict=True)

print('WzUmpEdgeIndexPolyAndMaDeg begin...')
WzUmpEdgeIndexPolyAndMaDeg.ump_edge_clf_dump(orders_pd_train_cn)
edge_index_poly_and_ma_deg = WzUmpEdgeIndexPolyAndMaDeg(predict=True)

print('WzUmpEdgePolyWithMaDeg begin...')
WzUmpEdgePolyWithMaDeg.ump_edge_clf_dump(orders_pd_train_cn)
edge_poly_with_ma_deg = WzUmpEdgePolyWithMaDeg(predict=True)

print('WzUmpEdgeIndexMaDeg  begin...')
WzUmpEdgeIndexMaDeg.ump_edge_clf_dump(orders_pd_train_cn)
edge_index_ma_deg = WzUmpEdgeIndexMaDeg(predict=True)

print('WzUmpEdgeIndexPoly  begin...')
WzUmpEdgeIndexPoly.ump_edge_clf_dump(orders_pd_train_cn)
edge_index_poly = WzUmpEdgeIndexPoly(predict=True)

print('WzUmpEdgeMaDeg begin...')
WzUmpEdgeMaDeg.ump_edge_clf_dump(orders_pd_train_cn)
edge_ma_deg = WzUmpEdgeMaDeg(predict=True)

print('WzUmpEdgeMaSpeed begin...')
WzUmpEdgeMaSpeed.ump_edge_clf_dump(orders_pd_train_cn)
edge_ma_speed = WzUmpEdgeMaSpeed(predict=True)

print('WzUmpEdgePoly  begin...')
WzUmpEdgePoly.ump_edge_clf_dump(orders_pd_train_cn)
edge_poly = WzUmpEdgePoly(predict=True)


# 内置的
print('AbuUmpEdgeDeg begin...')
AbuUmpEdgeDeg.ump_edge_clf_dump(orders_pd_train_cn)
edge_deg = AbuUmpEdgeDeg(predict=True)

print('AbuUmpEdgePrice begin...')
AbuUmpEdgePrice.ump_edge_clf_dump(orders_pd_train_cn)
edge_price = AbuUmpEdgePrice(predict=True)

print('AbuUmpEdgeMul begin...')
AbuUmpEdgeMul.ump_edge_clf_dump(orders_pd_train_cn)
edge_mul = AbuUmpEdgeMul(predict=True)

print('AbuUmpEegeDegExtend begin...')
AbuUmpEegeDegExtend.ump_edge_clf_dump(orders_pd_train_cn)
edge_deg_extend = AbuUmpEegeDegExtend(predict=True)

print('AbuUmpEdgeWave begin...')
AbuUmpEdgeWave.ump_edge_clf_dump(orders_pd_train_cn)
edge_wave = AbuUmpEdgeWave(predict=True)

print('AbuUmpEdgeFull begin...')
AbuUmpEdgeFull.ump_edge_clf_dump(orders_pd_train_cn)
edge_full = AbuUmpEdgeFull(predict=True)

print('fit edge complete!')
print(moment.utcnow().timezone("Asia/Shanghai").format("YYYYMMDD h:m:s A"))
WzUmpEdgePriceWithMaDeg begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_price_with_ma_deg_edge
WzUmpEdgeMaSpeedWithIndMaDegAndSmlr begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_ma_speed_ind_ma_deg_and_smlr_edge
WzUmpEdgeIndexPolyAndMaDeg begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_index_poly_and_ma_deg_edge
WzUmpEdgePolyWithMaDeg begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_poly_with_ma_deg_edge
WzUmpEdgeIndexMaDeg  begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_index_ma_deg_edge
WzUmpEdgeIndexPoly  begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_index_poly_edge
WzUmpEdgeMaDeg begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_ma_deg_edge
WzUmpEdgeMaSpeed begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_ma_speed_edge
WzUmpEdgePoly  begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_poly_edge
AbuUmpEdgeDeg begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_deg_edge
AbuUmpEdgePrice begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_price_edge
AbuUmpEdgeMul begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_mul_edge
AbuUmpEegeDegExtend begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_extend_edge_deg
AbuUmpEdgeWave begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_wave_edge
AbuUmpEdgeFull begin...
please wait! dump_pickle....: /root/abu/data/ump/ump_edge_hs_full_edge
fit edge complete!
20191229  3:43:36 PM

机器人裁判的应用

未启用裁判的测试集 - 用于对比:

In [7]:
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
simplefilter(action='ignore', category=ResourceWarning)

# 测试集回测时依然生成买入时刻特征
# abupy.env.g_enable_ml_feature = True #True
# 回测时不重新切割训练集数据和测试集
abupy.env.g_enable_train_test_split = False
# 回测时使用切割好的测试数据
abupy.env.g_enable_last_split_test = True


# 测试集依然使用10,30,50,90,120日走势拟合角度特征AbuFeatureDegExtend,做为回测时的新的视角来录制比赛(记录回测特征)
# feature.clear_user_feature()
# feature.append_user_feature(AbuFeatureDegExtend)

read_cash = 50000000 #5000w资金
o_pos_base =0.1 #abupy.beta.atr.g_atr_pos_base
print('o_pos_base:', o_pos_base)
abupy.beta.atr.g_atr_pos_base =  o_pos_base /6
print('abupy.beta.atr.g_atr_pos_base:' , abupy.beta.atr.g_atr_pos_base)


abu_result_tuple_test = None
order_has_result = None

def run_loop_back_test():
    global abu_result_tuple_test, order_has_result
    abu_result_tuple_test, _ = abu.run_loop_back(read_cash,
                                                 buy_factors,
                                                 sell_factors,
                                                 choice_symbols=None,
                                                 start='2016-02-02', end='2019-12-18',
                                                    n_process_kl = 1,
                                                    n_process_pick= 8,
                                                )
    # 把运行的结果保存在本地,以便之后分析回测使用,保存回测结果数据代码如下所示
    abu.store_abu_result_tuple(abu_result_tuple_test, n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                               custom_name='test_cn')
    ABuProgress.clear_output()
    AbuMetricsBase.show_general(*abu_result_tuple_test, only_show_returns=True).plot_max_draw_down()
    # 验证A股主裁是否称职
    # 选取有交易结果的数据order_has_result
    order_has_result = abu_result_tuple_test.orders_pd[abu_result_tuple_test.orders_pd.result != 0]
    order_has_result.filter(regex='^buy(_deg_|_price_|_wave_|_jump)').head()

def run_load_test():
    global abu_result_tuple_test, order_has_result
    abu_result_tuple_test = abu.load_abu_result_tuple(n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                                                 custom_name='test_cn')
    AbuMetricsBase.show_general(*abu_result_tuple_test, only_show_returns=True).plot_max_draw_down()
    # 验证A股主裁是否称职
    # 选取有交易结果的数据order_has_result
    order_has_result = abu_result_tuple_test.orders_pd[abu_result_tuple_test.orders_pd.result != 0]
    order_has_result.filter(regex='^buy(_deg_|_price_|_wave_|_jump)').head()

def select_test(select):
    if select == 'run loop back':
        run_loop_back_test()
    else:
        run_load_test()

# _ = ipywidgets.interact_manual(select_test, select=['load test data', 'run loop back',])
run_loop_back_test()
买入后卖出的交易数量:24170
买入后尚未卖出的交易数量:712
胜率:38.3533%
平均获利期望:6.6287%
平均亏损期望:-4.1943%
盈亏比:1.0012
策略收益: -38.7098%
基准收益: 8.9269%
策略年化收益: -10.3226%
基准年化收益: 2.3805%
策略买入成交比例:26.9110%
策略资金利用率比例:80.4678%
策略共执行945个交易日
最大回撤: 0.475819
最大回测启始时间:2016-11-22, 结束时间2019-01-31, 共回测25473912.105000

上面是没有启用机器人裁判的成绩。

以下启用机器人裁判,看看效果

单使用边裁

In [9]:
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
# simplefilter(action='ignore', category=ResourceWarning)


abupy.env.g_data_cache_type = EDataCacheType.E_DATA_CACHE_CSV
abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN
abupy.env.g_data_fetch_mode = EMarketDataFetchMode.E_DATA_FETCH_FORCE_LOCAL


# 开启内置主裁
abupy.env.g_enable_ump_main_deg_block = False
# abupy.env.g_enable_ump_main_price_block = True

# 开启内置边裁
abupy.env.g_enable_ump_edge_deg_block = True
# abupy.env.g_enable_ump_edge_price_block = True

# 回测时需要开启特征生成,因为裁判开启需要生成特征做为输入
abupy.env.g_enable_ml_feature = True
# 回测时使用上一次切割好的测试集数据
abupy.env.g_enable_last_split_test = True


"""    """
feature.clear_user_feature()
# 10,30,50,90,120日走势拟合角度特征的AbuFeatureDegExtend,做为回测时的新的视角来录制比赛
feature.append_user_feature(AbuFeatureDegExtend)

feature.append_user_feature(WzFeatureMaDeg)
feature.append_user_feature(WzFeatureIndexMaDeg)
feature.append_user_feature(WzFeatureMaSpeed)
feature.append_user_feature(WzFeatureIndexPoly)
feature.append_user_feature(WzFeaturePoly) # <- 问题嫌疑
feature.append_user_feature(WzFeatureIndexSimilarity)

# 打开使用用户自定义裁判开关
ump.manager.g_enable_user_ump = True
# 先clear一下
ump.manager.clear_user_ump()
# 把新的裁判AbuUmpMainDegExtend类名称使用append_user_ump添加到系统中
ump.manager.append_user_ump(AbuUmpEegeDegExtend)

# 机器人裁判,通过对比 个股买入点前n*1,n*2,n*x天至买入当天的均线角度 和 对应大盘
# 的均线角度 得知该股是否跑赢大盘,以及跑赢百分比
ump.manager.append_user_ump(WzUmpEdgeMaSpeed)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的价格rank(相当于压力位/支撑位)和
# 均线角度
ump.manager.append_user_ump(WzUmpEdgePriceWithMaDeg)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的个股对应
# 大盘指数(上证50,上证,深证,创业)的均线角度
ump.manager.append_user_ump(WzUmpEdgeIndexMaDeg)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的个股对应大盘指数的处于
# 趋势or震荡(采用多项式拟合技术)
ump.manager.append_user_ump(WzUmpEdgeIndexPoly)
# 下面就不一样介绍了
ump.manager.append_user_ump(WzUmpEdgeMaDeg)
ump.manager.append_user_ump(WzUmpEdgePoly)

ump.manager.append_user_ump(WzUmpEdgeMaSpeedWithIndMaDegAndSmlr)
ump.manager.append_user_ump(WzUmpEdgeIndexPolyAndMaDeg)
ump.manager.append_user_ump(WzUmpEdgePolyWithMaDeg)


# 把新的裁判AbuUmpMainDegExtend类名称使用append_user_ump添加到系统中
# ump.manager.append_user_ump(AbuUmpMainDegExtend)

#
# ump.manager.append_user_ump(WzUmpMainMaDeg)
# ump.manager.append_user_ump(WzUmpMainMaSpeed)
# ump.manager.append_user_ump(WzUmpMainIndexMaDeg)
# ump.manager.append_user_ump(WzUmpMainIndexPoly)
# # ump.manager.append_user_ump(WzUmpMainPoly)

# ump.manager.append_user_ump(WzUmpMainMaSpeedWithIndMaDegAndSmlr)
# ump.manager.append_user_ump(WzUmpMainIndexPolyAndMaDeg)
# ump.manager.append_user_ump(WzUmpMainPolyWithMaDeg)


read_cash = 10000000 #资金
o_pos_base =0.1 #abupy.beta.atr.g_atr_pos_base
print('o_pos_base:', o_pos_base)
pt_pos_base =  o_pos_base / 6
print('pt_pos_base:' , pt_pos_base)


buy_factors=[{'buy_default': 'post=36.88',
  'class': WzFactorBuyRsiV8,
  'position': {'class': AbuPtPosition,
               'past_day_cnt': 68,
               'pos_base': pt_pos_base },
  'rsi_timeperiod': 6,
  'stock_pickers': [{'class': WzPickNonNew,
                     'pick_period': 'week',
                     'weeks': 24},
                  ]
             }]

abu_result_tuple_test_ump = None

def run_loop_back_ump():
    global abu_result_tuple_test_ump, read_cash
    abu_result_tuple_test_ump, _ = abu.run_loop_back(read_cash,
                                            buy_factors,
                                            sell_factors,
#                                             choice_symbols = symbols,
                                            choice_symbols=None,
#                                             stock_picks= stock_pickers,
                                            start='2016-02-02', end='2019-12-18',
                                            n_process_kl = 8,
                                            n_process_pick= 8,
                                        )
    # 把运行的结果保存在本地,以便之后分析回测使用,保存回测结果数据代码如下所示
    abu.store_abu_result_tuple(abu_result_tuple_test_ump, n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                               custom_name='test_ump_cn')
    ABuProgress.clear_output()
    AbuMetricsBase.show_general(*abu_result_tuple_test_ump).plot_max_draw_down() #, returns_cmp=True

def run_load_ump():
    global abu_result_tuple_test_ump
    abu_result_tuple_test_ump = abu.load_abu_result_tuple(n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                                                 custom_name='test_ump_cn')
    AbuMetricsBase.show_general(*abu_result_tuple_test_ump).plot_max_draw_down() #, returns_cmp=True

def select_ump(select):
    if select == 'run loop back ump':
        run_loop_back_ump()
    else:
        run_load_ump()

# _ = ipywidgets.interact_manual(select_ump, select=['load test ump data','run loop back ump'])

# print("随便输出点什么表示已经执行过: ")
run_loop_back_ump()

# order_has_result.filter(regex='^buy(_deg_|_price_|_wave_|_jump)').head()
买入后卖出的交易数量:3103
买入后尚未卖出的交易数量:66
胜率:61.7145%
平均获利期望:7.3375%
平均亏损期望:-3.9325%
盈亏比:3.0760
策略收益: 115.2355%
基准收益: 8.9269%
策略年化收益: 30.7295%
基准年化收益: 2.3805%
策略买入成交比例:73.0514%
策略资金利用率比例:45.2427%
策略共执行945个交易日
alpha阿尔法:0.1970
beta贝塔:0.3426
Information信息比率:0.0804
策略Sharpe夏普比率: 2.1219
基准Sharpe夏普比率: 0.2208
策略波动率Volatility: 0.0988
基准波动率Volatility: 0.1656
最大回撤: 0.053874
最大回测启始时间:2018-03-12, 结束时间2018-07-11, 共回测893738.269000

边裁 + 3个自定义主裁

In [13]:
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
# simplefilter(action='ignore', category=ResourceWarning)


abupy.env.g_data_cache_type = EDataCacheType.E_DATA_CACHE_CSV
abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN
abupy.env.g_data_fetch_mode = EMarketDataFetchMode.E_DATA_FETCH_FORCE_LOCAL


# 开启内置主裁
abupy.env.g_enable_ump_main_deg_block = False
abupy.env.g_enable_ump_main_jump_block = True
# abupy.env.g_enable_ump_main_price_block = True

# 开启内置边裁
abupy.env.g_enable_ump_edge_deg_block = True
abupy.env.g_enable_ump_edge_price_block = True
abupy.g_enable_ump_edge_wave_block = True
abupy.g_enable_ump_edge_full_block = True

# 回测时需要开启特征生成,因为裁判开启需要生成特征做为输入
abupy.env.g_enable_ml_feature = True
# 回测时使用上一次切割好的测试集数据
abupy.env.g_enable_last_split_test = True


"""    """
feature.clear_user_feature()
# 10,30,50,90,120日走势拟合角度特征的AbuFeatureDegExtend,做为回测时的新的视角来录制比赛
feature.append_user_feature(AbuFeatureDegExtend)

feature.append_user_feature(WzFeatureMaDeg)
feature.append_user_feature(WzFeatureIndexMaDeg)
feature.append_user_feature(WzFeatureMaSpeed)
feature.append_user_feature(WzFeatureIndexPoly)
feature.append_user_feature(WzFeaturePoly) # <- 问题嫌疑
feature.append_user_feature(WzFeatureIndexSimilarity)

# 打开使用用户自定义裁判开关
ump.manager.g_enable_user_ump = True
# 先clear一下
ump.manager.clear_user_ump()
# 把新的裁判AbuUmpMainDegExtend类名称使用append_user_ump添加到系统中
ump.manager.append_user_ump(AbuUmpEegeDegExtend)

# 机器人裁判,通过对比 个股买入点前n*1,n*2,n*x天至买入当天的均线角度 和 对应大盘
# 的均线角度 得知该股是否跑赢大盘,以及跑赢百分比
ump.manager.append_user_ump(WzUmpEdgeMaSpeed)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的价格rank(相当于压力位/支撑位)和
# 均线角度
ump.manager.append_user_ump(WzUmpEdgePriceWithMaDeg)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的个股对应
# 大盘指数(上证50,上证,深证,创业)的均线角度
ump.manager.append_user_ump(WzUmpEdgeIndexMaDeg)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的个股对应大盘指数的处于
# 趋势or震荡(采用多项式拟合技术)
ump.manager.append_user_ump(WzUmpEdgeIndexPoly)
# 下面就不一样介绍了
ump.manager.append_user_ump(WzUmpEdgeMaDeg)
ump.manager.append_user_ump(WzUmpEdgePoly)

ump.manager.append_user_ump(WzUmpEdgeMaSpeedWithIndMaDegAndSmlr)
ump.manager.append_user_ump(WzUmpEdgeIndexPolyAndMaDeg)
ump.manager.append_user_ump(WzUmpEdgePolyWithMaDeg)


# 把新的裁判AbuUmpMainDegExtend类名称使用append_user_ump添加到系统中
# ump.manager.append_user_ump(AbuUmpMainDegExtend)

#
ump.manager.append_user_ump(WzUmpMainMaDeg)
# ump.manager.append_user_ump(WzUmpMainMaSpeed)
# ump.manager.append_user_ump(WzUmpMainIndexMaDeg)
# ump.manager.append_user_ump(WzUmpMainIndexPoly)
# # ump.manager.append_user_ump(WzUmpMainPoly)

# ump.manager.append_user_ump(WzUmpMainMaSpeedWithIndMaDegAndSmlr)
# ump.manager.append_user_ump(WzUmpMainIndexPolyAndMaDeg)
ump.manager.append_user_ump(WzUmpMainPolyWithMaDeg)
ump.manager.append_user_ump(WzUmpMainPriceWithMaDeg)


read_cash = 10000000 #资金
o_pos_base =0.1 #abupy.beta.atr.g_atr_pos_base
print('o_pos_base:', o_pos_base)
pt_pos_base =  o_pos_base / 1.0
print('pt_pos_base:' , pt_pos_base)


buy_factors=[{'buy_default': 'post=36.88',
  'class': WzFactorBuyRsiV8,
  'position': {'class': AbuPtPosition,
               'past_day_cnt': 68,
               'pos_base': pt_pos_base },
  'rsi_timeperiod': 6,
  'stock_pickers': [{'class': WzPickNonNew,
                     'pick_period': 'week',
                     'weeks': 24},
                  ]
             }]

abu_result_tuple_test_ump = None

def run_loop_back_ump():
    global abu_result_tuple_test_ump, read_cash
    abu_result_tuple_test_ump, _ = abu.run_loop_back(read_cash,
                                            buy_factors,
                                            sell_factors,
#                                             choice_symbols = symbols,
                                            choice_symbols=None,
#                                             stock_picks= stock_pickers,
                                            start='2016-02-02', end='2019-12-18',
#                                             n_process_kl = 8,
#                                             n_process_pick= 8,
                                        )
    # 把运行的结果保存在本地,以便之后分析回测使用,保存回测结果数据代码如下所示
    abu.store_abu_result_tuple(abu_result_tuple_test_ump, n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                               custom_name='test_ump_cn_')
    ABuProgress.clear_output()
    AbuMetricsBase.show_general(*abu_result_tuple_test_ump).plot_max_draw_down() #, returns_cmp=True

def run_load_ump():
    global abu_result_tuple_test_ump
    abu_result_tuple_test_ump = abu.load_abu_result_tuple(n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                                                 custom_name='test_ump_cn_')
    AbuMetricsBase.show_general(*abu_result_tuple_test_ump).plot_max_draw_down() #, returns_cmp=True

def select_ump(select):
    if select == 'run loop back ump':
        run_loop_back_ump()
    else:
        run_load_ump()

# _ = ipywidgets.interact_manual(select_ump, select=['load test ump data','run loop back ump'])

# print("随便输出点什么表示已经执行过: ")
run_loop_back_ump()

# order_has_result.filter(regex='^buy(_deg_|_price_|_wave_|_jump)').head()
买入后卖出的交易数量:1545
买入后尚未卖出的交易数量:36
胜率:64.5307%
平均获利期望:7.2300%
平均亏损期望:-3.8722%
盈亏比:3.5465
策略收益: 104.1546%
基准收益: 8.9269%
策略年化收益: 27.7746%
基准年化收益: 2.3805%
策略买入成交比例:31.2460%
策略资金利用率比例:55.9236%
策略共执行945个交易日
alpha阿尔法:0.1828
beta贝塔:0.4181
Information信息比率:0.0727
策略Sharpe夏普比率: 1.6135
基准Sharpe夏普比率: 0.2208
策略波动率Volatility: 0.1228
基准波动率Volatility: 0.1656
最大回撤: 0.068424
最大回测启始时间:2019-04-10, 结束时间2019-06-06, 共回测1305647.487000

边裁 + 3个自定义主裁。

上面买入成交比例过小,存在运气成分。 下面降低单只股单次买入的仓位,以提高买入成交率

In [14]:
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
# simplefilter(action='ignore', category=ResourceWarning)


abupy.env.g_data_cache_type = EDataCacheType.E_DATA_CACHE_CSV
abupy.env.g_market_target = EMarketTargetType.E_MARKET_TARGET_CN
abupy.env.g_data_fetch_mode = EMarketDataFetchMode.E_DATA_FETCH_FORCE_LOCAL


# 开启内置主裁
abupy.env.g_enable_ump_main_deg_block = False
abupy.env.g_enable_ump_main_jump_block = True
# abupy.env.g_enable_ump_main_price_block = True

# 开启内置边裁
abupy.env.g_enable_ump_edge_deg_block = True
abupy.env.g_enable_ump_edge_price_block = True
abupy.g_enable_ump_edge_wave_block = True
abupy.g_enable_ump_edge_full_block = True

# 回测时需要开启特征生成,因为裁判开启需要生成特征做为输入
abupy.env.g_enable_ml_feature = True
# 回测时使用上一次切割好的测试集数据
abupy.env.g_enable_last_split_test = True


"""    """
feature.clear_user_feature()
# 10,30,50,90,120日走势拟合角度特征的AbuFeatureDegExtend,做为回测时的新的视角来录制比赛
feature.append_user_feature(AbuFeatureDegExtend)

feature.append_user_feature(WzFeatureMaDeg)
feature.append_user_feature(WzFeatureIndexMaDeg)
feature.append_user_feature(WzFeatureMaSpeed)
feature.append_user_feature(WzFeatureIndexPoly)
feature.append_user_feature(WzFeaturePoly) # <- 问题嫌疑
feature.append_user_feature(WzFeatureIndexSimilarity)

# 打开使用用户自定义裁判开关
ump.manager.g_enable_user_ump = True
# 先clear一下
ump.manager.clear_user_ump()
# 把新的裁判AbuUmpMainDegExtend类名称使用append_user_ump添加到系统中
ump.manager.append_user_ump(AbuUmpEegeDegExtend)

# 机器人裁判,通过对比 个股买入点前n*1,n*2,n*x天至买入当天的均线角度 和 对应大盘
# 的均线角度 得知该股是否跑赢大盘,以及跑赢百分比
ump.manager.append_user_ump(WzUmpEdgeMaSpeed)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的价格rank(相当于压力位/支撑位)和
# 均线角度
ump.manager.append_user_ump(WzUmpEdgePriceWithMaDeg)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的个股对应
# 大盘指数(上证50,上证,深证,创业)的均线角度
ump.manager.append_user_ump(WzUmpEdgeIndexMaDeg)
# 机器人裁判,观察买入点前n*1,n*2,n*x天至买入当天的个股对应大盘指数的处于
# 趋势or震荡(采用多项式拟合技术)
ump.manager.append_user_ump(WzUmpEdgeIndexPoly)
# 下面就不一样介绍了
ump.manager.append_user_ump(WzUmpEdgeMaDeg)
ump.manager.append_user_ump(WzUmpEdgePoly)

ump.manager.append_user_ump(WzUmpEdgeMaSpeedWithIndMaDegAndSmlr)
ump.manager.append_user_ump(WzUmpEdgeIndexPolyAndMaDeg)
ump.manager.append_user_ump(WzUmpEdgePolyWithMaDeg)


# 把新的裁判AbuUmpMainDegExtend类名称使用append_user_ump添加到系统中
# ump.manager.append_user_ump(AbuUmpMainDegExtend)

#
ump.manager.append_user_ump(WzUmpMainMaDeg)
# ump.manager.append_user_ump(WzUmpMainMaSpeed)
# ump.manager.append_user_ump(WzUmpMainIndexMaDeg)
# ump.manager.append_user_ump(WzUmpMainIndexPoly)
# # ump.manager.append_user_ump(WzUmpMainPoly)

# ump.manager.append_user_ump(WzUmpMainMaSpeedWithIndMaDegAndSmlr)
# ump.manager.append_user_ump(WzUmpMainIndexPolyAndMaDeg)
ump.manager.append_user_ump(WzUmpMainPolyWithMaDeg)
ump.manager.append_user_ump(WzUmpMainPriceWithMaDeg)


read_cash = 10000000 #资金
o_pos_base =0.1 #abupy.beta.atr.g_atr_pos_base
print('o_pos_base:', o_pos_base)
pt_pos_base =  o_pos_base / 2.0
print('pt_pos_base:' , pt_pos_base)


buy_factors=[{'buy_default': 'post=36.88',
  'class': WzFactorBuyRsiV8,
  'position': {'class': AbuPtPosition,
               'past_day_cnt': 68,
               'pos_base': pt_pos_base },
  'rsi_timeperiod': 6,
  'stock_pickers': [{'class': WzPickNonNew,
                     'pick_period': 'week',
                     'weeks': 24},
                  ]
             }]

abu_result_tuple_test_ump = None

def run_loop_back_ump():
    global abu_result_tuple_test_ump, read_cash
    abu_result_tuple_test_ump, _ = abu.run_loop_back(read_cash,
                                            buy_factors,
                                            sell_factors,
#                                             choice_symbols = symbols,
                                            choice_symbols=None,
#                                             stock_picks= stock_pickers,
                                            start='2016-02-02', end='2019-12-18',
#                                             n_process_kl = 8,
#                                             n_process_pick= 8,
                                        )
    # 把运行的结果保存在本地,以便之后分析回测使用,保存回测结果数据代码如下所示
    abu.store_abu_result_tuple(abu_result_tuple_test_ump, n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                               custom_name='test_ump_cn_a')
    ABuProgress.clear_output()
    AbuMetricsBase.show_general(*abu_result_tuple_test_ump).plot_max_draw_down() #, returns_cmp=True

def run_load_ump():
    global abu_result_tuple_test_ump
    abu_result_tuple_test_ump = abu.load_abu_result_tuple(n_folds=3, store_type=EStoreAbu.E_STORE_CUSTOM_NAME, 
                                                 custom_name='test_ump_cn_a')
    AbuMetricsBase.show_general(*abu_result_tuple_test_ump).plot_max_draw_down() #, returns_cmp=True

def select_ump(select):
    if select == 'run loop back ump':
        run_loop_back_ump()
    else:
        run_load_ump()

# _ = ipywidgets.interact_manual(select_ump, select=['load test ump data','run loop back ump'])

# print("随便输出点什么表示已经执行过: ")
run_loop_back_ump()

# order_has_result.filter(regex='^buy(_deg_|_price_|_wave_|_jump)').head()
买入后卖出的交易数量:1911
买入后尚未卖出的交易数量:48
胜率:61.8001%
平均获利期望:7.2466%
平均亏损期望:-3.9527%
盈亏比:3.0422
策略收益: 86.5326%
基准收益: 8.9269%
策略年化收益: 23.0754%
基准年化收益: 2.3805%
策略买入成交比例:43.2874%
策略资金利用率比例:51.8363%
策略共执行945个交易日