VNPY 基於SAR和肯特納的交易策略

張國平發表於2018-07-04
一個比較簡單策略,主要是為了驗證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: UTF-8

  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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22259926/viewspace-2157326/,如需轉載,請註明出處,否則將追究法律責任。

相關文章