Hi, does anyone knows why my TS even if has not SL o TP it does some exit in the middle of the bar and not exactly at the start of it?
Some example in the attached picture… I use IG Market demo account to test. [attachment file=8886]
The code is the supertrend that I have posted in the library:
//-------------------------------------------------------------------------
// Codice principale : Supertrend
//-------------------------------------------------------------------------
defparam cumulateorders = false
//PARAMETRI VARIABILI
OraInizio =8
OraFine = 18
numerocontratti = 1
//PARAMETRI FISSI
mm = 10
BB = 25
ATRvolaDown = 15
ATRvolaUp = 25
ATR = 14
x = 2.5
supertrendLow = 3
SupertrendUp = 10
EMA=exponentialaverage[mm](close)
BBmiddle= (BollingerUp[BB](close)+BollingerDown[BB](close))/2
ora=currenthour
condizioneday= ora > OraInizio and ora < OraFine
condizionevola= AverageTrueRange[ATRvolaDown](close)>AverageTrueRange[ATRvolaUp](close)
// Condizioni per entrare su posizioni long
IF NOT LongOnMarket and condizioneday and Close < Supertrend[supertrendLow,SupertrendUp] and close > ema and condizionevola THEN
BUY numerocontratti CONTRACTS AT MARKET
stopprice=AverageTrueRange[ATR](close)*x
ENDIF
// Condizioni per uscire da posizioni long
If LongOnMarket AND Close > Supertrend[supertrendLow,SupertrendUp] and close < BBmiddle and close < EMA THEN
SELL AT MARKET
ENDIF
// Condizioni per entrare su posizioni short
IF NOT ShortOnMarket and condizioneday and Close > Supertrend[supertrendLow,SupertrendUp] and close < ema and condizionevola THEN
SELLSHORT numerocontratti CONTRACTS AT MARKET
stopprice=AverageTrueRange[ATR](close)*x
ENDIF
// Condizioni per uscire da posizioni short
IF ShortOnMarket AND Close < Supertrend[supertrendLow,SupertrendUp] and close > BBmiddle and close > ema THEN
EXITSHORT AT MARKET
ENDIF
// Stop e target: Inserisci qui i tuoi stop di protezione e profit target
set stop ploss stopprice
It might be your stoploss that exit trades in a middle of a bar.
Also you have set your stoploss with pLOSS which need “point” values and not real digit ones like your “stopprice” variable which is set among ATR.
I will try to change the pLOSS but the variable Stopprice cannot change in the middle of a bar. It is the ATR of Hour timeframe. The value of it change at every new bar. Cannot change in the middle of it. In the backtest it works properly…
Thanks for help Nicolas! I always appriciate.
David