'''
周内效应,跳过周四。
'''
import jqdata
## 初始化函数,设定要操作的股票、基准等等
def initialize(context):
# 设定沪深300指数作为基准
set_benchmark('000300.XSHG')
# True为开启动态复权模式,使用真实价格交易
set_option('use_real_price', True)
# 设定成交量比例
set_option('order_volume_ratio', 1)
# 股票类交易手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(open_tax=0, close_tax=0.001, \
open_commission=0.0003, close_commission=0.0003, \
close_today_commission=0, min_commission=5), type='stock')
g.stock = '000300.XSHG'
# 运行函数, 按周运行,在每周第四个交易日运行 买入
run_weekly(trade_buy, weekday=4, time='14:58')
# 运行函数, 按周运行,在每周第三个交易日运行 卖出
run_weekly(trade_sell, weekday=3, time='14:58')
## 交易
def trade_buy(context):
if context.current_dt.isoweekday() != 4:
# 不在周四, 跳过执行
return
# 获取仓位subportfolios[0]的可用资金
available_cash = context.portfolio.available_cash
log.info("Selling %s" % (available_cash))
order_value(g.stock, available_cash)
def trade_sell(context):
if context.current_dt.isoweekday() != 3:
# 不在周三, 跳过执行
return
# 获取仓位subportfolios[0]的可用资金
available_cash = context.portfolio.available_cash
log.info("Selling %s" % (available_cash))
# 获取总账户的持仓价值
if(context.portfolio.positions[g.stock].closeable_amount > 0):
# 清仓
order_target(g.stock, 0)