MetaStock -> Tools -> Indicator Builder -> New Copy and paste all formulae below. ==================== Periodicity detector ==================== ---8<--------------------------- { Chart periodicity detector } { References indicator "Calendar Week counter" } { ©Copyright 2003-2004 Jose Silva } { http://www.metastocktools.com } dummy:=Input("Chart detected: (1) Daily, (2) Weekly, ",0,0,0); dummy:=Input(" (3) Monthly, (4) Quarterly, (5) Yearly",0,0,0); Wk:=Fml("Calendar Week counter"); {FortnightCount:=Int((Wk+1)/2);} m:=Month(); Mth:=Cum(m<>ValueWhen(2,1,m)); q:=If(m=1 OR m=2 OR m=3,1, If(m=4 OR m=5 OR m=6,2, If(m=7 OR m=8 OR m=9,3,4))); Qtr:=Cum(q<>ValueWhen(2,1,q)); Yr:=Cum(Year()<>ValueWhen(2,1,Year())); chart:=If(LastValue(Cum(Wk=ValueWhen(2,1,Wk)))=0,2,1); chart:=If(LastValue(Cum(Mth=ValueWhen(2,1,Mth)))=0,3,chart); chart:=If(LastValue(Cum(Qtr=ValueWhen(2,1,Qtr)))=0,4,chart); chart:=If(LastValue(Cum(Yr=ValueWhen(2,1,Yr)))=0,5,chart); chart ---8<--------------------------- ================================ Periodicity detector app example ================================ ---8<--------------------------- {Chart periodicity detector application example} {Automatically adjusts EMA to chart periodicity} { References indicator "Calendar Week counter" } { ©Copyright 2003-2004 Jose Silva } { http://www.metastocktools.com } Wk:=Fml("Calendar Week counter"); {Frt:=Int((Wk+1)/2);} m:=Month(); Mth:=Cum(m<>ValueWhen(2,1,m)); q:=If(m=1 OR m=2 OR m=3,1, If(m=4 OR m=5 OR m=6,2, If(m=7 OR m=8 OR m=9,3,4))); Qtr:=Cum(q<>ValueWhen(2,1,q)); Yr:=Cum(Year()<>ValueWhen(2,1,Year())); chart:=If(LastValue(Cum(Wk=ValueWhen(2,1,Wk)))=0,2,1); chart:=If(LastValue(Cum(Mth=ValueWhen(2,1,Mth)))=0,3,chart); chart:=If(LastValue(Cum(Qtr=ValueWhen(2,1,Qtr)))=0,4,chart); chart:=If(LastValue(Cum(Yr=ValueWhen(2,1,Yr)))=0,5,chart); pds:=Input("daily EMA periods",1,25200,126); x:=Input("use Open=1 High=2 Low=3 Close=4 Volume=5 P=6",1,6,6); shift:=1+Input("EMA vertical shift %", -100,100,0)/100; plot:=Input("EMA=1, Crossover signals=2",1,2,1); x:=If(x=1,O,If(x=2,H,If(x=3,L,If(x=5,V,If(x=6,P,C))))); pds:=If(chart=1,pds,If(chart=2,pds/5, If(chart=3,pds/21,If(chart=4,pds/63,pds/252)))); pds:=If(pds<1,1,pds); pds:=If(pds>Cum(IsDefined(x)), Cum(IsDefined(x)),pds); Ema:=x*2/(pds+1)+PREV*(1-2/(pds+1)); Ema:=Ema*shift; signals:=Cross(x,Ema)-Cross(Ema,x); If(plot=2,signals,Ema) ---8<--------------------------- ===================== Calendar Week counter ===================== ---8<--------------------------- { Week counter v2.0, Gregorian calendar } { Count is independent of any missing chart data } { ©Copyright 2003-2004 Jose Silva } { http://www.metastocktools.com } limit:=2000; {do not change limit year} LimLeap:=Frac(limit/4)=0 AND Frac(limit/100)<>0 OR Frac(limit/400)=0; NoCount:=limit*365+Int(limit/4) -Int(limit/100)+Int(limit/400)-LimLeap; leap:=Frac(Year()/4)=0 AND Frac(Year()/100)<>0 OR Frac(Year()/400)=0; y:=Year()*365+Int(Year()/4) -Int(Year()/100)+Int(Year()/400)-NoCount; m:= If(Month()=2,31-leap, If(Month()=3,59, If(Month()=4,90, If(Month()=5,120, If(Month()=6,151, If(Month()=7,181, If(Month()=8,212, If(Month()=9,243, If(Month()=10,273, If(Month()=11,304, If(Month()=12,334, -leap))))))))))); DayNr:=y+m+DayOfMonth(); WkCount:=Int((DayNr-1)/7)+(Year()>=limit); WkCount ---8<--------------------------- http://www.metastocktools.com