1. 程式人生 > 實用技巧 >Backtrader中文筆記之Data Feeds

Backtrader中文筆記之Data Feeds

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:

從快速入門指南中可以清楚地看到,您可以向一個大腦例項新增資料來源。資料來源稍後將提供給不同的策略:

  • An array self.datas (insertion order)

  • 一個數組self.datas(插入順序)
  • Alias to the array objects:

  • 陣列物件的別名
    • self.data and self.data0 point to the first element

    • self.data和self.data0指向了第一個元素
    • self.dataX points to elements with index X in the array

    • self.dataX指向了array的第X索引的元素

A quick reminder as to how the insertion works:

關於插入如何工作的快速提示:

import backtrader as bt
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

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

  • compression (default: 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

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)

    用於分析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')