Good morning
I need help to code an second entry after the first position is “x” points in minus. The sl of the second position must be the same as that of the first position.
For example:
1. position: SL 200
2. position: entry at -150 / sl -50
Could anybody help?
JSParticipant
Senior
DefParam CUMULATEORDERS = True
StopDistance = 200
If LongOnMarket and TradePrice(1) - Close > 150 Then
Buy at Market
Set Stop pLoss StopDistance - (TradePrice(2) - TradePrice(1))
ElsIf ShortOnMarket and Close - TradePrice(1) > 150 Then
Sell at Market
Set Stop pLoss StopDistance - (TradePrice(1) - TradePrice(2))
End
JSParticipant
Senior
Not “End” but “EndIf” (last line)
Bear in mind that the stop loss is NOT separate for each position entered. It is calculated on PositionPrice (the average price). So there will only be ONE stop loss. If it’s hit, the first position entered will lose more than 200 pips and the second one will lose less than 200 pips. In the end you will suffer a 200-pip loss, anyway.
@JS
Thank you very much.
@robertogozzi
Thats true. In case of SL, i will catch the first entry with SL 200 and the second entry with SL 50. In total 250 SL
is this correct?
If LongOnMarket and TradePrice(1) - Close > 150 Then
Buy at Market
Set Stop pLoss StopDistance - (TradePrice(2) - TradePrice(1))
endif
// short omitted
As far as I know,
the buy order is executed at the start of the next candle. So TradePrice is not updated yet.
Pls find attached two backtests. Left side is the origin one, right side ist the version with the second entry. But something went wrong. I dont see the second entrys in the orderlist and SL is to high.
Thats true. In case of SL, i will catch the first entry with SL 200 and the second entry with SL 50. In total 250 SL
Not exactly, it’s always 200 pips from POSITIONPRICE (the average price, see also my pic). Example:
- first LONG entry at 15600 – PositionPrice = 15600) (initial SL at 15400) – if hit, the position will lose 200 pips (200 pips per position)
- second LONG entry at 15520 – PositionPrice = 15560,(15600+15520)/2) (updated SL at 15360) – if hit, the 1st position will lose 240 pips, while the 2nd position will lose 160 pips (so it’s 200 pips per position)