分享

Backtrader量化平台教程(四)SSA策略实际案例

 imelee 2018-03-07

        上次我们讲述了strategy的完整生命周期,并引入了一个indicator。今天,我们先讲述几个零碎的知识点,然后正式编写一个SSA策略,包括indicator的编写和策略的回测。

1.关于data

1.1datafeed

        在一开始我们就讨论过datafeed,也就是cerebro的本源,数据。我们看一下上次我们的策略是怎么来的。

  1. dataframe = pd.read_csv('dfqc.csv', index_col=0, parse_dates=True)  
  2. dataframe['openinterest'] = 0  
  3. data = bt.feeds.PandasData(dataname=dataframe,  
  4.                         fromdate = datetime.datetime(2015, 1, 1),  
  5.                         todate = datetime.datetime(2016, 12, 31)  
  6.                         )  
  7. # Add the Data Feed to Cerebro  
  8. cerebro.adddata(data)  
        我们的数据是从一个dfqc的csv文件里面来的,这里,可能大家没有这个数据,之前上传的csdn,不只是还没通过还是怎么的。今天就把他存到百度云,大家有需要的可以自行下载。这是东风汽车的股票数据,是从wind上获取的。大家没有wind也可以用tushare之类的免费api获得。

链接:http://pan.baidu.com/s/1slnULbv 密码:wi3k

        文件里面的数据大概是这样的:

2014-03-13 00:00:00.005,1.425,1.434,1.449,1.418,457767208.0
2014-03-14 00:00:00.005,1.429,1.422,1.436,1.416,196209439.0
2014-03-17 00:00:00.005,1.433,1.434,1.437,1.422,250946201.0
2014-03-18 00:00:00.005,1.434,1.425,1.437,1.424,245516577.0
2014-03-19 00:00:00.005,1.423,1.419,1.423,1.406,331866195.0
2014-03-20 00:00:00.005,1.412,1.408,1.434,1.407,379443759.0
2014-03-21 00:00:00.005,1.406,1.463,1.468,1.403,825467935.0
  1. dataframe = pd.read_csv('dfqc.csv', index_col=0, parse_dates=True)  
         大家注意我们把csv读入pandas的参数,index_col=0表示第一列时间数据是作为pandas 的index的,parse_dates=Ture是自动把数据中的符合日期的格式变成datetime类型。为什么要这样呢?其实读入后的pandas长怎么样都是由backtrader规定的。


