Hello,
I’d like to have a Signal whenever one of three MAs Fast crosses Slow. The code below gives Signal=1 on every single bar.
Can you please help spot where the error is?
Thank you!
PeriodMASlow = 21
PeriodMAFast = 8
Signal=0
MAType=0
for MAType= 1 to 3 do
if MAType = 1 then
MASlow1 = Average[PeriodMASlow](WeightedClose)
MAFast1 = Average[PeriodMAFast](close)
elsif MAType = 2 then
MASlow2 = ExponentialAverage[PeriodMASlow](WeightedClose)
MAFast2 = ExponentialAverage[PeriodMAFast](close)
elsif MAType = 3 then
MASlow3 = EndPointAverage[PeriodMASlow](WeightedClose)
MAFast3 = EndPointAverage[PeriodMAFast](close)
endif
if MAType=1 then
cond1 = (MAFast1 crosses over MASlow1)
a1= 0
for i1= 0 to 10 do
if cond1[i1] then
a1=1
break
else
a1=0
endif
next
endif
if MAType=2 then
cond2 = (MAFast2 crosses over MASlow2)
a2= 0
for i2= 0 to 10 do
if cond2[i2] then
a2=1
break
else
a2=0
endif
next
endif
if MAType=3 then
cond3 = (MAFast3 crosses over MASlow3)
a3= 0
for i3= 0 to 10 do
if cond3[i3] then
a3=1
break
else
a3=0
endif
next
endif
if a1=1 or a2=1 or a3=1 then
signal=+1
break
else
signal=0
endif
next
return signal as "Signal MA Cross"
There you go:
PeriodMASlow = 21
PeriodMAFast = 8
Signal=0
MAType=0
x =0
for MAType= 1 to 3 do
a1=0
a2=0
a3=0
if MAType = 1 then
MASlow1 = Average[PeriodMASlow](WeightedClose)
MAFast1 = Average[PeriodMAFast](close)
elsif MAType = 2 then
MASlow2 = ExponentialAverage[PeriodMASlow](WeightedClose)
MAFast2 = ExponentialAverage[PeriodMAFast](close)
elsif MAType = 3 then
MASlow3 = EndPointAverage[PeriodMASlow](WeightedClose)
MAFast3 = EndPointAverage[PeriodMAFast](close)
endif
if MAType=1 then
cond1 = (MAFast1 crosses over MASlow1)
a1= 0
for i1= 0 to 10 do
if cond1[i1] then
a1=1
break
else
a1=0
endif
next
endif
if MAType=2 then
cond2 = (MAFast2 crosses over MASlow2)
a2= 0
for i2= 0 to 10 do
if cond2[i2] then
a2=1
break
else
a2=0
endif
next
endif
if MAType=3 then
cond3 = (MAFast3 crosses over MASlow3)
a3= 0
for i3= 0 to 10 do
if cond3[i3] then
a3=1
break
else
a3=0
endif
next
endif
x = 0
if a1=1 or a2=1 or a3=1 then
signal=+1
x = signal
IF signal = signal[1] THEN
x = 0
endif
break
else
x =0
signal=0
endif
next
return x as "Signal MA Cross"
I added lines 6, 10-12, 64, 67-70, 73, then modified line 78 (the last one).
Thanks a million times Roberto! it works exactly as I wanted. Have a nice week and happy holidays season.