Hi,
So far I had one trailing stop condition which worked fine. Then I added a second condition which works partly.
The short trade was exited after the second Trailing Stop condition was met. However, it created instantly a new long trade which should not happen.
I guess something is not right with the order of the code.
Can anyone help?
Thanks.
Sascha
//if not order on market, reset the breakeven status
if not onmarket then
breakeven=0
endif
//check if the current order has made 2*ATR of signal day
if shortonmarket and tradeprice-close >=2*ATR then
breakeven=1
endif
//put stoploss at open price - 3 pip
if breakeven=1 then
buy at tradeprice-3*pointsize stop
endif
//------When adding this second set of Trailing Stop condition it causes a new long trade. Why?
//if not order on market, reset the breakeven status
if not onmarket then
breakeven=0
endif
//check if the ATR>=39
if shortonmarket and ADX[14]>=39 then
breakeven=1
endif
//put stoploss at open price
if breakeven=1 then
buy at tradeprice-1*pointsize stop
endif
//------
// Conditions to exit short positions
ignored, indicator3 = CALL "PowerX 2"
c6 = (indicator3 > -3)
c7 = (close > SMA50)
IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) and ONMARKET THEN
EXITSHORT AT MARKET
barorder=0
ENDIF
To close a short order, you must use EXITSHORT, not BUY instruction.
Thanks Nicolas.
However, I don’t just want to close the short position but I only want to adjust the stop loss to breakeven – 3 pip when CONDITION 1 is met and to breakeven – 1 pip when CONDITION 2 is met.
The first part of the code (which contains CONDITION 1) you helped me to create and it works fine in ProBacktest.
//if not order on market, reset the breakeven status
if not onmarket then
breakeven=0
endif
// (CONDITION 1)
//check if the current order has made 2*ATR of signal day
if shortonmarket and tradeprice-close >=2*ATR then
breakeven=1
endif
//put stoploss at open price - 3 pip
if breakeven=1 then
buy at tradeprice-3*pointsize stop
endif
// Conditions to exit short positions
ignored, indicator3 = CALL "PowerX 2"
c6 = (indicator3 > -3)
c7 = (close > SMA50)
IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) and ONMARKET THEN
EXITSHORT AT MARKET
barorder=0
ENDIF
I then added the code for CONDITION 2 to it and was hoping it would work. But it now creates a new long position after it closed a trade that met CONDITION 2.
I just want it to adjust the Stop loss.
// CONDITION 2
//if not order on market, reset the breakeven status
if not onmarket then
breakeven=0
endif
//check if the ATR>=39
if shortonmarket and ADX[14]>=39 then
breakeven=1
endif
//put stoploss at open price
if breakeven=1 then
buy at tradeprice-1*pointsize stop
endif
I have to correct myself. I thought that the first part of the code (CONDITION 1) was working, but now when I checked again I realized it is not working.
It produces the same mistake that after closing the short trade when the condition is met, it creates a new long trade.
So I guess the command buy at tradeprice-3*pointsize stop doesn’t work and somehow creates this countertrend trade.
I am confused now. How can I write the code that it changes the Stop Loss to entry price – 3 pips?
Thanks so much for your help.
Problem solved:
I read through everything again and then replaced the BUY instruction with EXITSHORT and now it works.
Somehow I assumed that EXITSHORT would exit the position at market (that was my confusion) but as I use STOP it adjusts the Stop Loss as I wanted.
Thank you again Nicolas.