up_down_alert

Download script
Author: Copyright � 2007, MetaQuotes Software Corp.
up_down_alert

Overview of Script Logic

This script is designed as a custom technical indicator for the MetaTrader 4 platform. Its primary purpose is to monitor price action across specific timeframes and identify potential "Buy" and "Sell" signals based on historical highs and lows. Instead of looking at every single candle equally, the script uses a sophisticated logic that segments data into meaningful chunks (like hours, days, or months) to determine market boundaries.

How the Logic Works

1. Timeframe Sensitivity The script is highly specific about which timeframes it operates on. It contains unique sets of rules for different intervals, including 5-minute, 15-minute, 30-minute, 60-minute, 4-hour (240), daily (1440), weekly (10080), and monthly charts. If you use the script on a timeframe not listed in its logic, it will remain inactive.

2. Identifying Highs and Lows For each of the supported timeframes, the script scans back through historical data to identify specific "peaks" (highest prices) and "valleys" (lowest prices).

  • On short-term charts (like 5 or 15 minutes), it looks for changes in the hour.
  • On medium-term charts (like 30 or 60 minutes), it targets specific four-hour marks (e.g., 00:00, 04:00, 08:00, etc.).
  • On long-term charts, it looks for daily, weekly, or even monthly transitions.

3. Calculation of Price Boundaries Once these highs and lows are identified, the script calculates an average for both the "Highs" and the "Lows" over a user-defined period (set by the HL_period parameter). This creates two dynamic price boundaries:

  • An upper boundary based on recent historical highs.
  • A lower boundary based on recent historical lows.

4. Signal Generation and Alerts The script constantly compares the current market price against these two calculated boundaries:

  • Buy Signal: If the current price closes at or below the average "Low" boundary, it identifies a potential buying opportunity. The script will then trigger an alert on your screen saying "New Signal Buying".
  • Sell Signal: If the current price closes at or above the average "High" boundary, it identifies a potential selling opportunity. The script will then trigger an alert saying "New Signal Selling".

5. Visual Representation In addition to sending alerts, the script places visual markers (arrows) on your chart. These markers appear at the moment the price interacts with the calculated boundaries, helping you visually confirm where the logic has detected a trend shift or a potential entry point.

Summary for the User

Essentially, this tool filters out the "noise" of every tiny price movement and instead focuses on significant historical milestones (hours, days, months). It tells you when the current price is hitting an extreme relative to those milestones, providing you with automated alerts and visual cues for potential trades.

Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
12 Views
0 Downloads
0 Favorites
up_down_alert
//+------------------------------------------------------------------+
//|                                                  updownalert.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Orange
//---- input parameters
extern int       HL_period=3;
extern int       NumBars=300;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//------
double ur[][2];
int h1[];
int l1[];

int hh=0;
int ll=0;

int sell_flag;
int buy_flag;

double spread;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,159);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
//----
   ArrayResize(ur,NumBars+1);
   ArrayResize(h1,HL_period);
   ArrayResize(l1,HL_period);
   
   spread = Ask-Bid;
   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  int   n,shift,Nbar,k,mm;
  double MaH,MaL;

//---- 
  if( Period() == 5 )
  {
    ArrayInitialize(ur,0);
    ArrayInitialize(h1,0);
    ArrayInitialize(l1,0);
    for( shift = NumBars; shift>=0; shift-- )
    {
      if( TimeHour(Time[shift + 1]) != TimeHour(Time[shift]) )
      {
        Nbar = -1;
        k = 0;
        mm = 0;
        n = shift - 15;
        while( n <= shift + HL_period * 12 + 5 )
        {
          if( n < 0 )
          {
            n = 1;
            Nbar = 0;
          }
          if( TimeHour(Time[n + 1]) != TimeHour(Time[n]) && Nbar == -1 )
          {
            Nbar = n;
            mm = n;
            n++;
            continue;
          }
          if( TimeHour(Time[n + 1]) != TimeHour(Time[n]) && Nbar != -1 )
          {
            h1[k] = Highest(NULL,0,MODE_HIGH,n - Nbar,n-(n-Nbar)+1);
            l1[k] = Lowest(NULL,0,MODE_LOW,n - Nbar,n-(n-Nbar)+1);
            k++;
            Nbar = n;
          }
          n++;
        }
        MaH = 0;
        MaL = 0;
        for( n = 0; n<HL_period; n++ )
        {
          MaH = MaH + High[h1[n]]+spread;
          MaL = MaL + Low[l1[n]];
        }
        MaH = MaH / HL_period;
        MaL = MaL / HL_period;
        ur[shift][0] = MaH;
        ur[shift][1] = MaL;
        if( Close[mm + 1]+spread/2 >= MaH ) { ll = 1; hh = 0; }
        if( Close[mm + 1]+spread/2 <= MaL ) { hh = 1; ll = 0; }
      }
      if( ur[shift,1] == 0 )
      {
        ur[shift][0] = ur[shift + 1][0];
        ur[shift][1] = ur[shift + 1][1];
      }
      if( ll == 1 )
      {
        ExtMapBuffer2[shift]=0;
        ExtMapBuffer1[shift]=ur[shift][1];
        if(buy_flag !=1)
         Alert("New Signal Buying =>", Symbol()); 
         else
         sell_flag=0;
         buy_flag=1;
      }
      if( hh == 1 )
      {
        ExtMapBuffer1[shift]=0;
        ExtMapBuffer2[shift]=ur[shift][0];
        if(sell_flag !=1)
        Alert("New Signal Selling =>", Symbol()); 
        else
         sell_flag=1;
         buy_flag=0;
      }
    }
    return(0);
  }
