美文网首页
9.1 Analyzers

9.1 Analyzers

作者: wanggs66 | 来源:发表于2020-04-28 15:07 被阅读0次

    Analyzer: provide an analysis of what's happened or even of what's autually happening.

    Analyzer

    Location in the ecosystem

    Analyzer objects are (like strategies, observers and datas) added to the system through a cerebro instance:

    • addanalyzer(ancls, *args, **kwargs)

    But when it comes to operation during cerebro.run the following will happen for each strategy present in the system

    • ancls will be instantiated with *args and **kwargs during a cerebro.run

    • The ancls instance will be attached to the strategy

    That means:

    • If the backtesting run contains for example 3 strategies then 3 instances of ancls will be created and each of them will be attached to a different strategy.

    Bottomline: an analyzer analyzes the performance of a single strategy and not the performance of an entires system

    Analyzer 中涉及到的sub-analyzers 或 slave-analyzers也会被加入到相同的策略当中去,但是他们不会被用户看到。

    Attributes

    为了实现具体的功能,在Analyzer中可以利用的一些属性有:

    • self.strategy: reference to the strategy subclass in which the analyzer object is operating. Anything accessible by the strategy can also be accessd by the analyzer
    • self.datas[x]: the array of data feeds present in the strategy. Although this could be accesed over the strategy reference, the shortcut makes work more comfortable.

    s- elf.data: shortcut to self.datas[0] for extra comfort.

    • self.dataX: shortcuts to the different self.datas[x]

    Some other aliases are available although they are probably an overkill:

    • self.dataX_Y where X is a reference to self.datas[X] and Y
      refers to the line, finally pointing to: self.datas[X].lines[Y]

    If the line has a name, the following is also available:

    • self.dataX_Name which resolves to self.datas[X].Name returning
      the line by name rather than by index
      For the first data, the last two shortcuts are available without the initial X numeric reference. For example:
    • self.data_2 refers to self.datas[0].lines[2]

    And

    • self.data_close refers to self.datas[0].close

    返回值

    Analyzer 基类创建了一个self.rets(collections.OrderDict类型)成员变量用于返回分析的结果。通过create_analysis 实现,子类可以通过重写该方法创造个性化的analyzers。

    Modus operandi

    尽管Analyzer 对象不是Lines对象,并且他们不像lines数据一样进行迭代, 但是他们的操作设计都遵循相同的模式。

    1. Instantiated before the system is put into motion (therefore calling init) 在init中初始化

    2. Signaled the begin of operations with start 在start方法中开始相关操作

    3. prenext / nextstart / next will be invoked following the calculated minimum period of the strategy the indicator is working in. prenext / nextstart / next等操作同strategy中一样

    The default behaviour of prenext and nextstart is to invoke next, because an analyzer may be analyzing from the very first moment the system is alive.

    It may be customary to call len(self) in Lines objects to check the actual amount of bars. This also works in Analyzers by returning the value for self.strategy

    1. Orders and trades will be notified just like they are to the strategy via notify_order and notify_trade

    2. Cash and value will also be notified like it is done with the strategy over the notify_cashvalue method

    3. Cash, value and fundvalue and fund shares will also be notified like it is done with the strategy over the notify_fund method

    4. stop will be invoked to signal the end of operations

    Once the regular operations cycle has been completed, the analyzers featuring additional methods for extracting/outputting information

    • get_analysis: which ideally (not enforced) returnes a dict -like object containing the analysis results. 返回dict-like 分析结果

    print uses a standard backtrader.WriterFile (unless overriden) to write the analysis result from get_analysis. 打印分析结果

    pprint (pretty print) uses the Python pprint module to print the get_analysis resutls.

    And finally:

    get_analysis creates a member attribute self.ret (of type collections.OrderedDict) to which analyzers write the analysis results.

    Subclasses of Analyzer can override this method to change this behavior

    Analyzer Patterns

    Analyzer 有两种使用模式:

    1. During execution by gathering information in the notify_xxx and next methods, and generating the current information of the analysis in next 通过Analyzer中的notigy_xxx 和next 获取相关信息,并在next中产生当前信息的分析结果

    The TradeAnalyzer, for example, uses just the notify_trade method to generate the statistics. TradeAnalyzer就是通过notify_trade 方法产生统计结果

    1. Gather (or not) the information as above, but generate the analysis in a single pass during the stop method 通过上面相同的方法获取相关的信息,但仅在stop方法中产生分析的结果

    The SQN (System Quality Number) gathers trade information during notify_trade but generates the statistic during the stop method

    Forensic Analysis of an Analyzer

    Analyzers 不是Lines对象,但是把它们无缝的加入Backtrader 系统,可以通过内部API 将 Lines对象进行整合。

    Reference

    class backtrader.Analyzer()

    Analyzer base class. All analyzers are subclass of this one

    An Analyzer instance operates in the frame of a strategy and provides an analysis for that strategy.

    Automagically set member attributes:

    • self.strategy (giving access to the strategy and anything accessible from it)

    • self.datas[x] giving access to the array of data feeds present in the the system, which could also be accessed via the strategy reference

    • self.data, giving access to self.datas[0]

    • self.dataX -> self.datas[X]

    • self.dataX_Y -> self.datas[X].lines[Y]

    • self.dataX_name -> self.datas[X].name

    • self.data_name -> self.datas[0].name

    • self.data_Y -> self.datas[0].lines[Y]

    This is not a Lines object, but the methods and operation follow the same design

    • init during instantiation and initial setup

    • start / stop to signal the begin and end of operations

    • prenext / nextstart / next family of methods that follow the calls made to the same methods in the strategy

    • notify_trade / notify_order / notify_cashvalue / notify_fund which receive the same notifications as the equivalent methods of the strategy

    • start()
      Invoked to indicate the start of operations, giving the analyzer time to setup up needed things

    • stop()
      Invoked to indicate the end of operations, giving the analyzer time to shut down needed things

    • prenext()
      Invoked for each prenext invocation of the strategy, until the minimum period of the strategy has been reached

    The default behavior for an analyzer is to invoke next

    • nextstart()
      Invoked exactly once for the nextstart invocation of the strategy, when the minimum period has been first reached

    • next()
      Invoked for each next invocation of the strategy, once the minum preiod of the strategy has been reached

    • notify_cashvalue(cash, value)
      Receives the cash/value notification before each next cycle

    • notify_fund(cash, value, fundvalue, shares)
      Receives the current cash, value, fundvalue and fund shares

    • notify_order(order)
      Receives order notifications before each next cycle

    • notify_trade(trade)
      Receives trade notifications before each next cycle

    • get_analysis()
      Returns a dict-like object with the results of the analysis

    The keys and format of analysis results in the dictionary is implementation dependent.

    It is not even enforced that the result is a dict-like object, just the convention

    The default implementation returns the default OrderedDict rets created by the default create_analysis method

    • create_analysis()
      Meant to be overriden by subclasses. Gives a chance to create the structures that hold the analysis.

    The default behaviour is to create a OrderedDict named rets

    • print(*args, **kwargs)
      Prints the results returned by get_analysis via a standard Writerfile object, which defaults to writing things to standard output

    • pprint(*args, **kwargs)
      Prints the results returned by get_analysis using the pretty print Python module (pprint)

    • len()
      Support for invoking len on analyzers by actually returning the current length of the strategy the analyzer operates on

    相关文章

      网友评论

          本文标题:9.1 Analyzers

          本文链接:https://www.haomeiwen.com/subject/ldtvwhtx.html