9.1 Analyzers

2020-04-28  本文已影响0人  wanggs66

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:

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

That means:

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中可以利用的一些属性有:

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

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

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

And

返回值

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

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:

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

The default behavior for an analyzer is to invoke next

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

The default behaviour is to create a OrderedDict named rets

上一篇 下一篇

猜你喜欢

热点阅读