Exploration to find out bullish stocks based on MACD cross over & 50 day Moving Average

Dear All,

I'm not so expert in AFL coding so just a beginner by asking doubts only i think i will learn AFL coding. Any questions asked by me is for educational purpose only. Can anybody help me to develop a AFL exploration to find out stocks which are bullish based on MACD cross over & 50 day Moving Average. Conditions are listed below:-

Buy: Just see the screen shot for the buy condition .
Sell: Entry price +(ATR during the entry time if all the conditions are satisfied in "BUY" * 6)

Hope now u guys got the idea.

In the exploration report i need the closing price, volume, Entry price based on buy, Target price based on the sell.

Awaiting your reply at the earliest. Sceen shot attached below

Regads,

SonjoeBuy%20Condition%20Screenshot

1 Like

M50 = MA(C,50);
AT  = ATR(50)*6;

C1  = Buy_Cond   = Cross(C,M50)  AND  MACD()>0;
CC  = C1_Clo     = IIf(C1,C,Null);
CA  = C1_ATR     = IIf(C1,AT,Null);
CV  = C1_Vol     = IIf(C1,V,Null);
BC  = Buy_Price  = IIf(C1,M50,Null);

C2  = ( CC + CA ) >= H;
SP  = Sell_Price = IIf(C2,CC+CA,Null);



Filter = 1;
AddColumn(BC,"Entry  Price",1.2,colorDefault,colorLime);
AddColumn(SP,"Target Price",1.2,colorDefault,colorRed);
AddColumn(CV,"Volume at Entry",1.2,colorDefault,colorLightGrey);
AddColumn(CC,"Close  at Entry",1.2,colorDefault,colorLightGrey);

3 Likes

Dear Sebastian,

