[关闭]
@Channelchan 2018-01-16T11:08:08.000000Z 字数 1499 阅读 1425

写一个均线百分比通道突破策略

股票代号: 600050

指标: MA(,timeperiod=60)

通道百分比: 3%

Buy: 突破上轨满仓买入

Sell: 突破下轨满仓卖出

图表: 画出指标的图表

  1. # Bollinger Band
  2. import rqalpha
  3. from rqalpha.api import *
  4. import talib as ta
  5. def init(context):
  6. context.s1 = "600050.XSHG"
  7. context.PERIOD = 60
  8. context.UP = 1.03
  9. context.LOW = 0.97
  10. def handle_bar(context, bar_dict):
  11. # record(context, bar_dict)
  12. record_Chart(context, bar_dict)
  13. prices = history_bars(context.s1, context.PERIOD+2, '1d', 'close')
  14. MA_middle = ta.EMA(prices,context.PERIOD)
  15. MA_upper = MA_middle*context.UP
  16. MA_lower = MA_middle*context.LOW
  17. cur_position = context.portfolio.positions[context.s1].quantity
  18. shares = context.portfolio.cash/bar_dict[context.s1].close
  19. if prices[-1]<MA_lower[-2] and cur_position > 0:
  20. order_target_value(context.s1, 0)
  21. if prices[-1]>MA_upper[-2]:
  22. order_shares(context.s1, shares)
  23. def record(context, bar_dict):
  24. pos_s1 = context.portfolio.positions[context.s1].quantity
  25. price_s1 = context.portfolio.positions[context.s1].avg_price
  26. capital = pos_s1*price_s1
  27. plot("capital", capital)
  28. def record_Chart(context, bar_dict):
  29. prices = history_bars(context.s1, context.PERIOD+2, '1d', 'close')
  30. MA_middle = ta.EMA(prices,context.PERIOD)
  31. MA_upper = MA_middle*context.UP
  32. MA_lower = MA_middle*context.LOW
  33. plot("prices", prices[-1])
  34. plot("MA_middle", MA_middle[-1])
  35. plot("MA_upper", MA_upper[-1])
  36. plot("MA_lower", MA_lower[-1])
  37. config = {
  38. "base": {
  39. "start_date": "2014-06-01",
  40. "end_date": "2018-01-15",
  41. "accounts": {'stock':1000000},
  42. "benchmark": "600050.XSHG"
  43. },
  44. "extra": {
  45. "log_level": "error",
  46. },
  47. "mod": {
  48. "sys_analyser": {
  49. "enabled": True,
  50. "plot": True
  51. }
  52. }
  53. }
  54. rqalpha.run_func(init=init, handle_bar=handle_bar, config=config)

output_1_0.png-111.4kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注