ATR issue
-
I'm doing a simple strategy plot for SPY with ATR indicator.
The ATR values for recent dates seems reasonable. However, as we go back in time the values increase to unreasonable values:
Checking this in other stock charting software make it clear that something is wrong with the ATR values.
Any suggestions? -
Something definitely goes wrong.
If you share more details like code, log, etc more suggestions can be made.
-
Even to the untrained eye it's clear that something must be wrong in the code. The
Buy/Sell
signals are miles away from what's the (supposed) target: theSPY
-
I've created a simple test to demonstrate the problem:
from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime # For datetime objects import os.path # To manage paths import sys # To find out the script name (in argv[0]) # Import the backtrader platform import backtrader as bt import backtrader.indicators as btind # Create a Stratey class NoStrategy(bt.Strategy): def __init__(self): bt.indicators.ATR(self.datas[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(NoStrategy) # Datas are in a subfolder of the samples. Need to find where the script is # because it could have been called from anywhere #modpath = os.path.dirname(os.path.abspath(sys.argv[0])) #datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt') # Create a Data Feed data1 = bt.feeds.YahooFinanceData( dataname="SPY", # Do not pass values before this date fromdate=datetime.datetime(2000, 1, 1), # Do not pass values before this date todate=datetime.datetime(2017, 10, 31), # Do not pass values after this date reverse=True) # Add the Data Feed to Cerebro cerebro.adddata(data1) # Run over everything cerebro.run() # Plot the result cerebro.plot()
The results for the ATR are pretty much the same (attached). What could be wrong?
-
If you are so confident that the results are wrong, why not just calculate it by hand for some wrong values and check? You can't be sure if they are wrong if you won't do it.
-
I've created a simple test to demonstrate the problem:
May it be that you are actually showing a different problem?
data1 = bt.feeds.YahooFinanceData(
The
Yahoo
API is known to be broken. This isn't new, it has been broken for months (first with the demise of the original API and then with the introduction of random data/non-valid data in the 2nd API) and this could be the source of the uncertainties of the ATR you see at the beginning.And it is also the reason for the incredible chart you posted originally. The close prices are just a single dot in the chart FAR AWAY from the
high
/low
components which are also part of theATR
calculation. That's why yourbuy
/sell
markers seem so strangely placed.Pass clean data (from CSV from example) to the same script and share the results. See this run from the
test_ind_atr.py
which is included in the sources of backtraderConclusion: get data which is known to be good data.
-
You are right!
When switching to a verified good data:from __future__ import (absolute_import, division, print_function, unicode_literals) import datetime # For datetime objects import os.path # To manage paths import sys # To find out the script name (in argv[0]) # Import the backtrader platform import backtrader as bt import backtrader.indicators as btind import backtrader.feeds as btfeeds # Create a Stratey class NoStrategy(bt.Strategy): def __init__(self): btind.ATR(self.datas[0]) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.addstrategy(NoStrategy) # Datas are in a subfolder of the samples. Need to find where the script is # because it could have been called from anywhere #modpath = os.path.dirname(os.path.abspath(sys.argv[0])) #datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt') # Create a Data Feed # data1 = bt.feeds.YahooFinanceData( # dataname="SPY", # # Do not pass values before this date # fromdate=datetime.datetime(2000, 1, 1), # # Do not pass values before this date # todate=datetime.datetime(2017, 10, 31), # # Do not pass values after this date # reverse=True) # Create a Data Feed data1 = btfeeds.GenericCSVData(dataname = '../data/spy-2000-2017.txt', fromdate = datetime.datetime(2000, 1, 1), todate = datetime.datetime(2017, 10, 31), dtformat=('%m/%d/%Y'), nullvalue = 0.0, datetime = 0, high = 3, low = 4, open = 2, close = 5, volume = 6, openinterest = 7) # Add the Data Feed to Cerebro cerebro.adddata(data1) # Run over everything cerebro.run() # Plot the result cerebro.plot()
ATR seems to be fine!