Exceptional tutorial! I spent the past couple days researching how to calculate proper cumulative returns based on your short,long, hold signal strategy and luckily the YT algo brought your video to my home screen. Keep up the great work!
Glad it helped. Technically if you assume you update your models at the end of the day you could skip the row shift, but then you have to deal with price slip from close to open the next day.
Great and Awesome video. Just created my first momentum strategy with python; and I look forward to creating more strategies using python. Very useful skill for the FinTech industry.
Thank you for this great tutorial! Liked and subscribed. I'm trying to learn python but trading tutorials that are any good are difficult to find, so your tutorials are very much appreciated.
HI. That functionality will be implemented by your trading platform. You want to find one that has an API that will allow you to integrate custom algorithms. It's not necessary to use Python though. Thinkorswim used to have a custom scripting language that let you write and run alogos but I am not sure if it still does.
Hi, you explained very well, could you please make a video on price action kind of trading, not using lagging indicators, then it would be great to see your coding, and is it possible to do a multitime frame price action trading, if so please give the code.
Wow, this video was a game-changer for me! It made creating my own trading bot a breeze. Highly recommend it! Also, check out "Algorithmic Trading Tutorial Python | Build Trading Algorithm from Scratch" by QuantInsti - another awesome video that helped me understand the fundamentals of algorithmic trading and build my own algorithm. Super helpful and definitely worth watching! Big thanks to both videos for giving me the knowledge and confidence to create my trading bot. Can't wait to see what it can do! 😄🚀
Nice video, thanks. A quick question - Why would you use numpy log to calculate returns? I reckon there is a more accurate pct_change() function that can be used directly. Also, numpy log also needs a diff.. Also, could you explain how taking natural log (numpy log) and diff together end up coming close to the pct_change function result, which seems more intuitive for returns calculation? Update: Okay, it turned out that I didn't know about log returns till now. I went and did my homework :) But should we use log returns here when investment and returns are actually for discrete periods (1,2,3...n days)?
Glad it helped. You can use pandas pct_change function, but then you need to make other adjustments, so we typically use "instantaneous" rate of return. log, or LN change. This is used to make it easier to compare to other types of securities that compound interest continuously. It is also used to estimate volatility and in valuing options.
Great video, thank you a lot !!! I have a question for you Math if you don't mind of course about the trading algorithm in cryptocurrencies, my algorithm works pretty well using technical analysis RSI and CCI but I want to improve it using machine learning for merging technical analysis and fundamental analysis what can I do here? what kind of machine learning models or algorithms do I have to look at? my second question: I took a look at the holt winter method forecasting but I think this method will not work cause of the randomness of cryptocurrencies to change over time, is it true what I just said ? or this is a method for extracting trends and seasonality in any kind of time series
Lots of people do use and try to use ML for trading algorithms. Securities are notoriously difficult to forecast (or else we would all be rich). For sure traditional time series forecasting models are pretty much useless unless you are using the model as a trading signal rather than as a forecasting tool. I think a good place to start is with Python for Finance, Hilpisch.
thank you for the video . i think the look ahead is not a problem here if you take a decision at the end of the day . right ? I am creating a complex AI model and i am generating features depending on SMA and EMA . It would be important for me to know what is the best practice in this regard . i am taking decision at the end of each 1 hour candle .
Yes if you can trade at the end of the day you wouldn't worry about look ahead. In practice, I guess you could assume a buy on close order, however since your model can't be updated until at least the exact close, it seems a little difficult to implement.
@@MattMacarty thanks , i am taking a decision two minutes before the closing of each hour in cryptos market . I guess i shall not worry about the look ahead .
Im a bit confused on why the look ahead was profitable but the shifting forward 1 day to catch the close was not profitable. Is shifting forward different then a cross over of the 10 and 22 for this example?
Sure you can try that. I think what we are basically seeing is that the entry and exit are critical in this strategy. Remember it's just for demonstration purposes
So the data we are using implies that on the current day you know the closing price. This would not be possible until after the market has closed. I have seen it handled both ways, but you really can't get a signal until after the market closes and so it's unlikely that you would be able to act on any signal until the following day.
Sir, would like to ask. I thought if you're going to use the jupyter notebook, it doesn't need any internet and yet you can still use it . does using any API requires internet? complete newbie, still on learning process Sir.
@@MattMacarty ahh i see with machine learning used or neural networks at all? also what is the algorithm that is used for this? (sorry for so many questions)
Hi Matt, I am learning Python programming right now. Just started so to me everything is completely new. I would like to ask you, with all the knowledge you have, have you been able to trade using your own algorithm with Python? If so what were the results? I amorally motivated to learn this in order to be able to trade. I think is more precise then guessing using Candlestick reading. Nice content by the way, Thanks bro.
Thanks for your comment. Sadly, no I don't have an algorithm to share with you. There are a couple of websites out there that have communities sharing algorithms though. Sites like Quantopian were great for people like you. I don't have one to recommend, but do a little research and I am sure you will find what you are looking for.
i can't import yfinance library in my jupyter notebook i always get error messages .....the command meant to see read panda data set to seem not to exist in my jupyter notebook modules thanks for the comrehensive videos anyways
the short return is incorrect? if S_0 = 10 and S_1 = 11, based on your formula (LN(11) - LN(10))*(short signal -1) = -0.09531018, exp(-0.09531018) = 0.909090. whereas being short at 10 to 11, implies loss of (11 - 10)*(short signal -1) = -1, 9 dollars left implies 9 / 10 = 0.9 cumulative returns
The return is based on the price movement for the day. If you are short and the price goes down, ignoring everything else, your position value increases by the amount the price decreased.
So the blue line shows what would happen if you bought in 2016 and just held gold without any adjustments. The orange line is what would have happened if you tried to catch the swings in prices based on a simple moving average.
Hello Matt, thanks for the video. I have a question, Why would you substract the returns to the system returns when your are not holding the asset, I mean when signal = -1 it reduces the system returns, but if you sold your position , the future price movement doesn't affect. Are you considered instead of multiply by -1 do it by 0?..
Hi so the model assumes you are always invested, either long or short. The -1 indicates a short position. But yes if you wanted to go flat you could replace -1 with 0.
Hi, try not to use price, lagging indicators, instead use price action strategy, will give accurate results, can you do this, many viewers are fed-up seeing these price lagging indicators usage in different channels of youtube.
This is for demonstrating how to write a simple back tester, not really interested in a strategy. Any strategy worth anything will not be found on RUclips.
@@MattMacartyI actually have one. But unfortunately I don’t know how to code which could’ve helped me. Since I go to school and cant keep up to the market.
How can we get the Min and Max price dates from the 21 day's of previous High and low and to get the monthly high and low price and date and the difference between them in days and time/days ?
I think probably the easiest way will be to start adding columns that track what you are looking for. If you just want to watch a max/min over a 21 day period, you can make a column something like df['21-max'].rolling(21).max(), etc.
I have a problem in the Plot trades on time series. I can plot out all the things except the markers, it shows "ValueError: iLocation based boolean indexing cannot use an indexable as a mask" Please help
i used a different set of data from 2021-02 to 2022-02 and upon comparing buy/hold with system, system outperforms buy/hold. what is your take on this?
I am sure at times a system can outperform. The question is will the same conditions prevail in the future? And then how do you know that conditions are no longer favorable for the system? Obviously people do make money with algorithmic trading. My examples are meant to show you how construct fairly simple strategies to give you an idea of "how to".
hi, what is the idea behind plotting the mark on 9-day when entry is 2 and plotting the mark on 21-day when entry is -2? are they just for display meaning it should mean no harm if i were to plot the other day around? thank you
at the same time, what is the role of np.exp() here? i understand since the log return represent somewhat similar to pct_change, we should do cumprod() but i don't understand what np.exp is for, knowing that it is e**x for x to be the value of that index
You can arrive at the same numbers using either pct change or exp. exp is also known as instanaeous rate of return and computationally it's somewhat easier to work with than pct change. Instanteaous rate of return is used as the standard particularly when dealing with derivatives.
I would test to see how it does, and compare to various strategies. The video was meant to show people how to test strategies, not how to trade profitably
It should. Try pip install --upgrade pandas-datareader from the command line, then try running your code again. If that doesn't work try pip install yfinance. This will require you to modify the code to work with yfinance (yfinance.download)
You can start with something like file = open([filename], 'rb'), then probably file.readlines(). And if each data point in a line is separated by a space or comma or anything you can you can split on the character.
Hai..i have problem ..when i run my code there are some error like this NameError : name 'pdr' is not defined I already follow the instruction and also update to the anaconda prompt....but still can't.please help.
Hi you can download the notebook from the link in the description. This is error means pdr has not been defined anywhere in your code. pdr is the alias I gave pandas_datareader in the first notebook cell. Maybe you have a typo?
Exceptional tutorial! I spent the past couple days researching how to calculate proper cumulative returns based on your short,long, hold signal strategy and luckily the YT algo brought your video to my home screen. Keep up the great work!
Thank you. Glad it helped
Great vid, especially the reminder about shifting rows. Thanks!
Glad it helped. Technically if you assume you update your models at the end of the day you could skip the row shift, but then you have to deal with price slip from close to open the next day.
Excellent. I'm about to watch all the excel, python, and mysql videos on your channel. Great teacher.
Thank you. Glad it helped
Python beginner thanks every tip helps
Glad it helped
Great Tutorial! Thank you!
Glad it helped
Thanks for the Insight sir.
Glad it helped
this is excellent work! many thanks
Glad it helped
Brilliant I'm falling off my chair. I tried TradeWindow etc now testing PlutoHQ Trading
Glad it helped
Informative tutorial. Thanks!
Glad it helped
Great and Awesome video. Just created my first momentum strategy with python; and I look forward to creating more strategies using python. Very useful skill for the FinTech industry.
Glad it helped.
Really useful.Thank you for your sharing.Clear explanation and nice tutorial.
Glad it was helpful!
Thank you for this great tutorial! Liked and subscribed.
I'm trying to learn python but trading tutorials that are any good are difficult to find, so your tutorials are very much appreciated.
Thanks. Glad they are helping
This is awesome, thank you so much for sharing!
Thanks. Glad it helped
can you make a video of how pandas performs the actual buy or sell orders ?
HI. That functionality will be implemented by your trading platform. You want to find one that has an API that will allow you to integrate custom algorithms. It's not necessary to use Python though. Thinkorswim used to have a custom scripting language that let you write and run alogos but I am not sure if it still does.
the code is simple and beautiful. love it!
Thanks. Glad it helped.
Thank you for the video!
Glad it helped
thankyou for the video @Matt Macarty, I didn't get the last part what is system return?
With this simple strategy approach, you end up with a small loss over the test period (-6%).
Hi, you explained very well, could you please make a video on price action kind of trading, not using lagging indicators, then it would be great to see your coding, and is it possible to do a multitime frame price action trading, if so please give the code.
Thanks. You mean something like this? ruclips.net/video/YjONqP78HHw/видео.html
Wow, this video was a game-changer for me! It made creating my own trading bot a breeze. Highly recommend it!
Also, check out "Algorithmic Trading Tutorial Python | Build Trading Algorithm from Scratch" by QuantInsti - another awesome video that helped me understand the fundamentals of algorithmic trading and build my own algorithm. Super helpful and definitely worth watching!
Big thanks to both videos for giving me the knowledge and confidence to create my trading bot. Can't wait to see what it can do! 😄🚀
Glad it helped. I have a few others that work with a broker: ruclips.net/p/PLiPFKCVZWHKqtlvkq-V610SXMr2Ii9uAR
wow simplemente gracias, he aprendido mucho con sus tutoriales, podría hacer un video sobre la gamma positiva y gamma negativa? saludos
Thanks. Glad it helped
thanks for the vid! was looking for some help on testing system returns and this vid helped
Glad it helped
thank you for this. learned a lot!
Glad it helped.
Everything works great. Thanks for the soft
Glad it helped
Thank you, boss.
Glad it helped
Nice video, thanks.
A quick question - Why would you use numpy log to calculate returns? I reckon there is a more accurate pct_change() function that can be used directly. Also, numpy log also needs a diff..
Also, could you explain how taking natural log (numpy log) and diff together end up coming close to the pct_change function result, which seems more intuitive for returns calculation?
Update: Okay, it turned out that I didn't know about log returns till now. I went and did my homework :)
But should we use log returns here when investment and returns are actually for discrete periods (1,2,3...n days)?
Glad it helped. You can use pandas pct_change function, but then you need to make other adjustments, so we typically use "instantaneous" rate of return. log, or LN change. This is used to make it easier to compare to other types of securities that compound interest continuously. It is also used to estimate volatility and in valuing options.
Great video, thank you a lot !!!
I have a question for you Math if you don't mind of course about the trading algorithm in cryptocurrencies, my algorithm works pretty well using technical analysis RSI and CCI but I want to improve it using machine learning for merging technical analysis and fundamental analysis what can I do here? what kind of machine learning models or algorithms do I have to look at?
my second question: I took a look at the holt winter method forecasting but I think this method will not work cause of the randomness of cryptocurrencies to change over time, is it true what I just said ? or this is a method for extracting trends and seasonality in any kind of time series
Lots of people do use and try to use ML for trading algorithms. Securities are notoriously difficult to forecast (or else we would all be rich). For sure traditional time series forecasting models are pretty much useless unless you are using the model as a trading signal rather than as a forecasting tool. I think a good place to start is with Python for Finance, Hilpisch.
thank you for the video . i think the look ahead is not a problem here if you take a decision at the end of the day . right ?
I am creating a complex AI model and i am generating features depending on SMA and EMA . It would be important for me to know what is the best practice in this regard . i am taking decision at the end of each 1 hour candle .
Yes if you can trade at the end of the day you wouldn't worry about look ahead. In practice, I guess you could assume a buy on close order, however since your model can't be updated until at least the exact close, it seems a little difficult to implement.
@@MattMacarty thanks , i am taking a decision two minutes before the closing of each hour in cryptos market . I guess i shall not worry about the look ahead .
Im a bit confused on why the look ahead was profitable but the shifting forward 1 day to catch the close was not profitable. Is shifting forward different then a cross over of the 10 and 22 for this example?
Sure you can try that. I think what we are basically seeing is that the entry and exit are critical in this strategy. Remember it's just for demonstration purposes
Can you please explain the look ahead problem that we faced here!!
So the data we are using implies that on the current day you know the closing price. This would not be possible until after the market has closed. I have seen it handled both ways, but you really can't get a signal until after the market closes and so it's unlikely that you would be able to act on any signal until the following day.
Plz make video trading in c+
Sorry. I don't know the language!
Nice video! ... thanks!
Glad it helped
I wonder if you can use this on penny stocks?
I mean the video is just to demonstrate some concepts, but if there is price action it shouldn't matter if it's a penny stock or not
Sir, would like to ask. I thought if you're going to use the jupyter notebook, it doesn't need any internet and yet you can still use it . does using any API requires internet? complete newbie, still on learning process Sir.
Some API's do require Internet. Typically if you are downloading data with an API is when you will need Internet.
great video, just wondering what AI technique did you use for this code
Thanks. This one is pretty much straight forward technical indicators.
@@MattMacarty ahh i see with machine learning used or neural networks at all? also what is the algorithm that is used for this? (sorry for so many questions)
@@simonibra19 there's no neural networks here. This is a simple 21-9 MACD technical indicator strategy
Hi Sir. What is the setting of the blue line if I will use MA? 😊
Do you mean you want the MA to be blue? You could use color = ‘b’
Hi Matt, I am learning Python programming right now. Just started so to me everything is completely new. I would like to ask you, with all the knowledge you have, have you been able to trade using your own algorithm with Python? If so what were the results? I amorally motivated to learn this in order to be able to trade. I think is more precise then guessing using Candlestick reading. Nice content by the way, Thanks bro.
Thanks for your comment. Sadly, no I don't have an algorithm to share with you. There are a couple of websites out there that have communities sharing algorithms though. Sites like Quantopian were great for people like you. I don't have one to recommend, but do a little research and I am sure you will find what you are looking for.
i can't import yfinance library in my jupyter notebook i always get error messages .....the command meant to see read panda data set to seem not to exist in my jupyter notebook modules thanks for the comrehensive videos anyways
Try this in your notebook: !pip install yfinance
Hi Matt. Which software you use in order to program? I have TradeSation live data. How can i use? Thanks.
I think you can use Python with many of the trading houses. I have Anaconda and a few other libraries that it doesn't come with.
the short return is incorrect? if S_0 = 10 and S_1 = 11, based on your formula (LN(11) - LN(10))*(short signal -1) = -0.09531018, exp(-0.09531018) = 0.909090.
whereas being short at 10 to 11, implies loss of (11 - 10)*(short signal -1) = -1, 9 dollars left implies 9 / 10 = 0.9 cumulative returns
The return is based on the price movement for the day. If you are short and the price goes down, ignoring everything else, your position value increases by the amount the price decreased.
Can someone explain what's the difference between Buy/Hold (blue line) and System line (orange one) at 14:31?
So the blue line shows what would happen if you bought in 2016 and just held gold without any adjustments. The orange line is what would have happened if you tried to catch the swings in prices based on a simple moving average.
Hello Matt, thanks for the video. I have a question, Why would you substract the returns to the system returns when your are not holding the asset, I mean when signal = -1 it reduces the system returns, but if you sold your position , the future price movement doesn't affect. Are you considered instead of multiply by -1 do it by 0?..
Hi so the model assumes you are always invested, either long or short. The -1 indicates a short position. But yes if you wanted to go flat you could replace -1 with 0.
Hi, try not to use price, lagging indicators, instead use price action strategy, will give accurate results, can you do this, many viewers are fed-up seeing these price lagging indicators usage in different channels of youtube.
This is for demonstrating how to write a simple back tester, not really interested in a strategy. Any strategy worth anything will not be found on RUclips.
@@MattMacartyI actually have one. But unfortunately I don’t know how to code which could’ve helped me. Since I go to school and cant keep up to the market.
How can we get the Min and Max price dates from the 21 day's of previous High and low and to get the monthly high and low price and date and the difference between them in days and time/days ?
I think probably the easiest way will be to start adding columns that track what you are looking for. If you just want to watch a max/min over a 21 day period, you can make a column something like df['21-max'].rolling(21).max(), etc.
I have a problem in the Plot trades on time series. I can plot out all the things except the markers, it shows "ValueError: iLocation based boolean indexing cannot use an indexable as a mask"
Please help
Yes it sounds like you are trying to plot the wrong data. I would maybe creagte a separate Series based on your data mask and plot that.
I can't get the data ( TypeError: string indices must be integers, not 'str' ) I tryied the override of yfinance but it doesn't work neither.
Try using yfinance directly: yf.download(...)
@@MattMacarty Just finished doing a program with tkinker with it thank you, Im new in coding. 😂 Great tutorial keep it up!!!
i used a different set of data from 2021-02 to 2022-02 and upon comparing buy/hold with system, system outperforms buy/hold. what is your take on this?
I am sure at times a system can outperform. The question is will the same conditions prevail in the future? And then how do you know that conditions are no longer favorable for the system? Obviously people do make money with algorithmic trading. My examples are meant to show you how construct fairly simple strategies to give you an idea of "how to".
@@MattMacarty that's very thoughtful thank you
hi, what is the idea behind plotting the mark on 9-day when entry is 2 and plotting the mark on 21-day when entry is -2? are they just for display meaning it should mean no harm if i were to plot the other day around? thank you
at the same time, what is the role of np.exp() here? i understand since the log return represent somewhat similar to pct_change, we should do cumprod() but i don't understand what np.exp is for, knowing that it is e**x for x to be the value of that index
at that*
Sure you can plot on either as long as it intersects at a cross. I use the 9-day so you don't have to reverse your logic.
You can arrive at the same numbers using either pct change or exp. exp is also known as instanaeous rate of return and computationally it's somewhat easier to work with than pct change. Instanteaous rate of return is used as the standard particularly when dealing with derivatives.
can i use this to trade XAUUSD?
I would test to see how it does, and compare to various strategies. The video was meant to show people how to test strategies, not how to trade profitably
@@MattMacarty ok sir,thank you for all the great content
where can i get national stock exhange of india historical data
From their website or you can buy your broker API Data if you want to use the data comercialy.
You might try twelvedata or alphavantage, freeium APIs for global data.
yes you can get it.i can help u out
Yahoo finance code doesn't work anymore?
It should. Try pip install --upgrade pandas-datareader from the command line, then try running your code again. If that doesn't work try pip install yfinance. This will require you to modify the code to work with yfinance (yfinance.download)
@@MattMacarty I figured it out in the end thanks tho
How to Read a Binary File Like Metastock Files Using Python
You can start with something like file = open([filename], 'rb'), then probably file.readlines(). And if each data point in a line is separated by a space or comma or anything you can you can split on the character.
Thank you Mr.Macraty for reply, can you please explain it in a video?
Hai..i have problem ..when i run my code there are some error like this
NameError : name 'pdr' is not defined
I already follow the instruction and also update to the anaconda prompt....but still can't.please help.
Hi you can download the notebook from the link in the description. This is error means pdr has not been defined anywhere in your code. pdr is the alias I gave pandas_datareader in the first notebook cell. Maybe you have a typo?
Ok sir...already get it...tq🙂