Hi everyone, I have been trying to get my code right for bringing my stoploss to breakeven, but it seems to be doing all sorts of weird stuff.
I was wondering if someone could double check my code and maybe correct it for me.
I use the ATR to give me values for my stop loss and breakeven level.
The first stoploss is set at 1.5 x the ATR.
Once the price moves in my favour by 1.0 x the ATR I want to move my stop to breakeven, but I want to use the original ATR value that was used when I entered the trade.
Ignore the indicator that I use, that was just there for the test.
Really appreciate any help! Thanks!
//--------------------------------
DEFPARAM CUMULATEORDERS = FALSE
IF NOT ONMARKET THEN
NEWSTOPLOSS = 0
MYBREAKEVENLEVEL = 0
ENDIF
//----TIME VARIABLES FOR LOWER TIME FRAMES----
GOODTOGO = 0
STARTTIME = (TIME => 075000)
ENDTIME = (TIME =< 160000)
IF STARTTIME AND ENDTIME THEN
GOODTOGO = 1
ENDIF
//----------------VARIABLES-----------------
//-----------------ATR INDICATOR-------------
MYATR = AVERAGETRUERANGE[14]
FIRSTSTOPLOSS = MYATR[1] *1.5
BREAKEVENTRIGGER = MYATR[1]
IF FIRSTSTOPLOSS < 6 AND FIRSTSTOPLOSS > 0.1 THEN
FIRSTSTOPLOSS = 6
ENDIF
IF BREAKEVENTRIGGER < 6 AND BREAKEVENTRIGGER > 0.1 THEN
BREAKEVENTRIGGER = 6
ENDIF
//----------------INDICATOR TO TEST-------------
MYAROONUP = AROONUP[14]
MYAROONDOWN = AROONDOWN[14]
//-----------------CONDITIONS----------------
IF MYAROONUP CROSSES OVER MYAROONDOWN THEN
SIGNAL = 1
ENDIF
IF MYAROONDOWN CROSSES OVER MYAROONUP THEN
SIGNAL = -1
ENDIF
//-------------RISK AND MONEY MANAGEMENT-----------
CAPITAL = 10000
EQUITY = CAPITAL + STRATEGYPROFIT
MAXTRADERISK = (EQUITY/100) * 2 //2% RISK
PSIZE = ROUND(MAXTRADERISK/FIRSTSTOPLOSS)
//---------------ENTRIES AND EXITS-------------
IF NOT LongOnMarket AND GOODTOGO = 1 AND SIGNAL = 1 THEN
BUY PSIZE CONTRACTS AT MARKET
MYBREAKEVENLEVEL = BREAKEVENTRIGGER
ENDIF
// Conditions to exit long positions
//If LongOnMarket AND YourConditions THEN
//SELL AT MARKET
//ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND GOODTOGO = 1 AND SIGNAL = -1 THEN
SELLSHORT PSIZE CONTRACTS AT MARKET
MYBREAKEVENLEVEL = BREAKEVENTRIGGER
ENDIF
// Conditions to exit short positions
//IF ShortOnMarket AND YourConditions THEN
//EXITSHORT AT MARKET
//ENDIF
// Stops and targets : Enter your protection stops and profit targets here
SET STOP PLOSS FIRSTSTOPLOSS
//-----------------STOPLOSS TO BREAKEVEN CODE------------------
IF LONGONMARKET THEN
IF NEWSTOPLOSS = 0 AND CLOSE - TRADEPRICE(1) >= MYBREAKEVENLEVEL THEN
NEWSTOPLOSS = TRADEPRICE(1) + MYBREAKEVENLEVEL
ENDIF
ENDIF
IF LONGONMARKET AND NEWSTOPLOSS > 0 THEN
SELL AT NEWSTOPLOSS STOP
ENDIF
IF SHORTONMARKET THEN
IF NEWSTOPLOSS = 0 AND CLOSE + TRADEPRICE(1) >= MYBREAKEVENLEVEL THEN
NEWSTOPLOSS = TRADEPRICE(1) -MYBREAKEVENLEVEL
ENDIF
ENDIF
IF SHORTONMARKET AND NEWSTOPLOSS > 0 THEN
EXITSHORT AT NEWSTOPLOSS STOP
ENDIF
I have just noticed that i forgot to add
SIGNAL = 0
at the top of the code in my variables section and that line 101 needed a “<=”.
Other than that can anyone spot any problems with this code?
Thankyou
Move lines 27-28 to just after BUY at line 64 and SELLSHORT at line 75.