克隆策略
期货日线MACD策略¶
版本 v1.0
目录¶
-
### MACD策略的交易规则
-
### 策略构建步骤
-
### 策略的实现
正文¶
一、MACD策略的交易规则¶
-
相关指标定义如下:
DIF=EMA(close,12)−EMA(close,26)
DEM=EMA(DIF,9)
-
DIF从下而上穿过DEA,买入开仓;
- DIF从上往下穿过DEA,卖出开仓;
二、策略构建步骤¶
1、确定期货合约和回测时间¶
- 通过证券代码列表输入要回测的期货合约,以及回测的起止日期
2、确定买卖条件信号¶
- 通过自定义Python模块m4获取合约基础数据,通过自定义Python模块m1获取DIF和DEA指标数据;
- 在输入特征列表中通过表达式引擎定义 buy_condition=where((shift(DIF,1) > shift(DEA,1)) & (shift(DIF,2)<shift(DEA,2)),1,0),实现买入信号。
- 在输入特征列表中通过表达式引擎定义 sell_condition=where((shift(DIF,1) < shift(DEA,1)) & (shift(DIF,2)>shift(DEA,2)),1,0),实现卖出信号。
- 通过衍生特征抽取模块实现买卖条件指标 buy_condition 和 sell_condition 数据的抽取。
- 通过缺失数据处理模块删去有缺失值的数据。
3、确定买卖原则¶
- 如果当日 buy_condition > 0,执行平空开多操作;
- 如果当日 sell_condition > 0,执行平多开空操作。
4、模拟回测¶
- 通过 trade 模块中的初始化函数定义交易手续费、滑点、杠杆比例和是否逐日结算;
- 通过 trade 模块中的准备函数定义 context.buy_condition 和 context.sell_condition 变量来获取并存放每日买卖交易信号;
- 通过 trade 模块中的主函数(handle函数)查看每日的交易信号,按照买卖原则执行相应的交易操作。
三、策略的实现¶
可视化策略实现如下:
{"Description":"实验创建于2018/10/16","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-1442:instruments","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-1483:input_1","SourceOutputPortId":"-25:data"},{"DestinationInputPortId":"-327:input_1","SourceOutputPortId":"-1483:data_1"},{"DestinationInputPortId":"-1442:options_data","SourceOutputPortId":"-1632:data"},{"DestinationInputPortId":"-336:input_data","SourceOutputPortId":"-327:data_1"},{"DestinationInputPortId":"-1632:input_data","SourceOutputPortId":"-336:data"},{"DestinationInputPortId":"-336:features","SourceOutputPortId":"-342:data"}],"ModuleNodes":[{"Id":"-25","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2018-04-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"2019-1-01","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"market","Value":"CN_FUTURE","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"I1905.DCE","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":0,"ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"-25"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-25","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1442","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n # 获取当日多头/空头信号数据\n today = data.current_dt.strftime('%Y-%m-%d') # 当前交易日期\n \n try:\n buy_condition = context.buy_condition[today]\n except:\n buy_condition = 0\n \n try:\n sell_condition = context.sell_condition[today]\n except:\n sell_condition = 0\n \n instrument = context.future_symbol(context.instruments[0]) # 交易标的\n curr_po=context.portfolio.positions[instrument] # 组合持仓\n curr_position = curr_po.amount # 持仓数量\n \n # 交易逻辑\n if sell_condition>0: # 卖出开仓\n if curr_position >= 0 and data.can_trade(instrument):\n order_target(instrument, -20)\n print(today,'今日DIF下穿DEA,平多,卖空开仓')\n elif buy_condition>0:# 买入开仓\n if curr_position <= 0 and data.can_trade(instrument):\n order_target(instrument, 20)\n print(today,'今日DIF上穿DEA,平空,买多开仓')","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n df = context.options['data'].read_df()\n df['date']=df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))\n df.set_index('date',inplace=True)\n context.buy_condition=df['buy_condition']\n context.sell_condition=df['sell_condition']\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 设置是否是结算模式\n context.set_need_settle(False)\n # 设置最大杠杆\n context.set_max_leverage(1, 'fill_amap')\n\n ","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"1000000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"期货","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-1442"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-1442"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-1442","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1483","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3,before_days):\n # 示例代码如下。在这里编写您的代码\n start_date=(pd.to_datetime(input_1.read_pickle()['start_date']) - datetime.timedelta(days=before_days)).strftime('%Y-%m-%d')\n end_date=input_1.read_pickle()['end_date']\n instruments=input_1.read_pickle()['instruments']\n fields=['open','high','low','close']\n df = DataSource('bar1d_CN_FUTURE').read(instruments,start_date,end_date,fields)\n df['adjust_factor']=1.0\n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{'before_days':60}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-1483"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-1483"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-1483"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-1483","OutputType":null},{"Name":"data_2","NodeId":"-1483","OutputType":null},{"Name":"data_3","NodeId":"-1483","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":4,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-1632","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-1632"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-1632","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-327","ModuleId":"BigQuantSpace.cached.cached-v3","ModuleParameters":[{"Name":"run","Value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n # 示例代码如下。在这里编写您的代码\n df = input_1.read_df()\n close = [float(x) for x in df['close']]\n import talib\n # 调用talib计算6日指数移动平均线的值\n df['EMA12'] = talib.EMA(np.array(close), timeperiod=6) \n df['EMA26'] = talib.EMA(np.array(close), timeperiod=12) \n # 调用talib计算MACD指标\n df['DIF'],df['DEA'],df['MACD'] = talib.MACD(np.array(close),\n fastperiod=6, slowperiod=12, signalperiod=9) \n data_1 = DataSource.write_df(df)\n return Outputs(data_1=data_1, data_2=None, data_3=None)\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"post_run","Value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"input_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"params","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"output_ports","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_1","NodeId":"-327"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_2","NodeId":"-327"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_3","NodeId":"-327"}],"OutputPortsInternal":[{"Name":"data_1","NodeId":"-327","OutputType":null},{"Name":"data_2","NodeId":"-327","OutputType":null},{"Name":"data_3","NodeId":"-327","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-336","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"{}","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-336"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-336"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-336","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":5,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-342","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"\n# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nbuy_condition=where((shift(DIF,1)>shift(DEA,1))&(shift(DIF,2)<shift(DEA,2)),1,0)\nsell_condition=where((shift(DIF,1)<shift(DEA,1))&(shift(DIF,2)>shift(DEA,2)),1,0)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"-342"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-342","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='-25' Position='47,2,200,200'/><NodePosition Node='-1442' Position='-34,451,200,200'/><NodePosition Node='-1483' Position='212,96,200,200'/><NodePosition Node='-1632' Position='321,359,200,200'/><NodePosition Node='-327' Position='309,193,200,200'/><NodePosition Node='-336' Position='387,284,200,200'/><NodePosition Node='-342' Position='618,193,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
In [12]:
# 本代码由可视化策略环境自动生成 2019年1月28日 14:03
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
def m4_run_bigquant_run(input_1, input_2, input_3,before_days):
# 示例代码如下。在这里编写您的代码
start_date=(pd.to_datetime(input_1.read_pickle()['start_date']) - datetime.timedelta(days=before_days)).strftime('%Y-%m-%d')
end_date=input_1.read_pickle()['end_date']
instruments=input_1.read_pickle()['instruments']
fields=['open','high','low','close']
df = DataSource('bar1d_CN_FUTURE').read(instruments,start_date,end_date,fields)
df['adjust_factor']=1.0
data_1 = DataSource.write_df(df)
return Outputs(data_1=data_1, data_2=None, data_3=None)
# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m4_post_run_bigquant_run(outputs):
return outputs
# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
def m1_run_bigquant_run(input_1, input_2, input_3):
# 示例代码如下。在这里编写您的代码
df = input_1.read_df()
close = [float(x) for x in df['close']]
import talib
# 调用talib计算6日指数移动平均线的值
df['EMA12'] = talib.EMA(np.array(close), timeperiod=6)
df['EMA26'] = talib.EMA(np.array(close), timeperiod=12)
# 调用talib计算MACD指标
df['DIF'],df['DEA'],df['MACD'] = talib.MACD(np.array(close),
fastperiod=6, slowperiod=12, signalperiod=9)
data_1 = DataSource.write_df(df)
return Outputs(data_1=data_1, data_2=None, data_3=None)
# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
def m1_post_run_bigquant_run(outputs):
return outputs
# 回测引擎:每日数据处理函数,每天执行一次
def m3_handle_data_bigquant_run(context, data):
# 获取当日多头/空头信号数据
today = data.current_dt.strftime('%Y-%m-%d') # 当前交易日期
try:
buy_condition = context.buy_condition[today]
except:
buy_condition = 0
try:
sell_condition = context.sell_condition[today]
except:
sell_condition = 0
instrument = context.future_symbol(context.instruments[0]) # 交易标的
curr_po=context.portfolio.positions[instrument] # 组合持仓
curr_position = curr_po.amount # 持仓数量
# 交易逻辑
if sell_condition>0: # 卖出开仓
if curr_position >= 0 and data.can_trade(instrument):
order_target(instrument, -20)
print(today,'今日DIF下穿DEA,平多,卖空开仓')
elif buy_condition>0:# 买入开仓
if curr_position <= 0 and data.can_trade(instrument):
order_target(instrument, 20)
print(today,'今日DIF上穿DEA,平空,买多开仓')
# 回测引擎:准备数据,只执行一次
def m3_prepare_bigquant_run(context):
df = context.options['data'].read_df()
df['date']=df['date'].apply(lambda x:x.strftime('%Y-%m-%d'))
df.set_index('date',inplace=True)
context.buy_condition=df['buy_condition']
context.sell_condition=df['sell_condition']
# 回测引擎:初始化函数,只执行一次
def m3_initialize_bigquant_run(context):
# 设置是否是结算模式
context.set_need_settle(False)
# 设置最大杠杆
context.set_max_leverage(1, 'fill_amap')
# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
def m3_before_trading_start_bigquant_run(context, data):
pass
m2 = M.instruments.v2(
start_date='2018-04-01',
end_date='2019-1-01',
market='CN_FUTURE',
instrument_list='I1905.DCE',
max_count=0
)
m4 = M.cached.v3(
input_1=m2.data,
run=m4_run_bigquant_run,
post_run=m4_post_run_bigquant_run,
input_ports='',
params='{\'before_days\':60}',
output_ports=''
)
m1 = M.cached.v3(
input_1=m4.data_1,
run=m1_run_bigquant_run,
post_run=m1_post_run_bigquant_run,
input_ports='',
params='{}',
output_ports=''
)
m6 = M.input_features.v1(
features="""
# #号开始的表示注释
# 多个特征,每行一个,可以包含基础特征和衍生特征
buy_condition=where((shift(DIF,1)>shift(DEA,1))&(shift(DIF,2)<shift(DEA,2)),1,0)
sell_condition=where((shift(DIF,1)<shift(DEA,1))&(shift(DIF,2)>shift(DEA,2)),1,0)"""
)
m5 = M.derived_feature_extractor.v3(
input_data=m1.data_1,
features=m6.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False,
user_functions={}
)
m7 = M.dropnan.v1(
input_data=m5.data
)
m3 = M.trade.v4(
instruments=m2.data,
options_data=m7.data,
start_date='',
end_date='',
handle_data=m3_handle_data_bigquant_run,
prepare=m3_prepare_bigquant_run,
initialize=m3_initialize_bigquant_run,
before_trading_start=m3_before_trading_start_bigquant_run,
volume_limit=0,
order_price_field_buy='open',
order_price_field_sell='open',
capital_base=1000000,
auto_cancel_non_tradable_orders=True,
data_frequency='daily',
price_type='后复权',
product_type='期货',
plot_charts=True,
backtest_only=False,
benchmark=''
)