//-----------------------------
  if( Period() == 15 )
  {
    ArrayInitialize(ur,0);
    ArrayInitialize(h1,0);
    ArrayInitialize(l1,0);
    for( shift = NumBars; shift>=0; shift-- )
    {
      if( TimeHour(Time[shift + 1]) != TimeHour(Time[shift]) )
      {
        Nbar = -1;
        k = 0;
        mm = 0;
        n = shift - 5;
        while( n <= shift + HL_period * 4 + 2 )
        {
          if( n < 0 )
          {
            n = 1;
            Nbar = 0;
          }
          if( TimeHour(Time[n + 1]) != TimeHour(Time[n]) && Nbar == -1 )
          {
            Nbar = n;
            mm = n;
            n++;
            continue;
          }
          if( TimeHour(Time[n + 1]) != TimeHour(Time[n]) && Nbar != -1 )
          {
            h1[k] = Highest(NULL,0,MODE_HIGH,n - Nbar,n-(n-Nbar)+1);
            l1[k] = Lowest(NULL,0,MODE_LOW,n - Nbar,n-(n-Nbar)+1);
            k++;
            Nbar = n;
          }
          n++;
        }
        MaH = 0;
        MaL = 0;
        for( n = 0; n<HL_period; n++ )
        {
          MaH = MaH + High[h1[n]]+spread;
          MaL = MaL + Low[l1[n]];
        }
        MaH = MaH / HL_period;
        MaL = MaL / HL_period;
        ur[shift][0] = MaH;
        ur[shift][1] = MaL;
        if( Close[mm + 1]+spread/2 >= MaH ) { ll = 1; hh = 0; }
        if( Close[mm + 1]+spread/2 <= MaL ) { hh = 1; ll = 0; }
      }
      if( ur[shift,1] == 0 )
      {
        ur[shift][0] = ur[shift + 1][0];
        ur[shift][1] = ur[shift + 1][1];
      }
      if( ll == 1 )
      {
        ExtMapBuffer2[shift]=0;
        ExtMapBuffer1[shift]=ur[shift][1];
        if(buy_flag !=1)
         Alert("New Signal Buying =>", Symbol()); 
         else
         sell_flag=0;
         buy_flag=1;
      }
      if( hh == 1 )
      {
        ExtMapBuffer1[shift]=0;
        ExtMapBuffer2[shift]=ur[shift][0];
          if(sell_flag !=1)
        Alert("New Signal Selling =>", Symbol()); 
        else
         sell_flag=1;
         buy_flag=0;
      }
    }
    return(0);
  }
