Stochastic_Cross_Alert_001

Download script
Author: Copyright � 2006, Robert Hill
Stochastic_Cross_Alert_001

This tool is designed as a visual and audible alerting system for MetaTrader users who want to automate the identification of specific "crosses" in the Stochastic Oscillator. Instead of requiring you to constantly watch every price movement, the script monitors the technical indicators for you and highlights exactly when the momentum shifts.

How the Logic Works

The script functions by monitoring two specific lines within the Stochastic Oscillator: a "Fast" line and a "Slow" (Signal) line. It looks for precise moments where these two lines intersect each other.

1. Identifying Buy Signals (Bullish Momentum) The script continuously checks if the Fast line has moved from being below the Slow line to suddenly crossing above it. When this happens, it interprets this as a potential upward momentum shift. The tool then:

  • Places a visual green arrow on your chart below the current price candle.
  • Triggers an immediate pop-up alert on your screen (if enabled).
  • Sends an automated email notification with all the relevant trade details (price, time, and symbol).

2. Identifying Sell Signals (Bearish Momentum) Conversely, the script monitors for when the Fast line moves from above the Slow line to crossing below it. This suggests a downward shift in momentum. In this scenario, the tool:

  • Places a visual red arrow on your chart above the current price candle.
  • Triggers an audible alert and on-screen pop-up.
  • Sends an automated email containing the Sell signal information.

3. Smart Visual Placement To ensure that the arrows do not clutter your price candles, the script calculates a dynamic "range" based on recent market volatility. It uses this calculation to place the Buy and Sell arrows at a consistent distance away from the high or low of the candle, ensuring they are clearly visible without overlapping the actual price action.

4. Notification Management The script is designed to be precise; it includes logic to ensure that you aren't bombarded with multiple alerts for the same single cross. It confirms the signal on the most recent completed data point before notifying you, providing a cleaner and more reliable trading experience.

Price Data Components
Series array that contains open time of each bar
Indicators Used
Stochastic oscillator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt sends emails
10 Views
0 Downloads
0 Favorites
Stochastic_Cross_Alert_001
//compile//
//+------------------------------------------------------------------+
//|                                       Stochastic_Cross_Alert.mq4 |
//|                         Copyright © 2006, Robert Hill            |
//|                                                                  |
//| Written Robert Hill for use with AIME for the stochastic cross   |
//| to draw arrows and popup alert or send email                     |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2006, Robert Hill"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 Red
#property indicator_width1  2
#property indicator_width2  2

extern bool SoundON=true;
extern bool EmailON=true;
//---- input parameters
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
extern int MA_Method = 0; // SMA 0, EMA 1, SMMA 2, LWMA 3
extern int PriceField = 0; // Low/High 0, Close/Close 1


double CrossUp[];
double CrossDown[];
int flagval1 = 0;
int flagval2 = 0;
//+------------------------------------------------------------------+
//| 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 tmp=0;
   double fastMAnow, slowMAnow, fastMAprevious, slowMAprevious;
   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 = 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;
       
      fastMAnow = iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i);
      fastMAprevious = iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i+1);

      slowMAnow = iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_SIGNAL, i);
      slowMAprevious = iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_SIGNAL, i+1);
      
      CrossUp[i] = 0;
      CrossDown[i] = 0;
      if ((fastMAnow > slowMAnow) && (fastMAprevious < slowMAprevious))
      {
         if (i == 1 && flagval1==0)
         {
           flagval1=1;
           flagval2=0;
           if (SoundON) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
           if (EmailON) SendMail("BUY signal alert","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
         }
         CrossUp[i] = Low[i] - Range*0.75;
      }
      else if ((fastMAnow < slowMAnow) && (fastMAprevious > slowMAprevious))
      {
         if (i == 1 && flagval2==0)
         {
          flagval2=1;
          flagval1=0;
         if (SoundON) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
         if (EmailON) SendMail("SELL signal alert","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
         }
         CrossDown[i] = High[i] + Range*0.75;
      }
   }

   return(0);
}

Comments