# How to create Moving Average Cross code afl

Dear Experts,

Condition:
Buy signal: When short term Moving average 20 cross above Moving average 50, and long-term Moving average 100 is above Moving average 200 to confirm uptrend.

Thanks and Best Regard,

@hungnsm11 this site works best if you show us your attempts at coding, and we can help you learn if you have made a mistake, or we see a better way to approach it.

Coming on the site, waiting an hour, then posting a “do it for me” type questions, does not endear you to us. Why should we help if you have not put the effort in to try???

When you come to post your work, make sure you read the “How to use this site” and use the proper code tags.

5 Likes

May be you want to check here (not verified) …

Hi,
It is Very Simple only.
Learn AFL and come up next time with an attempt.

``````_SECTION_BEGIN("20/50/100/200 EMA Trending");

W=EMA(C,20);
X=EMA(C,50);
Y=EMA(C,100);
Z=EMA(C,200);

Buy = Ref(W,-1)<Ref(X,-1) AND Ref(W,0)>Ref(X,0) AND Ref(Y,0)>Ref(Z,0) ;
Sell = Ref(W,-1)>Ref(X,-1) AND Ref(W,0)<Ref(X,0)  AND Ref(Y,0)<Ref(Z,0) ;

PlotShapes( shapeDownArrow*Sell, colorPink,0,H, -20) ;

_SECTION_END();
``````

All the Best.
Gloria Filamino

2 Likes

To check for buy signals according to your short term strategy, you can use the below code:

``````Buy = Cross(EMA(Close, 20), EMA(Close, 50));
``````

For your long term strategy, you can use this code:

``````Buy = Cross(EMA(Close, 100), EMA(Close, 200));
``````

You can also combine the above two:

``````Buy = Condition1 AND Condition2;
``````

This way you will only get buy signal when both conditions are met.

1 Like

@ Gloriafilamino plz help me also devolop an afl code.I want to develop an AFL code that recognises and gives indication like lines or dots in the specific time frame whenever 50 ema,100 ema,150 ema and 200 ema reach almost equal level for a short time like 5 min,10 min,15 min,30 min++ time.

Welcome to the forum.

There are a few things that you need to do to get help.

1. Get your "Verified Badge". If you don't know what that is, do a search and follow the steps to get Verified.
2. Search and Read - How to use this site.
3. Show YOUR work. (Use code blocks so it is easy for people to try to help you out).

This forum is meant as a "Help You Learn", but it means that You have to show you are trying.

2 Likes

@gopan once you have followed the suggestions by @snoopy.pa30 you should also carefully and thoroughly read,

And as to your specific coding request, define exactly what you are looking for and perhaps this function will help you achieve your goals.

https://www.amibroker.com/guide/afl/almostequal.html

Good luck, and welcome to AmiBroker! Once you have posted some attempts at code you will get more responses about how to fix your code.