1

I am new to MT4, know little basic programming for MQL4. I am trading in UTC+5:30 in Indian Stocks. I just want a small piece of code to get today's First candles HIGH and LOW in current TimeFrame. Our trading starts at 9:15 AM IST and ends at 3:30 PM IST.

e.g. if I select PERIOD_M15 (15 min chart) then I need to have day's first candle (i.e. from 9:15AM to 9:30AM) HIGH and LOW.

thanks in advance.

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81

2 Answers2

4

welcome to SOF!

you need input parameters (time of the day start):

input int InpTimeStartHour=9; input int InpTimeStartMinute=15; this can be as a one string but for simplicity such fields

bool getHighLowFistCandle(double &high,double &low){
   //check if first candle (zero-current) is after 9:15
   datetime lastCandle=iTime(Symbol(),0,1);
   if(TimeHour(lastCandle)<InpTimeStartHour || 
      (TimeHour(lastCandle)==InpTimeStartHour && TimeMinute(lastCandle)<InpTimeStartMinute){
      return(false);
   }
   //looking for that time candle starting from day start
   datetime todayStart=iTime(Symbol(),PERIOD_D1,0);
   int shift=iBarShift(Symbol(),0,todayStart);
   for(int i=shift;i>0;i--){
      datetime iCandleTime=iTime(Symbol(),0,i);
      if(TimeHour(iCandleTime)==InpTimeStartHour &&
         TimeMinute(iCandleTime)==InpTimeStartMinute){
          high=iHigh(Symbol(),0,i);
          low=iLow(Symbol(),0,i);
          return(true);
      }
   }
  return(false);
}
Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
0

Below is my indicator code,

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020,ANKUR SONI."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property  indicator_buffers 1
#property indicator_chart_window
#property indicator_width1 5
#property  indicator_color1 clrYellow

double engulfing[];
input int InpTimeStartHour=8;
input int InpTimeStartMinute=45;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, engulfing);
   SetIndexStyle(0, DRAW_ARROW);
   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[])
  {
   for(int i = (Bars - 2) ; i > 0 ; i--)
     {
      if(TimeHour(time[i]) == InpTimeStartHour && TimeMinute(time[i]) == InpTimeStartMinute)
        {
         double currentHigh = High[i];
         double currentLow = Low[i];
         double nextHigh = High[i-1];
         double nextLow = Low[i-1];
         if(nextHigh > currentHigh && nextLow < currentLow)
           {
            engulfing[i-1] = High[i-1] + 15;
           }
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
Ankur Soni
  • 5,725
  • 5
  • 50
  • 81