[关闭]
@Channelchan 2017-06-06T13:44:38.000000Z 字数 850 阅读 1868

1. 一个简单的MA策略模板

未分类


  1. import rqalpha
  2. from rqalpha.api import *
  3. import talib
  1. def init(context):
  2. context.s1 = "000001.XSHE"
  3. context.SHORTPERIOD = 10
  4. context.LONGPERIOD = 30
  1. def handle_bar(context, bar_dict):
  2. prices = history_bars(context.s1, context.LONGPERIOD+1, '1d', 'close')
  3. short_avg = talib.SMA(prices, context.SHORTPERIOD)
  4. long_avg = talib.SMA(prices, context.LONGPERIOD)
  5. plot("short avg", short_avg[-1])
  6. plot("long avg", long_avg[-1])
  7. # 计算现在portfolio中股票的仓位
  8. cur_position = context.portfolio.positions[context.s1].quantity
  9. # 计算现在portfolio中的现金可以购买多少股票
  10. shares = context.portfolio.cash/bar_dict[context.s1].close
  11. # 如果短均线从上往下跌破长均线,也就是在目前的bar短线平均值低于长线平均值,而上一个bar的短线平均值高于长线平均值
  12. if short_avg[-1] - long_avg[-1] < 0 and short_avg[-2] - long_avg[-2] > 0 and cur_position > 0:
  13. # 进行清仓
  14. order_target_value(context.s1, 0)
  15. # 如果短均线从下往上突破长均线,为入场信号
  16. if short_avg[-1] - long_avg[-1] > 0 and short_avg[-2] - long_avg[-2] < 0:
  17. # 满仓入股
  18. order_shares(context.s1, shares)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注