克隆策略

深度学习在期货高频上的应用

策略思想:

使用最近50分钟的价格、成交量、持仓量数据预测未来50分钟的涨跌幅。

交易标的:

股指期货 IF1906

模型算法:

LSTM深度学习算法

模型标注:

未来50分钟收益率

模型因子:

mean(open_intl,5)/amount
mean(open_intl,10)/amount
mean(open_intl,20)/amount
mean(open_intl,30)/amount
mean(open_intl,50)/amount
close/shift(close,30)
close/shift(close,20)
close/shift(close,10)
close/shift(close,5)
mean(amount,30)
max(high,10)/close 
open_intl
sum(open_intl,10)/amount 
amount/open_intl
mean(amount/open_intl,5)
mean(amount/open_intl,10)
mean(open_intl,5)

交易信号:

当预测值大于0.2时并且没有多仓,进场做多。(如果有空单,需要先平掉空单)
当预测值小于-0.2时并且没有空仓,进场做空。(如果有多单,需要先平掉多单)

    {"description":"实验创建于2017/11/15","graph":{"edges":[{"to_node_id":"-1483:options_data","from_node_id":"-214:data_1"},{"to_node_id":"-316:inputs","from_node_id":"-210:data"},{"to_node_id":"-218:inputs","from_node_id":"-210:data"},{"to_node_id":"-1474:inputs","from_node_id":"-218:data"},{"to_node_id":"-320:input_model","from_node_id":"-316:data"},{"to_node_id":"-332:trained_model","from_node_id":"-320:data"},{"to_node_id":"-214:input_1","from_node_id":"-332:data"},{"to_node_id":"-364:features","from_node_id":"-2295:data"},{"to_node_id":"-316:outputs","from_node_id":"-259:data"},{"to_node_id":"-293:input_1","from_node_id":"-620:data"},{"to_node_id":"-1481:inputs","from_node_id":"-1403:data"},{"to_node_id":"-1403:inputs","from_node_id":"-1474:data"},{"to_node_id":"-259:inputs","from_node_id":"-1481:data"},{"to_node_id":"-364:input_data","from_node_id":"-293:data_1"},{"to_node_id":"-373:input_data","from_node_id":"-364:data"},{"to_node_id":"-379:input_data","from_node_id":"-364:data"},{"to_node_id":"-384:input_data","from_node_id":"-373:data"},{"to_node_id":"-395:input_data","from_node_id":"-379:data"},{"to_node_id":"-3633:input_data","from_node_id":"-384:data"},{"to_node_id":"-399:input_data","from_node_id":"-395:data"},{"to_node_id":"-214:input_2","from_node_id":"-395:data"},{"to_node_id":"-332:input_data","from_node_id":"-399:data"},{"to_node_id":"-399:features","from_node_id":"-406:data"},{"to_node_id":"-3633:features","from_node_id":"-406:data"},{"to_node_id":"-320:training_data","from_node_id":"-3633:data"},{"to_node_id":"-1483:instruments","from_node_id":"-552:data"}],"nodes":[{"node_id":"-214","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"import pandas as pd\n\n# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n predictions = input_1.read_pickle()\n pred_result = predictions.reshape(predictions.shape[0]) \n dt = input_2.read_df()['date']\n pred_df = pd.Series(pred_result, index=dt)\n pred_df = pd.DataFrame(pred_df)\n ds = DataSource.write_df(pred_df)\n return Outputs(data_1=ds)\n","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-214"},{"name":"input_2","node_id":"-214"},{"name":"input_3","node_id":"-214"}],"output_ports":[{"name":"data_1","node_id":"-214"},{"name":"data_2","node_id":"-214"},{"name":"data_3","node_id":"-214"}],"cacheable":true,"seq_num":2,"comment":"模型预测结果输出","comment_collapsed":false},{"node_id":"-210","module_id":"BigQuantSpace.dl_layer_input.dl_layer_input-v1","parameters":[{"name":"shape","value":"50,17","type":"Literal","bound_global_parameter":null},{"name":"batch_shape","value":"","type":"Literal","bound_global_parameter":null},{"name":"dtype","value":"float32","type":"Literal","bound_global_parameter":null},{"name":"sparse","value":"False","type":"Literal","bound_global_parameter":null},{"name":"name","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"inputs","node_id":"-210"}],"output_ports":[{"name":"data","node_id":"-210"}],"cacheable":false,"seq_num":3,"comment":"","comment_collapsed":true},{"node_id":"-218","module_id":"BigQuantSpace.dl_layer_lstm.dl_layer_lstm-v1","parameters":[{"name":"units","value":"32","type":"Literal","bound_global_parameter":null},{"name":"activation","value":"linear","type":"Literal","bound_global_parameter":null},{"name":"user_activation","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_activation","value":"hard_sigmoid","type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_activation","value":"","type":"Literal","bound_global_parameter":null},{"name":"use_bias","value":"True","type":"Literal","bound_global_parameter":null},{"name":"kernel_initializer","value":"glorot_uniform","type":"Literal","bound_global_parameter":null},{"name":"user_kernel_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_initializer","value":"Orthogonal","type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_initializer","value":"Ones","type":"Literal","bound_global_parameter":null},{"name":"user_bias_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"unit_forget_bias","value":"True","type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_kernel_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"recurrent_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"recurrent_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_bias_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_activity_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"kernel_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_kernel_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_bias_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"dropout","value":"0","type":"Literal","bound_global_parameter":null},{"name":"recurrent_dropout","value":0,"type":"Literal","bound_global_parameter":null},{"name":"return_sequences","value":"True","type":"Literal","bound_global_parameter":null},{"name":"implementation","value":"0","type":"Literal","bound_global_parameter":null},{"name":"name","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"inputs","node_id":"-218"}],"output_ports":[{"name":"data","node_id":"-218"}],"cacheable":false,"seq_num":4,"comment":"","comment_collapsed":true},{"node_id":"-316","module_id":"BigQuantSpace.dl_model_init.dl_model_init-v1","parameters":[],"input_ports":[{"name":"inputs","node_id":"-316"},{"name":"outputs","node_id":"-316"}],"output_ports":[{"name":"data","node_id":"-316"}],"cacheable":false,"seq_num":5,"comment":"","comment_collapsed":true},{"node_id":"-320","module_id":"BigQuantSpace.dl_model_train.dl_model_train-v1","parameters":[{"name":"optimizer","value":"RMSprop","type":"Literal","bound_global_parameter":null},{"name":"user_optimizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"loss","value":"mean_squared_error","type":"Literal","bound_global_parameter":null},{"name":"user_loss","value":"","type":"Literal","bound_global_parameter":null},{"name":"metrics","value":"mse","type":"Literal","bound_global_parameter":null},{"name":"batch_size","value":"256","type":"Literal","bound_global_parameter":null},{"name":"epochs","value":"5","type":"Literal","bound_global_parameter":null},{"name":"custom_objects","value":"# 用户的自定义层需要写到字典中,比如\n# {\n# \"MyLayer\": MyLayer\n# }\nbigquant_run = {\n \n}\n","type":"Literal","bound_global_parameter":null},{"name":"n_gpus","value":"0","type":"Literal","bound_global_parameter":null},{"name":"verbose","value":"1:输出进度条记录","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_model","node_id":"-320"},{"name":"training_data","node_id":"-320"},{"name":"validation_data","node_id":"-320"}],"output_ports":[{"name":"data","node_id":"-320"}],"cacheable":true,"seq_num":6,"comment":"","comment_collapsed":true},{"node_id":"-332","module_id":"BigQuantSpace.dl_model_predict.dl_model_predict-v1","parameters":[{"name":"batch_size","value":"128","type":"Literal","bound_global_parameter":null},{"name":"n_gpus","value":"0","type":"Literal","bound_global_parameter":null},{"name":"verbose","value":"0:不显示","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"trained_model","node_id":"-332"},{"name":"input_data","node_id":"-332"}],"output_ports":[{"name":"data","node_id":"-332"}],"cacheable":true,"seq_num":7,"comment":"","comment_collapsed":true},{"node_id":"-2295","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"mean(open_intl,5)/amount\nmean(open_intl,10)/amount\nmean(open_intl,20)/amount\nmean(open_intl,30)/amount\nmean(open_intl,50)/amount\nclose/shift(close,30)\nclose/shift(close,20)\nclose/shift(close,10)\nclose/shift(close,5)\nmean(amount,30)\nmax(high,10)/close \nopen_intl\nsum(open_intl,10)/amount \namount/open_intl\nmean(amount/open_intl,5)\nmean(amount/open_intl,10)\nmean(open_intl,5)\nlabel= shift(close,-50)/close-1\n\n\n","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-2295"}],"output_ports":[{"name":"data","node_id":"-2295"}],"cacheable":true,"seq_num":8,"comment":"","comment_collapsed":true},{"node_id":"-259","module_id":"BigQuantSpace.dl_layer_dense.dl_layer_dense-v1","parameters":[{"name":"units","value":"1","type":"Literal","bound_global_parameter":null},{"name":"activation","value":"linear","type":"Literal","bound_global_parameter":null},{"name":"user_activation","value":"","type":"Literal","bound_global_parameter":null},{"name":"use_bias","value":"True","type":"Literal","bound_global_parameter":null},{"name":"kernel_initializer","value":"glorot_uniform","type":"Literal","bound_global_parameter":null},{"name":"user_kernel_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_initializer","value":"Zeros","type":"Literal","bound_global_parameter":null},{"name":"user_bias_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_kernel_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_bias_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_activity_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"kernel_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_kernel_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_bias_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"name","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"inputs","node_id":"-259"}],"output_ports":[{"name":"data","node_id":"-259"}],"cacheable":false,"seq_num":9,"comment":"","comment_collapsed":true},{"node_id":"-620","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2019-04-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2019-06-22","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_FUTURE","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"IF1906.CFE\n ","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-620"}],"output_ports":[{"name":"data","node_id":"-620"}],"cacheable":true,"seq_num":24,"comment":"证券标的及起始截止时间","comment_collapsed":false},{"node_id":"-1403","module_id":"BigQuantSpace.dl_layer_lstm.dl_layer_lstm-v1","parameters":[{"name":"units","value":"32","type":"Literal","bound_global_parameter":null},{"name":"activation","value":"sigmoid","type":"Literal","bound_global_parameter":null},{"name":"user_activation","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_activation","value":"hard_sigmoid","type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_activation","value":"","type":"Literal","bound_global_parameter":null},{"name":"use_bias","value":"True","type":"Literal","bound_global_parameter":null},{"name":"kernel_initializer","value":"glorot_uniform","type":"Literal","bound_global_parameter":null},{"name":"user_kernel_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_initializer","value":"Orthogonal","type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_initializer","value":"Zeros","type":"Literal","bound_global_parameter":null},{"name":"user_bias_initializer","value":"","type":"Literal","bound_global_parameter":null},{"name":"unit_forget_bias","value":"True","type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"kernel_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_kernel_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"recurrent_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"recurrent_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"bias_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_bias_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer","value":"None","type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer_l1","value":0,"type":"Literal","bound_global_parameter":null},{"name":"activity_regularizer_l2","value":0,"type":"Literal","bound_global_parameter":null},{"name":"user_activity_regularizer","value":"","type":"Literal","bound_global_parameter":null},{"name":"kernel_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_kernel_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"recurrent_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_recurrent_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"bias_constraint","value":"None","type":"Literal","bound_global_parameter":null},{"name":"user_bias_constraint","value":"","type":"Literal","bound_global_parameter":null},{"name":"dropout","value":0,"type":"Literal","bound_global_parameter":null},{"name":"recurrent_dropout","value":0,"type":"Literal","bound_global_parameter":null},{"name":"return_sequences","value":"False","type":"Literal","bound_global_parameter":null},{"name":"implementation","value":"0","type":"Literal","bound_global_parameter":null},{"name":"name","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"inputs","node_id":"-1403"}],"output_ports":[{"name":"data","node_id":"-1403"}],"cacheable":false,"seq_num":25,"comment":"","comment_collapsed":true},{"node_id":"-1474","module_id":"BigQuantSpace.dl_layer_dropout.dl_layer_dropout-v1","parameters":[{"name":"rate","value":"0.2","type":"Literal","bound_global_parameter":null},{"name":"noise_shape","value":"","type":"Literal","bound_global_parameter":null},{"name":"seed","value":"0","type":"Literal","bound_global_parameter":null},{"name":"name","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"inputs","node_id":"-1474"}],"output_ports":[{"name":"data","node_id":"-1474"}],"cacheable":false,"seq_num":10,"comment":"","comment_collapsed":true},{"node_id":"-1481","module_id":"BigQuantSpace.dl_layer_dropout.dl_layer_dropout-v1","parameters":[{"name":"rate","value":"0.1","type":"Literal","bound_global_parameter":null},{"name":"noise_shape","value":"","type":"Literal","bound_global_parameter":null},{"name":"seed","value":"0","type":"Literal","bound_global_parameter":null},{"name":"name","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"inputs","node_id":"-1481"}],"output_ports":[{"name":"data","node_id":"-1481"}],"cacheable":false,"seq_num":11,"comment":"","comment_collapsed":true},{"node_id":"-293","module_id":"BigQuantSpace.cached.cached-v3","parameters":[{"name":"run","value":"# Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端\ndef bigquant_run(input_1, input_2, input_3):\n \n start_date=input_1.read_pickle()['start_date']\n end_date=input_1.read_pickle()['end_date']\n ins=input_1.read_pickle()['instruments']\n df = DataSource('bar1m_IF1906.CFE').read(instruments=ins,start_date=start_date,end_date=end_date)\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\n ","type":"Literal","bound_global_parameter":null},{"name":"post_run","value":"# 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。\ndef bigquant_run(outputs):\n return outputs\n","type":"Literal","bound_global_parameter":null},{"name":"input_ports","value":"","type":"Literal","bound_global_parameter":null},{"name":"params","value":"{}","type":"Literal","bound_global_parameter":null},{"name":"output_ports","value":"","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_1","node_id":"-293"},{"name":"input_2","node_id":"-293"},{"name":"input_3","node_id":"-293"}],"output_ports":[{"name":"data_1","node_id":"-293"},{"name":"data_2","node_id":"-293"},{"name":"data_3","node_id":"-293"}],"cacheable":true,"seq_num":20,"comment":"","comment_collapsed":true},{"node_id":"-364","module_id":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","parameters":[{"name":"date_col","value":"date","type":"Literal","bound_global_parameter":null},{"name":"instrument_col","value":"instrument","type":"Literal","bound_global_parameter":null},{"name":"drop_na","value":"False","type":"Literal","bound_global_parameter":null},{"name":"remove_extra_columns","value":"False","type":"Literal","bound_global_parameter":null},{"name":"user_functions","value":"{}","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-364"},{"name":"features","node_id":"-364"}],"output_ports":[{"name":"data","node_id":"-364"}],"cacheable":true,"seq_num":12,"comment":"","comment_collapsed":true},{"node_id":"-373","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"date<'2019-06-01'","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-373"}],"output_ports":[{"name":"data","node_id":"-373"},{"name":"left_data","node_id":"-373"}],"cacheable":true,"seq_num":16,"comment":"","comment_collapsed":true},{"node_id":"-379","module_id":"BigQuantSpace.filter.filter-v3","parameters":[{"name":"expr","value":"date>='2019-06-01'","type":"Literal","bound_global_parameter":null},{"name":"output_left_data","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-379"}],"output_ports":[{"name":"data","node_id":"-379"},{"name":"left_data","node_id":"-379"}],"cacheable":true,"seq_num":21,"comment":"","comment_collapsed":true},{"node_id":"-384","module_id":"BigQuantSpace.dropnan.dropnan-v1","parameters":[],"input_ports":[{"name":"input_data","node_id":"-384"}],"output_ports":[{"name":"data","node_id":"-384"}],"cacheable":true,"seq_num":28,"comment":"去掉为nan的数据","comment_collapsed":true},{"node_id":"-395","module_id":"BigQuantSpace.dropnan.dropnan-v1","parameters":[],"input_ports":[{"name":"input_data","node_id":"-395"}],"output_ports":[{"name":"data","node_id":"-395"}],"cacheable":true,"seq_num":13,"comment":"去掉为nan的数据","comment_collapsed":true},{"node_id":"-399","module_id":"BigQuantSpace.dl_convert_to_bin.dl_convert_to_bin-v2","parameters":[{"name":"window_size","value":"50","type":"Literal","bound_global_parameter":null},{"name":"feature_clip","value":"5","type":"Literal","bound_global_parameter":null},{"name":"flatten","value":"False","type":"Literal","bound_global_parameter":null},{"name":"window_along_col","value":"instrument","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-399"},{"name":"features","node_id":"-399"}],"output_ports":[{"name":"data","node_id":"-399"}],"cacheable":true,"seq_num":17,"comment":"","comment_collapsed":true},{"node_id":"-406","module_id":"BigQuantSpace.input_features.input_features-v1","parameters":[{"name":"features","value":"mean(open_intl,5)/amount\nmean(open_intl,10)/amount\nmean(open_intl,20)/amount\nmean(open_intl,30)/amount\nmean(open_intl,50)/amount\nclose/shift(close,30)\nclose/shift(close,20)\nclose/shift(close,10)\nclose/shift(close,5)\nmean(amount,30)\nmax(high,10)/close \nopen_intl\nsum(open_intl,10)/amount \namount/open_intl\nmean(amount/open_intl,5)\nmean(amount/open_intl,10)\nmean(open_intl,5)","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"features_ds","node_id":"-406"}],"output_ports":[{"name":"data","node_id":"-406"}],"cacheable":true,"seq_num":15,"comment":"","comment_collapsed":true},{"node_id":"-3633","module_id":"BigQuantSpace.dl_convert_to_bin.dl_convert_to_bin-v2","parameters":[{"name":"window_size","value":"50","type":"Literal","bound_global_parameter":null},{"name":"feature_clip","value":"5","type":"Literal","bound_global_parameter":null},{"name":"flatten","value":"False","type":"Literal","bound_global_parameter":null},{"name":"window_along_col","value":"instrument","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"input_data","node_id":"-3633"},{"name":"features","node_id":"-3633"}],"output_ports":[{"name":"data","node_id":"-3633"}],"cacheable":true,"seq_num":14,"comment":"","comment_collapsed":true},{"node_id":"-552","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2019-06-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2019-06-22","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_FUTURE","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"IF1906.CFX\n ","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-552"}],"output_ports":[{"name":"data","node_id":"-552"}],"cacheable":true,"seq_num":18,"comment":"证券标的及起始截止时间","comment_collapsed":true},{"node_id":"-1483","module_id":"BigQuantSpace.hftrade.hftrade-v1","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 交易引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n print(\"in initial\", context.future_symbol(\"IF1906.CFX\"))\n context.prediction = context.options['data'].read().reset_index().rename(columns={0:\"value\"})\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 交易引擎:每个单位时间开盘前调用一次。\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_tick","value":"# 交易引擎:bar数据处理函数,每个单位执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"# 交易引擎:bar数据处理函数,每个单位执行一次\ndef bigquant_run(context, data):\n # 按日期过滤得到今日的预测数据\n now = data.current_dt.strftime('%Y-%m-%d %H:%M:%S')\n\n # 按日期过滤得到今日的预测数据\n try:\n prediction = context.prediction[context.prediction.date==now].iloc[0][\"value\"]\n except Exception as e:\n return\n instrument = context.instruments[0]\n# sid = context.symbol(instrument)\n# cur_position = context.portfolio.positions[sid].amount\n # 分别获取多头持仓,和空头持仓\n position_long = context.get_position(instrument, Direction.LONG)\n# position_short = context.get_position(instrument, Direction.SHORT)\n #获取当前最新价\n price = data.current(instrument, \"close\")\n order_num = 1\n # 交易逻辑\n if prediction > 0.2 and position_long.avail_qty == 0:\n context.buy_open(instrument, order_num, price, order_type=OrderType.MARKET)\n print(data.current_dt, '开多!')\n \n elif prediction < -0.2 and position_long.avail_qty > 0:\n context.sell_close(instrument, position_long.avail_qty, price, order_type=OrderType.MARKET)\n print(data.current_dt, '平多!')\n","type":"Literal","bound_global_parameter":null},{"name":"handle_trade","value":"# 交易引擎:成交回报处理函数,每个成交发生时执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_order","value":"# 交易引擎:委托回报处理函数,每个委托变化时执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"after_trading","value":"# 交易引擎:盘后处理函数,每日盘后执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":"200000","type":"Literal","bound_global_parameter":null},{"name":"frequency","value":"minute","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"真实价格","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"期货","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000300.HIX","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"disable_cache","value":"False","type":"Literal","bound_global_parameter":null},{"name":"show_debug_info","value":"False","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-1483"},{"name":"history_ds","node_id":"-1483"},{"name":"benchmark_ds","node_id":"-1483"},{"name":"options_data","node_id":"-1483"}],"output_ports":[{"name":"raw_perf","node_id":"-1483"}],"cacheable":false,"seq_num":19,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-214' Position='822,654,200,200'/><node_position Node='-210' Position='22,-89,200,200'/><node_position Node='-218' Position='9,25,200,200'/><node_position Node='-316' Position='16,476,200,200'/><node_position Node='-320' Position='355,484,200,200'/><node_position Node='-332' Position='599,574,200,200'/><node_position Node='-2295' Position='655,-381,200,200'/><node_position Node='-259' Position='18,376,200,200'/><node_position Node='-620' Position='335,-382,200,200'/><node_position Node='-1403' Position='17,197,200,200'/><node_position Node='-1474' Position='14,109,200,200'/><node_position Node='-1481' Position='14,275,200,200'/><node_position Node='-293' Position='415,-273,200,200'/><node_position Node='-364' Position='525,-167,200,200'/><node_position Node='-373' Position='419,-68,200,200'/><node_position Node='-379' Position='794,-62,200,200'/><node_position Node='-384' Position='408,42,200,200'/><node_position Node='-395' Position='786,39,200,200'/><node_position Node='-399' Position='742,380,200,200'/><node_position Node='-406' Position='636,180,200,200'/><node_position Node='-3633' Position='401,326,200,200'/><node_position Node='-552' Position='352,665,200,200'/><node_position Node='-1483' Position='403.77130126953125,818,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [4]:
    # 本代码由可视化策略环境自动生成 2021年8月19日 09:14
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m20_run_bigquant_run(input_1, input_2, input_3):
         
        start_date=input_1.read_pickle()['start_date']
        end_date=input_1.read_pickle()['end_date']
        ins=input_1.read_pickle()['instruments']
        df = DataSource('bar1m_IF1906.CFE').read(instruments=ins,start_date=start_date,end_date=end_date)
        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 m20_post_run_bigquant_run(outputs):
        return outputs
    
    # 用户的自定义层需要写到字典中,比如
    # {
    #   "MyLayer": MyLayer
    # }
    m6_custom_objects_bigquant_run = {
        
    }
    
    import pandas as pd
    
    # Python 代码入口函数,input_1/2/3 对应三个输入端,data_1/2/3 对应三个输出端
    def m2_run_bigquant_run(input_1, input_2, input_3):
        predictions = input_1.read_pickle()
        pred_result = predictions.reshape(predictions.shape[0]) 
        dt = input_2.read_df()['date']
        pred_df = pd.Series(pred_result, index=dt)
        pred_df = pd.DataFrame(pred_df)
        ds = DataSource.write_df(pred_df)
        return Outputs(data_1=ds)
    
    # 后处理函数,可选。输入是主函数的输出,可以在这里对数据做处理,或者返回更友好的outputs数据格式。此函数输出不会被缓存。
    def m2_post_run_bigquant_run(outputs):
        return outputs
    
    # 交易引擎:初始化函数,只执行一次
    def m19_initialize_bigquant_run(context):
        # 加载预测数据
        print("in initial", context.future_symbol("IF1906.CFX"))
        context.prediction = context.options['data'].read().reset_index().rename(columns={0:"value"})
    
    # 交易引擎:每个单位时间开盘前调用一次。
    def m19_before_trading_start_bigquant_run(context, data):
        pass
    
    # 交易引擎:bar数据处理函数,每个单位执行一次
    def m19_handle_tick_bigquant_run(context, data):
        pass
    
    # 交易引擎:bar数据处理函数,每个单位执行一次
    def m19_handle_data_bigquant_run(context, data):
        # 按日期过滤得到今日的预测数据
        now = data.current_dt.strftime('%Y-%m-%d %H:%M:%S')
    
        # 按日期过滤得到今日的预测数据
        try:
            prediction = context.prediction[context.prediction.date==now].iloc[0]["value"]
        except Exception as e:
            return
        instrument = context.instruments[0]
    #     sid = context.symbol(instrument)
    #     cur_position = context.portfolio.positions[sid].amount
        # 分别获取多头持仓,和空头持仓
        position_long = context.get_position(instrument, Direction.LONG)
    #     position_short = context.get_position(instrument, Direction.SHORT)
        #获取当前最新价
        price = data.current(instrument, "close")
        order_num = 1
        # 交易逻辑
        if prediction > 0.2 and position_long.avail_qty == 0:
            context.buy_open(instrument, order_num, price, order_type=OrderType.MARKET)
            print(data.current_dt, '开多!')
            
        elif prediction < -0.2 and position_long.avail_qty > 0:
            context.sell_close(instrument, position_long.avail_qty, price, order_type=OrderType.MARKET)
            print(data.current_dt, '平多!')
    
    # 交易引擎:成交回报处理函数,每个成交发生时执行一次
    def m19_handle_trade_bigquant_run(context, data):
        pass
    
    # 交易引擎:委托回报处理函数,每个委托变化时执行一次
    def m19_handle_order_bigquant_run(context, data):
        pass
    
    # 交易引擎:盘后处理函数,每日盘后执行一次
    def m19_after_trading_bigquant_run(context, data):
        pass
    
    
    m3 = M.dl_layer_input.v1(
        shape='50,17',
        batch_shape='',
        dtype='float32',
        sparse=False,
        name=''
    )
    
    m4 = M.dl_layer_lstm.v1(
        inputs=m3.data,
        units=32,
        activation='linear',
        recurrent_activation='hard_sigmoid',
        use_bias=True,
        kernel_initializer='glorot_uniform',
        recurrent_initializer='Orthogonal',
        bias_initializer='Ones',
        unit_forget_bias=True,
        kernel_regularizer='None',
        kernel_regularizer_l1=0,
        kernel_regularizer_l2=0,
        recurrent_regularizer='None',
        recurrent_regularizer_l1=0,
        recurrent_regularizer_l2=0,
        bias_regularizer='None',
        bias_regularizer_l1=0,
        bias_regularizer_l2=0,
        activity_regularizer='None',
        activity_regularizer_l1=0,
        activity_regularizer_l2=0,
        kernel_constraint='None',
        recurrent_constraint='None',
        bias_constraint='None',
        dropout=0,
        recurrent_dropout=0,
        return_sequences=True,
        implementation='0',
        name=''
    )
    
    m10 = M.dl_layer_dropout.v1(
        inputs=m4.data,
        rate=0.2,
        noise_shape='',
        seed=0,
        name=''
    )
    
    m25 = M.dl_layer_lstm.v1(
        inputs=m10.data,
        units=32,
        activation='sigmoid',
        recurrent_activation='hard_sigmoid',
        use_bias=True,
        kernel_initializer='glorot_uniform',
        recurrent_initializer='Orthogonal',
        bias_initializer='Zeros',
        unit_forget_bias=True,
        kernel_regularizer='None',
        kernel_regularizer_l1=0,
        kernel_regularizer_l2=0,
        recurrent_regularizer='None',
        recurrent_regularizer_l1=0,
        recurrent_regularizer_l2=0,
        bias_regularizer='None',
        bias_regularizer_l1=0,
        bias_regularizer_l2=0,
        activity_regularizer='None',
        activity_regularizer_l1=0,
        activity_regularizer_l2=0,
        kernel_constraint='None',
        recurrent_constraint='None',
        bias_constraint='None',
        dropout=0,
        recurrent_dropout=0,
        return_sequences=False,
        implementation='0',
        name=''
    )
    
    m11 = M.dl_layer_dropout.v1(
        inputs=m25.data,
        rate=0.1,
        noise_shape='',
        seed=0,
        name=''
    )
    
    m9 = M.dl_layer_dense.v1(
        inputs=m11.data,
        units=1,
        activation='linear',
        use_bias=True,
        kernel_initializer='glorot_uniform',
        bias_initializer='Zeros',
        kernel_regularizer='None',
        kernel_regularizer_l1=0,
        kernel_regularizer_l2=0,
        bias_regularizer='None',
        bias_regularizer_l1=0,
        bias_regularizer_l2=0,
        activity_regularizer='None',
        activity_regularizer_l1=0,
        activity_regularizer_l2=0,
        kernel_constraint='None',
        bias_constraint='None',
        name=''
    )
    
    m5 = M.dl_model_init.v1(
        inputs=m3.data,
        outputs=m9.data
    )
    
    m8 = M.input_features.v1(
        features="""mean(open_intl,5)/amount
    mean(open_intl,10)/amount
    mean(open_intl,20)/amount
    mean(open_intl,30)/amount
    mean(open_intl,50)/amount
    close/shift(close,30)
    close/shift(close,20)
    close/shift(close,10)
    close/shift(close,5)
    mean(amount,30)
    max(high,10)/close 
    open_intl
    sum(open_intl,10)/amount 
    amount/open_intl
    mean(amount/open_intl,5)
    mean(amount/open_intl,10)
    mean(open_intl,5)
    label= shift(close,-50)/close-1
    
    
    """
    )
    
    m24 = M.instruments.v2(
        start_date='2019-04-01',
        end_date='2019-06-22',
        market='CN_FUTURE',
        instrument_list="""IF1906.CFE
     """,
        max_count=0
    )
    
    m20 = M.cached.v3(
        input_1=m24.data,
        run=m20_run_bigquant_run,
        post_run=m20_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m12 = M.derived_feature_extractor.v3(
        input_data=m20.data_1,
        features=m8.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False,
        user_functions={}
    )
    
    m16 = M.filter.v3(
        input_data=m12.data,
        expr='date<\'2019-06-01\'',
        output_left_data=False
    )
    
    m28 = M.dropnan.v1(
        input_data=m16.data
    )
    
    m21 = M.filter.v3(
        input_data=m12.data,
        expr='date>=\'2019-06-01\'',
        output_left_data=False
    )
    
    m13 = M.dropnan.v1(
        input_data=m21.data
    )
    
    m15 = M.input_features.v1(
        features="""mean(open_intl,5)/amount
    mean(open_intl,10)/amount
    mean(open_intl,20)/amount
    mean(open_intl,30)/amount
    mean(open_intl,50)/amount
    close/shift(close,30)
    close/shift(close,20)
    close/shift(close,10)
    close/shift(close,5)
    mean(amount,30)
    max(high,10)/close 
    open_intl
    sum(open_intl,10)/amount 
    amount/open_intl
    mean(amount/open_intl,5)
    mean(amount/open_intl,10)
    mean(open_intl,5)"""
    )
    
    m17 = M.dl_convert_to_bin.v2(
        input_data=m13.data,
        features=m15.data,
        window_size=50,
        feature_clip=5,
        flatten=False,
        window_along_col='instrument'
    )
    
    m14 = M.dl_convert_to_bin.v2(
        input_data=m28.data,
        features=m15.data,
        window_size=50,
        feature_clip=5,
        flatten=False,
        window_along_col='instrument'
    )
    
    m6 = M.dl_model_train.v1(
        input_model=m5.data,
        training_data=m14.data,
        optimizer='RMSprop',
        loss='mean_squared_error',
        metrics='mse',
        batch_size=256,
        epochs=5,
        custom_objects=m6_custom_objects_bigquant_run,
        n_gpus=0,
        verbose='1:输出进度条记录'
    )
    
    m7 = M.dl_model_predict.v1(
        trained_model=m6.data,
        input_data=m17.data,
        batch_size=128,
        n_gpus=0,
        verbose='0:不显示'
    )
    
    m2 = M.cached.v3(
        input_1=m7.data,
        input_2=m13.data,
        run=m2_run_bigquant_run,
        post_run=m2_post_run_bigquant_run,
        input_ports='',
        params='{}',
        output_ports=''
    )
    
    m18 = M.instruments.v2(
        start_date='2019-06-01',
        end_date='2019-06-22',
        market='CN_FUTURE',
        instrument_list="""IF1906.CFX
     """,
        max_count=0
    )
    
    m19 = M.hftrade.v1(
        instruments=m18.data,
        options_data=m2.data_1,
        start_date='',
        end_date='',
        initialize=m19_initialize_bigquant_run,
        before_trading_start=m19_before_trading_start_bigquant_run,
        handle_tick=m19_handle_tick_bigquant_run,
        handle_data=m19_handle_data_bigquant_run,
        handle_trade=m19_handle_trade_bigquant_run,
        handle_order=m19_handle_order_bigquant_run,
        after_trading=m19_after_trading_bigquant_run,
        capital_base=200000,
        frequency='minute',
        price_type='真实价格',
        product_type='期货',
        before_start_days='',
        benchmark='000300.HIX',
        plot_charts=True,
        disable_cache=False,
        show_debug_info=False,
        backtest_only=False
    )
    
    DataSource(ae2ab2082c654d19a51ae9eef0fdd97cT)
    
    2021-08-18 09:33:11.438326 run trading v1.7.7 
    2021-08-18 09:33:11.438452 init history datas... 
    2021-08-18 09:33:11.457001 init trading env... 
    in initial Future(4 [IF1906.CFX])
    2019-06-03 09:31:00 开多!
    2019-06-03 09:45:00 平多!
    2019-06-03 09:50:00 开多!
    2019-06-03 09:59:00 平多!
    2019-06-03 10:08:00 开多!
    
    • 收益率31.16%
    • 年化收益率13089.38%
    • 基准收益率5.56%
    • 阿尔法0.35
    • 贝塔5.27
    • 夏普比率4.87
    • 胜率0.2
    • 盈亏比1.87
    • 收益波动率111.73%
    • 最大回撤11.41%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-b5d8ec808d42494bbbd6c15ef59c6080"}/bigcharts-data-end