Buongiorno, rilevo putroppo dei comportamenti diversi del bot, lanciato in testing in manuale ad una certa ora ed alla stessa ora lanciato in reale.
In Testing prosegue e va a profitto, mentre in reale dopo poche barre esce. Ho due timeframe ( uno di 5 minuti per il trend e di default per le entrate, entrambi con updateonclose).
La navigazione del trend avviene con un trailing stop che tiene conto della distanza minima per il broker, messo 7 punti.
Nella foto si vede il backtest con il trend che va avanti e dove ci sono le X delle uscite i vari lanci manuali.
Il bot quando esce dal trend esce anche con il Quit, per testarlo all’ora esatta devo tenere queste istruzioni così, altrimenti lanciato in manuale al momento oppirtuno nel trading system ho i valori di Partito invertiti .
if DateTradeTime and time=timeDacuitestare then
Partito=1
else
Partito=0 //// 1 Se voglio testare che funzioni solo nel giorno di test devo mettere qua =0
endif
///////////////////////////////////////
Di seguito il codice del bot:
//————————————————————————-
// 0525_5mUS_BUY_1-Range-Filter
//————————————————————————-
//0525_TREND-Range-Filter-Manual
///////////////////////////////////////////// Range Filter Inizio
//
DEFPARAM PreLoadBars = 6000
DEFPARAM CumulateOrders = False
////////////////////////////////////// Configurazioni
TrailingST=1
once PuoiEntrare=1
once BUYTrendTMADN=0
once SELLTrendTMAUP=0
IF IntradayBarIndex = 0 THEN
PuoiEntrare=1 //abilitare il trading all’inizio di ogni giorno
ENDIF
//////////////////////
timeDacuitestare=090900
DateTradeTime=(date=20251014)
once maxloss=25 //// 10
minmaxloss=25
//////
once Partito=0
if DateTradeTime and time=timeDacuitestare then
Partito=1
else
Partito=0 //// 1 Se voglio testare che funzioni solo nel giorno di test devo mettere qua =0
endif
///
if LongOnMarket or ShortOnMarket then
PuoiEntrare=0 /////DA SCOMMENTARE
else
if StrategyProfit=0 then
PuoiEntrare=1
else
QUIT /// DA SCOMMENTARE
endif
endif
// 0325-Media-Mobile-Triangolare
// — settings
TIMEFRAME(5 minutes,updateonclose)
ftype = 1 //Filter Type (1 or 2?)
rngqty = 2.618 //Range Size
rngscale = 6 //Range Scale (1=Points, 2=Pips, 3=Ticks, 4=% of Price, 5=ATR, 6=Average Change, 7=Standard Deviation
rngper = 14 //Range Period (for ATR, Average Change, and Standard Deviation)
smoothrange = 1 //Smooth Range (0=false ; 1=true)
smoothper = 27 //Smoothing Period
movsrc = 1 //Movement Source (0=Wicks, 1=Close)
//— end of settings
once rngfilt=close
if barindex>max(rngper,smoothper) then
//Range Size Function
rngscale=max(1,min(rngscale,7))
if rngscale=1 then
scale = rngqty*pointsize
elsif rngscale=2 then
scale = rngqty*.0001
elsif rngscale=3 then
scale = rngqty*ticksize
elsif rngscale=4 then
scale = close*rngqty/100
elsif rngscale=5 then
scale = rngqty*average[rngper,1](tr)
elsif rngscale=6 then
scale = rngqty*average[rngper,1](abs(close-close[1]))
elsif rngscale=7 then
scale = STD[rngper](close)
endif
if smoothrange then
r=average[smoothper,1](scale)
else
r=scale
endif
hh=close
ll=close
if movsrc<>1 then
hh=high
ll=low
endif
if ftype=1 then
if hh>rngfilt[1] then
if (hh-r)<rngfilt[1] then
rngfilt=rngfilt[1]
else
rngfilt=hh-r
endif
elsif (ll+r)>rngfilt[1] then
rngfilt=rngfilt[1]
else
rngfilt=ll+r
endif
else //ftype<>1
if hh>=rngfilt[1]+r then
rngfilt=(rngfilt[1]) + floor(abs(hh – (rngfilt[1]))/r)*r
elsif ll<=rngfilt[1]-r then
rngfilt=(rngfilt[1]) – floor(abs(ll – (rngfilt[1]))/r)*r
else
rngfilt=rngfilt[1]
endif
endif
hiband = rngfilt + r
loband = rngfilt – r
if hiband<hiband[1] and loband>loband[1] THEN //// and loband>loband[2] and hiband<hiband[2]
Narrow=1
VisT=10
ELSE
Narrow=0
VisT=120
endif
////////////
if rngfilt>rngfilt[1] then
upward=1
downward=0
endif
if rngfilt<rngfilt[1] then
upward=0
downward=1
endif
//Direction Conditions & colors
if upward then
cr=0
cg=255
cb=0
trendSup=1
endif
if downward then
cr=255
cg=5
cb=131
trendSup=-1
endif
endif
////////////////////////////// AT close STOP ///////////////////////////////
AvCandleTime = (time >= 150000 and time < 180000)
HotTime = (time >= 074000 and time < 093000) or (time >= 143000 and time < 163000)
if AvCandleTime then
DimCandleMax=abs(high-low)
endif
DimCandleNow=abs(open-low)
divavcandle=0.3
//////avcandle=average[400](high-low)
avcandle=average[400](DimCandleMax)
DivAvcandleTrig=0.7
FasciaTrig=DivAvcandleTrig*avcandle
PeriodXm60=60
///timeframe(1 minute,updateonclose)
innerXm60 = 2*weightedaverage[ round( PeriodXm60/2 ) ](close)-weightedaverage[PeriodXm60](close)
MMHULLXm60=weightedaverage[ round( sqrt(PeriodXm60) ) ]( innerXm60 )
//////
if trendSup=-1 then
LivFasciaTrig=MMHULLXm60-FasciaTrig
LivFasciaStop=MMHULLXm60+FasciaTrig
gcolstop=255
rcolstop=0
else
LivFasciaTrig=MMHULLXm60+FasciaTrig
LivFasciaStop=MMHULLXm60-FasciaTrig
gcolstop=0
rcolstop=255
endif
///////////////////////////////////////////////
///////////////////////////////////////////
// ===========================================================
///////////////////////////////////////////////////////////////////////// 1025-MarketSTructAlgoSignal-5m
/////timeframe(5 minutes,updateonclose)
// —– PARAMETRI —–
leftBarsTubeAlgoXm = 5 // barre per pivot interno
rightBarsTubeAlgoXm = 5
leftBarsTubeAlgoXmExt = 30 // barre per pivot esterno
rightBarsTubeAlgoXmExt = 30
distTubeAlgoXm = 2.0 // distTubeAlgoXmanza banda (ATR * distTubeAlgoXm)
zoneTubeAlgoXm = 1 // mostra zona (1 = true)
// —– VARIABILI —–
phTubeAlgoXm = 0
plTubeAlgoXm = 0
phTubeAlgoXmExt = 0
plTubeAlgoXmExt = 0
lastTubeAlgoXmHigh = 0
lastTubeAlgoXmLow = 0
prevTubeAlgoXmHigh = 0
prevTubeAlgoXmLow = 0
msTubeAlgoXm = 0
longsigTubeAlgoXm = 0
shortsigTubeAlgoXm = 0
longsigTubeAlgoXm2 = 0
shortsigTubeAlgoXm2 = 0
ATR14TubeAlgoXm = AverageTrueRange[14](close)
ma2TubeAlgoXm = 0
ma3TubeAlgoXm = 0
// ===== CALCOLO PIVOT INTERNO (swing) =====
IF barindex > leftBarsTubeAlgoXm + rightBarsTubeAlgoXm THEN
localTubeAlgoXmHigh = 1
localTubeAlgoXmLow = 1
FOR iTubeAlgoXm = barindex – leftBarsTubeAlgoXm TO barindex + rightBarsTubeAlgoXm
IF iTubeAlgoXm <> barindex THEN
IF high[iTubeAlgoXm] > high[barindex] THEN
localTubeAlgoXmHigh = 0
ENDIF
IF low[iTubeAlgoXm] < low[barindex] THEN
localTubeAlgoXmLow = 0
ENDIF
ENDIF
NEXT
IF localTubeAlgoXmHigh = 1 THEN
prevTubeAlgoXmHigh = lastTubeAlgoXmHigh
lastTubeAlgoXmHigh = high[barindex]
phTubeAlgoXm = high[barindex]
ENDIF
IF localTubeAlgoXmLow = 1 THEN
prevTubeAlgoXmLow = lastTubeAlgoXmLow
lastTubeAlgoXmLow = low[barindex]
plTubeAlgoXm = low[barindex]
ENDIF
ENDIF
// ===== CALCOLO PIVOT ESTERNO =====
IF barindex > leftBarsTubeAlgoXmExt + rightBarsTubeAlgoXmExt THEN
IF high = Highest[leftBarsTubeAlgoXmExt + rightBarsTubeAlgoXmExt +1](high) THEN
phTubeAlgoXmExt = high
ENDIF
IF low = Lowest[leftBarsTubeAlgoXmExt + rightBarsTubeAlgoXmExt +1](low) THEN
plTubeAlgoXmExt = low
ENDIF
ENDIF
// ===== CALCOLO MS e SEGNALI =====
msTubeAlgoXm = msTubeAlgoXm[1]
// CHoCH segnali
longsigTubeAlgoXm = close > phTubeAlgoXm AND msTubeAlgoXm[1] < 0
shortsigTubeAlgoXm = close < plTubeAlgoXm AND msTubeAlgoXm[1] > 0
// BOS segnali
longsigTubeAlgoXm2 = longsigTubeAlgoXm AND longsigTubeAlgoXm[1] = 0
shortsigTubeAlgoXm2 = shortsigTubeAlgoXm AND shortsigTubeAlgoXm[1] = 0
// Aggiorna MS
IF lastTubeAlgoXmHigh > prevTubeAlgoXmHigh AND lastTubeAlgoXmLow > prevTubeAlgoXmLow AND close > phTubeAlgoXm THEN
msTubeAlgoXm = 2
ELSIF close > phTubeAlgoXm THEN
msTubeAlgoXm = 1
ELSIF lastTubeAlgoXmHigh < prevTubeAlgoXmHigh AND lastTubeAlgoXmLow < prevTubeAlgoXmLow AND close < plTubeAlgoXm THEN
msTubeAlgoXm = -2
ELSIF close < plTubeAlgoXm THEN
msTubeAlgoXm = -1
ELSE
msTubeAlgoXm = msTubeAlgoXm[1]
ENDIF
// ===== CALCOLO BANDA / ZONE =====
maTubeAlgoXm = Average[leftBarsTubeAlgoXmExt](close)
atrTubeAlgoXm = AverageTrueRange[leftBarsTubeAlgoXmExt](close)
IF msTubeAlgoXm > 0 AND msTubeAlgoXm[1] > 0 THEN
ma2TubeAlgoXm = maTubeAlgoXm – atrTubeAlgoXm*distTubeAlgoXm
ma3TubeAlgoXm = ma2TubeAlgoXm + atrTubeAlgoXm
ELSIF msTubeAlgoXm < 0 AND msTubeAlgoXm[1] < 0 THEN
ma2TubeAlgoXm = maTubeAlgoXm + atrTubeAlgoXm*distTubeAlgoXm
ma3TubeAlgoXm = ma2TubeAlgoXm – atrTubeAlgoXm
ELSE
ma2TubeAlgoXm = 0
ma3TubeAlgoXm = 0
ENDIF
rbTubeAlgoXm=0
gbTubeAlgoXm=0
visdispTubeAlgoXm=20
upBTubeAlgoXm=max(ma2TubeAlgoXm,ma3TubeAlgoXm)
dnBTubeAlgoXm=min(ma2TubeAlgoXm,ma3TubeAlgoXm)
// ===== DISEGNO BANDA / ZONE =====
if ma2TubeAlgoXm>ma2TubeAlgoXm[1] or ma3TubeAlgoXm>ma3TubeAlgoXm[1] then
if low>dnBTubeAlgoXm then
rbTubeAlgoXm=0
gbTubeAlgoXm=255
visdispTubeAlgoXm=100
endif
endif
if ma2TubeAlgoXm<ma2TubeAlgoXm[1] or ma3TubeAlgoXm<ma3TubeAlgoXm[1] then
if high<upBTubeAlgoXm then
rbTubeAlgoXm=255
gbTubeAlgoXm=0
visdispTubeAlgoXm=100
endif
endif
if high<upBTubeAlgoXm then
rbTubeAlgoXm=255
gbTubeAlgoXm=0
visdispTubeAlgoXm=100
endif
if low>dnBTubeAlgoXm then
rbTubeAlgoXm=0
gbTubeAlgoXm=255
visdispTubeAlgoXm=100
endif
TIMEFRAME(default,updateonclose)
///GRAPHONPRICE LivFasciaStop
///graph trendSup
////graphonprice rngfilt //////coloured(cr,cg,cb) style(line,4) as “0525_TREND-Range-Filter-Manual”
// Condizioni per entrare su posizioni long
if Partito=1 THEN
EntrSELL=0
EntrBUY=1
endif
//////////////////////////
if Partito=1 and PuoiEntrare=1 then
IF NOT LongOnMarket AND EntrBUY=1 and PuoiEntrare=1 THEN
BUY 1 CONTRACTS AT MARKET
PuoiEntrare=0
set stop loss maxloss
if trendSup=-1 then
EntratoControtrendSup=1
else
EntratoControtrendSup=0
endif
ENDIF
// Condizioni per entrare su posizioni short
IF NOT ShortOnMarket AND EntrSELL=1 and PuoiEntrare=1 THEN
SELLSHORT 1 CONTRACTS AT MARKET
PuoiEntrare=0
set stop loss maxloss
if trendSup=1 then
EntratoControtrendSup=1
else
EntratoControtrendSup=0
endif
ENDIF
endif
/////////////
if EntratoControtrendSup=1 THEN
If LongOnMarket and trendSup=1 THEN
EntratoControtrendSup=0
ENDIF
IF ShortOnMarket and trendSup=-1 THEN
EntratoControtrendSup=0
endif
endif
///////////////////////
If LongOnMarket and trendSup=-1 and EntratoControtrendSup=0 THEN
SELL AT MARKET
ENDIF
/////
/////////////////////////////////////////
// Condizioni per uscire da posizioni short
IF ShortOnMarket and trendSup=1 and EntratoControtrendSup=0 THEN
EXITSHORT AT MARKET
endif
////////////
if DAYOFWEEK= 5 and time >215500 THEN
IF ShortOnMarket THEN
EXITSHORT AT MARKET
endif
IF LongOnMarket THEN
SELL AT MARKET
endif
endif
/////////////////////////////////////////////////////////////
if TrailingST=1 and EntratoControtrendSup=0 then
IF NOT ONMARKET THEN
newSL=0
Distance = 7
ENDIF
IF (BarIndex – TradeIndex) >= 0 THEN //0
//manage long positions
newSL=LivFasciaStop
//stop order to exit the positions
IF newSL>0 THEN
IF LongOnMarket THEN
//////////////////////
if LivFasciaStop>upBTubeAlgoXm then
newSL=upBTubeAlgoXm
else
newSL=LivFasciaStop
endif
/////////////////////
IF (close + Distance) > newSL THEN
SELL AT newSL STOP
ELSIF (close – Distance) < newSL THEN
SELL AT newSL LIMIT
ELSE
SELL AT Market
ENDIF
ELSIF ShortOnmarket THEN
if LivFasciaStop<dnBTubeAlgoXm then
newSL=dnBTubeAlgoXm
else
newSL=LivFasciaStop
endif
IF (close + Distance) < newSL THEN
EXITSHORT AT newSL STOP
ELSIF (close – Distance) > newSL THEN
EXITSHORT AT newSL LIMIT
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
ENDIF
ENDIF
endif
//*********************************************************************************