SMA_ CROSSOVER SIGNAL_v1

Download script
SMA_ CROSSOVER SIGNAL_v1

This tool is designed to help traders identify specific momentum shifts by highlighting the exact moments when two different Moving Average (MA) lines cross each other on a price chart. While many traders use moving averages to gauge trends, these lines can sometimes become cluttered or difficult to see clearly when zooming out or dealing with high-density candlestick data. This script solves that problem by acting as a precise visual marker for those "cross" events.

How the Logic Works

The script functions by constantly monitoring two distinct Moving Averages that you can customize: a Faster MA and a Slower MA. By comparing the current position of these lines against their previous positions, the logic identifies two specific types of signals:

  • Bullish Crossover (Buy Signal): The script looks for a moment where the Faster MA moves from being below the Slower MA to suddenly crossing above it. When this happens, the tool places a visual marker (an arrow) below the current price candle.
  • Bearish Crossover (Sell Signal): Conversely, it looks for when the Faster MA moves from being above the Slower MA to dropping below it. In this case, the tool places a visual marker above the current price candle.

Key Features and Customization

To ensure the tool fits your specific trading style, the script includes several customizable layers of logic:

  • Dynamic Positioning: Instead of placing the arrows directly on the candles (which can make the chart look messy), the script calculates the average range of recent price movements. It then offsets the markers slightly above or below the candles to ensure they remain clearly visible.
  • Full Versatility: You aren't locked into one type of calculation. You can choose between different Moving Average types (such as Simple, Exponential, Smoothed, or Linear Weighted) and select which specific price point the average should be based on (such as Open, Close, High, Low, Median, Typical, or Weighted price).
  • Real-Time Alerts: The script includes an optional alert system. If you enable this, the platform will provide a notification whenever a crossover occurs in real-time, ensuring you don't miss a signal even when you aren't staring directly at the chart.
  • Clean Chart Management: One of the primary benefits of this script is that it provides the data of the moving averages without cluttering your screen with the actual lines. This allows you to keep your chart clean while still benefiting from the crossover signals they generate.
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
19 Views
0 Downloads
0 Favorites
SMA_ CROSSOVER SIGNAL_v1
/*
  +------------------------------------------------------------------+
  | 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. (SMAs are initially set at 10 and 20)              |
  +------------------------------------------------------------------+
*/   

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
double alertBar;
extern bool UseAlert = True;
extern int FasterSMA = 10;
extern int FasterPeriodMode =  0; //0=sma, 1=ema, 2=smma, 3=lwma
extern int FasterPriceMaMode =  0; //0=price close, 1=price open, 2=price high, 3=price low, 4=price median, 5=price typical, 6=price weighted
extern int SlowerSMA = 20;
extern int SlowerPeriodMode =  0; //0=sma, 1=ema, 2=smma, 3=lwma
extern int SlowerPriceMaMode =  0; //0=price close, 1=price open, 2=price high, 3=price low, 4=price median, 5=price typical, 6=price weighted
//+------------------------------------------------------------------+
//| 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, FasterPeriodMode, FasterPriceMaMode, i);
      fasterSMAprevious = iMA(NULL, 0, FasterSMA, 0, FasterPeriodMode, FasterPriceMaMode, i+1);
      
      slowerSMAnow = iMA(NULL, 0, SlowerSMA, 0, SlowerPeriodMode, SlowerPriceMaMode, i);
      slowerSMAprevious = iMA(NULL, 0, SlowerSMA, 0, SlowerPeriodMode, SlowerPriceMaMode, i+1);
      
      
      if ((fasterSMAnow > slowerSMAnow) && (fasterSMAprevious < slowerSMAprevious)) {
         CrossUp[i] = Low[i] - Range*0.5;
         if (UseAlert==1 && Bars>alertBar) {Alert(Symbol(), "Buy signal", Period());alertBar = Bars;}
      }
      else if ((fasterSMAnow < slowerSMAnow) && (fasterSMAprevious > slowerSMAprevious)) {
         CrossDown[i] = High[i] + Range*0.5;
         if (UseAlert==1 && Bars>alertBar) {Alert(Symbol(), "Sell signal", Period());alertBar = Bars;}
      }
   }
   return(0);
}

Comments