Set Limit Hours For An Indicator - page 2

 
p4rnak #:

wow, Perfect.

I Have To Use This Under OnCalculate; Right?

Yes that sounds right

 
R4tna C #:

Yes that sounds right

still, not working...

it's keep time value to false.


look :


//+------------------------------------------------------------------+
//|                                                     Fractals.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_type1   DRAW_ARROW
#property indicator_type2   DRAW_ARROW
#property indicator_color1  Gray
#property indicator_color2  Gray
#property indicator_label1  "Fractal Up"
#property indicator_label2  "Fractal Down"
#property indicator_width1  5
#property indicator_width2  5
//---- indicator buffers
double ExtUpperBuffer[];
double ExtLowerBuffer[];
//--- 10 pixels upper from high price
int    ExtArrowShift=-6;
ulong secsSinceMidnight = TimeCurrent() % (24 * 3600);
   ulong secsStartTime         = 9  * 3600;
   ulong secsEndTime           = 23 * 3600;

   bool time = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

   
void OnInit()
  {
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtLowerBuffer,INDICATOR_DATA);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_ARROW,158);
   PlotIndexSetInteger(1,PLOT_ARROW,158);
//---- arrow shifts when drawing
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,ExtArrowShift);
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-ExtArrowShift);
//---- sets drawing line empty value--
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- initialization done

  }
//+------------------------------------------------------------------+
//|  Accelerator/Decelerator Oscillator                              |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
  
  PrintFormat("SecsSinceMidnight = %d TimeSinceMidnight = %s [start = %s end = %s]", 
               secsSinceMidnight, 
               TimeToString(datetime(secsSinceMidnight), TIME_SECONDS),
               TimeToString(datetime(secsStartTime), TIME_SECONDS),
               TimeToString(datetime(secsEndTime), TIME_SECONDS)
               );

   if((secsSinceMidnight > secsStartTime)
      && (secsSinceMidnight < secsEndTime))
     {
      time = true;
     }
   else
     {
      time = false;
     }
   
   PrintFormat("time = %s", string(time));
   
   
   int i,limit;
//---
   if(rates_total<5)
      return(0);
//---
   if(prev_calculated<7)
     {
      limit=2;
      //--- clean up arrays
      ArrayInitialize(ExtUpperBuffer,EMPTY_VALUE);
      ArrayInitialize(ExtLowerBuffer,EMPTY_VALUE);
     }
   else limit=rates_total-5;

   for(i=limit;i<rates_total-3 && !IsStopped();i++)
     {
      //---- Upper Fractal
      if(High[i]>=High[i+1] && High[i]>High[i+2] && High[i]>=High[i-1] && High[i]>High[i-2] && time == true) //High[i]>=High[i+1] && High[i]>High[i+2] && High[i]>=High[i-1] && High[i]>High[i-2]
         ExtUpperBuffer[i]=High[i];
      else ExtUpperBuffer[i]=EMPTY_VALUE;

      //---- Lower Fractal
      if(Low[i]<=Low[i+1] && Low[i]<Low[i+2] && Low[i]<=Low[i-1] && Low[i]<Low[i-2] && time == true) // Low[i]<=Low[i+1] && Low[i]<Low[i+2] && Low[i]<=Low[i-1] && Low[i]<Low[i-2]
         ExtLowerBuffer[i]=Low[i];
      else ExtLowerBuffer[i]=EMPTY_VALUE;
     }

//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }

//+------------------------------------------------------------------+
 
R4tna C #:

Yes that sounds right

Note : it's an MT5 indicator.

 
p4rnak #:

Note : it's an MT5 indicator.

This line needs to be called every time at the start of OnCalculate - at the moment you are setting it once at the beginning instead of checking the latest time (the print statement should reveal the time is not changing)

ulong secsSinceMidnight = TimeCurrent() % (24 * 3600);
Also I cannot see where you use the variable 'time' to control the program logic - at the moment you are just storing true or false 
 
R4tna C #:

This line needs to be called every time at the start of OnCalculate - at the moment you are setting it once at the beginning instead of checking the latest time

If you mean, Use That Line Under OnCalculate; Try This Before; But Didnt Work , Look :


//+------------------------------------------------------------------+
//|                                                     Fractals.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_type1   DRAW_ARROW
#property indicator_type2   DRAW_ARROW
#property indicator_color1  Gray
#property indicator_color2  Gray
#property indicator_label1  "Fractal Up"
#property indicator_label2  "Fractal Down"
#property indicator_width1  5
#property indicator_width2  5
//---- indicator buffers
double ExtUpperBuffer[];
double ExtLowerBuffer[];
//--- 10 pixels upper from high price
int    ExtArrowShift=-6;

   bool time = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

   
void OnInit()
  {
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtUpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtLowerBuffer,INDICATOR_DATA);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//---- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_ARROW,158);
   PlotIndexSetInteger(1,PLOT_ARROW,158);
//---- arrow shifts when drawing
   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,ExtArrowShift);
   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,-ExtArrowShift);
//---- sets drawing line empty value--
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
//---- initialization done

  }
//+------------------------------------------------------------------+
//|  Accelerator/Decelerator Oscillator                              |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
   ulong secsSinceMidnight = TimeCurrent() % (24 * 3600);
   ulong secsStartTime         = 9  * 3600;
   ulong secsEndTime           = 23 * 3600;
  PrintFormat("SecsSinceMidnight = %d TimeSinceMidnight = %s [start = %s end = %s]", 
               secsSinceMidnight, 
               TimeToString(datetime(secsSinceMidnight), TIME_SECONDS),
               TimeToString(datetime(secsStartTime), TIME_SECONDS),
               TimeToString(datetime(secsEndTime), TIME_SECONDS)
               );

   if((secsSinceMidnight > secsStartTime)
      && (secsSinceMidnight < secsEndTime))
     {
      time = true;
     }
   else
     {
      time = false;
     }
   
   PrintFormat("time = %s", string(time));
   
   
   int i,limit;
