Stochastic_CrossLevel_Alert

Download script
Author: Copyright � 2006, Robert Hill
Stochastic_CrossLevel_Alert

This tool is designed as an automated monitoring system for MetaTrader 4 that identifies specific momentum shifts in the market using the Stochastic Oscillator. Instead of requiring you to watch charts constantly, it automatically detects "crossovers" and provides immediate notifications via audio alerts or emails.

Core Monitoring Logic

The script analyzes price movement by looking at two different lines within a Stochastic Oscillator (the main trend line and the signal line). Its primary goal is to identify when these lines move from "overbought" or "oversold" zones back into the neutral zone, which often signifies a potential change in market direction.

How it Identifies Signals

The logic operates based on two specific triggers:

  • Buy Signals: The script monitors for instances where the price momentum crosses upward from a predetermined low level (the "Oversold" area). When the line moves from below this threshold to above it, the system identifies a potential upward move.
  • Sell Signals: Conversely, it monitors for moments when the momentum crosses downward from a high level (the "Overbought" area). When the line drops from above this threshold back down, it identifies a potential downward move.

Visual and Audible Notifications

Once a valid crossover is detected on the most recent price bar, the tool performs three actions simultaneously:

  1. Visual Markers: It draws colored arrows directly on your trading chart—Green arrows for potential buy opportunities (placed below the candle) and Red arrows for potential sell opportunities (placed above the candle).
  2. Audio Alerts: If enabled, it triggers a pop-up sound alert containing the current price, the specific time, and the currency pair being monitored.
  3. Email Notifications: If configured, it sends an automated email to your registered account, providing a written summary of the signal including the exact Bid/Ask prices and the timeframe.

Customization Options

The logic is built to be flexible, allowing users to choose:

  • Sensitivity: You can adjust the speed and smoothing of the calculations to suit different trading styles (scalping vs. long-term).
  • Target Line: You can choose whether the alerts are triggered by the main trend line or the secondary signal line.
  • Thresholds: Users can define exactly what constitutes "Overbought" or "Oversold" levels to filter out market noise.
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
11 Views
0 Downloads
0 Favorites
Stochastic_CrossLevel_Alert
//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=false;
//---- 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

extern string s0="Which line for cross";
extern string s1="Set to 0 for %K, 1 for %D";
extern int K_or_D = 0;
extern int StochLevelHigh = 80;
extern int StochLevelLow = 20;

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;
   bool CrossedUp = false, CrossedDown = false;
   
   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;
      CrossedUp = false;
      CrossedDown = false;
      
      if (K_or_D == 0)
      {
        if (fastMAnow > StochLevelLow && fastMAprevious < StochLevelLow) CrossedUp = true; 
        if (fastMAnow < StochLevelHigh && fastMAprevious > StochLevelHigh) CrossedDown = true; 
      }
      else
      {
        if (slowMAnow > StochLevelLow && slowMAprevious < StochLevelLow) CrossedUp = true; 
        if (slowMAnow < StochLevelHigh && slowMAprevious > StochLevelHigh) CrossedDown = true; 
      }
      
      if (CrossedUp)
      {
         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 (CrossedDown)
      {
         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