//-------------------------
  if( Period() == 30 )
  {
    ArrayInitialize(ur,0);
    ArrayInitialize(h1,0);
    ArrayInitialize(l1,0);
    for( shift = NumBars; shift>=0; shift-- )
    {
      if( TimeMinute(Time[shift])==0 && (TimeHour(Time[shift])==0 || TimeHour(Time[shift])==4 || TimeHour(Time[shift])==8 || TimeHour(Time[shift])==12 || TimeHour(Time[shift])==16 || TimeHour(Time[shift])==20) )
      {
        Nbar = -1;
        k = 0;
        mm = 0;
        n = shift - 10;
        while( n <= shift + HL_period * 8 + 5 )
        {
          if( n < 0 )
          {
            n = 1;
            Nbar = 0;
          }
          if( TimeMinute(Time[n])==0 && (TimeHour(Time[n])==0 || TimeHour(Time[n])==4 || TimeHour(Time[n])==8 || TimeHour(Time[n])==12 || TimeHour(Time[n])==16 || TimeHour(Time[n])==20) && Nbar == -1 )
          {
            Nbar = n;
            mm = n;
            n++;
            continue;
          }
          if( TimeMinute(Time[n])==0 && (TimeHour(Time[n])==0 || TimeHour(Time[n])==4 || TimeHour(Time[n])==8 || TimeHour(Time[n])==12 || TimeHour(Time[n])==16 || TimeHour(Time[n])==20) && Nbar != -1 )
          {
            h1[k] = Highest(NULL,0,MODE_HIGH,n - Nbar,n-(n-Nbar)+1);
            l1[k] = Lowest(NULL,0,MODE_LOW,n - Nbar,n-(n-Nbar)+1);
            k++;
            Nbar = n;
          }
          n++;
        }
        MaH = 0;
        MaL = 0;
        for( n = 0; n<HL_period; n++ )
        {
          MaH = MaH + High[h1[n]]+spread;
          MaL = MaL + Low[l1[n]];
        }
        MaH = MaH / HL_period;
        MaL = MaL / HL_period;
        ur[shift][0] = MaH;
        ur[shift][1] = MaL;
        if( Close[mm + 1]+spread/2 >= MaH ) { ll = 1; hh = 0; }
        if( Close[mm + 1]+spread/2 <= MaL ) { hh = 1; ll = 0; }
      }
      if( ur[shift,1] == 0 )
      {
        ur[shift][0] = ur[shift + 1][0];
        ur[shift][1] = ur[shift + 1][1];
      }
      if( ll == 1 )
      {
        ExtMapBuffer2[shift]=0;
        ExtMapBuffer1[shift]=ur[shift][1];
        if(buy_flag !=1)
         Alert("New Signal Buying =>", Symbol()); 
         else
         sell_flag=0;
         buy_flag=1;
      }
      if( hh == 1 )
      {
        ExtMapBuffer1[shift]=0;
        ExtMapBuffer2[shift]=ur[shift][0];
          if(sell_flag !=1)
        Alert("New Signal Selling =>", Symbol()); 
        else
         sell_flag=1;
         buy_flag=0;
      }
    }
    return(0);
  }
//----
  if( Period() == 60 )
  {
    ArrayInitialize(ur,0);
    ArrayInitialize(h1,0);
    ArrayInitialize(l1,0);
    for( shift = NumBars; shift>=0; shift-- )
    {
      if( TimeMinute(Time[shift])==0 && (TimeHour(Time[shift])==0 || TimeHour(Time[shift])==4 || TimeHour(Time[shift])==8 || TimeHour(Time[shift])==12 || TimeHour(Time[shift])==16 || TimeHour(Time[shift])==20) )
      {
        Nbar = -1;
        k = 0;
        mm = 0;
        n = shift - 6;
        while( n <= shift + HL_period * 4 + 3 )
        {
          if( n < 0 )
          {
            Nbar = 0;
            n = 1;
          }
          if( TimeMinute(Time[n])==0 && (TimeHour(Time[n])==0 || TimeHour(Time[n])==4 || TimeHour(Time[n])==8 || TimeHour(Time[n])==12 || TimeHour(Time[n])==16 || TimeHour(Time[n])==20) && Nbar == -1 )
          {
            Nbar = n;
            mm = n;
            n++;
            continue;
          }
          if( TimeMinute(Time[n])==0 && (TimeHour(Time[n])==0 || TimeHour(Time[n])==4 || TimeHour(Time[n])==8 || TimeHour(Time[n])==12 || TimeHour(Time[n])==16 || TimeHour(Time[n])==20) && Nbar != -1 )
          {
            h1[k] = Highest(NULL,0,MODE_HIGH,n - Nbar,n-(n-Nbar)+1);
            l1[k] = Lowest(NULL,0,MODE_LOW,n - Nbar,n-(n-Nbar)+1);
            k++;
            Nbar = n;
          }
          n++;
        }
        MaH = 0;
        MaL = 0;
        for( n = 0; n<HL_period; n++ )
        {
          MaH = MaH + High[h1[n]]+spread;
          MaL = MaL + Low[l1[n]];
        }
        MaH = MaH / HL_period;
        MaL = MaL / HL_period;
        ur[shift][0] = MaH;
        ur[shift][1] = MaL;
        if( Close[mm + 1]+spread/2 >= MaH ) { ll = 1; hh = 0; }
        if( Close[mm + 1]+spread/2 <= MaL ) { hh = 1; ll = 0; }
      }
      if( ur[shift,1] == 0 )
      {
        ur[shift][0] = ur[shift + 1][0];
        ur[shift][1] = ur[shift + 1][1];
      }
      if( ll == 1 )
      {
        ExtMapBuffer2[shift]=0;
        ExtMapBuffer1[shift]=ur[shift][1];
        if(buy_flag !=1)
         Alert("New Signal Buying =>", Symbol()); 
         else
         sell_flag=0;
         buy_flag=1;
      }
      if( hh == 1 )
      {
        ExtMapBuffer1[shift]=0;
        ExtMapBuffer2[shift]=ur[shift][0];
          if(sell_flag !=1)
        Alert("New Signal Selling =>", Symbol()); 
        else
         sell_flag=1;
         buy_flag=0;
      }
    }
    return(0);
  }
