Closed windowshopr closed 3 years ago
You need (first error) the .df
accessor:
self.SuperTrend = Indicators_Class.SUPERTREND(self.data.df, 7, 3)
Additionally (second error), you need to wrap the indicator computation in Strategy.I()
, i.e.:
self.SuperTrend = self.I(Indicators_Class.SUPERTREND, self.data.df, 7, 3)
Thank you! I knew it was going to be something easy, I just wasn't seeing it. I used the second recommendation you gave and it worked just fine. Thanks a lot!
Although, now that I say that, I should clarify another error I'm getting. I'm creating the SUPERTREND function in a way that the backtesting library will then plot the line on the chart for me, which it is not doing. So if you add bt.plot()
to the very last line, and make sure that self.SuperTrend = self.I(Indicators_Class.SUPERTREND, self.data.df, 7, 3)
is used, I receive this error at the plotting stage:
Traceback (most recent call last):
File "test.py", line 171, in <module>
bt.plot()
File "C:\Users\...\AppData\Roaming\Python\Python37\site-packages\backtesting\backtesting.py", line 1721, in plot
open_browser=open_browser)
File "C:\Users\chalu\AppData\Roaming\Python\Python37\site-packages\backtesting\_plotting.py", line 591, in plot
_plot_superimposed_ohlc()
File "C:\Users\...\AppData\Roaming\Python\Python37\site-packages\backtesting\_plotting.py", line 445, in _plot_superimposed_ohlc
raise ValueError('Invalid value for `superimpose`: Upsampling not supported.')
ValueError: Invalid value for `superimpose`: Upsampling not supported.
Any idea?
Yeah, this is the project issue tracker. It's polite to everyone subscribed to first use the search. See https://github.com/kernc/backtesting.py/issues/233.
My apologies, thank you for pointing me in the right direction, I think I see the timestamp issue and will fix on my end. Great support! Thanks! :)
UPDATE
That was exactly the issue, I am downloading datasets from Yahoo Finance and the format that the timestamps come in doesn't jive with backtesting, so to finish this off, to fix the plotting issue, I just did this right after importing the .csv file:
# Convert the individual stock's Datetime column to datetime objects
df['Datetime'] = pd.to_datetime(df['Datetime'])
df['Datetime'] = df['Datetime'].dt.strftime("%d-%m-%y %H:%M")
df['Datetime'] = pd.to_datetime(df['Datetime'])
This fixed the format and plotted nicely. Thanks again.
Expected Behavior
I'm looking to just add on to the indicators library, trying to copy the format used for the SMA. Currently, I'm trying to create the SuperTrend indicator, which can be generated using the Pandas TA library, however I'm running into some errors which I can't quite figure out what's happening. It has to do with the
next()
function of backtesting somehow (I think).I've attached a full working code (with comments) for anyone to recreate my issue. Here is a list of dependencies needed to run it:
Running this on Python 3.7.9 and Windows 10.
As you'll see, I define a custom
Indicators_Class
in the middle with some functions to return both the default SMA as a boilerplate, and the SUPERTREND, which I'm trying to get working. The top section just downloads AAPL's dataset from Yahoo Finance and saves it to a .csv file, which can be glanced over as the issue lies with either theIndicators_Class
or the backtesting section near the bottom.Steps to Reproduce
So, using this full code to recreate the issue (sorry about the length, but it is a full working example that downloads a dataset, etc.):
Actual Behavior
...I receive this traceback:
Which I thought had something to do with how I was passing in the data to the
SUPERTREND()
function, so I changed line 138:from
self.SuperTrend = Indicators_Class.SUPERTREND(self.data, 7, 3)
to
self.SuperTrend = Indicators_Class.SUPERTREND(df, 7, 3)
...after doing so, I get the following traceback:
...which I think I'm receiving because the length of
self.data
at that point during the backtest is only 2, and the length ofdf
that I passed into theSUPERTREND()
function is 506.Additional info
So, I'm trying to get this SUPERTREND indicator working by following the boilerplate SMA template, however I'm running into these two issues. I think it has to do with converting to
pd.Series()
in some capacity or another, but I'm stuck, would like to figure it out so I can keep adding to that indicator library for plotting purposes. Thanks!