Another example is a Doji candle whatever on top or bottom on a day chart has to be confirmed by the following candle. It can be determined the direction by Squeeze or Bollinger band break out method at lower time frame, for instance 5 minute's data(one minute even better), lower the time frame, higher the profit and lower the risk. I wish you could produce such video. That will defeat most indicators even if you may have to use MACD or TSI to help narrow down the approach.
Clearly explained the relationship among the three functions. Is there a way to feed an extra(lower) time frame, say 60 minutes to backtesting day data frame, to assistant decision making by checking the top and bottom? For instance, a daily candle''s High greater than close may be the end of uptrend finish, but the top signal has to be confirmed by next daily down candle. if a 60 minute's down candle (for instance, an engulfing pattern) can indicates the top, so a sell signal can be generated on last 60 minute's candle and next 60 minute's open of candle can be used to sell. Furthermore, same principle, 15m time frame can decide the turning point for 60m candle, lower the time frame, the more concise of the turning point of top and bottom, especially 5th wave is counted
Very Very good presentation!. Unfortunately when i try to use as data a dataframe with signals and prices i have created i get the error: " ValueError: `data` must be a pandas.DataFrame with columns 'Open', 'High', 'Low', 'Close', and (optionally) 'Volume'". Anyway I can get around that? (Meaning that for example I want a strategy where the price ratio of 2 stocks is monitored and when too far away from mean (daily closings) you execute with +/-2std criterion etc. no open high low etc columns in this case...)
The chart gets exported as a html to the folder you're running it from, so you can likely embed that. Might also be an easier way to directly get the html
For plotting, I get an error from bokeh module . Problem may be with the exact module versions I am using. Is it possible for you to share the environmrnt you are using ?
Lets say i have an good working system now with good indicators. How can i use this framework for real trading? For example on interactive brokers? What do i have to change? Where do i put my buy/sell signals etc?
Backtesting.py isn't really built for live trading. I'm sure you could adapt the framework to do it for you but you may as well write it from scratch. If you like backtesting.py for backtesting then I'd recommend just building a function/class that handles live trading that takes in a Strategy object and directly pulls the buy/sell logic from there
Why do I keep getting this error ImportError: cannot import name 'Backtest' from partially initialized module 'backtesting' (most likely due to a circular import)? I have tried renaming the folders but that doesn't work either
Really appreciate this video! Much like your "Signal Indicator" section, I have already added my indicator values in a column to my DataFrame - I see I can call it in def next(self) via "self.data.". However, is there any way I can plot it as a line on the candle bar chart please, like the examples (in other vids) using SMAs? Would be really great to see my indicator values plotted on the chart
Hey Chad, as always good work and many thanks for your effort! I implement an custom ma crossover and wondering how to skip candles? E.g. when using crossover function btpy immediately enter a position after closing a previous one. But I want to open a new position e.g. two candles after the previous closed one. I tried it with barssince but without success. How would you approach? Any suggestions? Have a nice day!
You can loop over the completed trades, find out when the most recent one was and when it closed, and then open your position that way. Alternatively just keep a class variable with the amount of consecutive days that you've not held a position, and write your logic based on that. Hope this helps!
Hello Chad, thank you very much for your AMAZING!! videos. At 28:35 in your video you mention that Self.data.close can be accessed with any index such as -1, -2, -3 and so on. But when I'm trying to just print "print(self.data.Close[-3])" in the "Next" function, I get the following error "index -3 is out of bounds for axis 0 with size 2". It appears that the index works only until "-2" and beyond that it errors out. Is this the right understanding ?
You should be able to check for the length and / or handle the exception. It'll just depend on how many bars of warm up data backtesting.py originally starts with
@@ChadThackray Thanks for your reply. Yes I was able to handle the exception. Thank you again for your videos on backtesting.py. You are probably the only guy on the internet making such detailed videos on backtesting.py. Please make more videos on this !! :)
Hello Chad, Thanks for your video! In 2nd example of "Bollinger Bands using external libraries", my understanding that the self.I() function returns numpy array, hence the Indicatior functrion "return bbands.to_numpy.T[:3]" which is numpy array matched the requirement of self.I() return. In last example of Momentom Strat, the indicator function return "data.Close.s.pct_change(periods=7) * 100" which is pandas.series instead of numpy array but it worked fine too. That is the part Im not very understanding, is it must return numpy arrary result to calling self.I()function? Back to 2nd example, Can we just return "return bbands" which is pandas.dataframe to self.I() instead? Sorry for my beginner questions as I am not python expert!
Python isn't very strict about type checking like other languages. You can return a numpy array, a pandas series, or a pandas dataframe, and backtesting.py will try and handle things appropriately. Using any of them is fine, but generally I prefer to use numpy arrays as they are the most predictible
hey Chad! first of thank you for the video. I was working on a Stochastic indicator and was wondering should I have the calculation for the %K line and %D line within def init or should i have the calculation done before starting the "Class Stochastic(strategy): " or before the def init(self): ? my workflow : I imported data from the broker. Turned it into pandas data frames. created function stoch_k() and stoch_d() to calculate the %K and %D . I created a function since in init it requires a function after self.I(func,data.close, etc) so my code look something like this self.Stochastic = self.I(stoch_k(),self.data.close,12) but i got an error said series object is not callable .... should i have def Indicator first, calculate Stochastic within it and return the value? how should i go about it since i would need 2 values, %K and %D which is a moving average of %K. Thank you
I figured it out! but now i got an error said Data Index is not datetime. Assuming simple priods, but 'pd.DateTimeIndex' is advised. I assum it has something to do with how i format the data ? i got the data from metatrader 5 and the date time is in seconds, i converted it into date time already so not sure why. from what i read i may need to clarify the column for Backtest to read it.
@@silverhathacker yeah backtesting.py is a bit picky with how it consumes data. Try and take some data from yfinance to compare. Yous should look exactly the same as that. Is your date an index or a column? I believe that makes a difference.
@@ChadThackray hey Chad, I figured it out. the Date time needed to be index. then i need to specify the column to use as indicator, so i printed out the Stochastick %K and was able to use it for backtesting. thank you so much !
finally a tutorial that doesn't just run through the minimalist talib function getting started guide on the docs page! Youre awesome
You're a good teacher, really important to zoom out to give the overview before zooming in. Thank You
Best lesson on Backtesting topic I ever had. Such a consise and clear content, just amazing. Thank you so much!
but can you run bt.optimise on your params in your custom indicator function?
Yeah i believe but then your indicator function has to be in your backtesting class
Thanks mate, you teach excellent!! cheers from Argentina.
Another example is a Doji candle whatever on top or bottom on a day chart has to be confirmed by the following candle. It can be determined the direction by Squeeze or Bollinger band break out method at lower time frame, for instance 5 minute's data(one minute even better), lower the time frame, higher the profit and lower the risk. I wish you could produce such video. That will defeat most indicators even if you may have to use MACD or TSI to help narrow down the approach.
Great educational stuff. Thanks. But 1 question - why isnt your wall clock working? :)
Very instructive stuff I've been looking for. Thanks
Hi chad very informative. Let's say I was using two sma,s how do I optimize them
May you please do a video on how you clean data, like choosing reliable sources.
Clearly explained the relationship among the three functions. Is there a way to feed an extra(lower) time frame, say 60 minutes to backtesting day data frame, to assistant decision making by checking the top and bottom? For instance, a daily candle''s High greater than close may be the end of uptrend finish, but the top signal has to be confirmed by next daily down candle. if a 60 minute's down candle (for instance, an engulfing pattern) can indicates the top, so a sell signal can be generated on last 60 minute's candle and next 60 minute's open of candle can be used to sell. Furthermore, same principle, 15m time frame can decide the turning point for 60m candle, lower the time frame, the more concise of the turning point of top and bottom, especially 5th wave is counted
You can exhaustively lay out all conditions of ant decision tree using nested if else statement
Hi teacher, it's not related to article but you need change battery for your clock.
This is really useful information! thank you so much Chad!
Very Very good presentation!. Unfortunately when i try to use as data a dataframe with signals and prices i have created i get the error: " ValueError: `data` must be a pandas.DataFrame with columns 'Open', 'High', 'Low', 'Close', and (optionally) 'Volume'". Anyway I can get around that? (Meaning that for example I want a strategy where the price ratio of 2 stocks is monitored and when too far away from mean (daily closings) you execute with +/-2std criterion etc. no open high low etc columns in this case...)
Many thanks Chad, is it posible to use another series into indicator function? for example use pct_change of AMZN, to create signals con GOOG?. Best
Thanks a lot for this great information, it's very clear and understandable!!
Great video. Thanks. Just wonder if there is a direct way to embed the interactive chart from the backtestng.py result in a flask framework!
The chart gets exported as a html to the folder you're running it from, so you can likely embed that.
Might also be an easier way to directly get the html
thank for good video. I wonder if I want something like adx di+ di- in the same indicator how to do it
thanks for your time
I would love to implement a strategy based on Donchain bands but I can't. can you help me ? A thousand thanks
Nice one guvnor
For plotting, I get an error from bokeh module . Problem may be with the exact module versions I am using. Is it possible for you to share the environmrnt you are using ?
Check the repo, there's a fix for it in the issues tab
Lets say i have an good working system now with good indicators. How can i use this framework for real trading? For example on interactive brokers? What do i have to change? Where do i put my buy/sell signals etc?
Backtesting.py isn't really built for live trading. I'm sure you could adapt the framework to do it for you but you may as well write it from scratch. If you like backtesting.py for backtesting then I'd recommend just building a function/class that handles live trading that takes in a Strategy object and directly pulls the buy/sell logic from there
Hi, does this package only work with GOOG, if not could you please explain how I can use different stocks?
Yeah you'll just need to download some other data, load it in as a pandas dataframe and you're good
Why do I keep getting this error ImportError: cannot import name 'Backtest' from partially initialized module 'backtesting' (most likely due to a circular import)? I have tried renaming the folders but that doesn't work either
I think you have a file called backtest or backtesting or similar
Really appreciate this video! Much like your "Signal Indicator" section, I have already added my indicator values in a column to my DataFrame - I see I can call it in def next(self) via "self.data.". However, is there any way I can plot it as a line on the candle bar chart please, like the examples (in other vids) using SMAs? Would be really great to see my indicator values plotted on the chart
In init(self), you could define an indicator that just uses the values from your dataframe. Then it would get plotted on the chart
@@ChadThackray thanks a lot, I have managed to plot the indicator successfully
Hey Chad, as always good work and many thanks for your effort!
I implement an custom ma crossover and wondering how to skip candles? E.g. when using crossover function btpy immediately enter a position after closing a previous one. But I want to open a new position e.g. two candles after the previous closed one. I tried it with barssince but without success. How would you approach? Any suggestions?
Have a nice day!
You can loop over the completed trades, find out when the most recent one was and when it closed, and then open your position that way.
Alternatively just keep a class variable with the amount of consecutive days that you've not held a position, and write your logic based on that.
Hope this helps!
how can real apply strategy
Hello Chad, thank you very much for your AMAZING!! videos. At 28:35 in your video you mention that Self.data.close can be accessed with any index such as -1, -2, -3 and so on. But when I'm trying to just print "print(self.data.Close[-3])" in the "Next" function, I get the following error "index -3 is out of bounds for axis 0 with size 2". It appears that the index works only until "-2" and beyond that it errors out. Is this the right understanding ?
You should be able to check for the length and / or handle the exception.
It'll just depend on how many bars of warm up data backtesting.py originally starts with
@@ChadThackray Thanks for your reply. Yes I was able to handle the exception. Thank you again for your videos on backtesting.py. You are probably the only guy on the internet making such detailed videos on backtesting.py. Please make more videos on this !! :)
my ml model give us 0 and 1. 1 buy and 0 is sell. thats it. Is backtrader ok with ml/dl models ?
You can definitely test the signals. Main problem with ML is avoiding bias in backtesting
legend
Hello Chad, Thanks for your video!
In 2nd example of "Bollinger Bands using external libraries", my understanding that the self.I() function returns numpy array, hence the Indicatior functrion "return bbands.to_numpy.T[:3]" which is numpy array matched the requirement of self.I() return.
In last example of Momentom Strat, the indicator function return "data.Close.s.pct_change(periods=7) * 100" which is pandas.series instead of numpy array but it worked fine too. That is the part Im not very understanding, is it must return numpy arrary result to calling self.I()function? Back to 2nd example, Can we just return "return bbands" which is pandas.dataframe to self.I() instead?
Sorry for my beginner questions as I am not python expert!
Python isn't very strict about type checking like other languages.
You can return a numpy array, a pandas series, or a pandas dataframe, and backtesting.py will try and handle things appropriately.
Using any of them is fine, but generally I prefer to use numpy arrays as they are the most predictible
Got it, thanks for your reply😀
hey Chad! first of thank you for the video.
I was working on a Stochastic indicator and was wondering should I have the calculation for the %K line and %D line within def init or should i have the calculation done before starting the "Class Stochastic(strategy): " or before the def init(self): ?
my workflow :
I imported data from the broker.
Turned it into pandas data frames.
created function stoch_k() and stoch_d() to calculate the %K and %D .
I created a function since in init it requires a function after self.I(func,data.close, etc) so my code look something like this
self.Stochastic = self.I(stoch_k(),self.data.close,12)
but i got an error said series object is not callable
....
should i have def Indicator first, calculate Stochastic within it and return the value? how should i go about it since i would need 2 values, %K and %D which is a moving average of %K.
Thank you
I figured it out! but now i got an error said Data Index is not datetime. Assuming simple priods, but 'pd.DateTimeIndex' is advised.
I assum it has something to do with how i format the data ? i got the data from metatrader 5 and the date time is in seconds, i converted it into date time already so not sure why. from what i read i may need to clarify the column for Backtest to read it.
@@silverhathacker yeah backtesting.py is a bit picky with how it consumes data. Try and take some data from yfinance to compare. Yous should look exactly the same as that.
Is your date an index or a column? I believe that makes a difference.
@@ChadThackray hey Chad, I figured it out. the Date time needed to be index. then i need to specify the column to use as indicator, so i printed out the Stochastick %K and was able to use it for backtesting. thank you so much !
dude you are awesome
Great 👍
how would this work with regards to crypto currency trading 24/7?
GREAT VIDEO
pivot point strategy by using backtesting ..video upload..