[00:00] Hey this is Bob on Analytics and this week we are developing a new strategy around what we're [00:17] calling the ADL Ultra indicator and it takes two inputs one is a breadth measure called the accumulation distribution line which combines price movement with volume and [00:30] the second is the ulcer index which measures a downside risk so effectively it's looking at the the depth and the strength of the drawdown from a maximum [00:44] value from the most recent highs and what we're going to be doing today is taking our breadth measure the accumulation distribution line and and creating the indicator, the ulcer index, from that data. [00:57] And then we're going to be using it to create a trading strategy. So for our bread measure, effectively what it does is create something that will track money flow volume. [01:12] And what it does is it takes the price action and converts that to a range between minus one and plus one and tracks where price closes within that range, and then multiplies it by the volume, [01:25] and then creates a cumulative sum from that. And then our Ultra Index measures drawdown risk effectively over, we're going to use a 14 period window, and that creates the indicator you're seeing red. [01:38] So our strategy today is to buy whenever our Ultra ADL indicator turns higher, and we're going to sell whenever it crosses below zero. so the idea of this one then is to buy pullbacks using the ulcer index when bread starts to turn [01:56] positive and then get out of the market every time our ulcer ADL turns below zero so if you're ready to start coding that up please read the disclaimer [02:17] Okay, let's get going then. And I've opened up a fresh Jupyter Notebook, which I am going to rename AVL Ultra. [02:33] And I'm just going to click in the first cell here, which should return a few times to create some empty cells. And to save time today, we'll be copying from our template here. This contains repeatable code snippets that we use each time to create a strategy and then backtest it. [02:49] Both the completed notebook that we made today plus the template will be available to download for free. You can get that on our website, www.bubble-analytics.com. The link is in the footer here in our GitHub profile. [03:03] So let's get going then. And the first job, as always, would be to import our libraries. These are our helper libraries that are going to help us to run our strategy and then backtest it. [03:16] So I'm just going to hit shift return a couple of times to initialize that in our notebook. And next job would be to grab some price data. So we're going to download some historical market data from Yahoo Finance. [03:29] We're going to do that using the Y Finance library here, which we imported as YF. So I paste that code snippet in and hit shift return. this is the output that you should expect to see. The key line is the one right at the top [03:43] so if I just comment these lines out. So we're going to use the Y-finance library which we imported as YF here using the download method and that takes a minimum of three parameters so you need to tell it the ticker and the period of time you want the data for and then the time frame [03:59] so in our case we're going to download a 10 years worth of daily timeframe data for QQQ the NASBET 100 ETF we're going to store it in a variable called DF and so if you want to see what's in DF [04:12] just type the variable name and click shift return and this is the output in raw format and effectively these lines just tied it up a little bit so first line there is going to get rid of these superfluous column names you don't need a multi header column here so if I just uncomment [04:31] and run that line you can see it gets rid of that ticker and then qqq across the top and next job we're going to split price and date into their own columns and to do that we're going to reset the index which will have the effect of making price the index with the row numbers on the [04:45] left hand side here and next line looks at the date column and even though the date in the dates in the date column look like actual dates they're not actually they're actually number strings so if if you want to use a date in a date format within your codebook at some point as we will do when we [05:03] create the x-axis on our chart and display once in a year you need to convert that to a date time format so that's what this line does here so it takes the date column uses pandas library which [05:15] we import here as pd and uses their two date time methods to convert that to a date time format and then finally because we're going to need the volume today i'm just going to delete this one completely and this is now your your data set and this is what [05:32] we'll be using going forward so let's start with our first breadth measure this is the accumulation distribution line I've come into StockCharts Chart School here where you can get a good explanation of what it is and how it [05:47] works and it is effectively this pin point here on their chart and what it does is it combines price action and volume effectively to create a breadth measure that is designed to show you if the rally or the decline that you're [06:06] seeing there is being fully participated in by the majority of stocks I'll give you a little example here I'm not sure I actually really told you this but if you look at the price action coming down here into this low and then [06:21] the little rally, you can see that the accumulation distribution line follows it quite well. So it falls when price is falling, bottoms out when price bottoms out, and starts to rally. But if you notice here, it turns over prior to the top being made with price. And it kind [06:37] of serves as a warning, as most breadth measures do, that perhaps the underlying strength of that rally is not as strong as people would believe, or sort of warns you that there's [06:49] potential for an imminent up so they call that divergences and divergences are not always that reliable in my opinion certainly i don't think you can create a strategy around it but uh but [07:02] let's get this one um created then so it's quite simple what it does is it creates something called the money flow multiplier through this line here and what that does is create a range between plus [07:15] one and minus one and of course um it interacts where the close is relative to that range so obviously the close higher to plus one as possible is the most bullish so if it closes at you know 0.9 [07:30] that's the most bullish and then effectively we take that money flow and multiply and multiply by the volume for that period and then the adl is effectively just the keynote of um of um money flow volume so let's get this created so I'm going to define a function called ADL [07:49] and ADL we don't need to give it a look back period or anything like that we just need to tell it where to find our data so ADL if I copy let me just copy that from there [08:01] and what we'll do is bring it in and bring it in and copy that in there so effectively we're looking at the money flow multiplier so we need to tell it where to find [08:14] the OHLC data for this one and the volume and that's it so we don't need a look back period or anything like that for this one so let's do the money flow multiplier we call that MSM and [08:26] that is equal to data close minus the low so we're going to need some brackets aren't we in a minute so we'll do it now so we want the close minus the low and from that we deduct the [08:44] high minus the close so in fact I'm going to just copy this like that so this [08:58] one so the is the close minus the low minus the high minus the close and then that is divided by the high minus value and then we should make some more brackets [09:21] here I think so this whole thing needs to go in one giant bracket so that's your money flow multiplier so that's what creates the range and then so the money flow volume which I'm just going to call msv here it's basically just the [09:36] calculation above MSM times the volume so let's get that in there and then our ADL then is effectively the cumulative sum of money for the volume so if I do MSV [09:50] and then what we're going to use is the come from method so cumulative sum method and from our whole function here all we're looking to do is return the value in return the value of ADL. [10:02] So now we're going to create a, I can delete this now, can't I? So now we're going to create a new column in our data frame, DF. So call the name, select what we want to call the column, [10:14] which is going to be ADL. And that is going to be the equivalent of running our function and then telling it where to find it. I wish I could see data. And if I now hit shift return to run all that, [10:27] you should see that we've got values in our newly created ADL column within our existing data frame so that's that done and let's now see we did it now i'll tell you what we'll create the chart first [10:42] it'll be easier to understand so we're going to recreate this chart for stock charts and it is going we're not going to display this volume thing uh part at the bottom here we're just going to have the accumulation distribution line and price above so on this chart here and we're going to use [10:57] plotly and this is all effectively one lot big column this chart with two rows so prices on row one here above and the accumulation distribution line on row two so um let's go and create that now [11:13] so to do that i'm back in my template here this is the chart section and what we're going to need need to do is make a subplot so we're going to tell plot me to make a subplot such price with an indicator below and in here we're telling it we've got one [11:27] column with two rows we're going to share the x-axis and then the row heights here dictate how much chart real estate price gets versus the indicator so in this case row one price get is getting 68% and the indicator below is 32% [11:43] so I'm going to grab all this section here including the couple lines above and going to come back and leave a few empty spaces here because we're going to create another indicator and our signal function so that's that part this [11:57] section here above what we're telling Plotly to do is rip the data so effectively if I didn't have these lines in here or comment them out and every time you ran the chart and rendered it it would show all 2,514 [12:13] rows and we don't want that but what we want to do is take a slice of our data which is what this part does here and effectively you tell it what row number or bar to start from and then how many [12:26] bars after that you want to display so you can start from bar 1000 and display 500 bars for example or as in my case what i normally do is take sort of anything around 300 plus so 350 here [12:41] and then what I'll do is make that 2200. So effectively we'll end up with 314 bars on that one. So that is the first part of it here and we've set up our subplots and then imported price. [12:59] So this is importing our OHLC data here and then styling it up colour wise and then we're putting that on row one So for row two what we going to do is feed in the values in this ADL column that we just created So if I come back in the template here and what we going to do is add some indicator data to our indicator subplot here [13:24] So I'm going to take this code snippet, and I'm going to paste that in. And all we need to tell it is what the column name is that we're looking to take the data from. In that case, that is ADL. [13:36] so and then you're just writing up really so I'll leave the color as it is I think but I tend to make the width a bit wider a bit chunkier and then this is for the price label on the right hand side so [13:50] we've got a chart region so I'm going to change the name of it to ADL and this will be on row two so column one row two for this one and so with that in place that's more or less what we need and we're going to render it so all the bits at the bottom here really is you styling up your [14:08] charts i'm going to take this whole little section here and go into start to play with this a little bit so first little code snippet here all it does is set the width and height of your charts and it [14:21] gets rid of annoying little range slider and then sets the theme so i've gone for the dark theme and then i've updated the grid teller only because it stands out quite a bit um on the you can get rid of this but it stands out quite a bit on the [14:34] um the existing theme colors and then down here if you want to save your image your chart image you can save a copy to wherever you have your codebook saved and i'm not going to do that today but you just rename it there [14:47] and leave that code in and run it and then uh this is probably the only sort of keyish bit here is that we want to update our x actually so that displays the date so at the moment what it would display would be the row numbers because the index is price here in this column so what we're [15:04] going to do is change it to the date and this is why we converted the date to a date time format originally it's so that plot we can now render a date at the bottom so in order to do that so we'll do that a bit better than sort of [15:19] standard here and we are going to create some range breaks basically so we're going to tell Plotly what part of the data we want to include so with stock market data typically you've got Monday to Friday minus bank holidays so but you [15:34] don't have any data on Saturday and Sunday Plotly doesn't understand its rendering stock market data so it is going to render Saturday and Sundays which means you're going to get gaps in your chart so in order to avoid the gaps [15:47] and make it a little bit smoother what we are going to say is I'm just going to get rid of this line here and what we're going to say is create a range break that starts on Saturday and ends by Monday [15:59] so actually we get rid of Saturday or Sunday and then finally in order to visualize the date on the x-axis we're going to have to set the date column as the index I'm just going to take this line up here [16:11] and come back in and put that above and then if we run all this a couple of times Okay so now we've got our chart formed, we're displaying the most recent bars and we have [16:27] our accumulation distribution line here below and you can see that it more or less follows price but this is kind of what I mean about divergence is so if you notice here price [16:43] makes a high in sort of late October and then doesn't make a new high until April starts to break out until mid-April and this is like kind of a double stop um but if you notice on the breadth [16:56] measure here we made a high as normal and then we carry on making highs and we even make a really nice new high here way above this one so the breadth measure was making a new high even though [17:08] price wasn't and that's kind of considered a bullisher when it sort of says that maybe the uh the sort of strength of the decline is not as bad as you think and you know it may be good even [17:20] though we start to pull back here but that's the kind of thing people would use it for and look for uh we're not going to do that today um i we tend to find that both measures are much better when combined with indicators to be honest. So next job then it would be to create our second indicator [17:38] and that's going to be the Ulster index and the Ulster index is really good it's basically tracking sort of the speed and strength of a decline from a most recent high so again read [17:53] the article by stock charts it explains it much better than I'll be able to but effectively it's looking at the close minus the highest close within a certain period so you'll look back period [18:05] it creates rate of change essentially and then it creates a squared average which is like it says 14 period sum of this potential or down squared and then takes the square root of [18:19] that squared average it sounds a lot more complicated than it is but effectively it is basically measuring downside risk, downside volatility. So the only change we're going to make to it here is that [18:31] whereas the standard indicator is going to use the price close, we're going to swap the price close for that ADL, our breadth measure. The other thing we're going to do is flip it. So, and this is just my taste, [18:44] but the ultimate debt here falls when price is rising and it rises when price falls. And that's annoying. and a bit counterintuitive so we're going to just change that so basically when price is rising the [18:59] also indexes as well so on so forth so that's the only change we're going to make there um if you want to go and get a spreadsheet they provide a spreadsheet for you as well so you could go in and have a look at the formulas but this is how you do it so and in fact let me copy all that [19:14] and what we'll do is define a new function then called ulcer and ulcer all that's going to take in is some data so we need to tell it what would you know which one [19:26] the closing process of the standard indicator in our case the ADL and then also the look back periods which is just going to be called n here and then so let me paste that in so the first thing we're going to create is the percent [19:40] drawdown which I'm just going to call pd and that would be equal to the ADL data that we just created so current value of ADL minus the highest value within [19:57] that lookback period so that would be data ADL and then we're going to create a rolling window so chain on the rolling method here which would be equivalent to n is going to be sorting but could be any look back period or n is the look back period [20:14] for us from which we're going to take the maximum value so we're going to take we're going to create a rolling window of these values from which we're going to take the highest and store that so it's the total value minus the highest within that look back period [20:29] divided by the highest within that look back period so if I just grab this here and paste that in and then the whole thing is times by 100 so that's our first line done and we're going to need [20:45] some brackets aren't we so we can just follow the example here so we're going to need two in there one in here and another one in here so that's our first line done now it's the squared average [20:59] and this is the the squared the square of the 14 period of percentual order so and so we'll call it SA for squared average and so that will be equal to the value that we just created above PD [21:15] and then which we're going to square just like that and from that we're just going to take rolling and for our look back period average so mean and so relatively [21:28] straightforward and then our alter index then is effectively the square root of our squared average and for that we're going to be using the numpy library which we imported here as MP so we use the MP as numpy so UI then is going to be [21:44] equal to the squared average above to which we're going to apply numpy square root method so which i think is that and then from that function we just want to return [21:59] the ui values so we're just looking for our ulcer index values so effectively we've created the ulcer index but on our breadth measure so just like we did above and we're going to create a new column [22:12] with these values in so i'm just going to call that ui and that would be equal to run our function for our function here select where to find the data and the lookup period of all our years which we [22:25] picked 14 so that is that let me now run it and you can see we've created our new columns and we have our new values and what I'm going to do is because we've got some num values here not a number [22:38] of values I'm just going to get rid of those the reason that you have them is that at this point in the dataset you don't have enough studies to compute this roll in average so we're going to use the dropna method to get rid of it so we call our data frame here df and then hit drop in 8 and [22:56] tell it to do it on the existing data frame so df.dropn8 and then like i said before i i don't like the i i think it's counterintuitive when you see an indicator that falls when price rises and [23:10] so on so forth so we're just going to change it so effectively i'm going to take the ui column here and that will be equal to one minus the value of the ui column effectively so [23:26] and what that'll do is inverse it so now it rises and price rises and vice versa so if we now run all this you should see now we've got values and no non-values here and we've still got values in [23:39] our column. So right let's go and put this on a chart. So we shouldn't really need to go back into our template for this or at least to load the price data but we're going to have to amend our existing chart here. So if you notice that we only created two subplots within the one column. [23:55] So now we're going to have to have three because we're going to add in the ulcer ADL indicator. So change that to three and then we're going to have to tell it what percentage each gets. so i'm going to change the price is going to get 50% and the indicators are going to get 25% each [24:12] so that's 50.25.25 there and then we can copy this whole little code snippet here and paste that below and what we're going to do is add the values in our UI column here so i'm going to change that [24:31] the ui and then change the name of it to ultra adr i guess and then we will make that because it's measuring drawdown risk we'll make that red and then obviously that's going to go in row three [24:48] so if i run that whole thing again shift return to run it and you can see now we've got our breadth measure above our ulcer index below so we're going to tart it up a little bit first thing I'm going to do is make the, because I've got two indicators there, I'm just going to make the [25:03] chart height a bit higher. So we'll make that 900 just so that we get more of a nice view of it and then we're going to start it up a little bit. So first thing I'm going to do is add some horizontal [25:15] lines for the alpha index. I just think, I don't know, I just find that helpful. So what we'll do is we'll grab some h lines we're going to create. So we're going to add levels lines to that indicator [25:27] and so we're going to create horizontal lines effectively so I'm going to grab this little code snippet here and then if I look at my chart I'm going to have one at one minus one and minus two [25:39] and you get one automatically at zero level anyway so so I'll come in and paste that in we're going to create three lines so I'm going to hit this three times and then we're going to [25:51] have to make these appear in row three so I'm going to mend that to there and we're going to have one at minus two and then we're going to have one at minus one and then finally we're [26:04] going to have one at one so and again if I now hit shift return to run that we've got that to me looks better and then let's chain on some let's try and chain on some labels just so we know [26:20] what we are doing and what indicators which are them so this part here the subplot title and then i show you how to chain it so we going to come and grab this little code snippet here from the template come back above and so the [26:37] first one I'm going to do is the ulcer ADL and the key line with this is just styling it up so Y to size 13 and the text that you want to put it and then this this is the [26:51] sort of key part of it for this little code snippet is the x-axis and y-axis is paper and what that's saying is create a coordinate a grid coordinate sort of square over my chart and effectively it's [27:04] creating a sort of a one-to-one coordinate base or not to one coordinate base so for the y-axis so down to up would be zero to one and for the x-axis left to right would be zero to one so [27:18] effectively when I say plot my label at x equals 1 I'm saying plus it's far right as it can go on the chart and then for my y value at 0.2 I'm saying [27:33] plus it's 20% up from the bottom here so if I just run this now it should give me an idea of where that label is going to... okay so we're not far off and so I ended up here and we're just going to move it up slightly by changing the y value so if I change [27:48] that to let's try that so let's try that one two four probably a bit high but there you go so if [28:01] I change my x value now to zero and run that again you would see that it would be over here on the I don't know whether I might keep that in check. [28:14] So anyway, so if you want to chain on a secondary label, you can't have multiple versions of this code snippet on their own. You need to chain them together. [28:26] So otherwise they use it with each other. So we're going to copy from the dict part here. So essentially I'm going to take it from that dict part all the way down to this first bracket at the end. [28:38] and then I'm going to copy that code snippet there and put a little comma and hit return to make some space and then paste that in and I'm going to then change our label to accumulation distribution [28:57] maybe and then we're going to all we need to do is effectively move it up on the screen a little bit so if I kept that the same in runout you would have two labels overlapping we're going to move up to here a little bit and that is about 50% higher [29:12] so maybe a little bit more so we'll make that say 52 and then if I run that again there we go so we've got labels for each on our chart and yeah looking quite good so next step then will be to [29:27] get our signal function in place so I'm going to come back into my template here and then grab this function um so all this function does is run through your data set or whatever data set you [29:43] tap to run through um line by line and looking for your buy and sell parameters to be hit and where it sees a buy signal it puts a one in the signal column where it sees a two uh where you [29:56] see the cell signal it puts a two and where there's no signal it has a zero and it creates a column within your existing data set called signal and that this is what's going to tell back testing when to buy and when to sell so so let's get this um set up then so [30:13] uh for our buy signals all we're going to be doing is looking at when our ultra radio indicator turns up so there's a big one there so that's what's going to have a look at that so i think there'll be a sell signal as well and for our sell signal is the ultra ADL dropping below one which it does [30:31] there yes so on and so for our for our sales signal effectively we're going to be saying if yesterday's value of ultra ADL was above one and today's value is below one as it is here on march [30:44] 16 17 that would be a sell signal and then for a buy signal all they're looking is for a turn higher so you can see here on yeah that's it isn't it so April the 6th to April the 7th that's where [30:58] the term comes so April the 7th is higher than April the 6th but April the 6th was lower than April the 2nd yeah so effectively that's charting the term isn't it so and then to do that we're [31:14] We're going to use the ILOC method. ILOC, so for our signal function, I is representative of the current bar, and then I minus 1 is the bar before, [31:26] I minus 2, 2 bars before current, I minus 3, 3 bars before current, so on and so forth. So the way you see .ILOC, I minus 1, it's yesterday's bar, so on. [31:38] Anyway, let's go and code this one. So the sales signal is really simple. So all we're saying is data.ui.iloc. So if yesterday's bar, which would be i minus 1 here, [31:50] is less than, sorry, greater than 0, and today's value for ui.iloc, and today's value is then .iloc i, [32:03] today's value is greater than 0, sorry, yeah, less than 0. What am I doing? that would be our sell signal. So effectively if yesterday's value of UI is above zero, [32:15] today's value is below zero, that's our sell signal, we get out of the market. And then for our buy signal we're charting the turn, so what we're effectively saying is today's value of UI, [32:28] so that would be dot i or guy, would need to be greater than yesterday's value, so it's data.ui.i-1 and the value yesterday so data.ui.i-1 would need to have been less than the day before that [32:51] data.ui.iobc i-2 if that makes sense so and let's run our signal function oh what have i done oh yeah here's one of those [33:09] i think that's okay yeah there we go so now we've got a signal column with our data set and this is what, let me run all that again because notice how when we get to this [33:23] point here we're setting the date of the index so if you then go back and make an amendment to your data set it puts price and date back into overlapping headers but anyway the signal column is there and [33:36] and let's now render the buy and sell signals on our chart and just make sure the actual methodology is working. So if I come back in the template into the chart section and see this little bit for entries, actually before I need to do this can't I, so come [33:53] back in the chart section, this little section above here, long entries and short entries, let's grab that and paste that in here. So all this is doing is running through the [34:05] dataset DF and it's looking for the values in the signal column and where it sees a 1, which is a buy signal, it returns the value of the low price minus the small offset and where [34:18] it sees signal equals 2 it returns the value of the high column plus the small offset and then these lines here create two new columns one for long entries one for short entries within our data frame and effectively we record those prices where we get the signals [34:34] that we have a NAND value instead and the prices within those columns are what plot is going to use to place the markers on the chart effectively so if I run that and then we have definitely made a mistake here because [34:51] so when I ran that if you see this line here it gives me the [35:04] value counts in the signal column and if you notice there there was no buy signals so all it gave you was zero signals and sell signals too but no buy signals so obviously there was some error in my code above that's what prompted me to go up and have a look [35:20] so basically I missed that so if I now run this and then run the next column again next code cell again and you can see that now we've got our buy signals in place 195 buy signals 39 sell signals and now we're going to render those on the chart [35:37] so if I come back into my template here and grab the chart section the entry section for the chart part and I am going to just wipe that in there and if I now run that [35:51] uh we should get buy and sell signals rendered here so for these uh little code snippets here all they're doing don't forget that we just created uh two new in fact let me just call it [36:04] I can't tell that line can you? BANG! Let me call that here So if I run it all again You can see that we've got two new columns within [36:16] our dataframe, long entries and short entries and then so what Plotly's doing here is it's looking in that column where it sees a price, or where it sees an entry so it's basically going to be a price or a NAND value here [36:28] so it's looking in those columns where it sees an entry it creates a marker then this part styles it up so we are telling it to make it size seven and for a biasing it's a wide arrow up and then on the short side looks in the short inches column create the marker [36:43] at scissor price and it's going to be a gold cross with the size of seven and that looks like this on the chart here so um let's go and have a look remember how we were looking for a turn here [36:58] So we cross down on March the 17th. There you go. There's our cell signal working. And then for our turn up, it was April the 7th, I think. And there you go. There's our long-end sheet working as well. So that's that part done. [37:11] So we kind of know that our signal is functional. We think what we've asked it to do is working. Now that we've got a signal column in place, we can create a backtest. So let's go back into the tempera, [37:26] I'm going to take this whole section here. So this is the backtest script and we're going to amend it slightly but effectively this is where we tell backtest from what to do where we get a one or [37:41] two in our signal column. So first part of the code here we take in the value as a function to take in the value of our signal column and we create a strategy class called myStrat and initialize [37:53] it here and then bring in the signal value using that signal function that we just created there and so where you see self.signal that's referring to the values in that column and then these are the main bits here so where self.signal is one so a buy signal if we're already sure we close [38:09] that position or if we don't already have a position we open one and we buy with the size of a 99% available capital and where we see a sell a self.signal equals two so a sell signal if we're [38:23] already long we close it or if you don't already have a position either way we short self-cut sale with the size equivalent to 99% of our available capital. This part here allows you [38:35] to put a time limit on your trades so you could set a long trade for example with a 2% profit target and a 2% stop and then say if you haven't reached either of those targets in five days [38:47] sell it anyway and so you keep that in if you want to do that. So and then these parts here are the premises for the back test so we're telling it to take the OHLC data from DF we're going to run the [39:01] my strategy class that we created above here and start in cash of 100 000 margin equals one means no leverage exclusive orders equals two means you can only have one position open at each time [39:13] Trade on close tool we're going to get rid of and hedging tool, we don't need hedging because we're never going to have one open trade, but trade on close, if you don't set [39:25] that, the default is it will buy at the open following a buy or sell signal. And then we're paying commission here equivalent to 0.05% per side, so total fees would be [39:38] 0 for a complete trade that a buy and a sell And finally this little bit here price this is price as at the close of a buy or sell signal and from that we going to key through our stop loss and our profit target [39:54] so let's run it raw as you normally do first so with no risk management whatsoever so hit shift return here and you can see it does really well. [40:07] So this is if you used it as a buy and hold strategy, if you were using it. I'm just kind of misreading, right, because it's saying that you're an investor. So effectively, it doesn't ever trade the market. [40:21] It flips short and gets long again, following signals only. So, for example, you were short on this bar here until this signal came. and then you were long until this people came. [40:37] Okay, so effectively you'd be holding your trades all the time and then just flipping long and short. And over the 10 years, you've flipped 77 times. [40:50] You win half of your trades, but you handsomely crush the winners and far outweigh the losers. And your Jordan was really good. And you beat the market handsomely. So really good strategy. [41:04] But the most amazing thing about that is that typically an investor is not going to flip short when you get a sell signal. It's typically just your trigger to get out of the market. So if you want to have a look at that instead, you can just comment out this line here. [41:17] So when we get a sell signal now, if we've got a long position, we close it, but we're not actually going short. And if we just run that again, it still does really, really well. So you're in the market 83% of the time, you beat the market, you've got a drawdown lower than the drawdown the market would have given you. [41:37] You're only buying 38% of the time, you've got a good win rate and again you're big and small sort of trades that are good. So great little strategy and you could use this as an investor but obviously in order, you don't really have enough trades to be completely sure that this is a valid strategy. [41:56] and burnt and I don't like the idea of that anyway so what we're going to do is add some stocks and profit targets in so first of all let's do the short side so I'm going to comment [42:10] out the buy side so what's happening here is that if I get a buy signal and I'm already short it will close it but it just won't go on so and then let's add some targets for [42:23] if I win it raw by the way, makes 9% with nothing which is really good for your short strategy but so that's basically you're getting short and then getting out of your short when a buy signal comes [42:36] so anyway right so let's add a little bit of risk management on here so for this I'm going to put stop loss equivalency 3% of the price [42:48] as at the close of a buy or sell signal and then I'm going to take profit target TP is equal to 5% so that's 0.95% times that price and if I just run that you can see it does really really well [43:06] so you're in the market 10% of the time you make 34% your drawdown is tiny 39 trades with a short strategy I mean just getting 50% of those right is really good so we're not too [43:21] worried about the low win rate here our worst trade because we've got stocks in place it's only 3.3% and our best trade probably due to a gap would be 8% [43:33] so really good strategy 34% made and although that seems small through a compounding return trading strategy anything you can make on the short side adds pretty well [43:45] you know handsomely to your overall return when you add in the longs so that's the short side let's now do the long side and for this one all we're going to do is put a take profit target because the strategy seems relatively safe he says we're going to [44:01] just have a take profit target and that would be equal to 1.05 times price effectively a 5% stop, a 5% profit target based on the price at the close of a buy or sell signal. [44:16] And then if I run that now, you can see in the market 77% of the time, so massively reduced your risk. But you beat the market handsomely. You've got 115 trades, which is now enough to give you some statistical relevance. [44:33] Obviously, the more you have, the best, but you can have a bit more confidence in your strategy than if it only had sort of 40 trades. The win rate is good, 65%, best trade 16%, worst trade 8% and the drawdown is far lower than the market actually gave Europe the last 10 years so a [44:49] passive strategy would beat that easily. So really good on the trading side in my opinion really, but also you could use it on the investment side as well. And then if you notice one of the [45:01] level anomaly here so you can see the best trade here is 16% so how can that be if we've got profit targets here at 5% and the reason is is because your your profit targets are set on the close [45:17] following that signal so if i if i want to have a look at that so if you this is all the data that you can pull out of your stats variable here so what's this going here is like your summary but [45:31] if you notice down at the bottom here we've got trades and you can get a list of your trades um and that's actually how we create the profit and loss graph so we'll do that now so if you think they create a variable called trades and that is going to be equal to our stats [45:51] i'm going to take this trade function bit here right there we go so i'm going to take this bit here and so we go to my stats and isolate this data here and then save it in a parameter called [46:05] trade so if you want to have a look at what's in trades and just run that now and what it's giving you is a list of all the trades that the strategy places so you've got your entry bar but also your entry prices and what the stock losses and take profit targets were and as that the trade being [46:22] opened and then what your p and l is in monetary value and also the return of percent value duration all the rest of it so okay so you've got all that there and what we're going to do is filter that data to look for our best trade so we can have a look at that on the uh chart so yeah so [46:41] create a new variable called best trade and what i'm going to do is just sort the values in uh this trade list that you see before and what I want it to filter for is the this here [46:56] and there's a couple ways you could do this but return percentage I'm going to get it to filter for that so I'll take the return percentage and what I'm going to [47:08] do is sort it so ascending equals false which basically means put the highest side at the top and you could do it to filter for a specific value so anything above 10% [47:23] or anything you know that kind of thing we could have done it that way but this is fine and then from here we're going to use the dot i want method and just return the first value so that would be in this case zero on an element now if i want to have a look at what's in [47:40] my best trade variable that I've just sorted from this data you can see that it uh so it entered at 167 but um closed at 195 and that's how we got our 16 return so what we're looking for is bar 928 [48:00] exit bar 945 okay so if I go and have a look at my chart now and just amend this to 900 and display the next 50 bars and what I'm looking for is probably [48:16] let's have a look at what the prices are yeah it's going to be that isn't it okay so this is the I think this is the one isn't it the ascending one so you can see that we closed at 185.36 and 5% above that is the 194.95 target that we gave it but we gapped down the next day [48:35] to open at 167.83. So if we now go and have a look. Yeah, there you go. So that's why even though you've got profit targets at 5%, [48:47] you end up with a 16% win. And that can go against you. So I'll leave this in here if you want to have a look at it. But the next job then would be our profit and loss graph. [48:59] And normally what you'd do to get that is create that trades list that we already created. so I don't need this line but I'll take everything up to it so let's do profit and loss and [49:11] I'm just going to paste that in all this all we're doing now is creating a cumulative P&L from our P&L data if you remember we had the actual profit and monetary value of wins or [49:24] losses and that free trade there so what we're doing is taking the value in that column and creating the cumulative sum and then plotly is going to render this on a chart the only thing i'm not going to have is this if you want to save your chart image and keep that line and run it [49:40] it'll save where you have your codebook stored so but effectively that's our p l chart it's really good because i don't know if you've spotted it but um the queue is up after 40 percent [49:53] drawdown 2021 into 2022 and this strategy just kept going you know and so trades really well in the downside and um that'll be our also index i think helping and then um not so good in the [50:08] real rocket you can't tell you it's gonna be a bit choppy concept but once it got going and avoided all the worst parts definitely and rocketed a really good strategy please with that one and then let's go to Monte Carlo simulation so I'm just going to grab all the section here [50:27] again I don't need that trade one because I've already created that in the code above so all this is doing is similar to the P&L graph is creating a cumulative return so rather than [50:39] looking at the monetary you know win or loss values for each trade taking the percent return creating a cumulative return and then this part is the key bit it's creating a loop that effectively randomizes the trade order and it does it 100 times and then through each of those [50:57] randomized trade order it creates an equity curve and then it's displaying them all on one shot so if I now get rid of that I don't want to see the chart image so if I now run that you get a really [51:10] nice bottom left to top right profile pretty steep but the key thing is what you want to see is really is all the lines to be together you don't want a lot of variance so what the monster is designed [51:24] to do is show you what would happen if all your losing trades came up as well and in our case our worst simulation you were down 24 after 14 trades before it then started to get going so overall [51:36] really safe or seems to be really safe um could you use a better leverage maybe but if you did you would be wise to put a stop loss in even if it's a lease one but you'd be wise to put a stop [51:50] loss on the buy side so once you've got everything covered then yeah you know why not but i'll add something anyway because it's going to rip it higher and don't forget you get more on the investments so if I did 2 to 1 here on the trading part of it so and hit there [52:08] you would end up with close to 5,000% over a 10 year period so really good strategy really like this one and the thing I like about it the most is that it does [52:21] well on the short side so it actually makes money on the short side decent but it It has done really well in the bear markets. So, excellent. If you have any questions about this one or any others, [52:33] email info at bubble-analytics.com. Otherwise, I'll leave it there and I'll see you next week. Thanks, Al. Bye-bye. [52:56] you