Good day,
I was wondering if anyone can assist with with a moving average cross for a number of days. For example below is the bearish moving average which shows all the instruments where its 50 day moving average crosses below its 200 moving average for today. However, I don’t want just today’s bearish Moving average crossover , but I want to see the last 5 days. If anyone can assist I would really appreciate it.
REM Calculate a simple moving average over the last 50 days
ma50 = AVERAGE[50]
REM Calculate a simple moving average over the last 200 days
ma200 = AVERAGE[200]
REM Calculate the speed at which the short MA crosses under the long MA
speed = MOMENTUM(ma50-ma200) * 100 / CLOSE
REM Display securities for which short MA has just crossed under long MA. Show the fastest crossings first.
IF ma50 CROSSES UNDER ma200 THEN
SCREENER (speed AS "Speed")
ENDIF
Thanks.
Hi, please find attached.
I have set the field “DayCount = 4” covering five days, change it to any number (e.g., changing it to 9 will give crossover in last 10 days).
Have fun …kaq52
//Author KAQ52
//This screener finds crossover of two averages in last n days
DayCount = 4 //change it as appropriate
REM Calculate a simple moving average over the last 50 days
ma50 = AVERAGE[50]
REM Calculate a simple moving average over the last 200 days
ma200 = AVERAGE[200]
REM Calculate the speed at which the short MA crosses under the long MA
speed = MOMENTUM(ma50-ma200) * 100 / CLOSE
Crossover=0 //flag for detecting crossover
For i = 0 to daycount
if (ma50[i] crosses over ma200[i]) or (ma50[i] crosses under ma200[i]) then
CrossOver=1
break
endif
next
REM Display securities for which short MA has just crossed under long MA. Show the fastest crossings first.
SCREENER [Crossover] (speed AS "Speed")
Replace lines 17-22 with this ONE:
CrossOver = summation[DayCount]((ma50 crosses over ma200) or (ma50 crosses under ma200))
As for your FOR…NEXT iterations, 0 TO DAYCOUNT will check the last 5 periods, while summation 4 (the last 4, not 0 to 4).
Thanks RobertGozi, your solution looks much precise.
My understanding so far is that “for i = 0 to daycount” with daycount=4 loop will get below five values, thus covering five days(including current one). Is this understanding not correct please?
ma20[0] —current candle
ma20[1]
ma20[2]
ma20[3]
ma20[4]
Yes, that’s correct.
Summation, instead, with the same value for DayCount you’ll check one less bar, 0 to 3.
Thank you very much for assistance. Appreciated.