真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

VNPY單品種期貨的網(wǎng)格交易策略的實(shí)現(xiàn)是怎樣的-創(chuàng)新互聯(lián)

這篇文章給大家介紹VNPY單品種期貨的網(wǎng)格交易策略的實(shí)現(xiàn)是怎樣的,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的察雅網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

這里做了單品種期貨網(wǎng)格交易策略實(shí)現(xiàn)。
當(dāng)bar.close在通道中時(shí)候,下個(gè)bar打到上軌開多單,打到下軌空單。
這里采用了均量交易法,就是每筆下單手?jǐn)?shù)都是一樣,并非金字塔式下單。
有多單時(shí)候,突破上線加多單,突破下線情況清空所有多單,當(dāng)多單到達(dá)定義的大手?jǐn)?shù)不再下單
為防止在一個(gè)線上下波動(dòng),造成重復(fù)開平倉(cāng)情況,如果突破平倉(cāng),比如平多單,后面n個(gè)bar不能再開多單,只能開空單;反之平空單后,
現(xiàn)在這個(gè)策略很粗糙,只做實(shí)現(xiàn)邏輯分析,可以回測(cè),考慮涉及倉(cāng)位控制,別實(shí)盤。

點(diǎn)擊(此處)折疊或打開

  1. # encoding: UTF-8

  2. from __future__ import division

  3. from vnpy.trader.vtGateway import *

  4. from math import isnan

  5. import numpy as np

  6. import pandas as pd

  7. from vnpy.trader.app.ctaStrategy.ctaTemplate import (CtaTemplate, TargetPosTemplate,

  8.                                                      BarGenerator,

  9.                                                      ArrayManager)

  10. class GridStrategy(CtaTemplate):

  11.     className = 'GridStrategy'

  12.     author = u'BillyZhang'

  13.     # 策略參數(shù)

  14.     historyBars = 200  # 歷史數(shù)據(jù)大小,用來確定網(wǎng)格基準(zhǔn)線

  15.     initDays = 20  # 初始化數(shù)據(jù)所用的天數(shù),隨著歷史數(shù)據(jù)大小要改變

  16.     gridlines = 10  # 網(wǎng)格線數(shù)量,單邊數(shù)量

  17.     ordersize = 10  # 大持倉(cāng)數(shù)量

  18.     order = 1      # 每次下單手?jǐn)?shù)

  19.     barMins = 30    #bar的時(shí)間

  20.     frozenBars = 1 #平倉(cāng)后,frozenBars個(gè)bar不再開反向單

  21.     atrWindow = 30         # ATR窗口數(shù)

  22.     slMultiplier = 5.0     # 計(jì)算止損距離的乘數(shù)

  23.     # 基本變量

  24.     upline = 0   #當(dāng)前上線

  25.     bottomline = 0 #當(dāng)前下線

  26.     frozen = 0 #當(dāng)前是否凍結(jié)開反向單

  27.     intraTradeHigh = 0

  28.     intraTradeLow = 0

  29.     atrValue = 0

  30.     # 參數(shù)列表,保存了參數(shù)的名稱

  31.     paramList = ['name',

  32.                  'className',

  33.                  'author',

  34.                  'vtSymbol',

  35.                  'historyBars'

  36.                  'initDays',

  37.                  'gridlines',

  38.                  'barMins',

  39.                  'order',

  40.                  'ordersize',

  41.                  'atrWindow',

  42.                  'slMultiplier'

  43.                  ]

  44.     # 變量列表,保存了變量的名稱

  45.     varList = ['inited',

  46.                'trading',

  47.                'pos',

  48.                'frozen',

  49.                'upline',

  50.                'bottomline'

  51.                'atrValue']

  52.     # 同步列表,保存了需要保存到數(shù)據(jù)庫(kù)的變量名稱

  53.     syncList = ['pos',

  54.                 'frozen']

  55.     # ----------------------------------------------------------------------

  56.     def __init__(self, ctaEngine, setting):

  57.         """Constructor"""

  58.         super(GridStrategy, self).__init__(ctaEngine, setting)

  59.         self.bg = BarGenerator(self.onBar, self.barMins, self.onXminBar) # 創(chuàng)建K線合成器對(duì)象

  60.         self.am = ArrayManager(self.historyBars + 50)

  61.     # ----------------------------------------------------------------------

  62.     def onInit(self):

  63.         """初始化策略(必須由用戶繼承實(shí)現(xiàn))"""

  64.         self.writeCtaLog(u'%s策略初始化' % self.name)

  65.         # 載入歷史數(shù)據(jù),并采用回放計(jì)算的方式初始化策略數(shù)值

  66.         initData = self.loadBar(self.initDays)

  67.         for bar in initData:

  68.             self.onBar(bar)

  69.         self.putEvent()

  70.     def onStart(self):

  71.         """啟動(dòng)策略(必須由用戶繼承實(shí)現(xiàn))"""

  72.         self.writeCtaLog(u'%s策略啟動(dòng)' % self.name)

  73.         self.putEvent()

  74.     def onStop(self):

  75.         """停止策略(必須由用戶繼承實(shí)現(xiàn))"""

  76.         self.writeCtaLog(u'%s策略停止' % self.name)

  77.         self.putEvent()

  78.     # -----------------------------------------------------------------------

  79.     def onXminBar(self, bar):

  80.         """收到X分鐘K線"""

  81.         # 全撤之前發(fā)出的委托

  82.         self.cancelAll()

  83.         # 保存K線數(shù)據(jù)

  84.         am = self.am

  85.         am.updateBar(bar)

  86.         if not am.inited:

  87.             return

  88.         # 這里采用了均量交易法,就是每筆。

  89.         # 空倉(cāng)時(shí)候,每次突破上線是開多單,突破下線是開空單;

  90.         # 有多單時(shí)候,突破上線加多單,突破下線情況清空所有多單,

  91.         # 有空單時(shí)候,突破下線加空單,突破上線清空所有空單,

  92.         # 為防止在一個(gè)線上下波動(dòng),造成重復(fù)開平倉(cāng)情況,如果突破平倉(cāng),比如平多單,后面n個(gè)bar不能再開多單,只能開空單;反之平空單后,

  93.         # 后面n個(gè)bar只能開多單。

  94.         # 計(jì)算網(wǎng)格,返回通道隊(duì)列, 再算出當(dāng)前點(diǎn)位所在通道,0為最下通道,2*self.gridlines - 1為最上通道

  95.         baseline = self.am.sma(self.historyBars)

  96.         # 過去300的標(biāo)準(zhǔn)差,按照頂一個(gè)gridlines取整做出一個(gè)隊(duì)列

  97.         intervallist = baseline+ np.array([n * 1.00 / self.gridlines for n in range(-1 * self.gridlines, self.gridlines + 1)]) * self.am.std(self.historyBars)

  98.         griploc = pd.cut([bar.close], intervallist, labels=[nx for nx in range(0,2*self.gridlines)])[0]

  99.         # 如果返回為nan,說明現(xiàn)在bar.close在標(biāo)準(zhǔn)差范圍以外,如果沒有倉(cāng)位,先不處理;如果有,按照ATR波動(dòng)移動(dòng)止盈

  100.         if isnan(griploc):

  101.             # 持有多頭倉(cāng)位

  102.             if self.pos > 0:

  103.                 self.intraTradeHigh = max(self.intraTradeHigh, bar.high)

  104.                 self.intraTradeLow = bar.low

  105.                 self.longStop = self.intraTradeHigh - self.atrValue * self.slMultiplier

  106.                 self.sell(self.longStop, abs(self.pos), True)

  107.             # 持有空頭倉(cāng)位

  108.             elif self.pos < 0:

  109.                 self.intraTradeHigh = bar.high

  110.                 self.intraTradeLow = min(self.intraTradeLow, bar.low)

  111.                 self.shortStop = self.intraTradeLow + self.atrValue * self.slMultiplier

  112.                 self.cover(self.shortStop, abs(self.pos), True)

  113.             return

  114.         #返回上下線:

  115.         self.upline = intervallist[griploc + 1]

  116.         self.bottomline = intervallist[griploc]

  117.         # 空倉(cāng)時(shí)候,每次突破上線是開多單,突破下線是開空單;

  118.         # 如果此時(shí)在最下一個(gè)通道,此時(shí)只掛往上的多單, 如果在最上面通道,此時(shí)只掛往下空單;如果在中間的,則同時(shí)開上下單

  119.         if self.pos == 0:

  120.             if griploc ==0:

  121.                 self.buy(self.upline, self.order, True)

  122.             elif griploc == 2*self.gridlines - 1:

  123.                 self.short(self.bottomline,self.order,True)

  124.             else:

  125.                 #此時(shí)如果frozen 為0, 直接開上下單:

  126.                 if self.frozen == 0:

  127.                     self.buy(self.upline, self.order, True)

  128.                     self.short(self.bottomline, self.order, True)

  129.                 #此時(shí)如果大于0,只能開空單,如果小于0,只能開多單

  130.                 elif self.frozen > 0:

  131.                     self.frozen = self.frozen -1

  132.                     self.short(self.bottomline, self.order, True)

  133.                 elif self.frozen < 0:

  134.                     self.frozen = self.frozen + 1

  135.                     self.buy(self.upline, self.order, True)

  136.         #如果持有多倉(cāng)時(shí)候,如果在中間通道,同時(shí)開上下單;如果最高點(diǎn)位不再開單,突破大標(biāo)準(zhǔn)差高點(diǎn),

  137.         elif self.pos > 0:

  138.             # 在最下通道不可能有多單,只用考量在中間段,pos 小于ordersize可以增多倉(cāng),否則只能向下平倉(cāng);和最高段情況,最高段設(shè)置往下平倉(cāng),

  139.             if griploc == 2*self.gridlines - 1:

  140.                 self.intraTradeHigh = bar.high

  141.                 self.sell(self.bottomline, abs(self.pos), True)

  142.             else:

  143.                 if abs(self.pos) < self.ordersize:

  144.                     self.buy(self.upline, self.order, True)

  145.                     self.sell(self.bottomline, abs(self.pos), True)

  146.                 else:

  147.                     self.sell(self.bottomline, abs(self.pos), True)

  148.         elif self.pos < 0:

  149.             # 最上通道通道不可能有空單,只用考慮中間段,和最低檔情況

  150.             if griploc == 0:

  151.                 self.intraTradeLow = bar.low

  152.                 self.cover(self.upline,abs(self.pos),True)

  153.             else:

  154.                 if abs(self.pos) < self.ordersize:

  155.                     self.cover(self.upline, abs(self.pos),True)

  156.                     self.sell(self.bottomline, self.order, True)

  157.                 else:

  158.                     self.cover(self.upline, abs(self.pos), True)

  159.     # ----------------------------------------------------------------------

  160.     def onTick(self, tick):

  161.         """收到行情TICK推送(必須由用戶繼承實(shí)現(xiàn))"""

  162.         self.bg.updateTick(tick)

  163.     # ----------------------------------------------------------------------

  164.     def onBar(self, bar):

  165.         """收到Bar推送(必須由用戶繼承實(shí)現(xiàn))"""

  166.         self.bg.updateBar(bar)

  167.     # ----------------------------------------------------------------------

  168.     def onOrder(self, order):

  169.         """收到委托推送"""

  170.         pass

  171.     # ----------------------------------------------------------------------

  172.     def onTrade(self, trade):

  173.         # 發(fā)出狀態(tài)更新事件

  174.         # 如果收到成交,清空所有掛單

  175.         self.cancelAll()

  176.         # 如果交易多頭方向,且現(xiàn)在倉(cāng)位為0,則應(yīng)該是空頭平倉(cāng),不再開空單

  177.         if trade.direction == DIRECTION_LONG and self.pos == 0:

  178.             self.frozen = -1* self.frozen

  179.         # 如果交易空頭方向,且現(xiàn)在倉(cāng)位為0,則應(yīng)該是多平倉(cāng),不再開多單

  180.         elif trade.direction == DIRECTION_SHORT and self.pos == 0:

  181.             self.frozen = self.frozen

  182.         self.putEvent()

  183.     # ----------------------------------------------------------------------

  184.     def onStopOrder(self, so):

  185.         """停止單推送"""

  186.         pass

關(guān)于VNPY單品種期貨的網(wǎng)格交易策略的實(shí)現(xiàn)是怎樣的就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。


文章名稱:VNPY單品種期貨的網(wǎng)格交易策略的實(shí)現(xiàn)是怎樣的-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://weahome.cn/article/csgpdo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部