版本 v1.0
本策略只模拟海龟交易买卖规则,暂不涉及复杂的头寸管理和风险控制。
### 海龟交易的交易规则
### 策略构建步骤
### 策略的实现
可视化策略实现如下:
# 本代码由可视化策略环境自动生成 2021年12月6日 22:15
# 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
# 回测引擎:初始化函数,只执行一次
def m9_initialize_bigquant_run(context):
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 回测引擎:每日数据处理函数,每天执行一次
def m9_handle_data_bigquant_run(context, data):
# 获取今日的日期
today = data.current_dt.strftime('%Y-%m-%d')
# 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
stock_hold_now = {e.symbol: p.amount * p.last_sale_price
for e, p in context.perf_tracker.position_tracker.positions.items()}
# 记录用于买入股票的可用现金,因为是尾盘卖股票尾盘买股票,需要记录卖出的股票市值并在买入下单前更新可用现金;
# 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用
cash_for_buy = context.portfolio.cash
try:
buy_stock = context.daily_stock_buy[today] # 当日符合买入条件的股票
except:
buy_stock=[] # 如果没有符合条件的股票,就设置为空
try:
sell_stock = context.daily_stock_sell[today] # 当日符合卖出条件的股票
except:
sell_stock=[] # 如果没有符合条件的股票,就设置为空
# 需要卖出的股票:已有持仓中符合卖出条件的股票
stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]
# 需要买入的股票:没有持仓且符合买入条件的股票
stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now ]
# 需要调仓的股票:已有持仓且不符合卖出条件的股票
stock_to_adjust=[ i for i in stock_hold_now if i not in sell_stock ]
# 如果有卖出信号
if len(stock_to_sell)>0:
for instrument in stock_to_sell:
sid = context.symbol(instrument) # 将标的转化为equity格式
cur_position = context.portfolio.positions[sid].amount # 持仓
if cur_position > 0 and data.can_trade(sid):
context.order_target_percent(sid, 0) # 全部卖出
# 因为是尾盘卖股票尾盘买股票,需要记录卖出的股票市值并在买入下单前更新可用现金;
# 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用
cash_for_buy += stock_hold_now[instrument]
# 如果有买入信号/有持仓
if len(stock_to_buy)+len(stock_to_adjust)>0:
weight = 1/(len(stock_to_buy)+len(stock_to_adjust)) # 每只股票的比重为等资金比例持有
for instrument in stock_to_buy+stock_to_adjust:
sid = context.symbol(instrument) # 将标的转化为equity格式
cur_position = context.portfolio.positions[sid].amount # 持仓
if data.can_trade(sid) and cur_position==0:
context.order_target_value(sid, weight*cash_for_buy) # 买入
# 回测引擎:准备数据,只执行一次
def m9_prepare_bigquant_run(context):
# 加载预测数据
df = context.options['data'].read_df()
# 函数:求满足开仓条件的股票列表
def open_pos_con(df):
return list(df[df['buy_condition']>0].instrument)
# 函数:求满足平仓条件的股票列表
def close_pos_con(df):
return list(df[df['sell_condition']>0].instrument)
# 每日买入股票的数据框
context.daily_stock_buy= df.groupby('date').apply(open_pos_con)
# 每日卖出股票的数据框
context.daily_stock_sell= df.groupby('date').apply(close_pos_con)
m1 = M.input_features.v1(
features="""# #号开始的表示注释
# 多个特征,每行一个,可以包含基础特征和衍生特征
buy_condition=where((close_0>=ts_max(close_0,20)),1,0)
sell_condition=where((close_0<=ts_min(close_0,20)),1,0)""",
m_cached=False
)
m2 = M.instruments.v2(
start_date=T.live_run_param('trading_date', '2017-07-15'),
end_date=T.live_run_param('trading_date', '2019-01-01'),
market='CN_STOCK_A',
instrument_list="""600519.SHA
600010.SHA""",
max_count=0
)
m7 = M.general_feature_extractor.v7(
instruments=m2.data,
features=m1.data,
start_date='',
end_date='',
before_start_days=60,
m_cached=False
)
m8 = M.derived_feature_extractor.v3(
input_data=m7.data,
features=m1.data,
date_col='date',
instrument_col='instrument',
drop_na=False,
remove_extra_columns=False,
m_cached=False
)
m3 = M.dropnan.v2(
input_data=m8.data
)
m9 = M.trade.v4(
instruments=m2.data,
options_data=m3.data,
start_date='',
end_date='',
initialize=m9_initialize_bigquant_run,
handle_data=m9_handle_data_bigquant_run,
prepare=m9_prepare_bigquant_run,
volume_limit=0.025,
order_price_field_buy='close',
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='000300.HIX'
)
[2019-10-12 09:24:52.432378] INFO: bigquant: input_features.v1 开始运行..
[2019-10-12 09:24:52.509098] INFO: bigquant: input_features.v1 运行完成[0.076743s].
[2019-10-12 09:24:52.514734] INFO: bigquant: instruments.v2 开始运行..
[2019-10-12 09:24:52.591658] INFO: bigquant: instruments.v2 运行完成[0.076898s].
[2019-10-12 09:24:52.644458] INFO: bigquant: general_feature_extractor.v7 开始运行..
[2019-10-12 09:24:52.916575] INFO: 基础特征抽取: 年份 2017, 特征行数=314
[2019-10-12 09:24:52.960213] INFO: 基础特征抽取: 年份 2018, 特征行数=477
[2019-10-12 09:24:53.030199] INFO: 基础特征抽取: 年份 2019, 特征行数=0
[2019-10-12 09:24:53.128869] INFO: 基础特征抽取: 总行数: 791
[2019-10-12 09:24:53.132424] INFO: bigquant: general_feature_extractor.v7 运行完成[0.487956s].
[2019-10-12 09:24:53.140732] INFO: bigquant: derived_feature_extractor.v3 开始运行..
[2019-10-12 09:24:53.236113] INFO: derived_feature_extractor: 提取完成 buy_condition=where((close_0>=ts_max(close_0,20)),1,0), 0.006s
[2019-10-12 09:24:53.243821] INFO: derived_feature_extractor: 提取完成 sell_condition=where((close_0<=ts_min(close_0,20)),1,0), 0.006s
[2019-10-12 09:24:53.291992] INFO: derived_feature_extractor: /y_2017, 314
[2019-10-12 09:24:53.323257] INFO: derived_feature_extractor: /y_2018, 477
[2019-10-12 09:24:53.429161] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.288414s].
[2019-10-12 09:24:53.434504] INFO: bigquant: dropnan.v1 开始运行..
[2019-10-12 09:24:53.506508] INFO: dropnan: /y_2017, 314/314
[2019-10-12 09:24:53.528873] INFO: dropnan: /y_2018, 477/477
[2019-10-12 09:24:53.605392] INFO: dropnan: 行数: 791/791
[2019-10-12 09:24:53.609608] INFO: bigquant: dropnan.v1 运行完成[0.175089s].
[2019-10-12 09:24:53.680051] INFO: bigquant: backtest.v8 开始运行..
[2019-10-12 09:24:53.682894] INFO: bigquant: biglearning backtest:V8.2.16
[2019-10-12 09:24:54.332874] INFO: bigquant: product_type:stock by specified
[2019-10-12 09:24:54.551378] INFO: bigquant: cached.v2 开始运行..
[2019-10-12 09:25:03.163772] INFO: bigquant: 读取股票行情完成:1222
[2019-10-12 09:25:03.853631] INFO: bigquant: cached.v2 运行完成[9.302269s].
[2019-10-12 09:25:03.897228] INFO: algo: TradingAlgorithm V1.5.9
[2019-10-12 09:25:03.944866] INFO: algo: trading transform...
[2019-10-12 09:25:04.872867] INFO: Performance: Simulated 358 trading days out of 358.
[2019-10-12 09:25:04.874492] INFO: Performance: first open: 2017-07-17 09:30:00+00:00
[2019-10-12 09:25:04.875944] INFO: Performance: last close: 2018-12-28 15:00:00+00:00
[2019-10-12 09:25:06.161999] INFO: bigquant: backtest.v8 运行完成[12.481949s].