JoinQuant详解
#常⽤函数详解
log ⽇志,
info() 信息 ,
warning() 警告,
set_level  设置级别
log.info('hell world')
⽇志 level (类型级别) 必须是‘order’,'history','strategy'⾃⼰在策略中的log
log.set_level('order','warning')    #只获取订单和警告信息
# 按⽉运⾏run_monthly(func, monthday, time='9:30', reference_security, force=False)
run_monthly(monthly, 1, '09:40')  # 指定每⽉第⼀个交易⽇, 在开盘后⼗分钟执⾏,montyly函数
# 按周运⾏ run_weekly(func, weekday, time='9:30', reference_security, force=False)
# 每天内何时运⾏(没有force属性) run_daily(func, time='9:30', reference_security)
运⾏⼀个简单策略
# 导⼊函数库
from jqdata import *
# 初始化函数,设定基准等等
def initialize(context):
# 设置基准,000300.XSHG以沪深300为基准范围
set_benchmark('000300.XSHG')
# 设置复权,选择⽤户真实价格True
set_option('use_real_price',True)
# 设置订单cost(成本),close_tax印花税千1,open_commission(买⼊佣⾦)万3,close_commission卖出万3,最⼩佣⾦5元,stock股票    set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003,min_commission=5), type='stock') # ⽤户⾃定义对象g, security股票代码变量, stocks股票集
# get_index_stocks获取股票集沪深300份股,赋值给security对象
g.security = get_index_stocks('000300.XSHG')
# handle句柄⽤来表⽰对象,该函数每个单位时间会调⽤⼀次,如果按天回测,则每天调⽤⼀次,如果按分钟,则每分钟调⽤⼀次。
def handle_data(context,data):
# portfolio证券组合,total_value:(买⼊⾦额+卖出⾦额总计total)
# total_value总⾦额
t_value = al_value
# 循环遍历获取的沪深300股票代码分别放⼊变量stock
for stock in g.security:
# attribute(属性)history(历史)
# attribute_history(security股票代码,数量count=1,df=False不返回DataFram数据加快回测速度)
# p拿到股票前⼀天的收盘价
p = attribute_history(stock,count=1,df=False)['close'][0]
# amount⾦额 = 所有⽂件.证券组合.positions仓位
# total_amount持仓总数量
amount = context.portfolio.positions[stock].total_amount
# avg平均价
# avg_cost平均成本
cost = context.portfolio.positions[stock].avg_cost
# 如果前⼀天收盘价⼩于4,且股票持仓数==o
# 建仓标准:低价买⼊
if p < 4 and amount == 0:
#target⽬标对象,order_target_value以值为⽬标下单
order_target_value(stock,0.1*t_value)
# ⽌赢条件:当股票持仓⼤于0,收盘价⼤于成本的1.25倍
elif amount > 0 and p >= cost * 1.25:
# 订单⽬标为0,表⽰卖出
Order_target(stock,0)
# ⽌损条件:
elif amount > 0 and p <= cost * 0.9:
setoption
Order_target(stock,0)