Hi Guys,
It’s my first time posting here. I need your little help please…
How to code, MA 20 moving average ABOUT to cross above MA 200?
Thank you so much in advance for your help!
Hi Guys,
It’s my first time posting here. I need your little help please…
How to code, MA 20 moving average ABOUT to cross above MA 200?
Thank you so much in advance for your help!
My first post here too!
AFAIK there is no way today, to look into the “future” on MA crossovers that are likely to happen tomorrow.
You can at best short list the tickers in which such a crossover MAY happen by using a proximity filter for the 2 MAs - absolute value of the difference between 20/200 is +/- 1%, say - still no guarantees that they will actually oblige and cross over the next period.
Hope this helps.
Hi @Eswar,
Thanks so much for the reply… I’m really a newbie in coding part, would you please show me a code for this? And from there, I will use it as a reference to other MA’s that I’m going to scan.
Thank you!
As @Eswar says, knowing MAs are close is no guarantee they will eventually cross. By the same token, being close at any point may mean they have already crossed. But with those things in mind, here’s some code to detect when they are close - within a given percentage apart.
MAPeriod1 = 20;
MAPeriod2 = 200;
ClosePercentageLimit = 2; // percentage limit for closeness
MAPercentDiff = abs((MA(C, MAPeriod1) / MA(C, MAPeriod2) - 1) * 100); // absolute percentage difference between MAs
MAsClose = MAPercentDiff < ClosePercentageLimit;
basically you could calculate what the price needs to be for these 2 functions to intersect.
to solve this mathematically was a bit of a pain in the $ss as I remember. Dimitris did some work on this, see:
http://traders.com/Documentation/FEEDbk_docs/2007/02/Abstracts_new/Tsokakis/tsokakis.html
also Howard Bandy gives a solution for this in his book “Quantitative Trading systems (2007)”. Since at the point where they intersect both functions are equal and therefor the difference between them is 0. Howard describes a numerical method to find the price for which these 2 functions are equal (page 184)
Hi @HelixTrader,
You’re awesome! Thank you so much for you help! I am using your code now to do some scans.
Hello @empottasch,
This stuff is amazing!
I’ll be studying this… thank you so much!
I’m loving this community! You guys are helpful!
I don’t think he wants to look into future but is looking for near cross occurrences from below or near cross from above. “About to cross” translates to me as possibly “near to cross”. So where is the future look?
// scan for near cross from below or near cross from above of two MAs
/// @link http://forum.amibroker.com/t/detect-when-moving-averages-are-about-to-cross/1997/8
percent = 0.5;// within percent range
MA1 = MA( Close, 20 ); // shorter SMA
MA2 = MA( Close, 200 ); // longer SMA
slope = LinRegSlope(MA1, 2);// slope of MA1 of previous few bars
MA1_from_above = slope < 0 AND MA1>MA2;// MA1 coming from above (negative slope) and being higher than MA2
MA1_from_below = slope > 0 AND MA1<MA2;// MA1 coming from below (positive slope) and being lower than MA2
almost_equal = AlmostEqual( MA1, MA2, percent * 100000 );
MA_NearCross_up = MA1_from_below AND almost_equal;
MA_NearCross_dn = MA1_from_above AND almost_equal;
Buy = MA_NearCross_up;
Short = MA_NearCross_dn;
hi, the reason I worked on this a few years back is to anticipate the price at which the cross will happen. If you have real time data you will see the cross happening in real time. If you have an automated system you can then buy immediately.
But it can be useful when working on a system. For instance when you have a system that buys when:
Buy = cross( MA( H, 20 ), MA( H, 200 ) );
then when doing the backtest you want to know at what price the cross took place. Your historical data will only have the O, H, L and C price of the bar at which the cross took place. So what BuyPrice do you use in your backtest?
You can calculate the price at which the cross takes place since at the cross MA( H, 20 ) = MA( H, 200 ). This price can be approached using Howard Bandy’s method.
thank you @fxshrat @HelixTrader @empottasch and the OP @bluesand22 for triggering these posts! Suddenly I have a way of automating something that i’ve only been eyeball trading!
Hello @fxshrat,
You got my point there! thank you!
I am scanning using your code above now… May I know if I can use this same code for MA 9 about/near to cross 200 ?
I only need to change?
MA1 = MA( Close, 20 ); // shorter SMA
into
MA1 = MA( Close, 9 ); // shorter SMA
Please help… thank you for your help @fxshrat! you’re amazing!
instead of hard-coding the “periods” (the second parameter) value in the MA() function calls (see the AmiBroker documentation) you may want to replace them with user-defined parameters that can be changed dynamically when you use your formula in charts and/or explorations.
Documentation for Param( ‘‘name’’, defaultval, min, max, step, sincr = 0 )
We all learn experimenting new things!
will you please change this code for MACD and SIGNAL. (I mean search scan macd near to cross signal)
@houmansat this has been briefly discussed before,
And one of the articles is coded for users in the library while the other has afl codes in the article.
http://www.amibroker.com/members/traders/01-2012.html
unfortunately i dont have access to amibroker website.will you please send me afl?
@houmansat the codes in that area are the work of @Tomasz for registered AmiBroker users who support the ongoing development of the software. I suggest you support the AmiBroker community by becoming an up to date registered user.