Download MetaTrader 5

MTF Indi not working properly

To add comments, please log in or register
madman1701
22
madman1701  

Hi Folks,


I am a noob at coding and have been getting some help with this, but buddy is busy so I am here to see if anyone may have a solution to my problem. I have what I thought would be something simple to code, lol. I want an arrow painted when all 4 tfs have closed in the same direction, but it is painting arrows all over the place and missing arrows where they should be, so something isn't right. It effectively should only paint an arrow at the beginning of a new hour if all conditions are met. Here is what we have:


Thanks in advance for any advice!

 for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; 
      if(
      iClose(NULL, PERIOD_H1, i+1) > iClose(NULL, PERIOD_H1, i+2)
      && iClose(NULL, PERIOD_M30, i+1) > iClose(NULL, PERIOD_M30, i+2)
      && iClose(NULL, PERIOD_M15, i+1) > iClose(NULL, PERIOD_M15, i+2)
      && iClose(NULL, PERIOD_M5, i+1) > iClose(NULL, PERIOD_M5, i+2)
      )
        { upBuff[i] = Low[i]; if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Signal UP"); time_alert = Time[0]; } }
      else { upBuff[i] = 0; }

      if(
      iClose(NULL, PERIOD_H1, i+1) < iClose(NULL, PERIOD_H1, i+2)
      && iClose(NULL, PERIOD_M30, i+1) < iClose(NULL, PERIOD_M30, i+2)
      && iClose(NULL, PERIOD_M15, i+1) < iClose(NULL, PERIOD_M15, i+2)
      && iClose(NULL, PERIOD_M5, i+1) < iClose(NULL, PERIOD_M5, i+2)
      )
          { dnBuff[i] = High[i]; if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Signal DN"); time_alert = Time[0]; } }
      else { dnBuff[i] = 0; }
GrumpyDuckMan
331
GrumpyDuckMan  
madman1701:

Hi Folks,


I am a noob at coding and have been getting some help with this, but buddy is busy so I am here to see if anyone may have a solution to my problem. I have what I thought would be something simple to code, lol. I want an arrow painted when all 4 tfs have closed in the same direction, but it is painting arrows all over the place and missing arrows where they should be, so something isn't right. It effectively should only paint an arrow at the beginning of a new hour if all conditions are met. Here is what we have:


Thanks in advance for any advice!

 for(int i = limit-1; i >= 0; i--)

Hello,

I am a beginner, if " i " equal "limit-1",  how is " i " greater or equal to 0 ?

whroeder1
15925
whroeder1  
if (i >= MathMin(5000-1, rates_total-1-50)) continue; 
      if(
      iClose(NULL, PERIOD_H1, i+1) > iClose(NULL, PERIOD_H1, i+2)
      && iClose(NULL, PERIOD_M30, i+1) > iClose(NULL, PERIOD_M30, i+2)
      && iClose(NULL, PERIOD_M15, i+1) > iClose(NULL, PERIOD_M15, i+2)
      && iClose(NULL, PERIOD_M5, i+1) > iClose(NULL, PERIOD_M5, i+2)
  1. You are mixing apples and oranges.
  2. Do your lookbacks correctly. Specifically PERIOD_H1/_Period * 3. And you must repaint all bars containing H1 bar zero.
  3. Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors.
whroeder1
15925
whroeder1  
GrumpyDuckMan: if " i " equal "limit-1",  how is " i " greater or equal to 0 ?

If limit is 100, then limit-1 is 99 and that is "greater or equal to 0."

nicholishen
911
nicholishen  

This is an interesting problem. The reason you aren't getting the results you want is because you are iterating through all the bars on the respective chart time-frame instead of your highest time-frame. In this case your highest time-frame is H1 so you need to iterate through H1 only and then resolve down to the lower time-frames. In essence you are saying for each H1 bar -> if M30 bar at the same time as this H1 bar is lower than its prev M30 bar... etc.


To start you need to set your limits on the H1 time-series. Do that by finding the shift that does not exceed the lowest time-frame's data. For example H1 may go to 2015, but your M5 data stops at 2017.

Once you have your limit, you need to iterate through H1. Here is an example.

//+------------------------------------------------------------------+
//|                                             MTF_Close_Arrows.mq4 |
//|                                                      nicholishen |
//|                            http://www.reddit.com/u/nicholishenFX |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "http://www.reddit.com/u/nicholishenFX"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot up
#property indicator_label1  "up"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot dn
#property indicator_label2  "dn"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

#define mShift(p,t) iBarShift(Symbol(),p,t)
#define mClose(p,i) iClose(Symbol(),p,i)
#define mTime(p,i)  iTime(Symbol(),p,i)
//--- indicator buffers
double         upBuffer[];
double         dnBuffer[];
datetime       last_time;
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0,upBuffer);
   SetIndexBuffer(1,dnBuffer);
   SetIndexArrow(0,233);
   SetIndexArrow(1,234);
   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[])
{
   static bool is_init = false;
   int limit = 1;
   if(!is_init)
      limit = mShift(PERIOD_H1,mTime(PERIOD_M5,Bars(Symbol(),PERIOD_M5)-1));
   is_init = true;
   for(int i=limit-1;i>=0;i--)
   {
      datetime h1_time  = mTime(PERIOD_H1,i)+PeriodSeconds(PERIOD_H1)-1;
      double close_h1   = mClose(PERIOD_H1,i);
      double close_m30  = mClose(PERIOD_M30,mShift(PERIOD_M30,h1_time));
      double close_m15  = mClose(PERIOD_M15,mShift(PERIOD_M15,h1_time));
      double close_m5   = mClose(PERIOD_M5 ,mShift(PERIOD_M5 ,h1_time)); 
      double prev_h1    = mClose(PERIOD_H1,i+1);
      double prev_m30   = mClose(PERIOD_M30,mShift(PERIOD_M30,h1_time)+1);
      double prev_m15   = mClose(PERIOD_M15,mShift(PERIOD_M15,h1_time)+1);
      double prev_m5    = mClose(PERIOD_M5 ,mShift(PERIOD_M5 ,h1_time)+1);
      
      int shift = mShift(Period(),h1_time);
      if(close_h1 > prev_h1 && close_m30 > prev_m30 && close_m15 > prev_m15 && close_m5 > prev_m5) 
         upBuffer[shift] = high[shift]+5*_Point;
      else
      if(close_h1 < prev_h1 && close_m30 < prev_m30 && close_m15 < prev_m15 && close_m5 < prev_m5)
         dnBuffer[shift] = low[shift]-5*_Point;
   }
   return(rates_total);
}
//+------------------------------------------------------------------+
madman1701
22
madman1701  

Ok cool. That makes sense. Thanks for the help.

To add comments, please log in or register