Moving Average touch within last few candles

Hello everyone,
I am new to afl programming. I want a afl ( I use daily candle) which will explore the stocks with the following condition:

i want candle low is less than 50 Period simple moving average within last 3 days
i want candle Open is greater than 50 Period simple moving average within last 3 days
i want candle Close is greater than 50 Period simple moving average within last 3 days

after reading few faq of this forum, i have come to derive the following code:

Filter = (Low<Ref(MA(Close,50),-2)) AND (Open>Ref(MA(Close,50),-2)) AND (Close>Ref(MA(Close,50),-2));


AddColumn (Open,"Open",1.2);
AddColumn (close,"Close",1.2);
AddColumn(Ref(Volume,-1),"Vol t-1",1.2);

is the above code is meaning the same of what i want to achive.

please help. Thanks in advance.

@swapon_uiu welcome to the AmiBroker Forum.

I believe the best answer to your question is something that you can find by yourself with a bit of extra work!

Add some exploration lines and check all the variables/arrays you are using in your code () (see the example here below).

What do you mean precisely by the expression "within last 3 days"?
It seems to me you are comparing the value of the 50-period moving average only to the reference values of 2 days ago (not to yesterday values nor today values).

(in the Analysis windows "Apply" this code to the "Current" symbol "20 Recent Bar(s)")

ma50 = MA( C, 50 );
ma50_2 = Ref( MA50, -2 );
cond1 = ( Low < ma50_2 );
cond2 = ( Open > ma50_2 );
cond3 = ( Close > ma50_2 );
cond123 = cond1 AND cond2 AND cond3;

// Sample exploration code
Filter = 1;
AddColumn( Low, "Low" );
AddColumn( Open, "Open" );
AddColumn( Close, "Close" );
AddColumn( ma50, "MA(C, 50)" );
AddColumn( ma50_2, "Ref(MA(C, 50), -2" );
AddColumn( cond1, "L < Ref(MA(C, 50), -2)", 1 );
AddColumn( cond2, "O > Ref(MA(C, 50), -2)", 1 );
AddColumn( cond3, "C > Ref(MA(C, 50), -2)", 1 );
AddColumn( cond123, "Conditions 1 2 3", 1 );

// Shows the most recent bars at top of results
if( Status( "action" ) == actionExplore )
    SetSortColumns( -2 );

The results you get running the exploration are what you had in mind?

Probably not... In such case modify the rules and its associated exploration code to verify your changes.

Explorations are one of the essential tools to debug your formulas and to speed up your AFL learning experience.

5 Likes

Thanks for your help beppe.

My original requirement was:
i want candle low is less than 50 Period simple moving average within last 3 days
i want candle Open is greater than 50 Period simple moving average within last 3 days
i want candle Close is greater than 50 Period simple moving average within last 3 days

from your code what I understand is that:
you are comparing the value of the 50-period moving average only to the reference values of 2 days ago (not to yesterday values nor today values).

but this is what I don't want. That's why I added the last line 'is the above code is meaning the same of what i want to achieve.'
obviously my code does not suffice to my requirement.

by 'within last 3 days' i wanted to mean any of the day of the last 3 days including current day.

candle low had to be below the 50 period moving average in one of the last 3 days. it could have been low in the t or t-1 or t-2 (or conditions)
candle open had to be greater than the 50 period moving average in one of the last 3 days. it could have been above 50MA in the t or t-1 or t-2 (or conditions)
candle Close had to be greater than 50 period moving average in one of the last 3 days. it could have been above 50MA in the t or t-1 or t-2 (or conditions)

i want the explorer to get the stocks list after meeting these 3 criteria. Hope you understand.

if you need any clarification, i would love to provide you.

Thanks once again

2 Likes

@swapon_uiu, I believe what @beppe is telling you is the code you submitted only compares the reference value from 2 days ago. Of course, you're wanting to compare to the last three days rather than just the one. There is a solution to your problem but you need to discover it for yourself - this will greatly enhance your efforts in learning AFL.
This forum is different from most in that "Solve this problem" or "Write this code for me" requests typically go unanswered. Instead, the student is referred to the AFL documentation and to the online AFL Function Reference and AmiBroker Tutorial.
I suggest you begin your search with HHV and LLV.

2 Likes

Thanks for your suggestion.

Let me try and get back to you if i fail.