Backtrader中文笔记之Extending a Datafeed

Issues in GitHub are actually pushing into finishing documentation parts or helping me to understand if backtrader has the ease of use and flexibility I envisioned from the first moments and the decisions made along the way.

GitHub的问题实际上是推动完成文档部分,或者帮助我了解backtrader是否具有我从一开始就设想的易用性和灵活性,以及在此过程中所做的决定。

In this case is Issue #9.

The question finally seems to boil down to:

  • Can the end user easily extend the existing mechanisms to add extra information in the form of lines that gets passed along other existing price information spots like open, high, etc?
  • 终端用户是否可以轻松地扩展现有机制,以lines的形式添加额外的信息,从而沿着其他现有的价格信息点(如open、high等)传递?

As far as I understand the question the answer is: Yes

 据我所知,这个问题的答案是可以的

The poster seems to have these requirements (from Issue #6):

提出者有这些要求

  • A data source which is being parsed into CSV format

  • 正在解析为CSV格式的数据源
  • Using GenericCSVData to load the information

  • 使用GenericCSVData加载信息

    This generic csv support was developed in response to this Issue #6

  • An extra field which apparently contains P/E information which needs to be passed along the parsed CSV Data

  • 一个额外的字段,显然包含P/E信息,需要沿着解析的CSV数据传递

Let’s build on the CSV Data Feed Development and GenericCSVData example posts.

让我们在CSV数据提要开发和GenericCSVData示例文章的基础上构建。

Steps:

步骤:

  • Assume the P/E information is being set in the CSV data which is parsed

  • 假设在解析的CSV数据中设置了P/E信息
  • Use GenericCSVData as the base class

  • 使用GenericCSVData作为基类
  • Extend the existng lines (open/high/low/close/volumen/openinterest) with pe

  • 用pe扩展现有的产品线(open/high/low/close/volumen/openinterest)
  • Add a parameter to let the caller determine the column position of the P/E information

  • 添加一个参数,让调用者确定P/E信息的列位置

 The result:

from backtrader.feeds import GenericCSVData

class GenericCSV_PE(GenericCSVData):

    # Add a 'pe' line to the inherited ones from the base class
    lines = ('pe',)

    # openinterest in GenericCSVData has index 7 ... add 1
    # add the parameter to the parameters inherited from the base class
    params = (('pe', 8),)

And the job is done …

Later and when using this data feed inside a strategy:

import backtrader as bt

....

class MyStrategy(bt.Strategy):

    ...

    def next(self):

        if self.data.close > 2000 and self.data.pe < 12:
            # TORA TORA TORA --- Get off this market
            self.sell(stake=1000000, price=0.01, exectype=Order.Limit)
    ...

Plotting that extra P/E line

There is obviously no automated plot support for that extra line in the data feed.

显然,对于数据馈送中的额外行没有自动绘图支持

The best alternative would be to do a SimpleMovingAverage on that line and plot it in a separate axis:

最好的选择是在这条线上做一个简单的移动平均,然后在一个单独的轴上绘制它

import backtrader as bt
import backtrader.indicators as btind

....

class MyStrategy(bt.Strategy):

    def __init__(self):

        # The indicator autoregisters and will plot even if no obvious
        # reference is kept to it in the class
        btind.SMA(self.data.pe, period=1, subplot=False)

    ...

    def next(self):

        if self.data.close > 2000 and self.data.pe < 12:
            # TORA TORA TORA --- Get off this market
            self.sell(stake=1000000, price=0.01, exectype=Order.Limit)
    ...
原文地址:https://www.cnblogs.com/sidianok/p/13475383.html