//--------------  
  if( Period() == 240 )
  {
    ArrayInitialize(ur,0);
    ArrayInitialize(h1,0);
    ArrayInitialize(l1,0);
    for( shift = NumBars; shift>=0; shift-- )
    {
      if( TimeDay(Time[shift + 1]) != TimeDay(Time[shift]) )
      {
        Nbar = -1;
        k = 0;
        mm = 0;
        n = shift - 8;
        while( n <= shift + HL_period * 8 + 4 )
        {
          if( n < 0 )
          {
            Nbar = 0;
            n = 1;
          }
          if( TimeDay(Time[n + 1]) != TimeDay(Time[n]) && Nbar == -1 )
          {
            Nbar = n;
            mm = n;
            n++;
            continue;
          }
          if( TimeDay(Time[n + 1]) != TimeDay(Time[n]) && Nbar != -1 )
          {
            h1[k] = Highest(NULL,0,MODE_HIGH,n - Nbar,n-(n-Nbar)+1);
            l1[k] = Lowest(NULL,0,MODE_LOW,n - Nbar,n-(n-Nbar)+1);
            k++;
            Nbar = n;
          }
          n++;
        }
        MaH = 0;
        MaL = 0;
        for( n = 0; n<HL_period; n++ )
        {
          MaH = MaH + High[h1[n]]+spread;
          MaL = MaL + Low[l1[n]];
        }
        MaH = MaH / HL_period;
        MaL = MaL / HL_period;
        ur[shift][0] = MaH;
        ur[shift][1] = MaL;
        if( Close[mm + 1]+spread/2 >= MaH ) { ll = 1; hh = 0; }
        if( Close[mm + 1]+spread/2 <= MaL ) { hh = 1; ll = 0; }
      }
      if( ur[shift,1] == 0 )
      {
        ur[shift][0] = ur[shift + 1][0];
        ur[shift][1] = ur[shift + 1][1];
      }
      if( ll == 1 )
      {
        ExtMapBuffer2[shift]=0;
        ExtMapBuffer1[shift]=ur[shift][1];
        if(buy_flag !=1)
         Alert("New Signal Buying =>", Symbol()); 
         else
         sell_flag=0;
         buy_flag=1;
      }
      if( hh == 1 )
      {
        ExtMapBuffer1[shift]=0;
        ExtMapBuffer2[shift]=ur[shift][0];
          if(sell_flag !=1)
        Alert("New Signal Selling =>", Symbol()); 
        else
         sell_flag=1;
         buy_flag=0;
      }
    }
    return(0);
  }
