I am having trouble implementing a stop loss in easy language. If someone can help that would be greatly appreciated. Below is the simple code. If i am short a position then i want my stop loss order to be the value of the highest high of the last 4 bars. Thats it! When i look at the results the stop order is not placed at the highest high of the last 4 bars. I also tried using 'stoplossposition' and i get an error.
What have you tried, copy-pasting the example code? Then it would indeed not work. That's why I said:
If you look on the page of that manual, you'll see..
Now, look at the example code:
The SetStopLoss is here set to the value of 'mystoploss', a variable which contains the HIGH of the instrument that you trade. Since that high will be around 1.37, judging from your screenshot, with the example code above, you will exit the position if the unrealized loss is 1.37 dollar for the whole position. Which in practice would be indeed an instantaneous exit after entry, especially if commissions are considered.
Solution: calculate the loss, in dollars for the whole position, that would happen if the position reaches the Highest(high, 4). Or alternatively, manually set the stop loss at the price of the Highest(high, 4).
The following 2 users say Thank You to Jura for this post:
..which submits a stoploss order at the 'mystoploss' level on every bar, as long there is a Short position (see also the first screenshot). That way you can use the price level instead of the dollar amount level.
Btw, if you look closely at the code example above, the 'mystoploss' variable is set with a new value on every bar. So, in a falling market, the value of 'mystoploss' will be lower on each update, since the Highest(high, 4) in a falling market is lower on each new bar. So, the code example above uses a trailing stop.
Good of course is that is what you intend it to do, but if you want a fixed stop-loss (that won't get updated with higher or lower Highest(High, 4) values), you could use something like the following code:
(See also the second attached image)
The following 5 users say Thank You to Jura for this post:
Your a genius! and thanks for solving the issue along with postiing images which totally helped see the problem. I originally did what your first coding showed below, but i was stumped as to why multicharts kept showing the buytocovers and all different levels. Now i realize that it was calculating and changing every bar as you clearly specified, acting more like a trailing.
The second part of the code is exacly what i was in search for. Basically a proper way of a stoploss at a fixed price! I wish the easylanguage getting started maneul and EL essentianls had these complete examples in it. Im sure it would help many people out with the basics on
1.how to implement a fixed stop loss based on price
2.how to implement a trailing stop loss based on price
3.how to implement a fixed stop loss based on dollar
4.how to implement a trailing stop loss based on dollar
The following user says Thank You to guppy for this post: