# Need help with a deviation exploration

Hi All, I new to this coding thing. Generally, I try to search for AFL codes which are a close match to query and then try to modify that to suit my needs. But I was unable to find a close match for below query. Again if it's trivial, let me know and will make sure to search more before posting here.

Here is the query. I want to create an exploration where it shows No. of times stock touched x% up & x% down from open price over a period of y days. For Example

For a period of 3 days:

1st Day
x% High from open (Yes touched), x% Down from open - (Not touched)
2nd Day
x% High from open- (Yes touched), x% Down from open - (Yes touched)
3rd Day
x% High from open - (Not touched), x% Down from open (Yes touched)

So exploration would return something like this:
For a period of 3 Days,
Stock ABC achieved "x% high" 66.7% times and "x% down" 33.3% times.

Thanks Again
Rahul

Even if someone can point me to an existing code/part code which might be based on similar logic, It would be really helpful. TIA.

@rgrahul08 if you have spent some time looking at some of the thousands of posts on the forum, you will notice that if you make some effort at coding a solution, often someone will try to help you fix your code. People want to see that you are interested in learning how to use AmiBroker and want to become part of the "family".

I suspect the reason no one has answered you is partly because you have not made any effort to code any part of your problem.

Also, what does this mean?

I assume you mean the LOW of the day is down x% from the Open?

Here is some sample codes to get you started. As with many solution in AmiBroker there are probably 5 or 6 different ways you could code this.

I have not taken the time to make sure this is accurate so you should debug this. Also making a loop to allow for greater flexibility than 3 bars would be a good idea.

``````Threshold = Param("Threshold", 0.02, 0.005, 0.05, 0.005); // your x % move

// find the "High Move" and "Low Move"
HM = H > O*(1+Threshold); // True if most recent bar High is > Open by your Threshold %
LM = L < O*(1-Threshold); // True if most recent bar Low is < Open by threshold

// define previous bar's moves
HM1 = Ref(HM, -1); // "high move" 1 bar previously
LM1 = Ref(LM, -1);

HM2 = Ref(HM, -2); // "high move" 2 bars previously
LM2 = Ref(LM, -2);

ThreeBarPctHM = (HM + HM1 + HM2)/3 *100;
ThreeBarPctLM = (LM + LM1 + LM2)/3 *100;
``````
4 Likes

Here's a slightly more flexible version of @portfoliobuilder's AFL, where the number of periods is not fixed at 3.

``````Threshold = Param("Threshold", 0.02, 0.005, 0.05, 0.005); // your x % move
periods = Param("Periods", 3, 1, 20, 1);

// find the "High Move" and "Low Move"
HM = H > O*(1+Threshold); // True if bar High is > Open by your Threshold %
LM = L < O*(1-Threshold); // True if bar Low is < Open by threshold

// Count the number of times it's happened recently, and express as a percentage.
nBarPctHM = Sum(HM,periods)/periods * 100;
nBarPctLM = Sum(LM,periods)/periods *100;
``````
2 Likes

Thanks a lot, this is exactly I was looking for. And I will definitely try to come up with the code first, next time I have a query.

It's just that, I don't know a lot of functions and did not know where to start on this one. I am trying to go through a lot of AFL codes to see how they work. Hopefully, within a week, I will get the basic understanding. Also, if you have any suggestion on how to start learning AFL from scratch, please let me know