VNPY 基于SAR和肯特纳的交易策略

一个比较简单策略,主要是为了验证SAR出场指标的;然后和可以结合其他下单值,做的一个简单组合。只是用来测试。

入场指标,cci,如果cci大于0,多头,cci小于0空头。下阻止单,金额就是Kelter上下轨。多头买入价格是通道上轨。空头买入价格是通道下轨。

出场指标,这里出场和入场适用不同周期的k线,因为这样可以更灵活跑回测。SAR价格作为卖出价格,开阻止单,如果触及SAR价格卖出。

先在class ArrayManager() 加入sar方法。当然最好还是继承新增。

点击(此处)折叠或打开

  1. def sar(self, acceleration = 0.02, maximum = 0.2, array =False):
  2.         “”“sar”“”
  3.         real = talib.SAR(self.high,self.low, acceleration= acceleration,maximum = maximum)
  4.         if array:
  5.             return real
  6.         return real[1]

然后创建策略。

点击(此处)折叠或打开

  1. # encoding: UTF8
  2. from __future__ import division
  3. from vnpy.trader.vtObject import VtBarData
  4. from vnpy.trader.vtConstant import EMPTY_STRING
  5. from vnpy.trader.app.ctaStrategy.ctaTemplate import (CtaTemplate,
  6.                                                      BarGenerator,
  7.                                                      ArrayManager)
  8. ########################################################################
  9. class SARKELStrategy(CtaTemplate):
  10.     “”“基于sar and Keltner 交易策略”“”
  11.     className = ‘SARKELStrategy’
  12.     author = u‘BillyZhang’
  13.     # 策略参数
  14.     sarAcceleration = 0.02 #加速线
  15.     sarMaximum = 0.2 #
  16.     cciWindow = 20 # CCI窗口数
  17.     keltnerWindow = 25 # keltner窗口数
  18.     keltnerlMultiplier = 6.0 # 乘数
  19.     initDays = 10 # 初始化数据所用的天数
  20.     fixedSize = 1 # 每次交易的数量
  21.     barMins = 15
  22.     barMinsClose = 10
  23.     # 策略变量
  24.     sarValue = 0 # sar指标数值
  25.     cciValue = 0 # CCI指标数值
  26.     keltnerup = 0
  27.     keltnerdown = 0
  28.     longStop = 0 # 多头止损
  29.     shortStop = 0 # 空头止损
  30.     # 参数列表,保存了参数的名称
  31.     paramList = [‘name’,
  32.                  ‘className’,
  33.                  ‘author’,
  34.                  ‘vtSymbol’,
  35.                  ‘sarAcceleration’,
  36.                  ‘sarMaximum’,
  37.                  ‘cciWindow’,
  38.                  ‘keltnerWindow’,
  39.                  ‘keltnerlMultiplier’,
  40.                  ‘initDays’,
  41.                  ‘fixedSize’,
  42.                  ‘barMinsClose’,
  43.                  ‘barMins’]
  44.     # 变量列表,保存了变量的名称
  45.     varList = [‘inited’,
  46.                ‘trading’,
  47.                ‘pos’,
  48.                ‘sarValue’,
  49.                ‘cciValue’,
  50.                ‘atrValue’,
  51.                ‘intraBarHigh’,
  52.                ‘intraBarLow’,
  53.                ‘longStop’,
  54.                ‘shortStop’]
  55.     # 同步列表,保存了需要保存到数据库的变量名称
  56.     syncList = [‘pos’,
  57.                 ‘intraTradeHigh’,
  58.                 ‘intraTradeLow’]
  59.     #
  60.     def __init__(self, ctaEngine, setting):
  61.         “”“Constructor”“”
  62.         super(SARKELStrategy, self).__init__(ctaEngine, setting)
  63.         self.bg = BarGenerator(self.onBar, self.barMins, self.onXminBar) # 创建K线合成器对象
  64.         self.am = ArrayManager()
  65.         self.bgclose = BarGenerator(self.onBar, self.barMinsClose, self.onminBarClose)
  66.         self.amClose = ArrayManager()
  67.     #
  68.     def onminBarClose(self, bar):
  69.         “”“分钟作为清仓周期”“”
  70.         # 如果没有仓位,那么不用care,直接skip
  71.         # 保存K线数据
  72.         amClose = self.amClose
  73.         amClose.updateBar(bar)
  74.         if not amClose.inited:
  75.             return
  76.         # 计算指标数值
  77.         self.sarValue = amClose.sar(self.sarAcceleration,self.sarMaximum)
  78.         # 判断是否要进行交易
  79.         if self.pos == 0:
  80.             return
  81.         # 当前无仓位,发送开仓委托
  82.         # 持有多头仓位
  83.         elif self.pos > 0:
  84.             self.cancelAll()
  85.             self.sell(self.sarValue, abs(self.pos), True)
  86.         # 持有空头仓位
  87.         elif self.pos < 0:
  88.             self.cancelAll()
  89.             self.cover(self.sarValue, abs(self.pos), True)
  90.         # 同步数据到数据库
  91.         self.saveSyncData()
  92.         # 发出状态更新事件
  93.         self.putEvent()
  94.         #
  95.     def onInit(self):
  96.         “”“初始化策略(必须由用户继承实现)”“”
  97.         self.writeCtaLog(u‘%s策略初始化’ % self.name)
  98.         # 载入历史数据,并采用回放计算的方式初始化策略数值
  99.         initData = self.loadBar(self.initDays)
  100.         for bar in initData:
  101.             self.onBar(bar)
  102.         self.putEvent()
  103.     #
  104.     def onStart(self):
  105.         “”“启动策略(必须由用户继承实现)”“”
  106.         self.writeCtaLog(u‘%s策略启动’ % self.name)
  107.         self.putEvent()
  108.     #
  109.     def onStop(self):
  110.         “”“停止策略(必须由用户继承实现)”“”
  111.         self.writeCtaLog(u‘%s策略停止’ % self.name)
  112.         self.putEvent()
  113.     #
  114.     def onTick(self, tick):
  115.         “”“收到行情TICK推送(必须由用户继承实现)”“”
  116.         self.bg.updateTick(tick)
  117.     #
  118.     def onBar(self, bar):
  119.         “”“收到Bar推送(必须由用户继承实现)”“”
  120.         self.bg.updateBar(bar)
  121.         self.bgclose.updateBar(bar)
  122.     #
  123.     def onXminBar(self, bar):
  124.         “”“收到X分钟K线”“”
  125.         # 全撤之前发出的委托
  126.         self.cancelAll()
  127.         # 保存K线数据
  128.         am = self.am
  129.         am.updateBar(bar)
  130.         if not am.inited:
  131.             return
  132.         # 计算指标数值
  133.         self.cciValue = am.cci(self.cciWindow)
  134.         self.keltnerup, self.keltnerdown = am.keltner(self.keltnerWindow,self.keltnerlMultiplier)
  135.         # 判断是否要进行交易
  136.         # 当前无仓位,发送开仓委托
  137.         if self.pos == 0:
  138.             if self.cciValue > 0:
  139.                 # ru
  140.                 self.buy(self.keltnerup,self.fixedSize, True)
  141.             elif self.cciValue < 0:
  142.                 self.short(self.keltnerdown, self.fixedSize, True)
  143.         # 同步数据到数据库
  144.         self.saveSyncData()
  145.         # 发出状态更新事件
  146.         self.putEvent()
  147.     #
  148.     def onOrder(self, order):
  149.         “”“收到委托变化推送(必须由用户继承实现)”“”
  150.         pass
  151.     #
  152.     def onTrade(self, trade):
  153.         # 发出状态更新事件
  154.         self.putEvent()
  155.     #
  156.     def onStopOrder(self, so):
  157.         “”“停止单推送”“”
  158.         pass
    原文作者:张国平
    原文地址: http://blog.itpub.net/22259926/viewspace-2157326/
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