I try to determine what is the best period for average crossing =Bigger pike after a crossing, but I don’t know why there is a problem with my array code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
defparamCALCULATEONLASTBARS=1000
oncereference=close
fora=1to10
EMA1=exponentialaverage[a](close)
EMA2=exponentialaverage[2*a](close)
if(EMA1crosses overEMA2)then
reference=close
endif
ifEMA1>EMA2then
ecart1=(close-reference)/pipsize
$ecart1[lastset($ecart1)+1]=ecart1
else
ecart1=0
endif
maxi=arraymax($ecart1)
ifecart1=maxithen
periodedelamoyenne=a
endif
next
returnecart1as"Ecart ",maxi,periodedelamoyenne
In fact it don’t determine the best period but only the best period for 10 (in this code)
I’ve edited your topic title – please try to use titles that are not something like ‘Coding help needed’ otherwise we get a forum full of them.
The problem is that you are checking for the price difference and the cross in the same loop for all averages.
I would code it like this with a separate array location for each MA. It will still eventually end up going for the highest number because that is the slowest moving average and so takes longer to catch up with price so the gap is bigger. Perhaps you should reset the ‘biggest’ variable at each bar and then check back over the last x bars in a loop to find the best MA.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.