Thank you friend for the code. But when i run ii was not getting the desired results. First and foremost i used to run the exploration in the daily chart and my MACD settings are Fast avg 6, Slow avg 18, Signal avg 9. My ATR periods are 22. As soon as i run the exploration it should give me the list of stock which meet the criteria listed below:-

  1. Buy Condition: The stock price should be above the 50 day moving average line and at the same time there should be a crossover which means the MACD should be > 0.
  2. Sell Condition: This will be the Entry price + (ATR during the entry time if all conditions are satisfied in "Np.1" multiplied by 6

The exploration should show only those stocks which meet the "Rule No.1" criteria and this will be run in the daily time frame chart. It should also show the closing price, Entry price, Target price & volume of the stock during the buy entry.

Hope now u have understood. Please do make the code simple so that i can understand so that i can learn from you. Possibly with an explanation ok

Regards,

Sonjoe Joseph.

@sonjoejh are you going to make any effort yourself? It's all there in the instructions and @Sebastian has given you are good start.

https://www.amibroker.com/guide/h_exploration.html

You are asking for the Close and the Volume,

AddColumn( Close, "Close" );
AddColumn(Volume,"Volume",1.0);

But the best way for your to learn is by trial and error, make some codes run the Exploration, modify the codes over and over.

2 Likes

Ok friend....i will give it a try i'm not so expert is coding. Don't know how to get a start. When i ask doubts only i will learn and then i will go through the codes. Anyway i will be trying...and if things not working please do help me ok friend

Regards,

Sonjoe

Hello Guys,

TimeFrameSet(inDaily);

MAvg50 = MA(C,50);
AvgTR = ATR(22);

BC = Cross(C,MAvg50) AND MACD(6,18) >= Signal(6,18,9);
CO = IIf(BC,O,Null);
CH = IIf(BC,H,Null);
CL = IIf(BC,L,Null); 
CC = IIf(BC,C,Null);
CA = IIf(BC,AvgTR,Null);
CV = IIf(BC,V,Null);
BP = IIf(BC,MAvg50,Null);

TP = BP + AvgTR * 3;


Filter = BP;

AddColumn(CO,"Open",1.2);
AddColumn(CH,"High",1.2);
AddColumn(CL,"Low",1.2);
AddColumn(CC,"Close",1.2);
AddColumn(AvgTR,"ATR",1.2);
AddColumn(CV,"Volume",1.2);
AddColumn(BP,"Entry Price",1.2);

Please do check the above code if its correct.....i have checked here and its giving good results for me. This time there was a slight change in the Target price ie Entry Price + (ATR * 3), 1. Buy Condition: The stock price should be above the 50 day moving average line and at the same time there should be a crossover which means the MACD should be > 0.

Tell me if its ok guys i need some experts advice.

Thanking You and With Regards,

Sonjoe

Please note that when you run your scan/exploration for a specific time interval , you should not use timeframeset() function
This can be done from backtester settings window under "General" tab , you go to "Periodity" and choose your appropriate interval for which scan will be performed

Beside the link provided by @portfoliobuilder , You can find more info for the right usage of timeframeset () & Backtester setting from the following links

http://www.amibroker.com/guide/w_settings.html
https://www.amibroker.com/guide/h_timeframe.html

Also note that you did not write this condition in your formula

so you should change BC to become

BC = Cross(C,MAvg50) AND MACD(6,18) >= Signal(6,18,9) AND MACD() > 0;
5 Likes

Thank you very much Sebastian for pointing my mistake and also in helping me. I will be correcting the code.

Regards,

Sonjoe

Dear Mr.Sebastian,

Good Morning. Sebastian i have noticed that if i give the code MACD() > 0 then its given me a very few list of stocks. If i don't give the code MACD() > 0 then its gives me a wide list of stocks which has a MACD crossover happening. So i have abandoned the code MACD() > 0.

Now one more thing suppose now i'm looking for shorting list of stocks i have just made a opposite of the code please do check it since i'm not happy with the results. Do check if the code is correct or were i have went wrong ok do correct me if i'm wrong.

//Exporation to find stocks which are bearish according to MA50,MACD & Volume 

MAvg50 = MA(C,50);
AvgTR = ATR(22);

SC = Cross(C,MAvg50) AND MACD(6,18) <= Signal(6,18,9) AND Volume > 100000;
CO = IIf(SC,O,Null);
CH = IIf(SC,H,Null);
CL = IIf(SC,L,Null); 
CC = IIf(SC,C,Null);
CA = IIf(SC,AvgTR,Null);
CV = IIf(SC,V,Null);
SP = IIf(SC,MAvg50,Null);

TP = SP - AvgTR * 3;
STP = SP - AvgTR * 6;


Filter = SP;

AddColumn(CO,"Open",1.2);
AddColumn(CH,"High",1.2);
AddColumn(CL,"Low",1.2);
AddColumn(CC,"Close",1.2);
AddColumn(AvgTR,"ATR",1.2);
AddColumn(CV,"Vol",1);
AddColumn(SP,"EP",1.2,colorBlack,colorRed);
AddColumn(TP,"FTP",1.2,colorBlack,colorGreen);

Awaiting the feedback

Thanking You,

Regards,

Sonjoe.

That's very normal , more conditions will result in less number of stock and vice versa , but just one thing i would like to pay your attention to it which is MACD() is not the same as MACD(6,18) , so make sure that this settings is what you really want , also it would be good to read about Filter , i have a feeling that you don't comprehend it well

This part is not clear enough for me , can you make more clarification please ? You mean you want to show stocks that fit for short selling? or what ?

1 Like

Dear Sebastian,

I think u still have confusion with regard to my MACD. In the attached file u can see my MACD settings & the conditions were the stock can go bullish which is texted in green. On the right hand side u can see the bearish conditions were the stock can go down which is texted in pale blue. I have explained very well in the diagram.

When a stock is shorted my targets are as follows:-

First target = Entry Price - (ATR * 3)
Second target = Entry PRice - (ATR *6)

When a stock is bullish my targets are as follows:-

First target = Entry Price + (ATR * 3)
Second target = Entry PRice + (ATR *6)

Now please recheck all the codes for bullish & bearish conditions and just tell me everything is fine ok.

Regards,

Sonjoe Joseph.

Bullish%20%26%20Bearish

No i am just make sure you understand how the function work , no more no less

i already a write a snippet code for you to work on it , to make some modification , and to see how you can export the result to analysis window because the main goal you declared in the first post was to learn , remember ? :slight_smile:

Anyway , here is a complete code to show bullish and bearish stocks according to your conditions , but you need to study all functions within the code to understand how it works as well as the links provided previously

// Assigning some Variables ...
MA50 = MA(C,50);
AR   = ATR(22);   


// Buy and Short Condtion ...
BC   = Buy_Condition     = Cross(C,MA50) and MACD(6,18) >= Signal (6,18,9);
SC   = Short_Condition   = Cross(MA50,C) and MACD(6,18) <= Signal (6,18,9);


// Entry Price ...
EPBC = Bullish_Entry_Price = ValueWhen(BC,C);
EPSC = Bearish_Entry_Price = ValueWhen(SC,C);


// Buy and Short Targets ...
FTBC = First_Target_BC	 = EPBC + AR*3 ;
STBC = Second_Target_BC	 = EPBC + AR*6 ;
FTSC = First_Target_SC	 = EPSC - AR*3 ;
STSC = Second_Target_SC	 = EPSC - AR*6 ;


// Exporting Results to Analysis Window ...

Filter = BC OR SC;

AddColumn(EPBC,"Entry Price as a Bull",1.2,colorDefault,colorLime);
AddColumn(FTBC,"First Target as a Bull",1.2,colorDefault,colorOrange);
AddColumn(STBC,"Second Target as a Bull",1.2,colorDefault,colorOrange);

AddColumn(EPSC,"Entry Price as a Bear",1.2,colorDefault,colorRed);
AddColumn(FTSC,"First Target as a Bear",1.2,colorDefault,colorGold);
AddColumn(STSC,"Second Target as a Bear",1.2,colorDefault,colorGold);

You are the one supposed to tell us , its your code now :slight_smile:

2 Likes

Dear Sebastian,

This formula was not giving correct results for me. By the way i'm happy with ur first formula and for shorting stock candidates i made some changes in the code and now its working perfectly fine.

Thank you for all your help and troubling u all this days. Will come back to you with some new ideas.

Regards,

Sonjoe

Dear Sebastian,

I'm facing a problem. I have given the code for two different exploration ie one for bullish stocks & the other one for bearish. But when i run the exploration for both its leaving out some stocks which meets the criteria. Dont know what's the problem. Do u have a teamviewer or Anydesk so that i can show live to you the actual problem. Please help me out to solve the problem. How can we connect each other

Thanking You ,

Regards,

Sonjoe

Good Morning sonjoejh
In exploration window ,
you can find an option titled by "Apply to" , make sure that your selection is Filter
Another option beside the first one titled by "Range" , make sure again that your selection is All quotes

Note that the result would show ONLY stock that met the conditions that you assigned earlier to Filter

it should work fine and give you the expected result

If i select Apply to "Filiter" no list is coming its full empty. If i select all the symbols only the selection will come but its missing some stocks which meets my conditions. Can u look into it ....i'm having teamviewer & Anydesk...will connect with u so that u can check were is the problem.

Regards,

Sonjoe

the code that you use is the same as the one i have written to you yesterday ?

When i choose appty to "Filter" that too its missing some of the stocks. The code which u have given me is not giving me correct results the first code was better that yesterday. So i'm using the first one but this code mssiing some stocks which meets the conditions. Don't know what to do ya my friend.

Regards,

Sonjoe

My Teanviewer ID: 858 392 603;