ta icon indicating copy to clipboard operation
ta copied to clipboard

Backtesting by slicing pandas dataframe?

Open lord-village opened this issue 4 years ago • 1 comments

Morning,

I'm hoping for a bit of advice here. I've written a trading bot to have a play effectively. I'm trying to fine tune it a bit, but I'm struggling.

Currently I'm doing this:

`historicData = self._auth_client.get_product_historic_rates(self.__symbol, granularity=self._period, start=start_iso, end=end_iso)

df = pd.DataFrame(historicData, columns=['time', 'high', 'low', 'open', 'close', 'volume']) self._history = df.reindex(index=df.index[::-1]) self._history.reset_index(inplace=True, drop=True)

adx, neg, pos = ta.trend.ADXIndicator(high=self._history['high'], low=self._history['low'], close=self._history['close'], n=9)`

That works fine while it's running, it gets the last 200 points in a rolling window ending now().

So, I wanted to back test, so I hash it a little bit and do this:

`tmp = se._history

for i in range(50, 200): se._history = tmp[:i] se._current_price = se._history['open'][-1:].values[0]`

i.e. I take my 200 points and slice it into 50, then add 1 every itteration.

The issue I seem to be having is that the ADX indicator returns NaN for every single value, so I'm never getting a buy or sell signal so my backtesting isn't working properly.

I'm not 100% familiar with pandas or this world, so if I can do something differently, happy to learn!

Many thanks,

Scott

lord-village avatar Sep 15 '20 06:09 lord-village

check this out

e271p314 avatar Sep 25 '20 06:09 e271p314