版本 v1.0
金叉死叉策略其实就是双均线策略。策略思想是:当短期均线上穿长期均线时,形成金叉,此时买入股票。当短期均线下穿长期均线时,形成死叉,此时卖出股票。研究表明,双均线系统虽然简单,但只要严格执行,也能长期盈利。
1、确定股票池和回测时间 通过证券代码列表输入要回测的两只股票,以及回测的起止日期。
当短期均线上穿长期均线时,形成金叉,此时买入股票。当短期均线下穿长期均线时,形成死叉,此时卖出股票。
通过 trade 模块中的初始化函数定义交易手续费。 通过 trade 模块中的主函数(handle函数)形成金叉买入股票;形成死叉卖出股票。并打印交易日志。
# 本代码由可视化策略环境自动生成 2021年11月25日 17:38
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 回测引擎:初始化函数,只执行一次
def m2_initialize_bigquant_run(context):
# 加载预测数据
context.ranker_prediction = context.options['data'].read_df()
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 回测引擎:每日数据处理函数,每天执行一次
def m2_handle_data_bigquant_run(context, data):
# 按日期过滤得到今日的预测数据
options_data = context.ranker_prediction[context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
# 标的为字符串格式
sid = context.symbol(context.instruments[0])# 标的为字符串格式
price = data.current(sid, 'price') # 最新价格
cash = context.portfolio.cash # 现金
cur_position = context.portfolio.positions[sid].amount # 持仓
short_mavg = options_data['mean5'].values # 短期均线值
long_mavg = options_data['mean50'].values # 长期均线值
#交易逻辑
# 如果短期均线大于长期均线形成金叉,并且没有持仓,并且该股票可以交易
if short_mavg > long_mavg and cur_position == 0 and data.can_trade(sid):
context.order(sid, int(cash/price/100)*100) # 买入
print('{}全仓买入{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))
# 如果短期均线小于长期均线形成死叉,并且有持仓,并且该股票可以交易
elif short_mavg < long_mavg and cur_position > 0 and data.can_trade(sid):
context.order_target_percent(sid, 0) # 全部卖出
print('{}卖出{}股票'.format(data.current_dt.strftime('%Y-%m-%d'),sid.symbol))
# 回测引擎:准备数据,只执行一次
def m2_prepare_bigquant_run(context):
pass
# 回测引擎:每个单位时间开始前调用一次,即每日开盘前调用一次。
def m2_before_trading_start_bigquant_run(context, data):
pass
m1 = M.instruments.v2(
start_date='2017-11-24',
end_date='2021-11-24',
market='CN_STOCK_A',
instrument_list='600519.SHA',
max_count=0
)
m3 = M.input_features.v1(
features="""
# #号开始的表示注释,注释需单独一行
# 多个特征,每行一个,可以包含基础特征和衍生特征,特征须为本平台特征
mean5=mean(close_0,5)
mean50=mean(close_0,50)"""
)
m4 = M.general_feature_extractor.v7(
instruments=m1.data,
features=m3.data,
start_date='',
end_date='',
before_start_days=90
)
m5 = M.derived_feature_extractor.v3(
input_data=m4.data,
features=m3.data,
date_col='date',
instrument_col='instrument',
drop_na=True,
remove_extra_columns=False,
user_functions={}
)
m2 = M.trade.v4(
instruments=m1.data,
options_data=m5.data,
start_date='',
end_date='',
initialize=m2_initialize_bigquant_run,
handle_data=m2_handle_data_bigquant_run,
prepare=m2_prepare_bigquant_run,
before_trading_start=m2_before_trading_start_bigquant_run,
volume_limit=0.025,
order_price_field_buy='open',
order_price_field_sell='close',
capital_base=1000000,
auto_cancel_non_tradable_orders=True,
data_frequency='daily',
price_type='真实价格',
product_type='股票',
plot_charts=True,
backtest_only=False,
benchmark=''
)
[2021-11-25 17:16:46.196457] INFO: moduleinvoker: instruments.v2 开始运行..
[2021-11-25 17:16:46.363891] INFO: moduleinvoker: 命中缓存
[2021-11-25 17:16:46.365667] INFO: moduleinvoker: instruments.v2 运行完成[0.169202s].
[2021-11-25 17:16:46.370050] INFO: moduleinvoker: input_features.v1 开始运行..
[2021-11-25 17:16:46.378661] INFO: moduleinvoker: 命中缓存
[2021-11-25 17:16:46.379933] INFO: moduleinvoker: input_features.v1 运行完成[0.009896s].
[2021-11-25 17:16:46.399554] INFO: moduleinvoker: general_feature_extractor.v7 开始运行..
[2021-11-25 17:16:46.413368] INFO: moduleinvoker: 命中缓存
[2021-11-25 17:16:46.414663] INFO: moduleinvoker: general_feature_extractor.v7 运行完成[0.015114s].
[2021-11-25 17:16:46.421659] INFO: moduleinvoker: derived_feature_extractor.v3 开始运行..
[2021-11-25 17:16:46.434276] INFO: moduleinvoker: 命中缓存
[2021-11-25 17:16:46.435468] INFO: moduleinvoker: derived_feature_extractor.v3 运行完成[0.013806s].
[2021-11-25 17:16:46.496850] INFO: moduleinvoker: backtest.v8 开始运行..
[2021-11-25 17:16:46.501789] INFO: backtest: biglearning backtest:V8.5.1
[2021-11-25 17:16:46.502927] INFO: backtest: product_type:stock by specified
[2021-11-25 17:16:46.692863] INFO: moduleinvoker: cached.v2 开始运行..
[2021-11-25 17:16:46.705714] INFO: moduleinvoker: 命中缓存
[2021-11-25 17:16:46.707484] INFO: moduleinvoker: cached.v2 运行完成[0.014652s].
[2021-11-25 17:16:47.384939] INFO: algo: TradingAlgorithm V1.8.5
[2021-11-25 17:16:48.811017] INFO: algo: trading transform...
[2021-11-25 17:16:49.194565] INFO: algo: handle_splits get splits [dt:2018-06-15 00:00:00+00:00] [asset:Equity(287 [600519.SHA]), ratio:0.9860074520111084]
[2021-11-25 17:16:49.196333] INFO: Position: position stock handle split[sid:287, orig_amount:1300, new_amount:1318.0, orig_cost:724.0000657366348, new_cost:713.8695, ratio:0.9860074520111084, last_sale_price:775.1300659179688]
[2021-11-25 17:16:49.197385] INFO: Position: after split: PositionStock(asset:Equity(287 [600519.SHA]), amount:1318.0, cost_basis:713.8695, last_sale_price:786.1300048828125)
[2021-11-25 17:16:49.198329] INFO: Position: returning cash: 347.6096
[2021-11-25 17:16:49.827465] INFO: algo: handle_splits get splits [dt:2019-06-28 00:00:00+00:00] [asset:Equity(287 [600519.SHA]), ratio:0.9854066967964172]
[2021-11-25 17:16:49.829063] INFO: Position: position stock handle split[sid:287, orig_amount:1300, new_amount:1319.0, orig_cost:917.0000178788554, new_cost:903.618, ratio:0.9854066967964172, last_sale_price:981.8099975585938]
[2021-11-25 17:16:49.830104] INFO: Position: after split: PositionStock(asset:Equity(287 [600519.SHA]), amount:1319.0, cost_basis:903.618, last_sale_price:996.3500366210938)
[2021-11-25 17:16:49.831040] INFO: Position: returning cash: 247.6597
[2021-11-25 17:16:50.444639] INFO: algo: handle_splits get splits [dt:2020-06-24 00:00:00+00:00] [asset:Equity(287 [600519.SHA]), ratio:0.9884570837020874]
[2021-11-25 17:16:50.446468] INFO: Position: position stock handle split[sid:287, orig_amount:1100, new_amount:1112.0, orig_cost:1104.000006536869, new_cost:1091.2566, ratio:0.9884570837020874, last_sale_price:1457.4801025390625]
[2021-11-25 17:16:50.447645] INFO: Position: after split: PositionStock(asset:Equity(287 [600519.SHA]), amount:1112.0, cost_basis:1091.2566, last_sale_price:1474.5001220703125)
[2021-11-25 17:16:50.448653] INFO: Position: returning cash: 1232.2736
[2021-11-25 17:16:51.296932] INFO: Performance: Simulated 972 trading days out of 972.
[2021-11-25 17:16:51.298665] INFO: Performance: first open: 2017-11-24 09:30:00+00:00
[2021-11-25 17:16:51.299688] INFO: Performance: last close: 2021-11-24 15:00:00+00:00
[2021-11-25 17:16:54.515601] INFO: moduleinvoker: backtest.v8 运行完成[8.018744s].
[2021-11-25 17:16:54.517047] INFO: moduleinvoker: trade.v4 运行完成[8.076123s].