MAcross
Download scriptThis tool is designed as a visual trading assistant that identifies specific momentum shifts in price action by monitoring the interaction between two different moving averages. Instead of just showing lines on your chart, it acts as an alert system that places visual markers (arrows) to highlight exactly when one average crosses over another.
How the Logic Works
1. Dual Moving Average Tracking The core of this script involves two separate "Moving Averages." Think of these as smoothed-out versions of the price history.
- The Faster Average: Reacts more quickly to recent price changes.
- The Slower Average: Provides a broader view of the overall trend, moving more deliberately.
By comparing these two, the script can distinguish between temporary "noise" in the market and a potential shift in the prevailing trend.
2. Identifying the "Cross" The script doesn't just check if one average is higher than the other at any given moment; it looks for the specific instant of intersection. It analyzes three consecutive points in time to confirm a "cross":
- It checks where the averages were in the past.
- It checks where they are currently.
- It checks where they are heading next.
A signal is only generated if the Faster Average moves from being below the Slower Average to above it (a potential upward move), or from above to below (a potential downward move).
3. Dynamic Positioning To ensure the visual markers don't clutter your price candles, the script calculates the "Range" of the market—specifically, the average height of the price bars over the last 10 periods. It then uses this calculation to place the arrows at a consistent distance away from the candles. This ensures that whether the market is volatile or calm, the arrows remain clearly visible and easy to read.
Visual Signals
- Upward Signal: When the Faster Average crosses above the Slower Average, a marker appears below the candle.
- Downward Signal: When the Faster Average crosses below the Slower Average, a marker appears above the candle.
In summary, this script automates the process of "watching" two trend lines for you, providing clear visual cues whenever their relationship changes, which may indicate a shift in market momentum.
//+------------------------------------------------------------------+
//| 18_28Cross.mq4 |
//| Copyright © 2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//----
double CrossUp[];
double CrossDown[];
extern int FasterMode=1; //0=sma, 1=ema, 2=smma, 3=lwma
extern int FasterMA =18;
extern int SlowerMode=1; //0=sma, 1=ema, 2=smma, 3=lwma
extern int SlowerMA =28;
extern int SoundAlert=1; // 0 = disabled
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, counter;
double fasterMAnow, slowerMAnow, fasterMAprevious, slowerMAprevious, fasterMAafter, slowerMAafter;
double Range, AvgRange;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
//----
limit=Bars-counted_bars;
for(i=0; i<=limit; i++)
{
counter=i;
Range=0;
AvgRange=0;
for(counter=i ;counter<=i+9;counter++)
{
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;
fasterMAnow=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i);
fasterMAprevious=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i+1);
fasterMAafter=iMA(NULL, 0, FasterMA, 0, FasterMode, PRICE_CLOSE, i-1);
slowerMAnow=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, i);
slowerMAprevious=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, i+1);
slowerMAafter=iMA(NULL, 0, SlowerMA, 0, SlowerMode, PRICE_CLOSE, i-1);
//----
if ((fasterMAnow > slowerMAnow) && (fasterMAprevious < slowerMAprevious) && (fasterMAafter > slowerMAafter))
{
CrossUp[i]=Low[i] - Range*0.5 ;
}
else if ((fasterMAnow < slowerMAnow) && (fasterMAprevious > slowerMAprevious) && (fasterMAafter < slowerMAafter))
{
CrossDown[i]=High[i] + Range*0.5;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments