Hello guys! More code snippets here! So, NinjaTrader offers a default function for you to set your stop size which is:
SetStopLoss(CalculationMode.Currency, 200);
Easy done! Just put this into the OnState.Configure and you are pretty much done and for many cases, this can be enough, however, for most of the cases that require a careful and more controlled approach to stop handling this is not enough at all.
So how do we do this? Before we start let’s go to NinjaTrader control center Tools -> Instruments -> find ES future contract and click edit to see its details.
We are interested in the information about the tick size and the point value. Now before we move into any code let’s see how are we going to calculate the stop based on the entry price and dollar value because these are the 2 parameters we have to work with.
Let’s assume our entry price equals 1000USD and our stop is 200USD. So what has to be the price for a long position if we were to loose 200USD?
It is EntryPrice - 200USD/PointValue which is 1000 - (200/50) = 996USD. How do we check that? Well, tick size is 0.25 points, which means that 1 tick equals 0.25 USD. Also, 1 tick equals PointValue * TickSize which is 50*0.25 = 12.5USD. 4 points is 16 ticks ie. 4/0.25 = 16 therefore 16*12.5 = 200USD.
Now you know the maths behind and you need to kick it in the correct place which is inside the OnExectuionUpdate method. If you have seen some of my courses you know that our entry orders have a name and in order to identify whether the execution update method has been called upon an entry order fill, we have a very easy check done. Please see the code snippet attached.
Once we know we are in the moment of entry we can calculate and set the stop. In the example provided I show how to do that. Please subscribe!