11#!/usr/bin/env python
22# -*- coding: utf-8; py-indent-offset:4 -*-
3- ###############################################################################
4- #
5- # Copyright (C) 2015-2020 Daniel Rodriguez
6- #
7- # This program is free software: you can redistribute it and/or modify
8- # it under the terms of the GNU General Public License as published by
9- # the Free Software Foundation, either version 3 of the License, or
10- # (at your option) any later version.
11- #
12- # This program is distributed in the hope that it will be useful,
13- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15- # GNU General Public License for more details.
16- #
17- # You should have received a copy of the GNU General Public License
18- # along with this program. If not, see <http://www.gnu.org/licenses/>.
19- #
20- ###############################################################################
3+
214from __future__ import (absolute_import , division , print_function ,
225 unicode_literals )
236
3417# analyzer元类
3518class MetaAnalyzer (bt .MetaParams ):
3619 def donew (cls , * args , ** kwargs ):
37- '''
20+ """
3821 Intercept the strategy parameter
39- '''
22+ """
4023 # Create the object and set the params in place
4124 _obj , args , kwargs = super (MetaAnalyzer , cls ).donew (* args , ** kwargs )
4225
@@ -100,7 +83,7 @@ def dopostinit(cls, _obj, *args, **kwargs):
10083
10184# Analyzer类
10285class Analyzer (with_metaclass (MetaAnalyzer , object )):
103- ''' Analyzer base class. All analyzers are subclass of this one
86+ """ Analyzer base class. All analyzers are subclass of this one
10487
10588 An Analyzer instance operates in the frame of a strategy and provides an
10689 analysis for that strategy.
@@ -115,7 +98,7 @@ class Analyzer(with_metaclass(MetaAnalyzer, object)):
11598 # 访问到strategy实例
11699
117100 - ``self.datas[x]`` giving access to the array of data feeds present in
118- the the system, which could also be accessed via the strategy reference
101+ the system, which could also be accessed via the strategy reference
119102
120103 - ``self.data``, giving access to ``self.datas[0]``
121104
@@ -156,13 +139,13 @@ class Analyzer(with_metaclass(MetaAnalyzer, object)):
156139 # 下面的不是line对象,但是方法和操作设计方法和strategy是类似的。最重要的事情是重写get_analysis,
157140 # 以返回一个字典形式的对象以保存分析的结果
158141
159- '''
142+ """
160143 # 保存结果到csv中
161144 csv = True
162145 # 获取analyzer的长度的时候,其实是计算的策略的长度
163146 def __len__ (self ):
164- ''' Support for invoking ``len`` on analyzers by actually returning the
165- current length of the strategy the analyzer operates on'''
147+ """ Support for invoking ``len`` on analyzers by actually returning the
148+ current length of the strategy the analyzer operates on"""
166149 return len (self .strategy )
167150
168151 # 添加一个child到self._children
@@ -232,70 +215,70 @@ def _stop(self):
232215
233216 # 通知cash 和 value
234217 def notify_cashvalue (self , cash , value ):
235- ''' Receives the cash/value notification before each next cycle'''
218+ """ Receives the cash/value notification before each next cycle"""
236219 pass
237220
238221 # 通知 fund
239222 def notify_fund (self , cash , value , fundvalue , shares ):
240- ''' Receives the current cash, value, fundvalue and fund shares'''
223+ """ Receives the current cash, value, fundvalue and fund shares"""
241224 pass
242225
243226 # 通知order
244227 def notify_order (self , order ):
245- ''' Receives order notifications before each next cycle'''
228+ """ Receives order notifications before each next cycle"""
246229 pass
247230
248231 # 通知 trade
249232 def notify_trade (self , trade ):
250- ''' Receives trade notifications before each next cycle'''
233+ """ Receives trade notifications before each next cycle"""
251234 pass
252235
253236 # next
254237 def next (self ):
255- ''' Invoked for each next invocation of the strategy, once the minum
256- preiod of the strategy has been reached'''
238+ """ Invoked for each next invocation of the strategy, once the minum
239+ period of the strategy has been reached"""
257240 pass
258241
259242 # prenext
260243 def prenext (self ):
261- ''' Invoked for each prenext invocation of the strategy, until the minimum
244+ """ Invoked for each prenext invocation of the strategy, until the minimum
262245 period of the strategy has been reached
263246
264247 The default behavior for an analyzer is to invoke ``next``
265- '''
248+ """
266249 self .next ()
267250
268251 # nextstart
269252 def nextstart (self ):
270- ''' Invoked exactly once for the nextstart invocation of the strategy,
253+ """ Invoked exactly once for the nextstart invocation of the strategy,
271254 when the minimum period has been first reached
272- '''
255+ """
273256 self .next ()
274257
275258 # start
276259 def start (self ):
277- ''' Invoked to indicate the start of operations, giving the analyzer
278- time to setup up needed things'''
260+ """ Invoked to indicate the start of operations, giving the analyzer
261+ time to set up needed things"""
279262 pass
280263
281264 # stop
282265 def stop (self ):
283- ''' Invoked to indicate the end of operations, giving the analyzer
284- time to shut down needed things'''
266+ """ Invoked to indicate the end of operations, giving the analyzer
267+ time to shut down needed things"""
285268 pass
286269
287270 # create_analysis 会创建一个有序字典
288271 def create_analysis (self ):
289- ''' Meant to be overriden by subclasses. Gives a chance to create the
272+ """ Meant to be overridden by subclasses. Gives a chance to create the
290273 structures that hold the analysis.
291274
292275 The default behaviour is to create a ``OrderedDict`` named ``rets``
293- '''
276+ """
294277 self .rets = OrderedDict ()
295278
296279 # 获取分析结果,会返回self.rets
297280 def get_analysis (self ):
298- ''' Returns a *dict-like* object with the results of the analysis
281+ """ Returns a *dict-like* object with the results of the analysis
299282
300283 The keys and format of analysis results in the dictionary is
301284 implementation dependent.
@@ -306,15 +289,15 @@ def get_analysis(self):
306289 The default implementation returns the default OrderedDict ``rets``
307290 created by the default ``create_analysis`` method
308291
309- '''
292+ """
310293 return self .rets
311294
312295 # print数据,通过writerfile打印相应的数据到标准输出
313296 def print (self , * args , ** kwargs ):
314- ''' Prints the results returned by ``get_analysis`` via a standard
297+ """ Prints the results returned by ``get_analysis`` via a standard
315298 ``Writerfile`` object, which defaults to writing things to standard
316299 output
317- '''
300+ """
318301 # 创建一个writer
319302 writer = bt .WriterFile (* args , ** kwargs )
320303 # writer开始
@@ -329,9 +312,9 @@ def print(self, *args, **kwargs):
329312 writer .stop ()
330313 # 使用pprint打印相关的信息
331314 def pprint (self , * args , ** kwargs ):
332- ''' Prints the results returned by ``get_analysis`` using the pretty
315+ """ Prints the results returned by ``get_analysis`` using the pretty
333316 print Python module (*pprint*)
334- '''
317+ """
335318 pp .pprint (self .get_analysis (), * args , ** kwargs )
336319
337320
0 commit comments