MT5 Indicator Visibility

 

Hey Guys,

Im facing this issue which is probably super simple to you guys but cant seem to figure it out. I made this indicator that basically just shows the candle color/direction of a different timeframe, so for example you are on a 5 min chart and will show you a histogram of what color or direction the 4 hour is. but with the current error i can only see the 4hr when im on 4hr tf, cannot see 4hr data on 5min.

Please let me know if i need to clarify.


//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_style1 STYLE_SOLID
#property indicator_width1 3
#property indicator_color1 0xFFFFFF
#property indicator_label1 "Up"

#property indicator_type2 DRAW_HISTOGRAM
#property indicator_style2 STYLE_SOLID
#property indicator_width2 3
#property indicator_color2 0x000000
#property indicator_label2 "Down"

input ENUM_TIMEFRAMES PeriodSelect=PERIOD_M5;

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

double myPoint; //initialized in OnInit
double Close[];
datetime TimeShift[];
double Open[];
double Low[];
double High[];

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

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   SetIndexBuffer(0, Buffer1);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   SetIndexBuffer(1, Buffer2);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   //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, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   if(CopyClose(Symbol(), PeriodSelect, 0, rates_total, Close) <= 0) return(rates_total);
   ArraySetAsSeries(Close, true);
   if(CopyTime(Symbol(), PERIOD_CURRENT, 0, rates_total, TimeShift) <= 0) return(rates_total);
   ArraySetAsSeries(TimeShift, true);
   if(CopyOpen(Symbol(), PeriodSelect, 0, rates_total, Open) <= 0) return(rates_total);
   ArraySetAsSeries(Open, true);
   if(CopyLow(Symbol(), PERIOD_CURRENT, 0, rates_total, Low) <= 0) return(rates_total);
   ArraySetAsSeries(Low, true);
   if(CopyHigh(Symbol(), PERIOD_CURRENT, 0, rates_total, High) <= 0) return(rates_total);
   ArraySetAsSeries(High, true);
   //--- 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   
      
      int barshift_H4 = iBarShift(Symbol(), PeriodSelect, TimeShift[i]);
      if(barshift_H4 < 0) continue;
      
      //Indicator Buffer 1
      if(Close[barshift_H4] > Open[barshift_H4] //Candlestick Close > Candlestick Open
      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(Close[barshift_H4] < Open[barshift_H4] //Candlestick Close < Candlestick Open
      )
        {
         Buffer2[i] = High[i]; //Set indicator value at Candlestick High
        }
      else
        {
         Buffer2[i] = 0;
        }
     }
   return(rates_total);
  }
 
Here is a pic of the issue. error
 
You are working with
Period_Current

So it won't display the histogram or direction of the 4H chart, cause period current is 5M.

if you want 4H charts to be displayed in all other TF only, then in the copy buffer 

Copy....()
// functions, Use PERIOD_H4
 
Thank-god Avwerosuoghene Odukudu:
You are working with

So it won't display the histogram or direction of the 4H chart, cause period current is 5M.

if you want 4H charts to be displayed in all other TF only, then in the copy buffer 

i have that variable PeriodSelect, can i change Period_current to PeriodSelect? i tried that and it still showed same error. Can you show me on my code where the changes need to be? Thank you for all the help!!!

 

Check the code, I can tell

int barshift_H4 = iBarShift(Symbol(), PeriodSelect, TimeShift[i]);

is returning "-1" when you are working on other TF less than "PeriodSelect"

 
Thank-god Avwerosuoghene Odukudu:

Check the code, I can tell

is returning "-1" when you are working on other TF less than "PeriodSelect"

you are right but im not sure how to fix it, i tried changing it around and still getting blank indicator on other timeframes. 

 
c0mrade:

you are right but im not sure how to fix it, i tried changing it around and still getting blank indicator on other timeframes. 

Am not sure why you included the iBarShift(), since you have copied the Time, close, open etc, you could simply compare the values before and after since everything is already in series, using normal iterations. You understand your code best and what you are trying to achieve, so you can think how best to solve the problem. Good luck

 
Thank-god Avwerosuoghene Odukudu:

Am not sure why you included the iBarShift(), since you have copied the Time, close, open etc, you could simply compare the values before and after since everything is already in series, using normal iterations. You understand your code best and what you are trying to achieve, so you can think how best to solve the problem. Good luck

If you can solve this a different way please show me how. I am just learning how to code so I am using examples from mql website and other users code. 
 
Anyone able to show me the proper way? Working code? I can’t get it to show on other time frames. 
 
Thank-god Avwerosuoghene Odukudu:
You are working with

So it won't display the histogram or direction of the 4H chart, cause period current is 5M.

if you want 4H charts to be displayed in all other TF only, then in the copy buffer 

can you fix the code for me so i can see example of what im doing wrong?