Multitimeframe indicator

 
Hello, I have an indicator that works on the basis of several timeframes. However, I have the problem that the buffers are not output correctly. I tried to find the problem but it didn't work. Would be grateful for help.
//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                                        Copyright 2019, Arthur S. |
//|                    https://www.mql5.com/en/users/michael12345678 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Arthur S."
#property link      "https://www.mql5.com/en/users/michael12345678"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 20
#property indicator_plots 10

#property indicator_label1 "Up_Period1"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrBlue

#property indicator_label2 "Down_Period1"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed

#property indicator_label3 "Up_Period5"
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrAqua

#property indicator_label4 "Down_Period5"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrOrangeRed

#property indicator_label5 "Up_Period15"
#property indicator_type5 DRAW_ARROW
#property indicator_color5 clrLightCoral

#property indicator_label6 "Down_Period15"
#property indicator_type6 DRAW_ARROW
#property indicator_color6 clrDarkTurquoise

#property indicator_label7 "Up_Period30"
#property indicator_type7 DRAW_ARROW
#property indicator_color7 clrHotPink

#property indicator_label8 "Down_Period30"
#property indicator_type8 DRAW_ARROW
#property indicator_color8 clrCadetBlue

#property indicator_label9 "Up_Period60"
#property indicator_type9 DRAW_ARROW
#property indicator_color9 clrCrimson

#property indicator_label10 "Down_Period60"
#property indicator_type10 DRAW_ARROW
#property indicator_color10 clrDarkMagenta

#property indicator_type11 DRAW_NONE
#property indicator_type12 DRAW_NONE
#property indicator_type13 DRAW_NONE
#property indicator_type14 DRAW_NONE
#property indicator_type15 DRAW_NONE
#property indicator_type16 DRAW_NONE
#property indicator_type17 DRAW_NONE
#property indicator_type18 DRAW_NONE
#property indicator_type19 DRAW_NONE
#property indicator_type20 DRAW_NONE

int MovingAverage12_Period1;
int MovingAverage22_Period1;
int MovingAverage12_Period5;
int MovingAverage22_Period5;
int MovingAverage12_Period15;
int MovingAverage22_Period15;
int MovingAverage12_Period30;
int MovingAverage22_Period30;
int MovingAverage12_Period60;
int MovingAverage22_Period60;

double MovingAverageBuffer12_Period1[];
double MovingAverageBuffer22_Period1[];
double MovingAverageBuffer12_Period5[];
double MovingAverageBuffer22_Period5[];
double MovingAverageBuffer12_Period15[];
double MovingAverageBuffer22_Period15[];
double MovingAverageBuffer12_Period30[];
double MovingAverageBuffer22_Period30[];
double MovingAverageBuffer12_Period60[];
double MovingAverageBuffer22_Period60[];

double UpBuffer_Period1[];
double DownBuffer_Period1[];
double UpBuffer_Period5[];
double DownBuffer_Period5[];
double UpBuffer_Period15[];
double DownBuffer_Period15[];
double UpBuffer_Period30[];
double DownBuffer_Period30[];
double UpBuffer_Period60[];
double DownBuffer_Period60[];

bool Up_Period1=false;
bool Down_Period1=false;
bool Up_Period5=false;
bool Down_Period5=false;
bool Up_Period15=false;
bool Down_Period15=false;
bool Up_Period30=false;
bool Down_Period30=false;
bool Up_Period60=false;
bool Down_Period60=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0,UpBuffer_Period1,INDICATOR_DATA);
   SetIndexBuffer(1,DownBuffer_Period1,INDICATOR_DATA);
   SetIndexBuffer(2,UpBuffer_Period5,INDICATOR_DATA);
   SetIndexBuffer(3,DownBuffer_Period5,INDICATOR_DATA);
   SetIndexBuffer(4,UpBuffer_Period15,INDICATOR_DATA);
   SetIndexBuffer(5,DownBuffer_Period15,INDICATOR_DATA);
   SetIndexBuffer(6,UpBuffer_Period30,INDICATOR_DATA);
   SetIndexBuffer(7,DownBuffer_Period30,INDICATOR_DATA);
   SetIndexBuffer(8,UpBuffer_Period60,INDICATOR_DATA);
   SetIndexBuffer(9,DownBuffer_Period60,INDICATOR_DATA);
   SetIndexBuffer(10,MovingAverageBuffer12_Period1,INDICATOR_DATA);
   SetIndexBuffer(11,MovingAverageBuffer22_Period1,INDICATOR_DATA);
   SetIndexBuffer(12,MovingAverageBuffer12_Period5,INDICATOR_DATA);
   SetIndexBuffer(13,MovingAverageBuffer22_Period5,INDICATOR_DATA);
   SetIndexBuffer(14,MovingAverageBuffer12_Period15,INDICATOR_DATA);
   SetIndexBuffer(15,MovingAverageBuffer22_Period15,INDICATOR_DATA);
   SetIndexBuffer(16,MovingAverageBuffer12_Period30,INDICATOR_DATA);
   SetIndexBuffer(17,MovingAverageBuffer22_Period30,INDICATOR_DATA);
   SetIndexBuffer(18,MovingAverageBuffer12_Period60,INDICATOR_DATA);
   SetIndexBuffer(19,MovingAverageBuffer22_Period60,INDICATOR_DATA);
   
   MovingAverage12_Period1=iMA(_Symbol,PERIOD_M1,12,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage12_Period1==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage22_Period1=iMA(_Symbol,PERIOD_M1,22,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage22_Period1==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage12_Period5=iMA(_Symbol,PERIOD_M5,12,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage12_Period5==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage22_Period5=iMA(_Symbol,PERIOD_M5,22,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage22_Period5==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage12_Period15=iMA(_Symbol,PERIOD_M15,12,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage12_Period15==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage22_Period15=iMA(_Symbol,PERIOD_M15,22,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage22_Period15==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage12_Period30=iMA(_Symbol,PERIOD_M30,12,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage12_Period30==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage22_Period30=iMA(_Symbol,PERIOD_M30,22,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage22_Period30==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage12_Period60=iMA(_Symbol,PERIOD_H1,12,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage12_Period60==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   MovingAverage22_Period60=iMA(_Symbol,PERIOD_H1,22,0,MODE_EMA,PRICE_OPEN);
   if(MovingAverage22_Period60==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   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[])
{
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(UpBuffer_Period1,true);
   ArraySetAsSeries(DownBuffer_Period1,true);
   ArraySetAsSeries(UpBuffer_Period5,true);
   ArraySetAsSeries(DownBuffer_Period5,true);
   ArraySetAsSeries(UpBuffer_Period15,true);
   ArraySetAsSeries(DownBuffer_Period15,true);
   ArraySetAsSeries(UpBuffer_Period30,true);
   ArraySetAsSeries(DownBuffer_Period30,true);
   ArraySetAsSeries(UpBuffer_Period60,true);
   ArraySetAsSeries(DownBuffer_Period60,true);
   ArraySetAsSeries(MovingAverageBuffer12_Period1,true);
   ArraySetAsSeries(MovingAverageBuffer22_Period1,true);
   ArraySetAsSeries(MovingAverageBuffer12_Period5,true);
   ArraySetAsSeries(MovingAverageBuffer22_Period5,true);
   ArraySetAsSeries(MovingAverageBuffer12_Period15,true);
   ArraySetAsSeries(MovingAverageBuffer22_Period15,true);
   ArraySetAsSeries(MovingAverageBuffer12_Period30,true);
   ArraySetAsSeries(MovingAverageBuffer22_Period30,true);
   ArraySetAsSeries(MovingAverageBuffer12_Period60,true);
   ArraySetAsSeries(MovingAverageBuffer22_Period60,true);
   
   int limit=MathMin(rates_total-1,rates_total-prev_calculated);
   
   int copied12_Period1=CopyBuffer(MovingAverage12_Period1,0,0,limit,MovingAverageBuffer12_Period1);
   int copied22_Period1=CopyBuffer(MovingAverage22_Period1,0,0,limit,MovingAverageBuffer22_Period1);
   int copied12_Period5=CopyBuffer(MovingAverage12_Period5,0,0,limit,MovingAverageBuffer12_Period5);
   int copied22_Period5=CopyBuffer(MovingAverage22_Period5,0,0,limit,MovingAverageBuffer22_Period5);
   int copied12_Period15=CopyBuffer(MovingAverage12_Period15,0,0,limit,MovingAverageBuffer12_Period15);
   int copied22_Period15=CopyBuffer(MovingAverage22_Period15,0,0,limit,MovingAverageBuffer22_Period15);
   int copied12_Period30=CopyBuffer(MovingAverage12_Period30,0,0,limit,MovingAverageBuffer12_Period30);
   int copied22_Period30=CopyBuffer(MovingAverage22_Period30,0,0,limit,MovingAverageBuffer22_Period30);
   int copied12_Period60=CopyBuffer(MovingAverage12_Period60,0,0,limit,MovingAverageBuffer12_Period60);
   int copied22_Period60=CopyBuffer(MovingAverage22_Period60,0,0,limit,MovingAverageBuffer22_Period60);
   
   for(int i=0;i<limit;i++)
   {
      UpBuffer_Period1[i]=EMPTY_VALUE;
      DownBuffer_Period1[i]=EMPTY_VALUE;
      UpBuffer_Period5[i]=EMPTY_VALUE;
      DownBuffer_Period5[i]=EMPTY_VALUE;
      UpBuffer_Period15[i]=EMPTY_VALUE;
      DownBuffer_Period15[i]=EMPTY_VALUE;
      UpBuffer_Period30[i]=EMPTY_VALUE;
      DownBuffer_Period30[i]=EMPTY_VALUE;
      UpBuffer_Period60[i]=EMPTY_VALUE;
      DownBuffer_Period60[i]=EMPTY_VALUE;
      
      if(MovingAverageBuffer12_Period1[i]>MovingAverageBuffer22_Period1[i] && MovingAverageBuffer12_Period1[i+1]<MovingAverageBuffer22_Period1[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M1,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         UpBuffer_Period1[Bar]=low[Bar]-0.00030;
            
         Up_Period1=true;
      }
      else if(MovingAverageBuffer12_Period1[i]<MovingAverageBuffer22_Period1[i] && MovingAverageBuffer12_Period1[i+1]>MovingAverageBuffer22_Period1[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M1,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         DownBuffer_Period1[Bar]=high[Bar]+0.00030;
            
         Down_Period1=true; 
      }
         
      if(MovingAverageBuffer12_Period5[i]>MovingAverageBuffer22_Period5[i] && MovingAverageBuffer12_Period5[i+1]<MovingAverageBuffer22_Period5[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M5,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         UpBuffer_Period5[Bar]=low[Bar]-0.00060;
            
         Up_Period5=true;
      }
      else if(MovingAverageBuffer12_Period5[i]<MovingAverageBuffer22_Period5[i] && MovingAverageBuffer12_Period5[i+1]>MovingAverageBuffer22_Period5[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M5,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         DownBuffer_Period5[Bar]=high[Bar]+0.00060;
            
         Down_Period5=true;
      }
         
      if(MovingAverageBuffer12_Period15[i]>MovingAverageBuffer22_Period15[i] && MovingAverageBuffer12_Period15[i+1]<MovingAverageBuffer22_Period15[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M15,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         UpBuffer_Period15[Bar]=low[Bar]-0.00090;
            
         Up_Period15=true;
      }
      else if(MovingAverageBuffer12_Period15[i]<MovingAverageBuffer22_Period15[i] && MovingAverageBuffer12_Period15[i+1]>MovingAverageBuffer22_Period15[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M15,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         DownBuffer_Period15[Bar]=high[Bar]+0.00090;
            
         Down_Period15=true;
      }
         
      if(MovingAverageBuffer12_Period30[i]>MovingAverageBuffer22_Period30[i] && MovingAverageBuffer12_Period30[i+1]<MovingAverageBuffer22_Period30[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M30,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         UpBuffer_Period30[Bar]=low[Bar]-0.00120;
            
         Up_Period30=true;
      }
      else if(MovingAverageBuffer12_Period30[i]<MovingAverageBuffer22_Period30[i] && MovingAverageBuffer12_Period30[i+1]>MovingAverageBuffer22_Period30[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_M30,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         DownBuffer_Period30[Bar]=high[Bar]+0.00120;
            
         Down_Period30=true;
      }
         
      if(MovingAverageBuffer12_Period60[i]>MovingAverageBuffer22_Period60[i] && MovingAverageBuffer12_Period60[i+1]<MovingAverageBuffer22_Period60[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_H1,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         UpBuffer_Period60[Bar]=low[Bar]-0.00150;
            
         Up_Period60=true;
      }
      else if(MovingAverageBuffer12_Period60[i]<MovingAverageBuffer22_Period60[i] && MovingAverageBuffer12_Period60[i+1]>MovingAverageBuffer22_Period60[i+1])
      {
         datetime CandleTime=iTime(_Symbol,PERIOD_H1,i);
            
         uint Bar=iBarShift(_Symbol,_Period,CandleTime,true);
            
         DownBuffer_Period60[Bar]=high[Bar]+0.00150;
            
         Down_Period60=true;
      }
   }
   
   Comment("Up_Period1: ",Up_Period1,
           "\nDown_Period1: ",Down_Period1,
           "\nUp_Period5: ",Up_Period5,
           "\nDown_Period5: ",Down_Period5,
           "\nUp_Period15: ",Up_Period15,
           "\nDown_Period15: ",Down_Period15,
           "\nUp_Period30: ",Up_Period30,
           "\nDown_Period30: ",Down_Period30,
           "\nUp_Period60: ",Up_Period60,
           "\nDown_Period60: ",Down_Period60);
   
   return(rates_total);
}
//+------------------------------------------------------------------+
 
Arthur Singer:
Hello, I have an indicator that works on the basis of several timeframes. However, I have the problem that the buffers are not output correctly. I tried to find the problem but it didn't work. Would be grateful for help.

Your 'limit' is based on the chart's timeframe, which cannot be directly used to access candle data of other timeframes.

Take a look at the illustration in this post : https://www.mql5.com/en/forum/315648#comment_12047919 - I was trying to explain the implications of making this mistake.

Need help for indicator
Need help for indicator
  • 2019.06.12
  • www.mql5.com
Hello, I'm trying to develop an indicator that analyzes on the M30 chart when two MAs cross and then shows it on the H1 chart, but it doesn't work...
 
How could I solve the problem?
 
Don't mix apples and oranges.
 
Arthur Singer:
How could I solve the problem?

Simple question, but complex answer... LOL.

I'll need to know more about your requirements before I can answer your question.

But generally, you cannot, meaningfully, display information from smaller timeframes on a chart of larger timeframe. So you cannot expect to show M1, M5, M15, etc. data, bar by bar, on H1 chart. However, displaying larger timeframe information on smaller timeframe chart is ok.

So if your chart displays M1, it is ok to declare buffers for M5, M15, and so on although you'll need to make sure their bar times are synchronized. But if your chart is H1, you cannot declare buffers for M1, M5, etc. because it becomes meaningless since 24 H1 bars cover 1 day, but 24 M1 bars will only cover 24 minutes - so using M1[20] to compute H1[20] is completely wrong. However, nothing stops you from retrieving 24x60 bars of M1 data for internal computation... in which case they should be stored in normal arrays 60 times larger, rather than indicator buffers.

In other words, to solve the problem you're having now, you'll have to think through your requirements first, before designing and implementing your code. 

 
What I want is that if two MAs cross on M1,M5,M15,M30 and H1, then this should be represented in the form of arrows on the H1 chart.
 

Hi they can help you quickly over here https://www.mql5.com/en/job

If you ask they can tell you how and what they do because it is a lot of code and need some work to fix it.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • www.mql5.com
I have this bot from binary.com,  is perfect. I  put in the bot, expected profit=100 stop loss= 500 win amount=5 initial amount=1 (the robot starts and when it gets to win 5 the bot ends.) I want to win 5 and start over with initials amount 1 in automatic until reach expected profit 100 let me know HELLO,i'm currently looking for someone...
 
Yes, but my problem isn't that big.
 
Arthur Singer: Yes, but my problem isn't that big.

Then code it yourself. If you don't know how to code, you can't possibly know how big it is.

You haven't stated a problem, you stated a want. You have only four choices:

  1. Search for it,
  2. Beg at
  3. learn to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  4. or pay (Freelance) someone to code it.
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help
          urgent help.
 
The code is above and as a problem I have already indicated that the indicator shows the arrows wrong or not at all.
 
Arthur Singer:
Yes, but my problem isn't that big.

Your problem is very big.

Reason: