My objective is to initiate a signal when close is less than an EMA for a certain number of bars.
I have never used FOR/WHILE loops before and need help resetting my counter when a trade is initiated. So far, I can only get it to reach the counter once, enter a trade then every time it closes a trade it will immediately re-open a trade as the counter has not reset.
Thank you for your help everyone.
i = 0
ema1 = exponentialaverage[500](close)
if not onmarket then
orig = 0
endif
while close<ema1 and i<11 do
i = i+1
if i>10 then
buysignal = 1
endif
wend
if not longonmarket and buysignal = 1 then
buy 1 contract at market
if orig = 0 then
orig = close
if i <>0 then
i = 0
endif
endif
else
if longonmarket and close>orig*1.02 then
sell at market
if i <>0 then
i = 0
endif
else
if longonmarket and close<orig*0.98 then
sell at market
if i <>0 then
i = 0
endif
endif
endif
endif
Try replacing lines 7-13 (and you can remove line 1) with these:
If (summation[11](close<ema1) = 11) then
buysignal = 1
Endif
If you still want to use the WHILE iteration, change line 7 this way:
while close[i]<ema1[i] and i<11 do