Hello all,
I need your help.
in a strategy several orders are opened, sometimes more sometimes less.
Now I want to limit the total loss and close all open positions without the “Quit” command.
Greets
Suffi
There are so many possible answers, I’ll start with one …
Are you using below as the 1st line of your code?
Defparam cumulateorders = false
Above will mean – unless code states (?) – you won’t open more than 1.
If you want to post your code then we may be able to help you a lot easier for us and quicker for you.
If you are accumulating orders and you want to limit the loss of the orders basket, you can set an exit stop level at a certain price, by using POSITIONPRICE (which is the mean of all orders opening price):
set the stoploss at mean opened price – 20 points, limiting the loss to “only” 20 points.
//long positions
if longonmarket then
//set the stoploss at mean opened price - 20 points, limiting the loss to "only" 20 points
set stop price positionprice - 20*pointsize
endif
Sell at Market // For Long.
ExitShort at Market // For Short.
That’s all. Unless I misinterpret the situation.
Hello all,
Thanks for the answers.
I can understand Nicolas approach.
I had thought of determining the maxloss in euros.
E.g. if loss< -500€ then close all
I am on holiday at the moment and only have the iPad with me, so I can’t post a code.
Greetings from New Zealand
Hello,
I am back from holiday and can now also post the code.
I have tried the following code but something does not fit.
DEFPARAM CumulateOrders = true
ONCE maxLOSS = 400 //Euro
IF LONGONMARKET THEN
IF ((tradeprice(0)-close)*POINTVALUE)>= maxLoss THEN
SELL AT Market
ENDIF
ENDIF
IF SHORTONMARKET THEN
IF ((close-tradeprice(0))*POINTVALUE)>= maxLoss THEN
EXITSHORT AT MARKET
ENDIF
ENDIF
Your version will check for exit conditions at the end of each bar.
Nicolas version sets a pending order which can trigger during a bar.
Hello,
I can’t get my head around this and I’m trying to explain it again.
Ex.
Position value 1€/point
maxloss: 500 €
1st pos. buy at 10.000 € results in a distance of 500 points for the SL.
2nd pos. buy at 10.100 € results in a buy price of 10.050.
Now I have 2 positions. The SL changes to 250 points, but to the new purchase price of 10.050€.
As more positions I have, the tighter the stoploss becomes from the changing purchase price.
I can’t get the code to work.
Greetings
Suffi
Try below and let us know?
ONCE maxLOSS = 400*countofposition //Euro
COUNTOFPOSITION
Hi,
I hope thats right.
If LongOnMarket THEN
set stop price POSITIONPRICE - (maxloss / (COUNTOFLONGSHARES * pointsize))
ENDIF
If ShortOnMarket THEN
set stop price POSITIONPRICE + (maxloss / (COUNTOFSHORTSHARES * pointsize))
ENDIF
Isn’t below what it should be?
Try yours and mine on Live data on Demo Account.
Let us know how it goes.
If LongOnMarket THEN
set stop price POSITIONPRICE - (maxloss * pointsize))
ENDIF
If ShortOnMarket THEN
set stop price POSITIONPRICE + (maxloss* pointsize))
ENDIF
hello,
this is the result of the code below, it should be correct.
maxloss=300 //€
If LongOnMarket THEN
set stop price POSITIONPRICE - (maxloss / (COUNTOFLONGSHARES * pointsize))
ENDIF
If ShortOnMarket THEN
set stop price POSITIONPRICE + (maxloss / (COUNTOFSHORTSHARES * pointsize))
ENDIF
As more positions I have, the tighter the stoploss becomes from the changing purchase price.
If you not want stop price to reduce with more positions then why are you using maxloss / COUNTOFSHARES in your code above?
If you are happy with the result you show in your screenshot then all is good?