Extraneous data in indicator subwindow

Pauper31
36

Hello all,

I created an MT5 indicator lately that is drawn in a subwindow. For some reason, a bar is drawn the subwindow that I do not expect. See image below:


Extra data drawn on subwindow

I converted this indicator from MQL4. The multi-colored line at the bottom of the subwindow should not be there.

I hope you can help me spot the error in the code below: Thanks in advance.

#property copyright "Metaquotes Corporation | 13 June 2022"
#property version   "1.00"
#property indicator_separate_window

// The design idea is from http://www.forexmarkethours.com/ 

#property indicator_minimum 0
#property indicator_maximum 1

#property indicator_buffers 4
#property indicator_plots 4

#property indicator_type1  DRAW_ARROW
#property indicator_label1 "Sydney"
#property indicator_color1 clrBlue
#property indicator_width1 1

#property indicator_type2  DRAW_ARROW
#property indicator_label2 "Tokyo"
#property indicator_color2 clrGold
#property indicator_width2 1

#property indicator_type3  DRAW_ARROW
#property indicator_label3 "London"
#property indicator_color3 clrGreen
#property indicator_width3 1

#property indicator_type4  DRAW_ARROW
#property indicator_label4 "NewYork"
#property indicator_color4 clrOrangeRed
#property indicator_width4 1

extern int  LocalGMT = 8;     // Local timezone GMT offset
extern int  BrokerGMT = 1;    // Broker MT4's GMT offset
extern bool ShowText = true;  // Show each session on the left top corner.

int SessionLocal[8], SessionBroker[8];
string short_name;