1.2 strategy中的data

        大家不知道有没有注意,strategy的初始化函数(init)中,

  1. self.dataclose = self.datas[0].close  
        有这样一句。在backtrader中,self.datas[0] 与self.data是等价的,换句话说,同一个东西不同的叫法罢了。为什么会有前面这种,笔者认为,backtrader应当可以支持多个datafeed,目前还没有使用到,先按下不表。

        看看前面的pandas的要求的结构,我们就知道,不仅仅有self.datas[0].close,还会有self.datas[0].open。也确实如此。只是我们通常拿close作为一个价格基准罢了。这里,

  1. self.datas[0].close  
        返回的是一个lines。lines是backtrader一个很重要的概念,我们可以理解为时间序列流,这类数据,后面可以跟index,也就是说,可以有
  1. self.datas[0].close[0]  
  1. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_6_5245707" name="code" class="python" style="font-size: 18px;">self.datas[0].close[1]</pre><pre code_snippet_id="2353235" snippet_file_name="blog_20170424_7_1483717" name="code" class="python" style="font-size: 18px;">self.datas[0].close[-1]</pre>  
  2. <pre></pre>  
  3. <p></p>  
  4. <pre></pre>  
  5. 等等,但是这里的index是有意义的,0代表当前时刻,-1代表前一时刻,1代表后一时刻,一次类推。  
  6. <p></p>  
  7. <p><span style="font-size:18px">所以在next中使用self.dataclose[0],<span style="font-size:18px">self.dataclose[-1]就很好理解了。</span></span></p>  
  8. <p></p>  
  9. <h1><a name="t3"></a><span style="font-size:18px">2.SSA</span></h1>  
  10. <p></p>  
  11. <p><span style="font-size:18px">        接下来,开始进入完整的一个策略的研究,同时学习编写一个indicator。</span></p>  
  12. <p><span style="font-size:18px">        SSA就是奇异谱分析。</span></p>  
  13.        <span style="font-size:18px"> 奇异谱分析是近年来兴起的一种研究非线性时间序列数据的强大的方法。它根据所观测到的时间序列构造出轨迹矩阵,并对轨迹矩阵进行分解、重构,从而提取出代表原时间序列不同成分的信号,如长期趋势信号、周期信号、噪声信号等,从而对时间序列的结构进行分析,并可进一步预测。</span>  
  14. <p><span style="font-size:18px">        说的通俗点,我们平时用均线,但是有一个问题,均线虽然算法本质上有平滑噪音的含义,但是有时候,也把重要的信息给平滑掉了。所以,我们是不是除了平滑这样的方法之外,要开拓新的方法来分离噪音和有用信息呢。</span></p>  
  15. <p><span style="font-size:18px">图像的压缩给我们一个很重要的启示,就是谱分解,如果不知道什么是谱分解的话,只要知道,这是一种先分解,然后获取重要信息的一种方法即可。</span></p>  
  16. <p><span style="font-size:18px">        奇异谱分解的基本思想很简单,分成如下几个步骤:</span></p>  
  17. <h2><a name="t4"></a><span style="font-size:18px">2.1将时间序列转化为轨迹矩阵</span></h2>  
  18. <p><span style="font-size:18px">假设有一个时间序列,当然,对于我们而言,就是股价:Y(T)=(y(1),⋯,y(T))</span></p>  
  19. <p><span style="font-size:18px">X=</span><span style="font-size:18px">(y1,y2,y3,⋯ym;</span></p>  
  20. <p><span style="font-size:18px">      y2,y3,y4,⋯ym+1;</span></p>  
  21. <p><span style="font-size:18px">     ..................................</span></p>  
  22. <p><span style="font-size:18px">     ..................................</span></p>  
  23. <p><span style="font-size:18px">      yn,yn+1,yn+2,⋯,yT)</span></p>  
  24. <p><span style="font-size:18px">就变成了这样一个n*m的矩阵。</span></p>  
  25. <p><span style="font-size:18px">其中,m为选取的窗口长度,可以当做MA中的窗口长度一样理解。n=T-m+1。</span></p>  
  26. <h2><a name="t5"></a><span style="font-size:18px">2.2SVD分解与矩阵重构</span></h2>  
  27. <p><span style="font-size:18px">计算X.T*X并对其进行奇异值分解(SVD),从而得到其m个特征值</span></p>  
  28. <p><span style="font-size:18px">λ1≥λ2≥⋯≥λm≥0</span></p>  
  29. <p><span style="font-size:18px">然后按照从大到小的原则进行矩阵重构。笔者建议大家看一下SVD分解的相关内容,可以更好的理解这一部分。</span></p>  
  30. <p><span style="font-family:MathJax_Math-italic; font-size:18px">假设重构后的矩阵为</span></p>  
  31. <p><span style="font-family:MathJax_Math-italic; font-size:18px">X=</span><span style="font-family:MathJax_Math-italic; font-size:18px">(x1,1 x1,2 x1,3 ⋯ x1,m;</span></p>  
  32. <p><span style="font-family:MathJax_Math-italic; font-size:18px"> x2,1 x2,2 x2,3  ⋯ x2,m+1 </span></p>  
  33. <p><span style="font-family:MathJax_Math-italic; font-size:18px"> ..........................</span></p>  
  34. <p><span style="font-family:MathJax_Math-italic; font-size:18px"> ..........................</span></p>  
  35. <p><span style="font-family:MathJax_Math-italic; font-size:18px"> xn,1 xn,2 xn,3 ⋯  xn,n+m−1)</span></p>  
  36. <h2><a name="t6"></a><span style="font-family:MathJax_Math-italic; font-size:18px">2.3序列重构</span></h2>  
  37. <p><span style="font-family:MathJax_Math-italic; font-size:18px">   可是我们要的是像MA一样的线呀,现在这个显然不是,所以我们要再把矩阵变成时间序列。方法还是有很多的,比如最常用的有对角相加:</span></p>  
  38. <p><span style="font-family:MathJax_Math-italic; font-size:18px">y1=x1,1 y2=(x1,2 + x2,1)/2  y3=(x1,3 + x2,2 + x3,1)/3</span></p>  
  39. <p><span style="font-family:MathJax_Math-italic; font-size:18px">......</span></p>  
  40. <h2><a name="t7"></a><span style="font-family:MathJax_Math-italic"><span style="font-size:18px">2.4 接下来,我们就用代码实现上面的这些步骤。</span></span></h2>  
  41. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><span style="font-size:18px"></span></span></p>  
  42. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_8_8037370" name="code" class="python">    def get_window_matrix(self, input_array, t, m):  
  43.         # 将时间序列变成矩阵  
  44.         temp = []  
  45.         n = t - m + 1  
  46.         for i in range(n):  
  47.             temp.append(input_array[i:i + m])  
  48.         window_matrix = np.array(temp)  
  49.         return window_matrix  
  50.   
  51.     def svd_reduce(self, window_matrix):  
  52.         # svd分解  
  53.         u, s, v = np.linalg.svd(window_matrix)  
  54.         m1, n1 = u.shape  
  55.         m2, n2 = v.shape  
  56.         index = s.argmax()  # get the biggest index  
  57.         u1 = u[:, index]  
  58.         v1 = v[index]  
  59.         u1 = u1.reshape((m1, 1))  
  60.         v1 = v1.reshape((1, n2))  
  61.         value = s.max()  
  62.         new_matrix = value * (np.dot(u1, v1))  
  63.         return new_matrix  
  64.   
  65.     def recreate_array(self, new_matrix, t, m):  
  66.         # 时间序列重构  
  67.         ret = []  
  68.         n = t - m + 1  
  69.         for p in range(1, t + 1):  
  70.             if p < m:  
  71.                 alpha = p  
  72.             elif p > t - m + 1:  
  73.                 alpha = t - p + 1  
  74.             else:  
  75.                 alpha = m  
  76.             sigma = 0  
  77.             for j in range(1, m + 1):  
  78.                 i = p - j + 1  
  79.                 if i > 0 and i < n + 1:  
  80.                     sigma += new_matrix[i - 1][j - 1]  
  81.             ret.append(sigma / alpha)  
  82.         return ret  
  83.   
  84.     def SSA(self, input_array, t, m):  
  85.         window_matrix = self.get_window_matrix(input_array, t, m)  
  86.         new_matrix = self.svd_reduce(window_matrix)  
  87.         new_array = self.recreate_array(new_matrix, t, m)  
  88.         return new_array</pre>   这里,我们调用SSA函数就可以获得重构后的时间序列。  
  89. <p></p>  
  90. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><span style="font-size:18px">那么按照逻辑,重构后的时间序列我们选取最后一个,就是我们要的当天的SSA了,然后就可以像趋势线一样使用。</span></span></p>  
  91. <h1><a name="t8"></a><span style="font-family:MathJax_Math-italic; font-size:18px"><span style="font-size:18px">3.indicator编写</span></span></h1>  
  92. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><span style="font-size:18px">   到这里,我们已经有了生成一个像SMA均线一样的函数了,接下来就可以开始写一个自己的indicator。到这里,我们先来看一下上次我们生成的indicator最后plot出来的图:</span></span></p>  
  93. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><span style="font-size:18px"><img src="http://img.blog.csdn.net/20170424200502314?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXRseXg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br>  
  94. <br>  
  95. </span></span></p>  
  96. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  我们可以从图上看出很多直观的信息。比如什么时候买,什么时候卖,之前的indicator线长什么样。</span><br>  
  97. </p>  
  98. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  不说SMA,我们写一下我们自己的indicator吧。</span></p>  
  99. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  首先,我们看一下我们调用SMA的时候是怎么调用的吧。</span></p>  
  100. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  101. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_9_9433592" name="code" class="python">        self.sma = bt.indicators.SimpleMovingAverage(  
  102.             self.datas[0], period=self.params.maperiod)  
  103. </pre>  其实们这里self.datas[0]不最为参数传入也是可以的。  
  104. <p></p>  
  105. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  即如果你写成:</span></p>  
  106. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  107. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_10_655092" name="code" class="python">        self.sma = bt.indicators.SimpleMovingAverage(period=self.params.maperiod)  
  108. </pre>效果是一毛一样的。  
  109. <p></p>  
  110. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  我们首先可以看到,这是一个和bt.indicator类有很大关系的一个东西,然后可以在调用的时候传入一个参数,在SMA中是平滑移动的窗口大小。</span></p>  
  111. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  实际上,任何一个indicator都是bt.indicator的子类,也就是说,我们自己写indicator的时候需要继承bt.indicator。既然是继承,那么接下来就是override几个函数,然后就可以实现功能了。而实际情况就是这样。</span></p>  
  112. <p><span style="font-family:MathJax_Math-italic; font-size:18px">我们再来观察一下我们在使用indicator的时候是怎么使用的。</span></p>  
  113. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  114. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_11_9401355" name="code" class="python"> if self.dataclose[0] > self.sma[0]:  
  115. </pre>前面说过,<br>  
  116. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_12_2574702" name="code" class="python" style="font-size: 18px;">self.dataclose[0]</pre>是一个lines,不难猜测,self.sma也是一个lines。所以说,我们自己写的indica最后返回的就应该是一个lines。  
  117. <p></p>  
  118. <p><span style="font-family:MathJax_Math-italic; font-size:18px">好了,我们开始吧。</span></p>  
  119. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  120. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_13_3272814" name="code" class="python">class ssa_index_ind(bt.Indicator):  
  121.     lines = ('ssa',)  
  122.     def __init__(self, ssa_window):  
  123.         self.params.ssa_window = ssa_window  
  124.         # 这个很有用,会有 not maturity生成  
  125.         self.addminperiod(self.params.ssa_window * 2)  
  126.   
  127.     def get_window_matrix(self, input_array, t, m):  
  128.         # 将时间序列变成矩阵  
  129.         temp = []  
  130.         n = t - m + 1  
  131.         for i in range(n):  
  132.             temp.append(input_array[i:i + m])  
  133.         window_matrix = np.array(temp)  
  134.         return window_matrix  
  135.   
  136.     def svd_reduce(self, window_matrix):  
  137.         # svd分解  
  138.         u, s, v = np.linalg.svd(window_matrix)  
  139.         m1, n1 = u.shape  
  140.         m2, n2 = v.shape  
  141.         index = s.argmax()  # get the biggest index  
  142.         u1 = u[:, index]  
  143.         v1 = v[index]  
  144.         u1 = u1.reshape((m1, 1))  
  145.         v1 = v1.reshape((1, n2))  
  146.         value = s.max()  
  147.         new_matrix = value * (np.dot(u1, v1))  
  148.         return new_matrix  
  149.   
  150.     def recreate_array(self, new_matrix, t, m):  
  151.         # 时间序列重构  
  152.         ret = []  
  153.         n = t - m + 1  
  154.         for p in range(1, t + 1):  
  155.             if p < m:  
  156.                 alpha = p  
  157.             elif p > t - m + 1:  
  158.                 alpha = t - p + 1  
  159.             else:  
  160.                 alpha = m  
  161.             sigma = 0  
  162.             for j in range(1, m + 1):  
  163.                 i = p - j + 1  
  164.                 if i > 0 and i < n + 1:  
  165.                     sigma += new_matrix[i - 1][j - 1]  
  166.             ret.append(sigma / alpha)  
  167.         return ret  
  168.   
  169.     def SSA(self, input_array, t, m):  
  170.         window_matrix = self.get_window_matrix(input_array, t, m)  
  171.         new_matrix = self.svd_reduce(window_matrix)  
  172.         new_array = self.recreate_array(new_matrix, t, m)  
  173.         return new_array  
  174.   
  175.     def next(self):  
  176.         data_serial = self.data.get(size=self.params.ssa_window * 2)  
  177.         self.lines.ssa[0] = self.SSA(data_serial, len(data_serial), int(len(data_serial) / 2))[-1]  
  178. </pre><br>  
  179.   大部分是ssa计算大代码,我们看一下之前没见过的。  
  180. <p></p>  
  181. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  182. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_14_7002567" name="code" class="python">lines = ('ssa',)  
  183.     def __init__(self, ssa_window):  
  184.         self.params.ssa_window = ssa_window  
  185.         # 这个很有用,会有 not maturity生成  
  186.         self.addminperiod(self.params.ssa_window * 2)</pre>  这里,lines是必须的,一个indicator至少要有一个lines,里面的ssa就是这个lines的命名,后续会出现。  
  187. <p></p>  
  188. <p><span style="font-family:MathJax_Math-italic; font-size:18px">既然是继承了一个类,那么init函数是必须的了。传入的参数是ssa是窗口大小,那么不言而已,我们收下就是了。<br>  
  189. </span></p>  
  190. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_15_6064476" name="code" class="python" style="font-size: 18px;"> self.addminperiod(self.params.ssa_window * 2)</pre>  这个是什么呢?我们在上一次将strategy的生命周期的时候讲到过,比如一个5day的均线,那么开始五天是没有indicator的,这个时候,策略会调用prenext方法。而在indicator中,这个函数就是告诉strategy,我需要几天才能成熟。根据ssa的计算方法,我们需要的是<pre code_snippet_id="2353235" snippet_file_name="blog_20170424_16_8714435" name="code" class="python" style="font-size: 18px;">self.params.ssa_window * 2</pre>  
  191. <p></p>  
  192. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  然后是next,和strategy一样</span></p>  
  193. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  194. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_17_4603780" name="code" class="python">    def next(self):  
  195.         data_serial = self.data.get(size=self.params.ssa_window * 2)  
  196.         self.lines.ssa[0] = self.SSA(data_serial, len(data_serial), int(len(data_serial) / 2))[-1]  
  197. </pre>  每一个时刻这个方法都会被调用一次。  
  198. <p></p>  
  199. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  200. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_18_8856921" name="code" class="python" style="font-size: 18px;">self.data.get(size=self.params.ssa_window * 2)</pre>  用于获得size数量的数据,当然是往历史方向延伸。  
  201. <p></p>  
  202. <p><span style="font-family:MathJax_Math-italic; font-size:18px">到这里,我们的indicator就完全写好了。</span></p>  
  203. <h1><a name="t9"></a><span style="font-family:MathJax_Math-italic; font-size:18px">4.调用indicator</span></h1>  
  204. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  205. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_19_7537146" name="code" class="python">self.ssa = ssa_index_ind(ssa_window=self.params.ssa_window, subplot=False)  
  206. </pre>  一模一样的调用方法。这里我们看到比SMA多了一个参数,subplot = False。这个最后再解释。  
  207. <p></p>  
  208. <p><span style="font-family:MathJax_Math-italic; font-size:18px">  把所有代码贴出来,然后运行一下吧。</span></p>  
  209. <p><span style="font-family:MathJax_Math-italic; font-size:18px"></span></p>  
  210. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_20_9315052" name="code" class="python"># -*- coding: utf-8 -*-  
  211.   
  212. from __future__ import (absolute_import, division, print_function,  
  213.                         unicode_literals)  
  214.   
  215. import datetime  # For datetime objects  
  216. import pandas as pd  
  217. import backtrader as bt  
  218. import numpy as np  
  219.   
  220. class ssa_index_ind(bt.Indicator):  
  221.     lines = ('ssa',)  
  222.     def __init__(self, ssa_window):  
  223.         self.params.ssa_window = ssa_window  
  224.         # 这个很有用,会有 not maturity生成  
  225.         self.addminperiod(self.params.ssa_window * 2)  
  226.   
  227.     def get_window_matrix(self, input_array, t, m):  
  228.         # 将时间序列变成矩阵  
  229.         temp = []  
  230.         n = t - m + 1  
  231.         for i in range(n):  
  232.             temp.append(input_array[i:i + m])  
  233.         window_matrix = np.array(temp)  
  234.         return window_matrix  
  235.   
  236.     def svd_reduce(self, window_matrix):  
  237.         # svd分解  
  238.         u, s, v = np.linalg.svd(window_matrix)  
  239.         m1, n1 = u.shape  
  240.         m2, n2 = v.shape  
  241.         index = s.argmax()  # get the biggest index  
  242.         u1 = u[:, index]  
  243.         v1 = v[index]  
  244.         u1 = u1.reshape((m1, 1))  
  245.         v1 = v1.reshape((1, n2))  
  246.         value = s.max()  
  247.         new_matrix = value * (np.dot(u1, v1))  
  248.         return new_matrix  
  249.   
  250.     def recreate_array(self, new_matrix, t, m):  
  251.         # 时间序列重构  
  252.         ret = []  
  253.         n = t - m + 1  
  254.         for p in range(1, t + 1):  
  255.             if p < m:  
  256.                 alpha = p  
  257.             elif p > t - m + 1:  
  258.                 alpha = t - p + 1  
  259.             else:  
  260.                 alpha = m  
  261.             sigma = 0  
  262.             for j in range(1, m + 1):  
  263.                 i = p - j + 1  
  264.                 if i > 0 and i < n + 1:  
  265.                     sigma += new_matrix[i - 1][j - 1]  
  266.             ret.append(sigma / alpha)  
  267.         return ret  
  268.   
  269.     def SSA(self, input_array, t, m):  
  270.         window_matrix = self.get_window_matrix(input_array, t, m)  
  271.         new_matrix = self.svd_reduce(window_matrix)  
  272.         new_array = self.recreate_array(new_matrix, t, m)  
  273.         return new_array  
  274.   
  275.     def next(self):  
  276.         data_serial = self.data.get(size=self.params.ssa_window * 2)  
  277.         self.lines.ssa[0] = self.SSA(data_serial, len(data_serial), int(len(data_serial) / 2))[-1]  
  278.   
  279. # Create a Stratey  
  280. class MyStrategy(bt.Strategy):  
  281.     params = (  
  282.         ('ssa_window', 15),  
  283.         ('maperiod', 15),  
  284.     )  
  285.   
  286.     def log(self, txt, dt=None):  
  287.         ''''' Logging function fot this strategy'''  
  288.         dt = dt or self.datas[0].datetime.date(0)  
  289.         print('%s, %s' % (dt.isoformat(), txt))  
  290.   
  291.     def __init__(self):  
  292.         # Keep a reference to the "close" line in the data[0] dataseries  
  293.         self.dataclose = self.datas[0].close  
  294.   
  295.         # To keep track of pending orders and buy price/commission  
  296.         self.order = None  
  297.         self.buyprice = None  
  298.         self.buycomm = None  
  299.   
  300.         # Add a MovingAverageSimple indicator  
  301.         self.ssa = ssa_index_ind(ssa_window=self.params.ssa_window, subplot=False)  
  302.         # bt.indicator.LinePlotterIndicator(self.ssa, name='ssa')  
  303.         self.sma = bt.indicators.SimpleMovingAverage(period=self.params.maperiod)  
  304.     def start(self):  
  305.         print("the world call me!")  
  306.   
  307.     def prenext(self):  
  308.         print("not mature")  
  309.   
  310.     def notify_order(self, order):  
  311.         if order.status in [order.Submitted, order.Accepted]:  
  312.             # Buy/Sell order submitted/accepted to/by broker - Nothing to do  
  313.             return  
  314.   
  315.         # Check if an order has been completed  
  316.         # Attention: broker could reject order if not enougth cash  
  317.         if order.status in [order.Completed]:  
  318.             if order.isbuy():  
  319.                 self.log(  
  320.                     'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %  
  321.                     (order.executed.price,  
  322.                      order.executed.value,  
  323.                      order.executed.comm))  
  324.   
  325.                 self.buyprice = order.executed.price  
  326.                 self.buycomm = order.executed.comm  
  327.             else:  # Sell  
  328.                 self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %  
  329.                          (order.executed.price,  
  330.                           order.executed.value,  
  331.                           order.executed.comm))  
  332.   
  333.             self.bar_executed = len(self)  
  334.   
  335.         elif order.status in [order.Canceled, order.Margin, order.Rejected]:  
  336.             self.log('Order Canceled/Margin/Rejected')  
  337.   
  338.         self.order = None  
  339.   
  340.     def notify_trade(self, trade):  
  341.         if not trade.isclosed:  
  342.             return  
  343.   
  344.         self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %  
  345.                  (trade.pnl, trade.pnlcomm))  
  346.   
  347.     def next(self):  
  348.         # Simply log the closing price of the series from the reference  
  349.         self.log('Close, %.2f' % self.dataclose[0])  
  350.   
  351.         # Check if an order is pending ... if yes, we cannot send a 2nd one  
  352.         if self.order:  
  353.             return  
  354.   
  355.         # Check if we are in the market  
  356.         if not self.position:  
  357.   
  358.             # Not yet ... we MIGHT BUY if ...  
  359.             if self.dataclose[0] > self.ssa[0]:  
  360.   
  361.                 # BUY, BUY, BUY!!! (with all possible default parameters)  
  362.                 self.log('BUY CREATE, %.2f' % self.dataclose[0])  
  363.   
  364.                 # Keep track of the created order to avoid a 2nd order  
  365.                 self.order = self.buy()  
  366.   
  367.         else:  
  368.   
  369.             if self.dataclose[0] < self.ssa[0]:  
  370.                 # SELL, SELL, SELL!!! (with all possible default parameters)  
  371.                 self.log('SELL CREATE, %.2f' % self.dataclose[0])  
  372.   
  373.                 # Keep track of the created order to avoid a 2nd order  
  374.                 self.order = self.sell()  
  375.     def stop(self):  
  376.         print("death")  
  377.   
  378. if __name__ == '__main__':  
  379.     # Create a cerebro entity  
  380.     cerebro = bt.Cerebro()  
  381.     # Add a strategy  
  382.     cerebro.addstrategy(MyStrategy)  
  383.     # 本地数据,笔者用Wind获取的东风汽车数据以csv形式存储在本地。  
  384.     # parase_dates = True是为了读取csv为dataframe的时候能够自动识别datetime格式的字符串,big作为index  
  385.     # 注意,这里最后的pandas要符合backtrader的要求的格式  
  386.     dataframe = pd.read_csv('dfqc.csv', index_col=0, parse_dates=True)  
  387.     dataframe['openinterest'] = 0  
  388.     data = bt.feeds.PandasData(dataname=dataframe,  
  389.                             fromdate = datetime.datetime(2015, 1, 1),  
  390.                             todate = datetime.datetime(2016, 12, 31)  
  391.                             )  
  392.     # Add the Data Feed to Cerebro  
  393.     cerebro.adddata(data)  
  394.     # Set our desired cash start  
  395.     cerebro.broker.setcash(100.0)  
  396.     # 设置每笔交易交易的股票数量  
  397.     cerebro.addsizer(bt.sizers.FixedSize, stake=10)  
  398.     # Set the commission  
  399.     cerebro.broker.setcommission(commission=0.0)  
  400.     # Print out the starting conditions  
  401.     print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())  
  402.     # Run over everything  
  403.     cerebro.run()  
  404.     # Print out the final result  
  405.     print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())  
  406.     cerebro.plot()  
  407. </pre>结果是这样的:  
  408. <p></p>  
  409. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><img src="http://img.blog.csdn.net/20170424202750157?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXRseXg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br>  
  410. <br>  
  411.   如果我们把</span></p>  
  412. <pre code_snippet_id="2353235" snippet_file_name="blog_20170424_21_6425114" name="code" class="python" style="font-size: 18px;">subplot=False</pre>删去,大家可以自己运行一下看看plot出来的是怎么样的呢。  
  413. <p></p>  
  414. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><br>  
  415. <br>  
  416. <br>  
  417. <br>  
  418. </span></p>  
  419. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><br>  
  420. </span></p>  
  421. <p><span style="font-family:MathJax_Math-italic; font-size:18px"><br>  
  422. </span></p>  
  423.               

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多