添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

backtrader comes with a set of Data Feed parsers (at the time of writing all CSV Based) to let you load data from different sources.

backtrader附带了一组数据源解析器(在编写所有基于CSV的代码时),允许您从不同的源加载数据

Yahoo (online or already saved to a file)

VisualChart (see www.visualchart.com

Backtrader CSV (own cooked format for testing)

Generic CSV support

From the Quickstart guide it should be clear that you add data feeds to a Cerebro instance. The data feeds will later be available to the different strategies in:

从快速入门指南中可以清楚地看到,您可以向一个 Cerebro 实例添加数据源。数据源稍后将提供给不同的策略:

An array self.datas (insertion order)

  • 一个数组self.datas(插入顺序)
  • Alias to the array objects:

  • 数组对象的别名
  • import backtrader.feeds as btfeeds data = btfeeds.YahooFinanceCSVData(dataname='wheremydatacsvis.csv') cerebro = bt.Cerebro() cerebro.adddata(data) # a 'name' parameter can be passed for plotting purposes

    Data Feeds Common parameters

    Data Feeds常见参数

    This data feed can download data directly from Yahoo and feed into the system.

    这个数据源可以直接从Yahoo下载数据并输入到系统中

    Parameters:

    dataname (default: None) MUST BE PROVIDED(必须提供)

    The meaning varies with the data feed type (file location, ticker, …)

    其含义随数据馈送类型(文件位置、ticker等)而变化

    name (default: ‘’)

    Meant for decorative purposes in plotting. If not specified it may be derived from dataname (example: last part of a file path)

  • 在绘图的时候用于装饰用。如果没有指定,它可能来至与dataname(例如:文件路径的最后一部分)
  • fromdate (default: mindate)

    Python datetime object indicating that any datetime prior to this should be ignored

  • Python日期时间对象,该对象指示在此之前的任何日期时间都应被忽略
  • todate (default: maxdate)

    Python datetime object indicating that any datetime posterior to this should be ignored

  • Python日期时间对象,该对象指示在此之后的任何日期时间都应被忽略
  • timeframe (default: TimeFrame.Days)

  • 时间框架(默认:用天的时间框架)
  • Potential values: Ticks , Seconds , Minutes , Days , Weeks , Months and Years

  • 可选的值: Ticks , Seconds , Minutes , Days , Weeks , Months and Years
  • compression (default: 1)

  • 压缩(默认:1)
  • Number of actual bars per bar. Informative. Only effective in Data Resampling/Replaying.

  • 每个bar的实际数量,仅对数据重采样/重放有效。
  • sessionstart (default: None)

    Indication of session starting time for the data. May be used by classes for purposes like resampling

  • 指示数据的会话开始时间。可能被类用于重采样等目的
  • sessionend (default: None)

    Indication of session ending time for the data. May be used by classes for purposes like resampling

  • 指示数据的会话结束时间。可能被类用于重采样等目的
  • CSV Data Feeds Common parameters

    CSV 数据源普通参数

    Parameters (additional to the common ones):

    额外的参数

    headers (default: True)

    Indicates if the passed data has an initial headers row

  • 表示传递的数据是否具有初始标题行
  • separator (default: “,”)

    Separator to take into account to tokenize each of the CSV rows

  • 分隔符,用于标记每个CSV行
  • GenericCSVData

    一般CSVData

    This class exposes a generic interface allowing parsing mostly every CSV file format out there.

    Parses a CSV file according to the order and field presence defined by the parameters

    Specific parameters (or specific meaning):

    这个类公开了一个通用接口,允许解析大部分CSV文件格式。

    解析CSV文件根据参数的顺序以及字段的定义

    dataname

    The filename to parse or a file-like object

  • 分析文件名或者文件对象
  • datetime (default: 0) column containing the date (or datetime) field

  • 列包含date或datetime的字段
  • time (default: -1) column containing the time field if separate from the datetime field (-1 indicates it’s not present)

    时间(默认值:-1)如果与日期时间字段分开,则包含时间字段的列(-1表示不存在)

    open (default: 1) , high (default: 2), low (default: 3), close (default: 4), volume (default: 5), openinterest (default: 6)

    Index of the columns containing the corresponding fields

  • 列的索引包含不同的领域
  • If a negative value is passed (example: -1) it indicates the field is not present in the CSV data

  • 如果传递负值(例如:-1),则表示CSV数据中不存在该字段
  • nullvalue (default: float(‘NaN’))

    Value that will be used if a value which should be there is missing (the CSV field is empty)

  • 如果缺少应存在的值(CSV字段为空)时将使用的值
  • dtformat (default: %Y-%m-%d %H:%M:%S)

    Format used to parse the datetime CSV field

    用于分析datetime CSV字段的格式

    tmformat (default: %H:%M:%S)

    Format used to parse the time CSV field if “present” (the default for the “time” CSV field is not to be present)

  • 用于分析“present”时的timeCSV字段的格式(“time”CSV字段的默认值不存在)
  • An example usage covering the following requirements:

    包含以下要求的示例用法:

    Limit input to year 2000

  • 输入限制在2000年以后
  • HLOC order rather than OHLC

  • 输入数据为hight,low,open,close
  • Missing values to be replaced with zero (0.0)

  • 缺省的值为0.0
  • Daily bars are provided and datetime is just the day with format YYYY-MM-DD

  • 时间格式化为YYYY-MM-DD
  • No openinterest column is present

  • 当前没有 openinterest 的列
  • import datetime
    import backtrader as bt
    import backtrader.feeds as btfeeds
    data = btfeeds.GenericCSVData(
        dataname='mydata.csv',
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2000, 12, 31),
        nullvalue=0.0,
        dtformat=('%Y-%m-%d'),
        datetime=0,
        high=1,
        low=2,
        open=3,
        close=4,
        volume=5,
        openinterest=-1
    

    Slightly modified requirements:

    略微改进的要求

    Limit input to year 2000

    HLOC order rather than OHLC

    Missing values to be replaced with zero (0.0)

    Intraday bars are provided, with separate date and time columns

  • 提供日内的bars,分成日期与时间两个柱
  • Date has format YYYY-MM-DD
  • Time has format HH.MM.SS (instead of the usual HH:MM:SS)
  • No openinterest column is present

    import datetime
    import backtrader as bt
    import backtrader.feeds as btfeed
    data = btfeeds.GenericCSVData(
        dataname='mydata.csv',
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2000, 12, 31),
        nullvalue=0.0,
        dtformat=('%Y-%m-%d'),
        tmformat=('%H.%M.%S'),
        datetime=0,
        time=1,
        high=2,
        low=3,
        open=4,
        close=5,
        volume=6,
        openinterest=-1
    

     This can also be made permanent with subclassing:

     也可以通过子类化使其永久化
    import datetime
    import backtrader.feeds as btfeed
    class MyHLOC(btfreeds.GenericCSVData):
      params = (
        ('fromdate', datetime.datetime(2000, 1, 1)),
        ('todate', datetime.datetime(2000, 12, 31)),
        ('nullvalue', 0.0),
        ('dtformat', ('%Y-%m-%d')),
        ('tmformat', ('%H.%M.%S')),
        ('datetime', 0),
        ('time', 1),
        ('high', 2),
        ('low', 3),
        ('open', 4),
        ('close', 5),
        ('volume', 6),
        ('openinterest', -1)
    

     This new class can be reused now by just providing the dataname:

    这个新的类可以让我们只提供dataname就可以重用

    data = btfeeds.MyHLOC(dataname='mydata.csv')
    

    此章主要介绍GenericCSVData的读取方式配置,通过实例的方式写入配置,也可以通过继承的方式,设置params来写入配置。