HELP on consecutive days over moving average

#1
I am learning AFL by seeing examples and start to write my idea. I used to use excel to do my analysis, it's easier as u can manipulate and use VBA, but not efficient and fast enough. I really wanna learn. Please help.

I always like to know if the stock has continuously rise over 20-dayMoving average of Close? I can do it easily in excel, how can I do it in amibroker? in Explore? or in scan of the Analysis mode?


Rule:
1. if today C > MA(C,20), check previous day, if Ref(C,-1) > MA(Ref(C,-1),20), then +1, else 1, vice versa
2. the first day rise above or below moving average of 20day close, will results in 1, -1.

Date Close MA(C,20) C>MA20 Consecutive Days
1/31/2013 3.62 3.82 FALSE -1
1/30/2013 4 3.85 TRUE 2
1/29/2013 3.9 3.85 TRUE 1
1/28/2013 3.43 3.85 FALSE -4
1/25/2013 3.57 3.82 FALSE -3
1/24/2013 3.6 3.82 FALSE -2
1/23/2013 3.45 3.81 FALSE -1
1/22/2013 4.2 3.82 TRUE 16
1/21/2013 4.1 3.77 TRUE 15
1/18/2013 4 3.74 TRUE 14
1/17/2013 4 3.72 TRUE 13
1/16/2013 3.9 3.7 TRUE 12
1/15/2013 3.86 3.68 TRUE 11
1/14/2013 3.98 3.64 TRUE 10
1/11/2013 4.06 3.61 TRUE 9
1/10/2013 3.9 3.57 TRUE 8
1/9/2013 3.76 3.53 TRUE 7
1/8/2013 3.69 3.5 TRUE 6
1/7/2013 3.65 3.46 TRUE 5
1/4/2013 3.68 3.42 TRUE 4
1/3/2013 3.78 3.38 TRUE 3
1/2/2013 3.77 3.32 TRUE 2
12/31/2012 3.81 3.27 TRUE 1
 

amanfree

Active Member
#2
Code:
Color = iif(C>MA(C,20), colorBlue,colorRed);

if (Color == colorBlue)
 {
   x= BarsSince(Color == colorBlue);
 }

elseif (Color == colorRed)
 {
   x= BarsSince(Color == colorRed)*-1;
 }
Plot(x, "Bars Since C and MA cross",ParamColor("Color", colorBrown ),styleStaircase|styleThick|styleOwnScale|styleNoRescale);
Just try it on amibroker and remove the syntax error. it shud work fine other wise.
 

amanfree

Active Member
#3
other approach may be:

Code:
Color = IIf(C>MA(C,20), colorBlue,colorRed);

a = BarsSince(Color == colorBlue);

b = BarsSince(Color == colorRed);

x = IIf (Color == colorBlue,a ,  IIf (Color == colorRed, b, 0 ));

Plot(x, "Bars Since C and MA cross",ParamColor("Color", colorBrown ),styleStaircase|styleThick|styleOwnScale|styleNoRescale);
 
#4
what I mean is I want to check the number of consecutive rise or drop below moving average of 20MA in the scan/explore screen.

is that i can use for loop?

for( i = BarIndex(); i> 0; i-- )

{
//myema[ i ] = 0.1 * Close[ i ] + 0.9 * myema[ i - 1 ];
//MATest = C > 3.7;//MA(Close,20);
if (C > MA(C,20))
MA20Rise = MA20Rise + 1 ;
else
//{
MA20Rise = MA20Rise -1;
}
 

amanfree

Active Member
#6
what I mean is I want to check the number of consecutive rise or drop below moving average of 20MA in the scan/explore screen.

is that i can use for loop?

for( i = BarIndex(); i> 0; i-- )

{
//myema[ i ] = 0.1 * Close[ i ] + 0.9 * myema[ i - 1 ];
//MATest = C > 3.7;//MA(Close,20);
if (C > MA(C,20))
MA20Rise = MA20Rise + 1 ;
else
//{
MA20Rise = MA20Rise -1;
}


bhai that is what i have done without using barindex loop. barindex loop is slow inherently.
 

amanfree

Active Member
#8
Thx first but seems ur code doesn't work in scan or explore. Plz help.
ys that is the logic part na, attach explore scan code after that
 
#9
Hi aman

Good code, only thing is to use barssince you may need to remove excess signals, something like we do for buy/sell :)

Code:
up	=	C > MA(C,20);
dn	=	C < MA(C,20);
up	=	ExRem(up,dn);
dn	=	ExRem(dn,up);
X	=	IIf(BarsSince(up) < BarsSince(dn), BarsSince(up), 0);
Y	=	IIf(BarsSince(up) > BarsSince(dn), BarsSince(dn), 0);
Filter	=	1;
AddColumn(C,"Close");
AddColumn(MA(C,20),"M A");
AddColumn(X,"UP_Count");
AddColumn(Y,"DN_Count");
Cheers
::thumb::
 
#10
Hi aman

Good code, only thing is to use barssince you may need to remove excess signals, something like we do for buy/sell :)

Code:
up	=	C > MA(C,20);
dn	=	C < MA(C,20);
up	=	ExRem(up,dn);
dn	=	ExRem(dn,up);
X	=	IIf(BarsSince(up) < BarsSince(dn), BarsSince(up), 0);
Y	=	IIf(BarsSince(up) > BarsSince(dn), BarsSince(dn), 0);
Filter	=	1;
AddColumn(C,"Close");
AddColumn(MA(C,20),"M A");
AddColumn(X,"UP_Count");
AddColumn(Y,"DN_Count");
Cheers
::thumb::
good job bro, actually i didnt had amibroker to test the code :D
 

Similar threads