Multi-timeframe Strategies in Backtesting.py
HTML-код
- Опубликовано: 26 июн 2024
- We implement multiple timeframe strategies in backtesting.py, showing how to re-sample data to higher timeframes.
⭐ Code:
github.com/ChadThackray/multi...
00:00 - Intro
00:45 - Resample-apply overview
01:30 - Example with custom momentum indicator
08:05 - Resample_apply documentation
09:24 - Example using pandas TA
Free Backtesting.py course:
• Backtesting.py - Full ...
Thank you Chad! Your video has translated into my fully functional backtesting system. Cheers!
Thanks for your great tutorials
Genious!
Thank you for wonderful help @chad
very good chad
Awesome video - I am curious - what approach would you suggest if I were to screen for Highest time frame in range where K% of stochastic RSI is above/or just crossing 80 - I need the Time frame to be returned. I have seen some people using vectorization. I will be doing this for 4 different time frame sets.
Thank you for this tutorial, it's helpful!
Is there a way to check for a signal on the daily, but then use 1-min, 5-min, whatever for the actual entry? Both would be based on price action, not an indicator
That's what he explains here when he says you should start from a lower timeframe and resample to a larger one. If you wanna validate a signal on the daily timeframe and take the position in a let's say 5m timeframe, you should feed the 5m dataframe, resample to 1H, set the signals there and then evaluate those signals and take the trade based on that evaluation, but in the 5m timeframe.
I think the tricky part here is to set the signal, since it's way more difficult to spot them based on price action rather than in an indicator.
Hi @chad. I’m having a problem using resampling_apply with the Adx, as I have to pass the high, low, close and the time period from ta lib. It works fine when using I() but when using resampling_i think because there are more than one series, it’s not liking it. Any ideas of the statement I’d use please?
same here
Hi @iiii05, I think my reply is little late as I have seen the video now and your reply,
for the problem you mentioned,
If you carefully look the def init() its just that you are creating new set of data(defining your own conditions like 10 mins data or 1 hour data from 1 min data candle sticks), as the I () function takes care of the each sample output converting the type of the sample to numpy, but that is not the case in the resample_I() ass there is no self conditions defined in it
There are two ways but 2nd one is more efficient what I think,
1. Check the output condition of I() it is numpy type as you are using pandas it will be pandas.series and in resample_I() is not defined you have to provide the numpy data.
2. as you are using pandas_ta lib you can use the resample sizing function outside the backtesting class and add the new column to the data frame and now you can normally use the I() and test your startegy
Note:(I like the second method)
If you have found some efficient solution please so teach me , Thankyou
Thanks very much, solved a long lasting problem. I am having another problem. I have backtested a strategy in which I haven't provided the size of strategy to keep every signal using 100% of cash.
But when I am checking the trades, there are lot of very small unwarranted trades of around 1$ value. How can I exclude these trades
Hey bro, you can think practically this is not live trading its showing you the possible conditions that could have taken place by t he conditions you mentioned too enter the trade, so its the refining time you need after what you got the analysis,
If you want to some targeted money analysis, you can use the backtest.optimisation and plot the heat map you will have the better view of your trades performance.
if my explanation differs your question, Please do teach me Thankyou.
Hi Chad, Thanks for your amazing videos.
I have a 1Min timeframe but I'm try to get the ATR for 5Min:
self.atr5m = resample_apply('5Min', ta.atr, self.data.High.s, self.data.Low, self.data.Close, length=14)
but I'm getting this:
-> 129 raise RuntimeError(f'Indicator "{name}" errored with exception: {e}')
131 if isinstance(value, pd.DataFrame):
132 value = value.values.T
RuntimeError: Indicator "atr(H[5Min],L,C,14)" errored with exception: input array lengths are different
Hey bro, you are getting this error because the backtest only work with the datatype numpy series not panda series, in resample_apply() there is no conversion for this as such present in the I(), so you have to do it manually.
anyone having the TypeError(
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'
error when using the resampler despite having formatted the index of the data to DateTimeIndex?
yes, I saw this when resample_apply("H"... was used, resample_apply('1H'... fixed the issue, not sure if this helps but I saw this error when I made the typo.
Same here, i wonder if you already fixed it
same here too
How to know how many time the price crossed a certain level of price ?
You have all the previous price history in self.data, so you can just write a function that scans through there for a specific price level
Could you connect this video with custom indicator video?
Yeah you can do both multi-timeframe and use your own custom indicators
@@ChadThackray How is the ao indicator MTF code statement? I would like to see it at the end of the video.
I did not understand why it did not calculate the following. There is no error, but the values are filled with nan. I see only +1 and -1 (decision column)
self.super1_w = resample_apply(
'W-FRI',ta.supertrend, self.data.High.s, self.data.Low.s,
self.data.Close.s, length=10, multiplier=1.0, plot=True)
Actually I only want the trade to be executed if there's no active trade in place already. Also my strategy can go both long and short.
I believe you can do something like `if not self.position` somewhere within the next() function and then anything inside that if statement will only be execute if you don't have a position