Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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.
vars:mystoploss(0);
mystoploss=highest(high,4)[1];
ifclose>close[1]then begin
sellshort("SSS")1contractsnextbaratmarket;
setstoploss(mystoploss);
end;
Can you help answer these questions from other members on NexusFi?
I tired this and it still does not work. It basically just makes my stoploss the same as my entry. Please see attached picture.
What have you tried, copy-pasting the example code? Then it would indeed not work. That's why I said:
Jura
Also see this post on the MC forum, and page 90 from the EasyLanguage Essentials manual.
If you look on the page of that manual, you'll see..
Quoting
SetStopLoss - sets a stop loss order at a fixed dollar risk from entry.
(..)
By default these exits operate on a position basis and stop amounts are set in dollars.
Now, look at the example code:
vars: mystoploss(0);
mystoploss = Highest(high, 4)[1];
if close > close[1] then begin
sellshort ("SSS") 1 contracts next bar at market;
end;
setstoploss(mystoploss);
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)[1]. Or alternatively, manually set the stop loss at the price of the Highest(high, 4)[1].
I now understand that its calculated in dollar 'amount' after reading your post and links. Im surprised there isnt a simple way to setstoploss by 'price'.
Do you have any suggestions how i can do it by 'price' without having to set it manuely.
Jura
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:
vars: mystoploss(0);
mystoploss = Highest(high, 4)[1];
if close > close[1] then begin
sellshort ("SSS") 1 contracts next bar at market;
end;
setstoploss(mystoploss);
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)[1]. Or alternatively, manually set the stop loss at the price of the Highest(high, 4)[1].
I now understand that its calculated in dollar 'amount' after reading your post and links. Im surprised there isnt a simple way to setstoploss by 'price'.
Do you have any suggestions how i can do it by 'price' without having to set it manuely.
You could use something like..
vars: mystoploss(0);
mystoploss = Highest(high, 4)[1];
if close > close[1] then begin
sellshort ("SSS") 1 contracts next bar at market;
end;
if MarketPosition = -1 then
buytocover ("XS2") next bar at mystoploss stop;
..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)[1] values), you could use something like the following code:
vars: mystoploss(0);
if MarketPosition = 0 and close > close[1] then begin
mystoploss = Highest(high, 4)[1];
sellshort ("SSS") 1 contracts next bar at market;
end;
if MarketPosition = -1 then
buytocover ("XS2") next bar at mystoploss stop;
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
Jura
You could use something like..
vars: mystoploss(0);
mystoploss = Highest(high, 4)[1];
if close > close[1] then begin
sellshort ("SSS") 1 contracts next bar at market;
end;
if MarketPosition = -1 then
buytocover ("XS2") next bar at mystoploss stop;
..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)[1] values), you could use something like the following code:
vars: mystoploss(0);
if MarketPosition = 0 and close > close[1] then begin
mystoploss = Highest(high, 4)[1];
sellshort ("SSS") 1 contracts next bar at market;
end;
if MarketPosition = -1 then
buytocover ("XS2") next bar at mystoploss stop;