Combining Strategies in Pine Script with FOR Loops

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the chatting view tutorial today we're going to be doing some pine script together oh yes folks what i really wanted to start with in my pine script adventures with you guys is how to take two strategies and combine them together into one cohesive strategy and the easiest way to do that is to use something called look max so today we're going to be taking the rsi strategy and the macd strategy we're going to be using a very standard macd crossover uh which you can get uh down below you'll see a video from trade rush that goes over it and we're going to be using those rules which is whenever there's a crossover below the histogram the signal line crosses above i'm sorry the macd line crosses above the signal line and it is below the histogram that is a long position now we won't be filtering with the 200 ema today we're just going to be getting those signals and then we're also going to be combining that with the rsi so whenever the rsi crosses up and over the value of minus 30 that is a long position now for a short position we're going to be using just the opposite which is a downward cross here and we'd be looking for a cross like this from the rsi so that's what we're going to be looking for and we're going to be using look back functions to combine the two strategies together so let's get started first of all we're going to open up our macd strategy code or macd code which comes from the public library of trading view now what you can do is just copy all of this stuff right here that is the calculations and the inputs we're going to just copy that directly as it is and we're going to start a new blank strategy we're going to just get rid of all of this except for the very first thing and we're going to call this macd rsi strategy the next thing we're going to do is remove the default margin functions because i really don't want to use any of this except for overlay equals true next thing i'm going to do is create a little area and call this macd strategy calculations and i'm going to paste in the macd then i'm going to just remove all of the plot items because we're not going to use any of these plot functions here or plot variables we're just going to be using the inputs and the calculations now for the macd we have the very simple rules we're going to just start with those so we're going to say macd underscore l is equal to whenever remember the long position is going to be if the crossover is we're in version 5 now so ta.crossover macd signal and macd line is less than zero on the histogram that is the rule for the macd strategy for long positions next we're going to take this and we're going to simply copy it down below and we're going to change this to a macd underscore s for short and we're going to change crossover to cross under and we're going to change the macd being less than zero to macd is greater than zero so now we already have finished our macd strategy calculation the next easy part is going to be the rsi the rsi even easier because you don't actually have to get any calculations for it you can calculate it directly using the rsi function we're going to create an rsi expression right here and we're going to type in ta.rsi and we're going to use close and then rsi length we're just going to say l-e-n for length and then we're going to create an input similar to this one right here and we're going to call this one rsi len for length and this is going to be the rsi length default set to 14 just like that we're going to remove the type equals input.integer because tradingview doesn't use those anymore and in fact we're going to have to change all of the previous ones that use that it's actually input.int now and we're going to just go ahead and change all of these to the new setups so let's do that really quick [Music] and we are done so yes as a version 5 pine script you can no longer use type in a input function instead you use input dot and then the input actual type that you're using which is int float uh source string and there's a couple more as well but that's all there is for that and then we're going to go down here and we're going to then do our rsi long and draw calculations so we're going to say rsi long is equal to ta.crossover rsi 30. so we're going to be crossing over 30 and then for short positions we're going to be doing precisely the opposite at 70 so it'll be cross under 70. and now comes sort of the tricky part which is where we're going to actually combine these two strategies using a for loop and that's going to be where things get a little more convoluted but i promise you it'll all make sense in the end so let's just start first of all we're going to define some variables [Music] and those variables are going to be as follows so we're going to start with some bull variables and those are going to be as follows macd l c and this is going to stand for long check so we're going to check the long status and then we're going to go to here and type in equals false then we're going to do the same thing [Music] macd short check equals false bool rsi long check equals false and bull rsi short check equals false so we've just defined some variables and i'm going to tell you how we're going to use those variables in just a second so the next thing we're going to do is to actually create our for loop so we're going to say this is the loop checker and checks every single bar and we're going to say on a look back period and we're going to have to define that look back period with an input but we'll come back to that so what we're going to do is say 4 i because that's the standard way you say i for index equals 1 to look back and in this case i actually want to say 0 to look back by 1. so what we're saying here is for the index we're going to check 0 to a specific integer and we're going to change this value 0 to the integer by 1. you can always do this by whatever value value you want you could do 2 3 and it would check in groups of 2s and 3s but i want to do it by one i want to check every single bar then we're going to start defining some if statements so we're going to say if macd long [Music] then macd long check change to true now this isn't going to be enough because we have not yet defined what our look back is going to be so what we're going to do is we're going to create two new inputs and those are going to be bull inputs and those are going to be specifically for both the rsi and the macd strategy and it's going to work as follows we're going to say macd lb for look back is equal to input dot bool we're going to give it a title and we're going to say use macd look back question mark as in are we going to use the macd lookback and then we're going to say default value equals false that's it next we're going to copy this because it's going to be easier to do that and we're going to add it to the rsi and we're going to say rsi look back and we're going to say use rsi look back default value is false now what we can then do is create another nested if statement which is going to be macd underscore lb in other words are we using the macd look back well if we are macd l and then this little function here which is a series look back and we're actually going to change that one to an i so this will be if we're using the macd lookback question mark then we want to do macd long in the past we want to look into the past by one to the look back from zero to look back period and we're going to do it by one increment and then if that's not true if we're not using the macd to look back we're going to just say macd l so just the long position and if either of these are true we will return true next we're going to simply copy and paste this for short condition so we're going to put in s s and s and i'm going to move this down below and this will be our long checks and this will be down here our short checks and now all we have to do is copy and paste this one and replace everything macd with rsi [Music] and it's basically doing the same thing it's checking the rsi long and long value it's going to say all right if that's true if we're using the rsa to look back then do rsa long but look back in the past x bars whatever look back is and then if not true then just rsi long at this particular bar then we're going to copy this one and well let's copy this one and do the same thing down here definitely going to be lazy and we're going to change all the l's to s's here and there you have the long and the short checks now i realize this is all a little bit confusing but it'll make sense once we actually plot everything and i'll show you how it's working now the next thing we want to do is say a long in condition and a short condition so we're going to say long condition is equal to [Music] macd [Music] lc and rsi sc so whenever bo i'm sorry lc so whenever the macd long check and the rsi long check are both true then the long condition is valid and we can enter a long position and then we'll do the same for a short position again it's really easy to do the long and short conditions for everything because it's all just sort of inverse of each other now we just change this to s and this to s and there you go so this will be our long and short conditions the next thing we want to do is create our entries so we're going to create entries and we're going to say if long condition and not long condition one bar ago and the reason for this right here by the way is to make sure that we don't constantly enter new long conditions or trigger anything too many times we just want to trigger one time so in other words the very first time you have long condition and it was not a long condition one bar ago that is going to be when it's true because the lung condition can last more than one bar it's very unlikely but it's possible so then we're going to say strategy dot order and we're going to go enter long as our id and then we're going to create a comment long entry and if we want we can even make an alert message enter long order something like that would be fine and then we're going to do the same thing for short conditions and since we won't be using any take profit targets or anything like that in this strategy what we want to do or what we can do is close the long condition whenever the short condition is true and then we can close the short condition whenever the long condition is true so how we're going to do that is essentially whenever we have this like this enter we're instead going to start by doing strategy dot close and we're going to close this order here [Music] and we're going to comment that as close short and i guess we could even put an alert message equals close short order and then just to make it easy again i want to just copy and paste this down below to close the long position so enter long will be closed whenever we enter short and this should make things relatively straightforward now of course you could create take profit targets and all this stuff and stop losses but we're not really going to focus on that today we just want to focus on the two strategies being combined together now what might be really helpful is to plot whenever these are both true or whenever one of them is triggered and what we're going to do now is plot a shape whenever the rsi short condition or lung condition or true or the macd long condition or short condition are true so we can see when the individual conditions are true on the chart so we're going to start by doing plot shape and plot shape has a lot of different parameters and you can see them here we have series title location color offset text color text size whatever there's all kinds of interesting stuff here and we're going to start with just the series so the series is going to be what condition makes the plot shape true and in this case it's going to be rsi long condition and not rsi long condition one bar ago again we want to get just the first instance just like the long condition and short condition we want to get just the first instance of this happening right here next we're going to say style so we're going to give it a specific label style and we're going to go shape and if you type shape and hit control spacebar you'll get a whole lot of options here and we're just going to do a triangle up and we're going to say location equals location control space and we'll do below bar and then we'll do color equals color dot green and that will be our rsi entry so we'll know the rsi is the triangle up then we'll do rsi short condition and instead of triangle up we'll do triangle down and we'll just change the color to red and we'll change it to above bar the last thing we're going to do then is copy both of these settings and then change this to macd and this to macd and then we're going to change this to circle i think is an option yes so we'll use a circle shape to indicate the macd and we'll do again above and below bar and green and red so the macd will be circles and the rsi will be triangles that way we can differentiate them now we're going to save the strategy and let's see if we got all of this right the first time [Music] looks like we did miss some stuff but this isn't the result of my own stupidity this is actually the result of the way that some stuff has changed in calculating smas so this is something i did overlook when we want to create a old strategy into a new one they haven't updated all of the old library yet so i assume that will come with time but for now you can just go through and manually type in ta dot for all of these and that should resolve your issues [Music] so one thing i forgot to do was add in the look back period so we're going to create an input for that i'm going to put this at the very top and i'm going to say look back is equal to input dot int and i'm going to say title equals look back period and i'm going to give it a default value of 10. so 10 bars into the past will be what is default and then we're going to save this strategy and i actually did forget to put in the directions which is pretty sad because that is something that's pretty standard so if you go to strategy.order one of the things you need to tell it is uh strategy dot long [Music] and then for this one it's going to be obviously strategy dot short so i think that might be the last of our problems maybe it appears to be so so we're going to add this to the chart and as you can see there are some entries being made and taken but a lot of them are not too good but we do see our plot shapes occurring i do want to see where these trades happen so let's go to the very last one on the list i want to see where this happened i see yeah so this is one of the rare occasions where both of them happened together so this is what i was talking about you can see how there's a circle overlaid with a triangle here and what that means is that the rsi and the macd were true at exactly the same time and because of this we closed the long position and because they're both short we entered a short position the problem with this as i've tried to kind of outline here is that what we've done is we've forced the strategy to only look at this specific bar and what we want to do is go into the strategy now and we're going to actually use one of them with a look back period and you'll see that we actually change where we enter [Music] now one thing that is happening that probably shouldn't be happening is that we're entering multiple trades at a time and this is definitely a problem and another problem is we're also using contracts and i don't like using contracts i'm going to change that to percent of equity i'm gonna set that to set ten percent and we'll set it to ten thousand dollars by default and we'll go ahead and give it some commission as well i don't actually expect this to be profitable because we haven't really done anything with targets but just to give an idea of what's going on and as you can see here for example we get a long entry position when there is an macd crossover and the rsi crossed over one two three four five six bars in the past and we can actually confirm that by going to rsi and macd and there you have it you can see a crossover with the default settings i didn't well interesting i didn't actually do a whole lot to uh the default settings but right here there is a crossover at 30 which is what we wanted and then right here there oh excuse me right here there is a crossover of the macd line and so we get that crossover confirmed on the next bar because it does wait one bar to confirm it and then this one confirmed in the next bar and therefore we enter long and that is how that works now what we're going to do is change a couple of these settings but nothing too major to really worry about but one of the settings that you can actually change is swapping the rsi using the look back with the macd using the look back so let's do that and see if anything improves and the answer is of course no it really didn't now there is one thing that is actually happening here which you might notice is that we are actually pyramiding orders which is something i don't like to do so we actually see here is that this long entry gets pyramided with this long entry here now while that's not generally a problem some people might want to do that and that's totally fine if you'd like to but if you want to avoid doing that there is something you can add to your strategies which i like to do like this so i will say is long is equal to and these are going to be trade functions so we're going to say strategy dot position size is a greater than zero so if your strategy position size is greater than zero you are in a long position likewise if you are short position your strategy position size is going to be less than zero and if you are not currently in a trade your strategy dot position size will be equal to zero and what we can use these for is for these checks down here and we can actually say instead of not long condition one we can say is not in [Music] or [Music] is short now what we've done here is say if we're not currently in a trade or we are currently in a short trade then it is okay to enter a new long position if we're in a long position it won't be okay now we're going to do the same down here except the inverse which is is long and what that is saying is if we are currently in a long position or not in any position at all and the reason we include is not in is because our very first trade needs to be considered the very first trade will be either a long or a short depending on what the triggers are and if you're not already in a trade of course we can't start trading so we need to have that is not in there to function so we'll go ahead and save the strategy and we'll see now that strategy entry that came from over here no longer occurs so we only enter one time per trade now again we're not seeing fantastic results here maybe the rsi and the macd aren't ideal together that's quite the possibility or maybe it could be filtered out with something like a 200 ema but regardless of that fact what i wanted to illustrate here was not the profitability of this setup but instead how to combine two strategies together i hope that really cleared up some of that for you and taught you something new about trading view pine code and how to use for loops in a really clever way to get two strategies into a look back function together looking for a better way to trade at prism investors we have custom made proprietary trading view scripts available to everyone for no cost you can access all of our demo strategies free forever once you decide you like the strategies and want to upgrade you can purchase our everything bundle as a monthly subscription and gain access to all of our scripts or pay once and have access to the scripts forever frequent updates and new strategies added every month our professional trading view scripts give you ease of automating your trading with advanced money management strategies so you won't blow up your account again visit prisoninvestors.net to learn more
Info
Channel: Prism Investors
Views: 816
Rating: undefined out of 5
Keywords: trading, crypto, forex, tradingview, scripts, forex trading, day trading, cryptocurrency, tradingview tutorial, tradingview for beginners, tradingview indicators, tradingview setup, stock trading, trading forex, tradingview forex, trading view tutorial, learn forex, technical analysis, best tradingview indicator, investing, day trading forex, traders, finance, fundamental analysis, make money online
Id: HXMUsh82GmQ
Channel Id: undefined
Length: 26min 28sec (1588 seconds)
Published: Sun Oct 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.