//---
   if(rates_total<5)
      return(0);
//---
   if(prev_calculated<7)
     {
      limit=2;
      //--- clean up arrays
      ArrayInitialize(ExtUpperBuffer,EMPTY_VALUE);
      ArrayInitialize(ExtLowerBuffer,EMPTY_VALUE);
     }
   else limit=rates_total-5;

   for(i=limit;i<rates_total-3 && !IsStopped();i++)
     {
      //---- Upper Fractal
      if(High[i]>=High[i+1] && High[i]>High[i+2] && High[i]>=High[i-1] && High[i]>High[i-2] && time == true) //High[i]>=High[i+1] && High[i]>High[i+2] && High[i]>=High[i-1] && High[i]>High[i-2]
         ExtUpperBuffer[i]=High[i];
      else ExtUpperBuffer[i]=EMPTY_VALUE;

      //---- Lower Fractal
      if(Low[i]<=Low[i+1] && Low[i]<Low[i+2] && Low[i]<=Low[i-1] && Low[i]<Low[i-2] && time == true) // Low[i]<=Low[i+1] && Low[i]<Low[i+2] && Low[i]<=Low[i-1] && Low[i]<Low[i-2]
         ExtLowerBuffer[i]=Low[i];
      else ExtLowerBuffer[i]=EMPTY_VALUE;
     }

//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }

//+------------------------------------------------------------------+
Découvrez de nouvelles opportunités MetaTrader 5 avec la communauté et les services MQL5
Découvrez de nouvelles opportunités MetaTrader 5 avec la communauté et les services MQL5
  • 2023.12.06
  • www.mql5.com
MQL5 : langage de stratégies de trading intégré à la plateforme de trading MetaTrader 5, permet d'écrire vos propres robots de trading, indicateurs techniques, scripts et bibliothèques de fonctions
 
p4rnak #:

If you mean, Use That Line Under OnCalculate; Try This Before; But Didnt Work , Look :


Please share the output of the print statements - a few executions please

 
R4tna C #:

Please share the output of the print statements - a few executions please

I tested it in OnCalulate() - works fine for me:


2023.12.06 22:36:16.745 time = true

2023.12.06 22:36:18.277 SecsSinceMidnight = 68777 TimeSinceMidnight = 19:06:17 [start = 09:00:00 end = 23:00:00]

2023.12.06 22:36:18.277 time = true

2023.12.06 22:36:19.642 SecsSinceMidnight = 68778 TimeSinceMidnight = 19:06:18 [start = 09:00:00 end = 23:00:00]

2023.12.06 22:36:19.642 time = true

2023.12.06 22:36:19.787 SecsSinceMidnight = 68778 TimeSinceMidnight = 19:06:18 [start = 09:00:00 end = 23:00:00]

2023.12.06 22:36:19.787 time = true


Changing to 21:00

2023.12.06 22:36:29.492 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.492 time = false

2023.12.06 22:36:29.605 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.605 time = false

2023.12.06 22:36:29.667 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.667 time = false

2023.12.06 22:36:29.732 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.732 time = false


 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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[])
  {

   ulong secsSinceMidnight = TimeCurrent() % (24 * 3600);
   ulong secsStartTime         = 21  * 3600;
   ulong secsEndTime           = 23 * 3600;
   bool time_bool = false;
   PrintFormat("SecsSinceMidnight = %d TimeSinceMidnight = %s [start = %s end = %s]",
               secsSinceMidnight,
               TimeToString(datetime(secsSinceMidnight), TIME_SECONDS),
               TimeToString(datetime(secsStartTime), TIME_SECONDS),
               TimeToString(datetime(secsEndTime), TIME_SECONDS)
              );

   if((secsSinceMidnight > secsStartTime)
      && (secsSinceMidnight < secsEndTime))
     {
      time_bool = true;
     }
   else
     {
      time_bool = false;
     }

   PrintFormat("time = %s", string(time_bool));
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
R4tna C #:

I tested it in OnCalulate() - works fine for me:


2023.12.06 22:36:16.745 time = true

2023.12.06 22:36:18.277 SecsSinceMidnight = 68777 TimeSinceMidnight = 19:06:17 [start = 09:00:00 end = 23:00:00]

2023.12.06 22:36:18.277 time = true

2023.12.06 22:36:19.642 SecsSinceMidnight = 68778 TimeSinceMidnight = 19:06:18 [start = 09:00:00 end = 23:00:00]

2023.12.06 22:36:19.642 time = true

2023.12.06 22:36:19.787 SecsSinceMidnight = 68778 TimeSinceMidnight = 19:06:18 [start = 09:00:00 end = 23:00:00]

2023.12.06 22:36:19.787 time = true


Changing to 21:00

2023.12.06 22:36:29.492 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.492 time = false

2023.12.06 22:36:29.605 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.605 time = false

2023.12.06 22:36:29.667 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.667 time = false

2023.12.06 22:36:29.732 SecsSinceMidnight = 68788 TimeSinceMidnight = 19:06:28 [start = 21:00:00 end = 23:00:00]

2023.12.06 22:36:29.732 time = false



maybe Im using time recall in a wrong place :


if(High[i]>=High[i+1] && High[i]>High[i+2] && High[i]>=High[i-1] && High[i]>High[i-2] && time == true)
 
p4rnak #:

Are you sure you want to stop the indicator?😄

If you want to ignore bars whose times are outside the time range you specify, then you should analyze the times of the bars, not the current time