728x90
Data Feeds
더보기
- Yahoo (online or already saved to a file)
- VisualChart (see www.visualchart.com
- Backtrader CSV (own cooked format for testing)
- Generic CSV support
- Data Feed 는 기존 주식 데이터인 OHLCV 데이터에 datetime 칼럼을 넣어 작동하는 기능입니다.
Quick Start
더보기
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
GenericCSVData
더보기
Paremeter
- dataname
- datetime (default: 0)
- time (default: -1) - datetime 과 또다른 time 존재시 표시 (-1 = time 존재 x)
- open (default: 1) , high (default: 2), low (default: 3), close (default: 4), volume (default: 5), openinterest (default: 6)
- nullvalue (default: float(‘NaN’)) - null value 특정할시 필요
- dtformat (default: %Y-%m-%d %H:%M:%S) - datetime format 할때 필요
- tmformat (default: %H:%M:%S) - time format 할때 필요
Pandas Data
더보기
Parameter
- datetime (default: None)
- None : datetime is the “index” in the Pandas Dataframe
- -1 : autodetect position or case-wise equal name
- 0 : numeric index to the colum in the pandas dataframe
- string : column name (as index) in the pandas dataframe
- open, high, low, high, close, volume, openinterest (default: -1 for all of them)
- -1 : autodetect position or case-wise equal name
- 0 : numeric index to the colum in the pandas dataframe
- string : column name (as index) in the pandas dataframe
-
data = bt.feeds.PandasData(dataname=dataframe)
Data Resampling
더보기
Paremeter
- timeframe (default: bt.TimeFrame.Days)
- compression (default: 1) - 1 bar 에 표시할 time
The steps
- Load a data
- Resample it according to the user specified arguments
- The script also allows for loading a 2nd data
- Add the data to cerebro
- Add the resampled data (larger timeframe) to cerebro
- run
Example
# Load the Data
datapath = args.dataname or '../../datas/2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=datapath)
cerebro.adddata(data) # First add the original data - smaller timeframe
tframes = dict(daily=bt.TimeFrame.Days, weekly=bt.TimeFrame.Weeks,
monthly=bt.TimeFrame.Months)
# Handy dictionary for the argument timeframe conversion
# Resample the data
if args.noresample:
datapath = args.dataname2 or '../../datas/2006-week-001.txt'
data2 = btfeeds.BacktraderCSVData(dataname=datapath)
# And then the large timeframe
cerebro.adddata(data2)
else:
cerebro.resampledata(data, timeframe=tframes[args.timeframe],
compression=args.compression)
# Run over everything
cerebro.run()
Data - Replay
더보기
Parameter
- timeframe (default: bt.TimeFrame.Days)
- compression (default: 1)
- bar2edge (default: True) - time boundaries as the target of the closed bar.
- adjbartime (default: False) - 마지막 시간대 상관없이 bar 조절
- rightedge (default: True) - 시간대에 맞춤
This is where Data Replay comes in to help
- The strategy operates on data with a timeframe X (example: daily)
- Data for a smaller timeframe Y (example: 1 minute) is available
- Replay a daily bar using the 1 minute data
The steps
- Load a data feed
- Pass the data to cerebro with replaydata
- Add a strategy
Example
# Load the Data
datapath = args.dataname or '../../datas/2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=datapath)
# Handy dictionary for the argument timeframe conversion
tframes = dict(
daily=bt.TimeFrame.Days,
weekly=bt.TimeFrame.Weeks,
monthly=bt.TimeFrame.Months)
# First add the original data - smaller timeframe
cerebro.replaydata(data,
timeframe=tframes[args.timeframe],
compression=args.compression)
Rolling over Futures
더보기
Start date 와 expiration dates 가 계속해서 이어질수 있는 데이터들을 합칠수 있다
Parameter
- checkdate (default: None) - 데이터들의 병합이 잘 되었는지 체크 (True or false)
- checkcondition (default: None) - data0 <-> data1 문제 체크
Example
import backtrader as bt
cerebro = bt.Cerebro()
data0 = bt.feeds.MyFeed(dataname='Expiry0')
data1 = bt.feeds.MyFeed(dataname='Expiry1')
...
dataN = bt.feeds.MyFeed(dataname='ExpiryN')
drollover = cerebro.rolloverdata(data0, data1, ..., dataN, name='MyRoll', **kwargs)
cerebro.run()
Reference :
더보기
https://www.backtrader.com/docu/datafeed/
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. Yahoo (online or already saved to a file) VisualChart (see www.visualchart.com Backtrader CSV (own cooked format
www.backtrader.com
'금융공학 > BackTrader 공부' 카테고리의 다른 글
BackTrader 5 : Indicators (0) | 2021.12.29 |
---|---|
BackTrader 1 : Concepts (0) | 2021.12.24 |
BackTrader 4 : Strategy (0) | 2021.12.23 |
BackTrader 2 : Cerebro (0) | 2021.12.19 |
BackTrader 0 : Introduce (0) | 2021.12.19 |