Trouble plotting ADX.
-
I am attempting to plot the ADX indicator in my strategy. Here is a code snippet:
import backtrader as bt class adxTest(bt.Strategy): params = (('smoothed', 30), ('adx', 14)) def __init__(self): self.smoothed_moving_average = bt.indicators.SMMA( self.data.close, period = self.params.smoothed, plotname = 'smoothed 30 day moving average' ) self.average_directional_movement_index = bt.indicators.ADX( self.data.close, period = self.params.adx, movav = self.smoothed_moving_average, plotname = 'average direction movement index' )
Here is the run.py script:
import os, sys import pandas as pd import backtrader as bt from strategies.adxTest import adxTest cerebro = bt.Cerebro() os.chdir('..') datapath = f'{os.path.abspath(os.curdir)}/data/data.csv' data = pd.read_csv(datapath, index_col = 'Date', parse_dates = True) feed = bt.feeds.PandasData(dataname = data) cerebro.adddata(feed) cerebro.addstrategy(adxTest) cerebro.run() cerebro.plot()
When I run this I get this error:
AttributeError: 'Lines_LineSeries_LineSeriesStub' object has no attribute 'high'
However, if I comment out the ADX and just leave the SMMA it works and plots correctly. Clearly there is an issue here where the ADX expects a high attribute and it doesn't have one, but it gives me little indication as to where to look as the error just points to the line:
cerebro.run()
According to the indicators reference in the docs the ADX needs 2 parameters, the first being a period, which I've set to the default 14 and an SMMA which I provided as well. I have attempted to google this error but none of the previous posts pointed me in the direction of solving this issue. I've decided to post here as a result. It appears the data feed may be incomplete. The data I have is a Yahoo CSV for Bitcoin from June 2021 to June 2022. As far as I can tell this data has a complete OHLC data set.
If there is any obvious syntax errors or something else I am doing wrong please help. TIA
-
I just checked and the SMMA has the following attributes:
['IndType', 'ObsType', 'PriceClose', 'PriceDateTime', 'PriceHigh', 'PriceLow', 'PriceOpen', 'PriceOpenInteres', 'PriceVolume', 'StratType', '_OwnerCls', '__abs__', '__add__', '__bool__', '__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__div__', '__doc__', '__eq__', '__floordiv__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pow__', '__radd__', '__rdiv__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__weakref__', '_addnotification', '_clk_update', '_clock', '_getline', '_lineiterators', '_ltype', '_makeoperation', '_makeoperationown', '_mindatas', '_minperiod', '_next', '_nextforce', '_notify', '_once', '_operation', '_operation_stage1', '_operation_stage2', '_operationown', '_operationown_stage1', '_operationown_stage2', '_opstage', '_owner', '_periodrecalc', '_plotinit', '_plotlabel', '_roperation', '_stage1', '_stage2', 'addindicator', 'addminperiod', 'advance', 'alias', 'aliased', 'array', 'backwards', 'bind2line', 'bind2lines', 'bindlines', 'csv', 'data', 'data0', 'data0_0', 'data_0', 'datas', 'ddatas', 'dnames', 'extend', 'forward', 'frompackages', 'getindicators', 'getindicators_lines', 'getobservers', 'home', 'incminperiod', 'l', 'line', 'line0', 'line_0', 'linealias', 'lines', 'minbuffer', 'next', 'nextstart', 'once', 'once_via_next', 'oncestart', 'oncestart_via_nextstart', 'p', 'packages', 'params', 'plotinfo', 'plotlabel', 'plotlines', 'prenext', 'preonce', 'preonce_via_prenext', 'qbuffer', 'reset', 'rewind', 'setminperiod', 'updateminperiod']
How can i give it a 'high' attribute?