gap indicator

 

good day all

i tried coding a gap indicator based on the rules in the attached pic but as an amateur i don't think it worked, can anyone kindly help?

Files:
gap.png  26 kb
 
Enter the code correctly
 
so basically tthis is what i was trying to do 

as soon as a gap is detected from the close price of the previous bar to the open price of the new bar, an arrow will be printed based on the following rules:



1) A down signal appears when the previous bar was a bear bar and there is a gap by atleast 1 pip lower on the open of the next bar. And vice-versa. arrow will not repaint or disappear if the gap is filled

2) user definable number of pips for the gap



3) And a look back function



will appreciate the help greatly! thank you

 
#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0xFFAA00
#property indicator_label2 "Buy"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | gap1 @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 241);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      if(Open[i] > Close[1+i] //Candlestick Open > Candlestick Close
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(Open[i] < Close[1+i] //Candlestick Open < Candlestick Close
      )
        {
         Buffer2[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer2[i] = 0;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
ahmedk:

I think you should name one of them "Sell":

#property indicator_label1 "Buy"
#property indicator_label2 "Buy"

Your two conditions basically resulted in setting either Buffer1 or Buffer2 every bar.

That explains why you mentioned "don't think it worked". So you'll need to do more work in devising your test conditions.

      if(Open[i] > Close[1+i] //Candlestick Open > Candlestick Close
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(Open[i] < Close[1+i] //Candlestick Open < Candlestick Close
      )
        {
         Buffer2[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer2[i] = 0;
        }
 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post and remove your redundant one.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Please don't post a link to or attach an image, just insert the image.
              Messages Editor

  3. Why did you post your MT4 question in the Root / MT5 Indicators section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  4. ahmedk: so basically tthis is what i was trying to do as soon as a gap is detected from the close price
    Your image and your gap has nothing to do with close price. Only previous candle low vs next candle high, etc.

  5.    int limit = rates_total - prev_calculated;
       //--- counting from 0 to rates_total
       ArraySetAsSeries(Buffer1, true);
       ArraySetAsSeries(Buffer2, true);
       //--- initial zero
       if(prev_calculated < 1)
         {
          ArrayInitialize(Buffer1, 0);
          ArrayInitialize(Buffer2, 0);
         }
       else
          limit++;
       
       //--- main loop
       for(int i = limit-1; i >= 0; i--){
          if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation
    
    The set as series isn't necessary in MT4, nor is the initialization.
  6. The increment, and terrible if, can be avoided.
              How to do your lookbacks correctly.
Reason: