TradingView Pine Script Tutorial 9 – Creating a Strategy

TradingView Pine Script Tutorial 9 – Creating a Strategy

YouTube Video

YouTube Video Transcript

[Music] hey this is David for peak bits and this is our ninth video in the scripting tutorial series and we are finally on the topic of creating a strategy now what we’re going to be doing is we’re actually going to be converting our indicator that we’ve been working on into a strategy and having it place orders and create positions for us and then we’ll take a look at the back testing and a performance report of the strategy so let’s go ahead and get started now the first thing I’m going to do I’m actually gonna hide myself here okay so now you can actually see a little bit better I know the text is kind of small my apologies for that but the scaling on the zoom here doesn’t work real great so if you’re on a phone or something seeing zoom in otherwise I recommend really watching on a desktop where you have higher resolution and you can see these things a little bit better anyways now the first thing we did to convert this to a strategy as we change our original function which was study into a strategy and I’ve got the code and the reference manual pulled up here so we can take a look at what a strategy is and what all the different parameters are for and what they do we’re not gonna go over all of them we’re just gonna touch a few for now so you can get an idea of what’s going on but before we get too far into it that’s the main takeaway you change your study to a strategy and then we can play around with a few things you can see on the charts we have our entries and our exits the entries are blue the exits are this magenta purple kind of pink color now once you have the strategy you can open up the strategy tester and here you can see a performance summary of your strategy so with the default information and what I’ve put into the strategy these are the net profit and as of right now it doesn’t include fees but we’ll get to that later there’s also some things you can compare it to gross profit gross loss max drawdown other information you can find important for your back testing including Sharpe ratio a lot of great information it also includes your list of traits now keep in mind that when you’re working on smaller resolutions or time frames like 15-minute candles there is a limit to how far back the back-testing goes now I’m just looking at the BLX symbol which has the longest history of data but it only has daily candles so the back testing can handle the entire history of Bitcoin trading with that so let’s actually get back into the code you can actually see our changes here with our strategy what it is done now let’s get into the actual changes here now we also had a short title and a title with our study they also use overlay with that but what we are doing now is we are adding a few new parameters we are adding what’s called pyramiding a default quantity type and a default quantity value so let’s take a look at what those are pyramiding is the maximum number of entries allowed in the same direction okay we’re just gonna be going long so we really only want to keep track of one entry with this particular strategy if you wanted to do more than one then you can you just have to input the value in pyramiding now I can show you in a little bit the impacts this causes with our current strategy we’ll leave it how it is for now we also had our default quantity type which we chose the percent of equity and I sent to 100 because we’re gonna be trading with 100 percent of our equity based on what you put into the backtester and then for the default quantity value was that the 1 excuse me a default quantity value that is the percent of equity that you’re using so that is the 100 percent of equity so we tell it what we’re gonna use here and how much of it we’re gonna use here and that kind of takes care of things now let me pull myself back into the screen here okay let’s continue looking at this right now we can see our list of trades let’s go back to our Pyne editor and let’s see how changing the pyramiding impacts this let’s set that’s a 3 this will give us up to 3 entries I have to wait for the change in the strategy to be compiled and applied so this will take just a moment okay there you go there’s a lot more in trees now so let’s take a look at our strategy tester you can see there are a lot more trades our performance we have a very high net profit and we also on the buy-and-hold return that is if you were to buy at the beginning of the back-testing period and hold it and that’s your returns so if you bought Bitcoin at the very start here which has been very very small amounts and sold it at the current candle that would be your return percentage and this is what you would have got if you played the markets since it began now obviously this has been highly volatile so these numbers are gonna be absolutely incredibly large compared to a lot of the other things you’re looking at especially comparing anything to crypto over the last few years you can get wild swings and the numbers can be very high here especially if a good strategy so one thing I forgot to show you earlier is actually the overview and this gets posted on the script itself and for some reason I haven’t updated here but as you can see let’s let this load underneath the description you see the performance overview as well let’s go back to our overview here and see our equity is the blue line and our drawdown is the red line and you have some other information here that you’ve already seen on the other page but it’s just a nice kind of dashboard or summary for your back testing now let’s go back to our list of traits now one of the issues I had with the pyramiding is when we change it to three and we allowed multiple entries is that a particular entry was closed by a different exit okay so these aren’t really positions that are managed with like a unique ID these are just entries so it doesn’t matter what caused them to open anything can close them if a exit is called so let’s take a look at an example here the reason we entered along here at number two was because we had the MA one crossover ma three and with our default settings that is a 50 over 200 s ma cross and it exits exited excuse me it closed the entry on a one over two now it says one over two but it was really the one crossing under the two and the reason it says that is because in the script you’re actually supposed to use an ID and actually let’s go look at the reference here because this will make a bit more sense so when we close you’re technically supposed to be able to use an ID of the identifier for the entry so if we had entered if we had entered with a one over two based on the documentation it should close when you have a one over two as the ID or the identifier for your clothes but for some reason it doesn’t work like that I’m not sure if I’m just not reading the documentation correctly or what but you can see that with the identifier being the same it actually closed one of a different type now this can cause issues when you’re back testing goes back before one of these occurred so if we started our back testing all the way back here and we were already if we it the first thing that happened was we were already above and the first thing that happened was across under then if you had an entry for any other reason it would have closed it and that’s what you’re seeing here is we had our 1 over 2 and it closed the very first one over to here but then our second position was a one over three but our next close was actually a one under two so it ended up closing the second position because the second closed was because of the one under two it’s a little bit confusing but essentially based on how its set up I don’t believe there’s much order to it but that’s why I decided to keep the pyramiding at one and we just focus on one entry and it doesn’t really matter to us what causes the entry or the exit as long as there is a crossover now I have added settings in here to take care of that and we’ll get to that in just a moment to where you can make it to where there’s only one crossover that you’re looking for let’s go ahead down now I did make a change to how the labels handled crossovers we had to separate the logic out for detecting the crossovers so we did that here they have to be visible and this is just kind of optional you don’t actually have to have them visible on your chart to do this you would have to change the code in order to enable that but for me to trigger it in this situation I was checking that they were visible and the crossover did occur and we were storing that in that variable then if we wanted to show the crosses and I have it disabled and let’s actually show you what that would do you can actually see the exes and the crossovers now because of that but we can still trade even if those aren’t on thanks to this change here I just wanted to point that out that’s not a huge change it didn’t impact any functionality but if you’re following along you might want to go back up and check out how we’re doing this and if you’re not too familiar with coding this is basically a conditional statement and if they are all true because we’re using and here it will return a true value here and when we use it to check for conditions here it’s going to return a true there as well if it was true what we checked it to the other conditions now there’s really not a whole lot of code involved to this we’re checking now for when we should do our entries now the entry for a one over two crossover requires that we have our crossover and that we have something called trade ma one two crossovers set to true as well now what is that this is actually another setting we’ve added and I’ll scroll back up in just a moment to where you can configure it whether or not you want to trade the specific crossovers this is just another example of adding options into your code so you saw the example with the pyramiding how we can have multiple entries and it didn’t matter which one closed it well if we only wanted to trade a particular crossover we could just uncheck these or let’s say we just wanted to do 250 over and under the 200s ma that would be the ma 1 3 okay you can see it crossed over the 200 here and all the way over here it crossed under and that’s when it exited that probably wouldn’t been a great time to accent you would have liked to have exited over here somewhere but this is just an example there’s a tutorial you can see that the logic dictates that that’s when it should have closed and it did now I wanted to show you that these were just other settings like we had for our indicators before and you can see that is the case it is exactly like the check box for show ribbons except we have just changed the name slightly to correspond with what trades they would impact and let’s just take another look at that here by default they are all on as I said this is just a tutorial so these things may not be perfect but they do show you how things work now let me see if there’s anything else here that I wanted to point out I did I did want to go over the entries and exits we kind of mentioned this but let’s go look into the code a little bit more we talked about these conditions already and how they had to be true of course to execute but there’s also the strategy entry and the strategy clothes that we need to look at now I’ve already mentioned that they have an identifier and when you close you can also pass in the identifier here to attempt to close that entry but from what I can tell that hasn’t been working so keep that in mind as I’ve already mentioned the clothes may close one with a different identifier now if you know the correct way to do that and I’m reading the documentation incorrectly and I’m expecting the wrong thing to happen here please let me know and we can go back and correct that in a later video but for now we’re gonna continue on with our entry we need to specify the direction and that is going to be a long in this case let’s pull up the reference manual and look at the entry you can see you can enter a long or a short and you can use the strategy type I believe it was yes strategy got long it just returns a true value if you’re true you’re going long if you’re false you’re going short and you can see that here in their documentation now when you pass in this third variable here that’s actually a quantity and that variable is missing from mine because if you hard code that in as one it means you will trade one contract or one percent of your equity however you have your back testing setup it will just use one every single time you trade instead of using the parameters that you’ve set so keep that in mind that’s why we don’t have it here and then I’ve also added a comment as well which really it hasn’t really done anything as far as I can tell perhaps it shows up in the back testing results somewhere for something important and then with the close let’s look at the reference manual again go to close now they close is set to where as I said you’re supposed to be able to use an identifier to be able to close an order by referencing its identifier perhaps when it says close an order it’s just referring to an open order so if we were using limit orders instead maybe it would have closed the limit order instead of closing the position but I think that’s what the cancel is for if you can correct me that would be great but from the documentation that I’m reading that’s what it means now back to close you can also set your conditions in the statement and this also applies to the entry as well you can see both here the entry and the clothes have conditions on them so if you wanted to declare strategy got close and then put the conditions in it you could have but we’re checking for the conditions here because if we want to do any other logic we can do that here as well as opposed to fixating the logic within this one line of code this way we do it we can open ourselves up to doing more code at the same time if we need it to as if right now we don’t but maybe later on that’ll be useful so I keep that in mind when I’m coding things I like them to be flexible and if we put our condition within the closed statement then it’s not quite as flexible and we might have to go back and change it again anyway now continuing on I did mention that this is a long only using market orders and you see there is an option to use limit orders and there are several different options for strategies on how to place stops and use different types of orders and there are even options to configure Commission’s with the back-testing and we’ll get into all of that later on just the fact that we are omitting the limit parameter on the strategy means that we are using market orders by default now if you specify on your entry anything valid on the limit perimeter it will use the limit orders now if you do the same thing for your clothes it’ll do the same thing there so just keep this in mind and always look at the reference or the reference manual I should say to get an idea of what you are able to do there’s definitely a lot just related to the strategy itself now let’s go back and take another look at the back testing just to give you an idea of what’s going on here and I know we’ve covered it some but we’ll go over it again because we’ve changed some of the settings we are only trading crossovers on em a one over three and under three so we’ve only had a handful of these on the daily chart in the entire history of Bitcoin you can see the first one was on this particular date February 10th 2012 and there was not an exit until April 10th 2014 so from here to here and you can see our second trade July 13th to September 16th 2014 and so on because there was a really good one here that was the 2/7 2017 bull market and yeah that was a nearly three-year trade two and a half years at least and one other thing that I wanted to point out that was very important with the trades and the back testing is that we currently have an open position now the back testing is really good except I believe it is missing out on calculating our profit here and this is something that you’re having to deal with when you’re actually trading is that if you have an open position it is either in profit or not so when you’re looking at this here you perhaps do have that drawdown from when your entire percentage in is now down to here so you went long here your peak profit of that trade would have been up here and you’ve been losing profits ever since then and that’s really not captured here and [Music] that’s pretty critical also when you’re looking at the performance summary because when you’re looking at buy-and-hold returns this includes the entire price history or the maximum amount back that it will go the maximum number of candles back it will go but the net profit is limited because it also doesn’t include our profit here we have a trade that’s started let’s go back to the list of trades we have a trade that started at five thousand five hundred and thirty six and the current price is that the video is eight thousand two hundred nineteen so that’s a pretty good profit but it isn’t included in our profit summary so it’s kind of hard to compare these numbers if you’re doing shorter timeframes lower resolutions then it’s probably gonna be more accurate because you’re in and out more often on your trades but when you have a trade open for several months and there’s huge price changes it can impact the results in the performance summary to make you think that perhaps your strategy isn’t quite as good as it really is so once we would calculate the actual amount in here then perhaps it’s better than to buy and holder turn now one way to do this would be to on the last candle of the backtest just tell it to close any open positions and that would tell you where you would be at if you closed them all today but at the same time you also want to know if you have any open positions as well plus if you were to use it to do the trading with the strategy then you also you wouldn’t want all your positions to close immediately because it’s the the current candle is the same one that it opened on or on the next candle when there’s another one that I can process but anyway we’re gonna take a look at limit orders and the stops Commission’s all sorts of things if you have any ideas for what you want to see here please let me know leave a comment on the video if you liked the video please like also please subscribe if you like these videos if you like these types of videos please like I also do other things on the channel crypto related so please subscribe and thank you very much and have a great day [Music]