TradingView Pine Script Tutorial 10 (A) – Take Profit & Stop Loss

TradingView Pine Script Tutorial 10 (A) – Take Profit & Stop Loss

YouTube Video

YouTube Video Transcript

[Music] hey this is David for big bits in this video our tenth video in the pine script development tutorial series we are going to talk about percentage based profits and stop losses so these are actually going to be market orders with the strategy and if you’ve been following along you’ll notice I’ve cleaned up the strategy quite a bit it’s very basic if you’ve just joined in this video it’s gonna look kind of dumb because the strategy is just worthless when you’re looking at it but that’s because we’ve made it highly flexible and this is just an example for the tutorial series now you can see the strategy has it by when we have the crossover of our first moving average over the second one and we’ve added in the new feature of the take profit or stop-loss which had it exit here with a stop loss of 15% now I get into how that works again in just a minute but we also had a buy here and it continued to sell with our old strategy settings of crossing under the second moving average as well so we’ve managed to add in our take profit and stop-loss alongside of our moving average crossovers that we use to also potentially exit the trades now if all this sounds confusing in your head and watch the previous videos it’s probably a good idea to go back and maybe even start from the beginning so let’s continue on and let’s take a look at how this is actually done now the only difference we’ve made in our actual strategy entries or exits is we’ve added a new line in every condition where we had an entry we placed an exit and what the exit does it essentially triggers a market order once your profit gets hit or your loss gets hit now these are all different parameters in the exit function and let’s take a look the first one is profit or lot the profit or loss excuse me and that’s the text you’re gonna see here on the strategy itself when it prints on the chart and we’ve associated that with the one over to entry ID and we have created a new called profit target and a new variable called lost target these are used to calculate the percentage differences so that we can actually get the right numbers for it to exit at no matter what we are looking at now I’ll go over how we calculate those in just a minute but let’s go over how the profit and loss targets actually work when you’re working with the exit now keep in mind we are just doing market orders there is an option to use a limit order to take profit it’s not a big deal to me to use the market orders personally I don’t trade very often with strategies might look at a daily chart and trade on strategies so there’s probably not gonna be a lot of trades going on for me so the market fees aren’t that big of a deal but if you’re interested in using limit orders to trade shorter timeframes – and in most cases you get better fee rates on limit orders so you might want to consider using limit for your take profit instead of a market order but we’re going to use market and when we do this we have to give the parameter a value that it expects and that value is based in ticks now what a tick is that is the interval between prices shown on the chart and let’s go back to our chart here you can see the interval on our chart here is based in pennies since we’re using the US dollar as our base currency here so each tick is point zero one dollars or one cent now if we wanted to set our take profit exactly one dollar above where our entry was we would have to use 100 ticks so to do this with percentages we have to calculate it and the same thing goes for the loss parameter it’s also calculated in ticks now it’s important to note that the loss isn’t really a negative number that you’re working with the the system knows that the loss is a negative number the measurement in ticks is just that it’s a measurement so it’s gonna be a positive number so if you’re going down you don’t have to use a negative tick number it’s just a measure of distance downward and the system knows that and I’ll show you how we calculate that in just a moment but before we do the catch that’s right here I’ll explain that all much better in just a moment but let’s take a look we had to add some new inputs here we added our take profit percentage and our stop loss percentage we have the minimum value at point zero one and once again that’s because they have to be positive numbers even though you’re setting a loss below where you enter and I set the step size which is the interval between the values on the settings to point five we’ll just half a percent if you need to tune that down you can just change it to 0.25 or 0.1 whatever you want but let me show you the settings here and show you how this is gonna actually impact the chart now I have some arbitrary numbers of 30 and 15 percent said it’s the profit and loss respectively and this probably won’t work for most people it probably wouldn’t even work for me but for our example purpose here I’m going to change the stop-loss percentage to 15.5% and you should see that this actually moves down to the next candle because the percentage down increased now you can see I just kind of proved my point that the percentage increases on the stop-loss means it goes down further and you don’t have to use a negative percentage on tradingview like that so I’m just gonna save our changes to our settings there I’m gonna go back down let’s look at our calculations for how we calculated how many ticks we would need now the profit target this is a pretty straightforward calculation we have our clothes times our percentage divided by how many ticks we have now let me explain this and I can actually kind of show you through some of the math here our take profit percentage is 30% now we have to divide that by 100 so we get the actual value we need to multiply the close by so if it’s 30% and we have the number 30 we have divided by 100 we now have 0.3 okay if the price were say $10,000 and that was the close we would multiply that by 0.3 which is $3,000 now if we were to just use 3000 ticks and I remember the ticks are point zero one on this chart so if we did 3000 of those that would only be 30 dollars up for our take profit and that’s not what we want we want 30% based on the example which would be 3000 so we divide that 3000 by the minimum tick size by that point zero one and now it’s I believe 300,000 so now it’ll actually place the product the the take profit up three thousand dollars at 13,000 and similarly we do the same exact calculation for the Lost Target we just use the stop-loss percentage instead in that calculation so there you have it that’s really all there is to it I’m trying to think if there’s really anything else to do here with the staff lock the stop loss and take profit I really don’t think there is in this video you can see where we entered and where we exit if you have an idea of something you like to see added to this please leave a comment if you liked the video definitely please like it if you like videos like this please subscribe I’m always coming out with new ones related to development Trading cryptocurrency those sorts of things so I would really appreciate that if you would but stay tuned to the next video and you know have a great day [Music]