double Sydney[];
double Tokyo[];
double London[];
double NewYork[];
//+------------------------------------------------------------------+
int OnInit() {

   SetIndexBuffer(0,Sydney,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_ARROW,110);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   SetIndexBuffer(1,Tokyo,INDICATOR_DATA);
   PlotIndexSetInteger(1,PLOT_ARROW,110);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   SetIndexBuffer(2,London,INDICATOR_DATA);
   PlotIndexSetInteger(2,PLOT_ARROW,110);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
      
   SetIndexBuffer(3,NewYork,INDICATOR_DATA);
   PlotIndexSetInteger(3,PLOT_ARROW,110);
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   
   short_name = "Market Sessions";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   
   SessionLocalTime(LocalGMT);
   SessionBroker(BrokerGMT); 
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
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[]) {

   if(rates_total<0) return(0);
   if(prev_calculated<0 || prev_calculated>rates_total) return(0);
   int limit, hr;
   if(prev_calculated==0) { limit=rates_total; ArrayInitialize(Sydney,0.0); ArrayInitialize(Tokyo,0.0); ArrayInitialize(NewYork,0.0); ArrayInitialize(London,0.0); }
   else limit=rates_total-prev_calculated; 
   
   for(int i=0; i<limit; i++) {     
      hr = TimeHourMQL4(time[i]);
      DrawBrokerSession(hr,i);
   }
   if(ShowText) ShowSessionLocal();
    
   return(rates_total);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {

   Comment("");
}
//+------------------------------------------------------------------+
void SessionLocalTime(int LocalGMTTime) {

   int gmt_diff = 8-LocalGMTTime;
            
   SessionLocal[0] = 6 - gmt_diff;
   SessionLocal[1] = 14 - gmt_diff;
   SessionLocal[2] = 8 - gmt_diff;
   SessionLocal[3] = 16 - gmt_diff;
   SessionLocal[4] = 16 - gmt_diff;
   SessionLocal[5] = 0 - gmt_diff;
   SessionLocal[6] = 21 - gmt_diff;
   SessionLocal[7] = 5 - gmt_diff;
      
   for(int i=0; i<8; i++) if(SessionLocal[i]<0) SessionLocal[i] += 24;
}
//+------------------------------------------------------------------+
void SessionBroker(int BrokerGMTTime) {

   int gmt_diff = 1-BrokerGMTTime;
            
   SessionBroker[0] = 23 - gmt_diff;
   SessionBroker[1] = 7 - gmt_diff;
   SessionBroker[2] = 1 - gmt_diff;
   SessionBroker[3] = 9 - gmt_diff;
   SessionBroker[4] = 9 - gmt_diff;
   SessionBroker[5] = 17 - gmt_diff;
   SessionBroker[6] = 14 - gmt_diff;
   SessionBroker[7] = 22 - gmt_diff;
      
   for(int i=0; i<8; i++) if(SessionBroker[i]<0) SessionBroker[i] += 24;      
}  

//+------------------------------------------------------------------+
void DrawBrokerSession(int hrs,int index) {

   if(SessionBroker[0]<SessionBroker[1])  
      if(SessionBroker[0]<=hrs && hrs <=SessionBroker[1]) Sydney[index] = 0.8; 
   if(SessionBroker[0]>SessionBroker[1]) { 
      if(hrs>=SessionBroker[0] && hrs<=23)  Sydney[index] = 0.8;
      if(hrs>=0 && hrs<=SessionBroker[1])   Sydney[index] = 0.8;
   }
   if(SessionBroker[2]<SessionBroker[3])  
      if(SessionBroker[2]<=hrs && hrs <=SessionBroker[3]) Tokyo[index] = 0.6;
   if(SessionBroker[2]>SessionBroker[3]) { 
      if(hrs>=SessionBroker[2] && hrs<=23)  Tokyo[index] = 0.6;
      if(hrs>=0 && hrs<=SessionBroker[3])   Tokyo[index] = 0.6;
   }
   if(SessionBroker[4]<SessionBroker[5])  
      if(SessionBroker[4]<=hrs && hrs <=SessionBroker[5]) London[index] = 0.4;
   if(SessionBroker[4]>SessionBroker[5]) { 
      if(hrs>=SessionBroker[4] && hrs<=23)  London[index] = 0.4;
      if(hrs>=0 && hrs<=SessionBroker[5])   London[index] = 0.4;
   }
   if(SessionBroker[6]<SessionBroker[7])  
      if(SessionBroker[6]<=hrs && hrs <=SessionBroker[7]) NewYork[index] = 0.2;
   if(SessionBroker[6]>SessionBroker[7]) { 
      if(hrs>=SessionBroker[6] && hrs<=23)  NewYork[index] = 0.2;
      if(hrs>=0 && hrs<=SessionBroker[7])   NewYork[index] = 0.2;
   }     
}
//+------------------------------------------------------------------+
void ShowSessionLocal() {

   Comment("\n", 
           "Forex market session on local time (GMT ",LocalGMT,")   ",TimeToString(TimeLocal(),TIME_SECONDS),"\n",
           "Sydney   session:  ", IntegerToString(SessionLocal[0])+":00 ----", IntegerToString(SessionLocal[1])+":59","\n",
           "Tokyo    session:  ", IntegerToString(SessionLocal[2])+":00 ----", IntegerToString(SessionLocal[3])+":59","\n",
           "London   session:  ", IntegerToString(SessionLocal[4])+":00 ----", IntegerToString(SessionLocal[5])+":59","\n",
           "NewYork  session:  ", IntegerToString(SessionLocal[6])+":00 ----", IntegerToString(SessionLocal[7])+":59","\n");              
}
//+------------------------------------------------------------------+
int TimeHourMQL4(datetime datetoday) {
                                            
   MqlDateTime tm;
   TimeToStruct(datetoday,tm);           
   return(tm.hour);
}
Navdeep Singh
11722
Pauper31:

Hello all,

I created an MT5 indicator lately that is drawn in a subwindow. For some reason, a bar is drawn the subwindow that I do not expect. See image below:


I converted this indicator from MQL4. The multi-colored line at the bottom of the subwindow should not be there.

I hope you can help me spot the error in the code below: Thanks in advance.

I don't see where you are setting value equal to empty when condition is not valid for a buffer
Pauper31
36
Pauper31  
Navdeep Singh #:
I don't see where you are setting value equal to empty when condition is not valid for a buffer

Hi thanks for the response. I thought initialization would do. Can you give a tip or clearer suggestion on what is wrong?

William Roeder
27845
William Roeder  
   for(int i=0; i<limit; i++) {     
      Sydney[i]=Tokyo[i]=London[i]=NewYork[i]=EMPTY_VALUE;
      ⋮
Pauper31
36
Pauper31  
William Roeder #:

Thanks William. It works like charm!

Here is what I did.

   if(prev_calculated==0) { 
      limit=rates_total; 
      for(int i=0; i<limit; i++) { Sydney[i] = Tokyo[i] = London[i] = NewYork[i] = EMPTY_VALUE; } 
   }
   else limit=rates_total-prev_calculated;
Thank-god Avwerosuoghene Odukudu
1335
Pauper31 #:

Thanks William. It works like charm!

Here is what I did.

You initialized to zero, 
if(prev_calculated==0) { limit=rates_total; ArrayInitialize(Sydney,0.0); ArrayInitialize(Tokyo,0.0); ArrayInitialize(NewYork,0.0); ArrayInitialize(London,0.0); }
   else limit=rates_total-prev_calculated; 
whereas empty plot was regarded as EMPTY_VALUE,
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
So, zero(0) would be plotted for all regions not in session.