//----
  if( Period() == 1440 )
  {
    ArrayInitialize(ur,0);
    ArrayInitialize(h1,0);
    ArrayInitialize(l1,0);
    for( shift = NumBars; shift>=0; shift-- )
    {
      if( TimeDayOfWeek(Time[shift + 1]) == 5 && TimeDayOfWeek(Time[shift]) == 1 )
      {
        Nbar = -1;
        k = 0;
        mm = 0;
        n = shift - 6;
        while( n <= shift + HL_period * 5 + 3 )
        {
          if( n < 0 )
          {
            Nbar = 0;
            n = 1;
          }
          if( TimeDayOfWeek(Time[n + 1])==5 && TimeDayOfWeek(Time[n])==1 && Nbar == -1 )
          {
            Nbar = n;
            mm = n;
            n++;
            continue;
          }
          if( TimeDayOfWeek(Time[n + 1])==5 && TimeDayOfWeek(Time[n])==1 && Nbar != -1 )
          {
            h1[k] = Highest(NULL,0,MODE_HIGH,n - Nbar,n-(n-Nbar)+1);
            l1[k] = Lowest(NULL,0,MODE_LOW,n - Nbar,n-(n-Nbar)+1);
            k++;
            Nbar = n;
          }
          n++;
        }
        MaH = 0;
        MaL = 0;
        for( n = 0; n<HL_period; n++ )
        {
          MaH = MaH + High[h1[n]]+spread;
          MaL = MaL + Low[l1[n]];
        }
        MaH = MaH / HL_period;
        MaL = MaL / HL_period;
        ur[shift][0] = MaH;
        ur[shift][1] = MaL;
        if( Close[mm + 1]+spread/2 >= MaH ) { ll = 1; hh = 0; }
        if( Close[mm + 1]+spread/2 <= MaL ) { hh = 1; ll = 0; }
      }
      if( ur[shift,1] == 0 )
      {
        ur[shift][0] = ur[shift + 1][0];
        ur[shift][1] = ur[shift + 1][1];
      }
      if( ll == 1 )
      {
        ExtMapBuffer2[shift]=0;
        ExtMapBuffer1[shift]=ur[shift][1];
        if(buy_flag !=1)
         Alert("New Signal Buying =>", Symbol()); 
         else
         sell_flag=0;
         buy_flag=1;
      }
      if( hh == 1 )
      {
        ExtMapBuffer1[shift]=0;
        ExtMapBuffer2[shift]=ur[shift][0];
          if(sell_flag !=1)
        Alert("New Signal Selling =>", Symbol()); 
        else
         sell_flag=1;
         buy_flag=0;
      }
    }
    return(0);
  }
//----
  if( Period() == 10080 )
  {
    ArrayInitialize(ur,0);
    ArrayInitialize(h1,0);
    ArrayInitialize(l1,0);
    for( shift = NumBars; shift>=0; shift-- )
    {
      if( TimeMonth(Time[shift + 1]+1440*60) != TimeMonth(Time[shift]+1440*60) )
      {
        Nbar = -1;
        k = 0;
        mm = 0;
        n = shift - 8;
        while( n <= shift + HL_period * 8 + 4 )
        {
          if( n < 0 )
          {
            Nbar = 0;
            n = 1;
          }
          if( TimeMonth(Time[n + 1]+1440*60) != TimeMonth(Time[n]+1440*60) && Nbar == -1 )
          {
            Nbar = n;
            mm = n;
            if( n>NumBars-18 ) Print(TimeToStr(Time[n]));
            n++;
            continue;
          }
          if( TimeMonth(Time[n + 1]+1440*60) != TimeMonth(Time[n]+1440*60) && Nbar != -1 )
          {
            h1[k] = Highest(NULL,0,MODE_HIGH,n - Nbar,n-(n-Nbar)+1);
            l1[k] = Lowest(NULL,0,MODE_LOW,n - Nbar,n-(n-Nbar)+1);
            k++;
            Nbar = n;
          }
          n++;
        }
        MaH = 0;
        MaL = 0;
        for( n = 0; n<HL_period; n++ )
        {
          MaH = MaH + High[h1[n]]+spread;
          MaL = MaL + Low[l1[n]];
        }
        MaH = MaH / HL_period;
        MaL = MaL / HL_period;
        ur[shift][0] = MaH;
        ur[shift][1] = MaL;
        if( Close[mm + 1]+spread/2 >= MaH ) { ll = 1; hh = 0; }
        if( Close[mm + 1]+spread/2 <= MaL ) { hh = 1; ll = 0; }
      }
      if( ur[shift,1] == 0 )
      {
        ur[shift][0] = ur[shift + 1][0];
        ur[shift][1] = ur[shift + 1][1];
      }
      if( ll == 1 )
      {
        ExtMapBuffer2[shift]=0;
        ExtMapBuffer1[shift]=ur[shift][1];
        if(buy_flag !=1)
         Alert("New Signal Buying =>", Symbol()); 
         else
         sell_flag=0;
         buy_flag=1;
      }
      if( hh == 1 )
      {
        ExtMapBuffer1[shift]=0;
        ExtMapBuffer2[shift]=ur[shift][0];
          if(sell_flag !=1)
        Alert("New Signal Selling =>", Symbol()); 
        else
         sell_flag=1;
         buy_flag=0;
      }
    }
    return(0);
  }
//----
  return(0);
}
//+------------------------------------------------------------------+

Comments