Hi Guys,
I am looking to code some type of scalping, limited time exposure to market (5mins on a 5 second chart) the below code appears to be correct to me but when i run it the max time with it being a loss is still open. is there something wrong with the code?
note i am running the SL code from forums as well & have a sell for TP & loss.
any help is appreciated,
//trailing stop function
trailingstart = 3.5 //trailing will start @trailinstart points profit
trailingstep = 1 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
extrapip=2
if ONMARKET and barindex-tradeindex(1) > 60 and close > (tradeprice(1)+extrapip*pipsize) then
sell at market
endif
extrapip1=2
if ONMARKET and barindex-tradeindex(1) > 60 and close < (tradeprice(1)-extrapip1*pipsize) then
sell at market
endif
Lines 40-48 should read:
extrapip=2
if LongONMARKET and barindex-tradeindex(1) > 60 and close > (tradeprice(1)+extrapip*pipsize) then
sell at market
endif
extrapip1=2
if ShortONMARKET and barindex-tradeindex(1) > 60 and close < (tradeprice(1)-extrapip1*pipsize) then
Exitshort at market
endif
Your trailing stop is so tight that I am afraid it works only when backtesting.
It doesn’t account for the minimum distance required by IG.