I'm hoping I might get some help here despite btoandav20
not being a part of backtrader core.
I have developed a custom Indicator
to detect swing highs and this is working well with GenericCSVData
downloaded from the Oanda v20 API. I can access values stored in the Indicator
lines
in my strategy like so:
def next(self):
print(f'len(self) is {len(self)}, prior high is {self.swing_highs.prior_high[0]}')
This works & prints out the current bar and the current value of the prior_high line
e.g.
...
len(self) is 5024, prior high is 1.18408
len(self) is 5025, prior high is 1.18408
len(self) is 5026, prior high is 1.18408
len(self) is 5027, prior high is 1.18704
len(self) is 5028, prior high is 1.18704
len(self) is 5029, prior high is 1.18704
len(self) is 5030, prior high is 1.18704
len(self) is 5031, prior high is 1.18704
len(self) is 5032, prior high is 1.18704
len(self) is 5033, prior high is 1.18704
len(self) is 5034, prior high is 1.18808
len(self) is 5035, prior high is 1.18808
len(self) is 5036, prior high is 1.18808
...
I am now attempting to update my backtest to use btoandav20
for data instead of downloading it from the API & saving as a csv file. However by doing so, I have found that I am unable to access the values stored in my Indicator
lines
like before, now all that is printed is nan
:
...
len(self) is 5024, prior high is nan
len(self) is 5025, prior high is nan
len(self) is 5026, prior high is nan
len(self) is 5027, prior high is nan
len(self) is 5028, prior high is nan
len(self) is 5029, prior high is nan
len(self) is 5030, prior high is nan
len(self) is 5031, prior high is nan
len(self) is 5032, prior high is nan
len(self) is 5033, prior high is nan
len(self) is 5034, prior high is nan
len(self) is 5035, prior high is nan
len(self) is 5036, prior high is nan
...
See below for the full backtest script, note that nothing has changed in the Strategy
or Indicator
, I am simply switching between what data feed is loaded by adding either data
or data0
:
import datetime as dt
import backtrader as bt
import backtrader.feeds as btfeeds
from GenericCSVDataForex import GenericCSVDataForex
import btoandav20
import exampleauth as auth
from test_strategy import TestStrategy
StoreCls = btoandav20.stores.OandaV20Store
DataCls = btoandav20.feeds.OandaV20Data
account_id, token = auth.example_auth()
data = GenericCSVDataForex(
dataname = 'EUR_USD_H1_2020-01-01.csv',
timeframe=bt.TimeFrame.Minutes,
compression=60,
fromdate=dt.datetime(2010, 1, 1),
todate=dt.datetime.today(),
dtformat=('%Y-%m-%dT%H:%M:%S.000000000Z'),
datetime=1,
open=2,
high=3,
low=4,
close=5,
volume=6,
cc=7,
openinterest=-1
)
if __name__ == '__main__':
cerebro = bt.Cerebro(stdstats=True)
cerebro.broker.set_cash(2000)
storekwargs = dict(
token=token,
account=account_id,
practice=True)
store = StoreCls(**storekwargs)
DataFactory = store.getdata
datakwargs = dict(
dataname='EUR_USD',
timeframe=bt.TimeFrame.Minutes, compression=60,
qcheck=0.5,
historical=True,
fromdate=dt.datetime(2020, 1, 1),
todate=dt.datetime.today(),
bidask=False,
useask=False,
backfill_start=True,
backfill=True,
tz='Europe/London'
)
data0 = DataFactory(**datakwargs)
cerebro.adddata(data0)
cerebro.addstrategy(TestStrategy)
cerebro.run(exactbars=False, tradehistory=True)
cerebro.plot(style='candlestick')
The plotting works and looks exactly the same for both data feeds, so I can see that the indicator values are still being calculated correctly, I just can't seem to access them in the Strategy
when using the btoandav20
data feed.
Any help would be greatly appreciated!