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:
- 數組對象的別名
-
-
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
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
andYears
- 可選的值:
Ticks
,Seconds
,Minutes
,Days
,Weeks
,Months
andYears
-
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來寫入配置。