Time Function Query

 

Hi guys.

I am currently struggling with a little problem and I hoping someone can help please.

I am currently coding a very simple indicator which returns an arrow on an MA cross.

But I would now like to include a time filter to the indicator.

I would like for the indicator to only be active during two periods during the day:

Period 1 = 8:00 - 11:00

Period 2 = 13:00 - 16:00

So I am trying to define these two time ranges/periods of time and save them into an array and use them as part of my indicator signal logic, but unfortunately, I don't have the solution.

Can you please help?


I think the problems are my StartHour1/LastHour1/StartHour2/LastHour2 integers and:

I am also using TimeHour(iTime(NULL,0,i))  within a for loop to return the hour of a candle. But this is not working.


The current code is below. Any help would be appreciated.


//+------------------------------------------------------------------+
//|                                       ChrisBaileyMAIndicator.mq4 |
//|                                                     Chris Bailey |
//|                                      https://www.daxtrader.co.uk |
//+------------------------------------------------------------------+
#property copyright "Chris Bailey"
#property link      "https://www.daxtrader.co.uk"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 clrGreen
#property indicator_color2 clrBlack
#property indicator_color3 clrYellow
#property indicator_color4 clrBlueViolet
#property  indicator_width3 2
#property  indicator_width4 2

//External Settings

extern               int                     FastMAPeriod             =  8;             // Fast MA Period
extern               int                     FastMAShift              =  -5;            // Fast MA Shift
extern               ENUM_MA_METHOD          FastMAMethod             =  1;             // Fast MA Method  
extern               ENUM_APPLIED_PRICE      FastMAPrice              =  4;             // Fast MA Price 

extern               int                     SlowMAPeriod             =  8;             // Slow MA Period
extern               int                     SlowMAShift              =  0;             // Slow MA Shift
extern               ENUM_MA_METHOD          SlowMAMethod             =  1;             // Slow MA Method  
extern               ENUM_APPLIED_PRICE      SlowMAPrice              =  4;             // Slow MA Price 
 
extern               int                     StartHour1                =  8;            //Start operation hour
extern               int                     LastHour1                 =  11;           //Last operation hour

extern               int                     StartHour2                =  13;           //Start operation hour
extern               int                     LastHour2                 =  16;           //Last operation hour
 
   
   
double FastMA[];
double SlowMA[];
int Hour[];
double uparrow[];
double downarrow[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,FastMA);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexLabel(0,"Fast MA");
//---
   SetIndexBuffer(1,SlowMA);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexLabel(1,"Slow MA");   
//---
   SetIndexBuffer(2,uparrow);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexLabel(2,"Moving averages crossed up");
//---
   SetIndexBuffer(3,downarrow);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);
   SetIndexLabel(3,"Moving averages crossed down");
//---
 
   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 counted_bars=IndicatorCounted();
   int   limit = Bars-counted_bars;
   for(int i=0; i<limit; i++)
   {
      double CurrentFastMA  = iMA(NULL,0,FastMAPeriod,FastMAShift,FastMAMethod,FastMAPrice,i);
      double PreviousFastMA = iMA(NULL,0,FastMAPeriod,FastMAShift,FastMAMethod,FastMAPrice,i+1);
      double CurrentSlowMA = iMA(NULL,0,SlowMAPeriod,SlowMAShift,SlowMAMethod,SlowMAPrice,i);
      double PreviousSlowMA = iMA(NULL,0,SlowMAPeriod,SlowMAShift,SlowMAMethod,SlowMAPrice,i+1); 

     
      FastMA[i]=CurrentFastMA;
      SlowMA[i]=CurrentSlowMA; 
      Hour[i]=TimeHour(iTime(NULL,0,i));  


      if(CurrentFastMA>CurrentSlowMA && PreviousFastMA<PreviousSlowMA && ((Hour[i]>=StartHour1 && Hour[i]<=LastHour1)||(Hour[i]>=StartHour2 && Hour[i]<=LastHour2)))
            {
         uparrow[i]=Close[i]; // Buy = Ask
            }
      else if(CurrentFastMA<CurrentSlowMA && PreviousFastMA>PreviousSlowMA && ((Hour[i]>=StartHour1 && Hour[i]<=LastHour1)||(Hour[i]>=StartHour2 && Hour[i]<=LastHour2)))
            {
         downarrow[i]=Close[i]; // Sell = Bid
            }            


} 
   return(rates_total);
  }
//+------------------------------------------------------------------+

27-10-2017 Dax Technical Analysis
27-10-2017 Dax Technical Analysis
  • www.daxtrader.co.uk
Welcome to the 27-10-2017 Dax Technical Analysis. So the DAX REACHES ALL-TIME HIGH AGAIN! Finally, we see the Dax breaking out of that range and wow, wasn’t that a ride! Congratulations to the premium guys who also caught [all of / some of] that move with us in the premium channel. One of the positions … Read more by clicking here Morning...
 
Christopher Bailey:

Hi guys.

I am currently struggling with a little problem and I hoping someone can help please.

I am currently coding a very simple indicator which returns an arrow on an MA cross.

But I would now like to include a time filter to the indicator.

I would like for the indicator to only be active during two periods during the day:

Period 1 = 8:00 - 11:00

Period 2 = 13:00 - 16:00

So I am trying to define these two time ranges/periods of time and save them into an array and use them as part of my indicator signal logic, but unfortunately, I don't have the solution.

Can you please help?


I think the problems are my StartHour1/LastHour1/StartHour2/LastHour2 integers and:

I am also using TimeHour(iTime(NULL,0,i))  within a for loop to return the hour of a candle. But this is not working.


The current code is below. Any help would be appreciated.



Figured it out, thanks anyway...

 

If you still consider about hh:mm:ss, specifically mm & ss , consider using  stringSeperate of period 1 & 2, stringToTime ^ timetostring. Check documentation about this inbuilt functions.

Hope that may add to your help also.