MAMy_Cross(AM)
Download scriptThis tool is designed as a visual indicator for MetaTrader 4, aimed at helping traders identify specific trend shifts and price momentum by analyzing moving averages. Instead of just showing lines on a chart, it identifies precise moments of "crosses" and marks them with directional arrows (Up and Down) to signal potential entry or exit points.
How the Logic Works
The script operates by calculating several different types of Moving Averages simultaneously. It doesn't look at the price in isolation; rather, it focuses on the relationship between current prices, previous prices, and "weighted" averages over a set period.
1. Volatility Measurement Before determining any signals, the script calculates the "Range" of the market. It looks back at the recent history of highs and lows to understand how much the price is currently swinging. This ensures that the arrows placed on your chart are positioned at a mathematically appropriate distance from the candles, keeping the visual display clean regardless of whether the market is moving fast or slow.
2. The Two Signal Layers The indicator tracks two different types of "crosses" to provide a more nuanced view of the market:
- Primary Signal (Y-Axis Logic): This focuses on the immediate relationship between the current closing price and a specific weighted average. It looks for a clean "flip"—where the price was previously below the weight but has now moved above it (or vice versa). This is intended to capture direct momentum shifts.
- Secondary Signal (X-Axis Logic): This is a more complex calculation. It combines the difference between the opening price and the weighted average with the difference between the closing price and that same average. By combining these two factors, it identifies a more holistic shift in the price structure, essentially looking for "confirmation" of the trend change.
3. Visual Output When the logic detects these mathematical flips:
- Upward Signals: If the conditions for a positive cross are met, the script places an arrow below the candle to indicate upward momentum.
- Downward Signals: If the conditions for a negative cross are met, it places an arrow above the candle to indicate downward momentum.
Summary for the Trader
In simple terms, this script filters through complex moving average math to tell you: "The price just crossed a significant threshold." It provides two layers of confirmation—one based on immediate price action and one based on a broader weighted structure—allowing you to see where the trend is potentially shifting in real-time.
//+------------------------------------------------------------------+
//| MAMy_Cross(AM).mq4 |
//| Andrey Matvievskiy |
//| |
//+------------------------------------------------------------------+
#property copyright "Andrey Matvievskiy"
#property link ""
//----
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Silver
#property indicator_color4 Silver
//----
double CrossUp1[];
double CrossDown1[];
double CrossUp2[];
double CrossDown2[];
extern int period = 3;
extern int ma_method = 3;
extern int CountedBars = 3;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW,0,0);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp1);
SetIndexStyle(1, DRAW_ARROW,0,0);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown1);
SetIndexStyle(2, DRAW_ARROW,0,0);
SetIndexArrow(2, 233);
SetIndexBuffer(2, CrossUp2);
SetIndexStyle(3, DRAW_ARROW,0,0);
SetIndexArrow(3, 234);
SetIndexBuffer(3, CrossDown2);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, counter;
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=CountedBars;
//----
for(i=1; 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;
//----
double close = iMA(NULL, 0, period, 0, ma_method, 0, i);
double open = iMA(NULL, 0, period, 0, ma_method, 1, i);
double weighted = iMA(NULL, 0, period, 0, ma_method, 6, i);
double close_2 = iMA(NULL, 0, period, 0, ma_method, 0, i+1);
double open_2 = iMA(NULL, 0, period, 0, ma_method, 1, i+1);
double weighted_2 = iMA(NULL, 0, period, 0, ma_method, 6, i+1);
//----
double X = ((weighted - open) + (close - weighted))*1000;
double Y = (close - weighted)*1000;
double X_2 = ((weighted_2 - open_2) + (close_2 - weighted_2))*1000;
double Y_2 = (close_2 - weighted_2)*1000;
//----
if ((Y > 0) &&(Y_2 < 0))
{
CrossUp1[i]=Low[i] - Range*0.3;
}
else if ((Y < 0) &&(Y_2 > 0))
{
CrossDown1[i]=High[i] + Range*0.3;
}
if ((X > 0) &&(X_2 < 0))
{
CrossUp2[i]=Low[i] - Range*0.6;
}
else if ((X < 0) &&(X_2 > 0))
{
CrossDown2[i]=High[i] + Range*0.6;
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments