creating MTF indicator

 
//+------------------------------------------------------------------+
//|                                                          new.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 3
#property indicator_color1 Blue
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 3
#property indicator_color2 Red
#property indicator_label2 "Sell"


double Buffer1[];
double Buffer2[];

datetime time_alert; 

double myPoint; 

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

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 225);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 226);
   
   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;
   
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   
   for(int i = limit-1; i >= 0; i--)
     {
      if(iHigh(NULL,PERIOD_M30,i) > iHigh(NULL,PERIOD_M30,1+i)
      && iHigh(NULL,PERIOD_M30,1+i) < iHigh(NULL,PERIOD_M30,1+i+1) //Candlestick High crosses above Candlestick High
      
            
      && iClose(NULL,PERIOD_M30,i) == Close[i] 
      )
        {
         Buffer1[i] = Low[i] - 3 * myPoint; 
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); 
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = 0;
        }
      
      if(iLow(NULL,PERIOD_M30,i) < iLow(NULL,PERIOD_M30,1+i)
      && iLow(NULL,PERIOD_M30,1+i) > iLow(NULL,PERIOD_M30,1+i+1) //Candlestick Low crosses below Candlestick Low
      
      
      && iClose(NULL,PERIOD_M30,i) == Close[i] 
      )
        {
         Buffer2[i] = High[i] + 3 * myPoint; 
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); 
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = 0;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Hello Coders,

I am new to coding mql4, I was trying to code a simple MTF indicator based on simple Logic. But when I attached the indicator to the chart. On M30 the arrows are showing fine, But when I switch to M5 I cant see the Arrow, can you please modify my code so that the arrows can be seen on M5 or any other time frame too.

Regards,

Siraj

 
 for(int i = limit-1; i >= 0; i--)
     {
      if(iHigh(NULL,PERIOD_M30,i) > iHigh(NULL,PERIOD_M30,1+i)
  1. Why did you post your MT4 question in the Root / MT5 General 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.

  2. You are mixing apples and oranges.

  3. See How to do your lookbacks correctly for repainting all M30 bar zero bars

 
whroeder1:
  1. Why did you post your MT4 question in the Root / MT5 General 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.

  2. You are mixing apples and oranges.

  3. See How to do your lookbacks correctly for repainting all M30 bar zero bars

sorry to post the message in wrong section, can I shift my post to the correct section ? Can you help to modify the code please
 
SIRAJ Multani :

Hello Coders,

I am new to coding mql4, I was trying to code a simple MTF indicator based on simple Logic. But when I attached the indicator to the chart. On M30 the arrows are showing fine, But when I switch to M5 I cant see the Arrow, can you please modify my code so that the arrows can be seen on M5 or any other time frame too.

Regards,

Siraj


Date & time of historical data are different between 5 minutes and 30 minutes, you have to adjust then using "iBarShift" as follows.

for(int i = limit-1; i >= 0; i--)
     {
        int y = iBarShift(NULL, PERIOD_M30, Time[i], false);
        
      if(iHigh(NULL,PERIOD_M30,y) > iHigh(NULL,PERIOD_M30,1+y)
      && iHigh(NULL,PERIOD_M30,1+y) < iHigh(NULL,PERIOD_M30,1+y+1) //Candlestick High crosses above Candlestick High
      
            
      && iClose(NULL,PERIOD_M30,y) == Close[i] 
      )
 
Naguisa Unada:

Date & time of historical data are different between 5 minutes and 30 minutes, you have to adjust then using "iBarShift" as follows.

Wow......what a fantastic solution ....I have never taught about that.............I have become your FAN my friend........Thank you so so much !!!
 
for(int i = limit-1; i >= 0; i--)
     {
        int y = iBarShift(NULL, PERIOD_M30, Time[i], false);
Since the source data is a higher TF, you must reprocess all source bar zero values to get the stair step effect, (one source bar, multiple chart bars.)
          How to do your lookbacks correctly № 9 through #14.
Reason: