Hi Ivan thank you.
There is some issue with double arrows. After the SELL entry BUY exist is happening without the changes to colour.
Please see the attached picture which shows the incorrect exit and timelines and the entire code used. Can you please guide me refining this?
// — settings
SlowLength = 7 // Slow length
FastLength = 3 // Fast length
// — end of settings
fasthigh = Highest[SlowLength](high)//[1]
fastlow = Lowest[SlowLength](low)//[1]
slowhigh = Highest[FastLength](high)//[1]
slowlow = Lowest[FastLength](low)//[1]
if high<slowhigh then
thighs=slowhigh
else
thighs=high
endif
if low>slowlow then
tlows=slowlow
else
tlows=low
endif
if high<fasthigh then
thighf=fasthigh
else
thighf=high
endif
if low>fastlow then
tlowf=fastlow
else
tlowf=low
endif
if close>slowln[1] then
slowln=tlows
else
slowln=thighs
endif
if close>fastln[1] then
fastln=tlowf
else
fastln=thighf
endif
once trend1=-1
if close<slowln and close<fastln then
trend1=1
endif
if close>slowln and close>fastln then
trend1=0
endif
if trend1<>trend[1] then
if trend1=0 then
r=30
g=144
b=255
else
r=200
g=20
b=60
endif
endif
src = Close //Data type
mav = 1 //Moving Average Value: 0=SMA, 1=EMA, 2=WMA, 3=DEMA, 4=TMA, 5=VAR, 6=WWMA, 7=ZLEMA, 8=TSF, 9=HMA, 10=T3
length = 100 //Moving Average Length
Periods = 10 //ATR period
Multiplier = 0.5 //ATR Multiplier
T3a1 = 0.7 //Tillson T3 Volume Factor
valpha = 2 / (length+1)
If src > src[1] then //Green candle
vud1 = src – src[1] //Green profit
else
vud1 = 0
EndIf
If src < src[1] then //Red candle
vdd1 = src[1] – src //Red Loss
else
vdd1=0
EndIf
vUD = Summation[9](vud1) //Summation [9] Green profit
vDD = Summation[9](vdd1) //Summation [9] Red Loss
vCMO = ((vUD – vDD) / vUD + vDD) //Cumulative Market Oscillator
Once VAR = 0
VAR = (valpha * Abs(vCMO) * src) + (1 – valpha * Abs(vCMO)) * VAR[1]
xDEMA = 2 * ExponentialAverage[length](src) – ExponentialAverage[length](ExponentialAverage[length](src))
wwalpha = 1 //Length
Once WWMA = 0
WWMA = wwalpha * src + (1 – wwalpha) * WWMA[1]
If (length / 2) = Round(length / 2) then //Check even/odd length for “zero lag”
zxLag = (length / 2)
Else
zxLag = ((length-1) / 2)
EndIf
zxEMAData = src + src – src[zxLag]
xZLEMA = ExponentialAverage[length](zxEMAData) //Zero Lag Exponential Average
lrc = LinearRegression[length](src)
lrc1 = lrc[1]
lrs = lrc – lrc1
TSF = LinearRegression[length](src) + lrs //Time Series Forecast
HMA = WeightedAverage[Round(SQRT(length))](2 * WeightedAverage[length/2](src) – WeightedAverage[length](src)) //Hull Moving Average
T3e1 = ExponentialAverage[length](src)
T3e2 = ExponentialAverage[length](T3e1)
T3e3 = ExponentialAverage[length](T3e2)
T3e4 = ExponentialAverage[length](T3e3)
T3e5 = ExponentialAverage[length](T3e4)
T3e6 = ExponentialAverage[length](T3e5)
T3c1 = -T3a1 * T3a1 * T3a1
T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1
T3c3 = -6 * T3a1 * T3a1 – 3 * T3a1 – 3 * T3a1 * T3a1 * T3a1
T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1
T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3 //T3 Moving Average
Once MA = 0
If mav = 0 then
MA = Average[length,0](src) //SMA
ElsIf mav = 1 then
MA = Average[length,1](src) //EMA
ElsIf mav = 2 then
MA = Average[length,2](src) //WEMA
ElsIf mav = 3 then
MA = xDEMA //DEMA
ElsIf mav = 4 then
MA = Average[Floor(length / 2) + 1](Average[Ceil(length / 2)](src)) //TMA
ElsIf mav = 5 then
MA = VAR //VAR a.k.a. VIDYA Variable Index Dynamic Moving Average
ElsIf mav = 6 then
MA = WWMA //WWMA Welles Wilder’s Moving Average
ElsIf mav = 7 then
MA = xZLEMA //ZLEMA Zero Lag Exponential Moving Average
ElsIf mav = 8 then
MA = TSF //TSF Time Series Forecast
ElsIf mav = 9 then
MA = HMA //HMA Hull Moving Average
ElsIf mav = 10 then
MA = T3 //T3 Tillson Moving Average
EndIf
atr = AverageTrueRange[Periods](src)
up = MA – Multiplier * atr
up1 = up[1]
If src[1] > up1 then
up = Max(up, up1)
Else
up = up
EndIf
dn = MA + Multiplier * atr
dn1 = dn[1]
If src[1] < dn1 then
dn = Min(dn, dn1)
else
dn = dn
EndIf
Once trend = 1
If trend = -1 and Close > dn1 then
trend = 1
ElsIf trend = 1 and Close < up1 then
trend = -1
EndIf
if not longonmarket and close>dn and((trend1=0 and close>slowln) or (trend1=1 and close crosses over slowln)) then
buy 1 contract at market
endif
if longonmarket and (close<slowln or trend1=1) then
sell at market
endif
if not shortonmarket and close < dn and ((trend1 = 0 and close < slowln) or (trend1 = 1 and close crosses under slowln))then
SELLSHORT 1 contract AT MARKET
endif
if shortonmarket and (close > slowln or trend1 = 1)then
exitshort at market
endif
graphonprice up as “Up trend”coloured(“red”)
graphonprice dn as “Down trend”coloured(“Red”)
graphonprice slowln as “slowln”coloured(200,20,60)
graphonprice fastln as “fastln”coloured(30,144,255)