SMA Cross Over Arrow
Download scriptThis tool is designed to help traders identify specific trend shifts by highlighting "crossovers" between two different Simple Moving Averages (SMA). While many traders use moving averages to gauge momentum, these lines can sometimes become cluttered or difficult to read—especially on smaller timeframes where the price candles may obscure the exact moment of intersection. This script solves that problem by automatically detecting those intersections and placing clear visual markers on your chart.
How it Works
The logic follows a three-step process to determine when a signal should be shown:
1. Customization of Speed The tool allows you to define two different moving averages: a "Faster" SMA (which reacts quickly to price changes) and a "Slower" SMA (which represents the broader trend). By comparing these two, the script can identify when short-term momentum is breaking away from the long-term average.
2. Precise Crossover Detection Instead of simply showing you where the lines are currently sitting, the script analyzes the relationship between the two averages over a sequence of bars. It looks for a specific "flip" in positioning:
- Bullish Signal: The script identifies a point where the Faster SMA was previously below the Slower SMA but has moved above it.
- Bearish Signal: The script identifies a point where the Faster SMA was previously above the Slower SMA but has moved below it.
3. Intelligent Visual Placement To ensure the signals are easy to see without cluttering your workspace, the script calculates the "Range" (the volatility) of the price over the last 10 bars. When a crossover is detected, it places an arrow at a specific distance from the candle:
- Buy Signals appear slightly below the low of the candle.
- Sell Signals appear slightly above the high of the candle.
By using this calculation, the script ensures that the arrows stay clearly visible and don't overlap with the price action itself. This allows you to remove the actual moving average lines from your chart entirely, leaving a clean workspace that only displays the most important trend-reversal signals.
//+------------------------------------------------------------------+
//| SMA-Crossover_Signal.mq4 |
//| |
//+------------------------------------------------------------------+
/*
+------------------------------------------------------------------+
| Allows you to enter two sma periods and it will then show you at |
| Which point they crossed over. It is more usful on the shorter |
| periods that get obscured by the bars / candlesticks and when |
| the zoom level is out. Also allows you then to remove the smas |
| from the chart.
+------------------------------------------------------------------+
*/
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
double CrossUp[];
double CrossDown[];
extern int FasterSMA = 14;
extern int SlowerSMA = 34;
//+------------------------------------------------------------------+
//| 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 fasterSMAnow, slowerSMAnow, fasterSMAprevious, slowerSMAprevious, fasterSMAafter, slowerSMAafter;
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;
fasterSMAnow = iMA(NULL, 0, FasterSMA, 0, MODE_SMA, PRICE_CLOSE, i);
fasterSMAprevious = iMA(NULL, 0, FasterSMA, 0, MODE_SMA, PRICE_CLOSE, i+1);
fasterSMAafter = iMA(NULL, 0, FasterSMA, 0, MODE_SMA, PRICE_CLOSE, i-1);
slowerSMAnow = iMA(NULL, 0, SlowerSMA, 0, MODE_SMA, PRICE_OPEN, i);
slowerSMAprevious = iMA(NULL, 0, SlowerSMA, 0, MODE_SMA, PRICE_OPEN, i+1);
slowerSMAafter = iMA(NULL, 0, SlowerSMA, 0, MODE_SMA, PRICE_OPEN, i-1);
if ((fasterSMAnow > slowerSMAnow) && (fasterSMAprevious < slowerSMAprevious) && (fasterSMAafter > slowerSMAafter)) {
CrossUp[i] = Low[i] - Range*0.5;
}
else if ((fasterSMAnow < slowerSMAnow) && (fasterSMAprevious > slowerSMAprevious) && (fasterSMAafter < slowerSMAafter)) {
CrossDown[i] = High[i] + Range*0.5;
}
}
return(0);
}
Comments