标签:mount hand history alt ima 初始化 股价 pen option
熟悉聚宽框架使用代码:
# 导入函数库
from jqdata import *
# 初始化函数
def initialize(context):
# 设置基准
set_benchmark(‘000300.XSHG‘)
# 动态复权
set_option(‘use_real_price‘, True)
# 设置手续费
# 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type=‘stock‘)
# 用户定义
# get_index_stocks 获取成分股
g.security = get_index_stocks(‘000300.XSHG‘)
def handle_data(context, data):
# 账户总资金
t_value = context.portfolio.total_value
for stock in g.security:
# 前一天收盘价
p = attribute_history(stock, count=1, df=False)[‘close‘][0]
# 持仓总数
amount = context.portfolio.positions[stock].total_amount
# 平均成本
cost = context.portfolio.positions[stock].avg_cost
# 持仓标准:低价买入
if p < 4 and amount == 0:
order_target_value(stock, 0.1 * t_value)
# 止盈标准:收益率>25%
elif amount > 0 and p >= cost * 1.25:
order_target(stock, 0)
# 止损标准:收益率<10%
elif amount > 0 and p <= cost * 0.9:
order_target(stock, 0)
回测结果:
标签:mount hand history alt ima 初始化 股价 pen option
原文地址:https://blog.51cto.com/tobeys/2445812