Transcription
Hi and welcome back. Today's strategy uses the Marov process principle for trading decisions. I back tested this strategy across 20 different assets achieving an average annual return of 97%. The entire back test was fully automated using Python language. And the code I will be using in this video is available for download via the link in the description. Feel free to grab it, run the simulation yourself, and even try out your own tweaks and modifications.
The strategy is very simple. We will use the daily time frame and we will wait for consecutive candles of the same direction or the same color. In this example, I'm showing three consecutive red candles. When this pattern occurs, we will enter the market on the next candle's open and close by the end of the day or when the candle closes. So, in brief, our trades are opened for one single day. In the opposite direction, we can wait for three consecutive green candles and we short the market at the open of the next candle. We close the trade at the end of the day.
Now, if you are wondering what is a markov process, it's simply a random stochastic process where the future states only depend on the current state and not on the past states. In other words, my decision of trading and the direction of the trade will depend only on the last few candles disregarding previous price movements. This strategy has a single adjustable parameter, the number of consecutive candles we wait for before opening a trade. For example, instead of looking for patterns with three candles of the same color, you might want to detect patterns with four or any other number of candles. The key is to choose the number of candles that most often leads to a reversal candle, maximizing our chances of a winning trade. This probability can be easily calculated in Python using historical data.
Now, let's dive into the code and walk through the back testing results to see how this simple strategy managed to pull off a 97% annual return across 20 assets and over 10 years of data. And as I mentioned, you can download the Python code from the link in the description of this video. So, this is our Jupyter notebook file. In the first cell, I'm importing Y Finance and Pandas. I'm going to define the list of tickers we're going to or assets we're going to download. So we have Apple, Microsoft, Google, Amazon, Tesla, Nvidia and so on. Then we use the download function to um download the list of data for these uh assets between 2015 and 2025. So that's 10 years of data. This is the daily time frame and it's going to be downloaded and stored in a data frame called data.
Then I'm going to define a function called count candle streaks. It takes two parameters, the data frame and also the number of consecutive candles we're looking for. So we need to define our variables. At this point we have an up streaks equals 0, up followed by a red, down streaks and down followed by green. For the moment, these are all equal to zero. For ticker in tickers, which means for each asset in our list in the data frame, we're going to query the data frame specifically for this asset. And then we're going to test if these conditions are true. And this loop is going to test for green streaks and red streaks. and it's going to count the number of upstreaks followed by a red candle and the number of down streaks followed by a green candle.
So now we can compute the up fraction. So that's the up followed by red divided by the total up streaks. Then we have the down fraction that's the down followed by a green candle divided by the total number of the down streaks only if down streaks is different than zero or is positive. At the end, this function will return the green candles followed by red fraction and the red candles followed by green fraction over the total occurrences of green candle streaks and red candle streaks.
Now we can use the function for example for n equal 2. That means two red candles followed by one green candle or two green candles followed by a red candle. And we're going to print the matrix here. So for green candles followed by red, we have two green candles followed by a red candle, that's 49%. So 0.49. However, two red candles followed by green candle, it's 53%. So this is a good thing to know because we know ahead before putting this as an indicator for trading, what's the percentage of the times the indicator is going to predict correctly in the future. So we could change this two to three for example and that's going to change these numbers. So now we have 49% and 52%. We could increase to four and we have 48% and 53% and so on. Ideally if you want to trade both sides long and short we want both of these numbers to be above 50%. So we need 53, 53 or 54, 54. That would be in the ideal case. But it doesn't work this way. It seems that this indicator is good for a long only strategy at least on the assets we are using for the back testing because we're only having a fraction that's above 0.5 for the cases where we have red candles followed by a green candle which signals an uptrend reversal. And actually the function is very versatile. You can try any number of streaks that you uh you can imagine. So I'm trying here for 10 consecutive candles followed by a reversal candle. And actually we have 65% in the um uh in the case where if you take 10 red candles followed by one green candle the fraction is 65%. So if you're going long only taking 10 red candles is going to give you a high win rate. But this doesn't mean that the total return of the strategy is going to be as high.
Okay. Now there's only one way to find out if this actually works is by back testing the strategy. I've defined two strategies actually two classes. I'm using the backtesting.py package as usual. The first class is consecutive red and green candles strategy. So that's going long and short at the same time. So it can trade both sides in one single strategy. The second strategy is a long only strategy. So that's consecutive red candles followed by one green candle. The class is relatively simple. We have one adjustable parameter which is n the number of consecutive candles. At the moment it's equal to three. And then we have the long entry conditions in this case. So if we have this streak of candles where the closing price is below the opening price. So these are red candles and their number is above three consecutive candles. Then we apply a buy position. We open a buy position. In the opposite case, if they are green candles, we open a selling position or a short position. And that's it. We're going to close the position or any open position by the end of the candle because we're using the daily time frame. So it's going to be by the end of the day.
And now we're going to define a new dictionary named results. This is where we're going to store or save the results for each of the tickers or the assets. And then for each of these assets, we're going to extract the specific data frame from the wide data frame. We're going to rename the columns just in case the name is not suitable for the back testing package. And then we run the back test. So we define the back test using the data frame, the strategy. So I'm going to define red green strategy because this is strategy that uses short and long positions. Cash is 100,000. I'm using a commission and these are exclusive orders. We're just allowing one open trade at a time for the moment. We're not using any margin. So that's or any leverage. And then that's it. We basically run the back test. We uh save the results in the results dictionary for each of the symbols or for each of the assets.
Then if we suppose that we are running this strategy on multiple assets in parallel at the same time allowing just one open trade at a time, we could aggregate the returns at the end of the simulation just to know if we would run it simultaneously on all of these assets how much we would expect. And that would be the sum of the returns that we got for each of the assets. So, I'm going to run this again just to be sure that the numbers are accurate. And with the um uh setup that we just included, we can see that the Apple led to 31%, Microsoft 156%. So, most of these stocks are in the positive. Tesla is -1.47, Nvidia is - 4.5 and so on. So, we have - 455, we have - 31% and so on. But at the end, the sum of the returns is 1,223%. That's over 10 years of data. Now, if you divide by 10, that's around 122% returns per year. So, the average return actually is 61%. It's going to depend also on the uh commission that we are using. But I've done my research and for this type of stocks, for the assets that we have used, this is more than enough. That's 0.02%. 0.02%. Of course, this depends on your account, the type of broker, and the type of assets you will be trading. For Forex, you might want to increase this to up to 0.001. This is mainly to cover the spread and commission fees and so on. But for stocks, it's much cheaper. For futures, it's also around uh 0.00.2%. So, that's almost like stocks. And for cryptocurrencies, it's uh kind of expensive. So that's 0.1 up to 0.2%. So we need to replace it by 0.001 or 0.002.
Now since these are US stocks and we know most of them are climbing up the Microsoft and Apple and uh Nvidia, we could choose to use the long only strategy. So that's consecutive red strategy with three consecutive red candles. So I'm going to change it here into red strategy. I'm going to run it again and we can see that now we have 974%. The average return is 48% per year and the maximum draw down across all of these tickers or assets is minus 62%. So that's specifically for one uh particular asset. The numbers here show you how one asset can compensate for the others. So some of them are negative, some of them are positive and they can kind of compensate each others. So if a strategy doesn't work for a particular asset, no matter what you do for different number of consecutive candles, you could simply eliminate it from your list of assets and maybe decrease the risk in this case.
One last thing before I finish, it's very easy to change the number of consecutive candles. Let's try n equal 10. So if I run it, I update the class and we run again just out of curiosity since it doesn't cost us much. We have only 10% in returns. The reason is when you are very selective in this case. It's true that you will have a higher win rate but you will have less trades because it's very rare to have 10 consecutive candles of the same color. So just to be careful when you are modifying these numbers. Don't go to the extreme cases. Just stick with 2, 3, 4 up to five I would say. Five candles we could try it now. So five candles we have 208%. So that's 20% per year. So it's not very impressive either. So sticking with the classic numbers 2, 3 or 4 is probably the safest way.
Anyway, this would be it for this video. I hope you guys liked it. I hope you found this information helpful. If so, please leave a comment, drop a like, tell me what you think about this video or this strategy. If you have any other ideas to be developed and revisited and uh back tested on this channel. Until our next one, trade safe and see you next time.