Set Limit Hours For An Indicator - page 3

 
p4rnak #:


maybe Im using time recall in a wrong place :


That should work, but it is fiddly - you could just exit if time is false

   PrintFormat("time = %s", string(time));

        if (!time) return(rates_total);

Also OnCalculate has an input array called time

                const datetime& time[],

So you should rename your variable to something which does not conflict

 
R4tna C #:
   PrintFormat("time = %s", string(time));         if (!time) return(rates_total);

Im really really appreciate for what you do for me.

Look, This is my main indicator code and I want it work from 9AM to 23PM; Could You Please Edit this code for me,

//+------------------------------------------------------------------+
//|                                                     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;

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

//+------------------------------------------------------------------+


Please?

 
Vladislav Boyko #:

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

No, I just want to print fractals on a specific time.

 
p4rnak #:

No, I just want to print fractals on a specific time.

You are trying to change the condition for defining a fractal:

Forum on trading, automated trading systems and testing trading strategies

Set Limit Hours For An Indicator

p4rnak, 2023.12.06 17:23

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

This code fills the indicator buffers.

If you want to apply a time range to printing to a journal, then apply it to printing to a journal and do not touch the calculation of indicator buffers.

But I still don't understand what you mean by "print fractals on a specific time".

[EDIT]

It looks like you want to be notified of new fractals that will appear within the time range you specify. In this case, you still shouldn't change the code that calculates the buffer values

 
Vladislav Boyko #:

You are trying to change the condition for defining a fractal:

This code fills the indicator buffers.

If you want to apply a time range to printing to a journal, then apply it to printing to a journal and do not touch the calculation of indicator buffers.

But I still don't understand what you mean by "print fractals on a specific time".

[EDIT]

It looks like you want to be notified of new fractals that will appear within the time range you specify. In this case, you still shouldn't change the code that calculates the buffer values

Got u, But I Try To Use if(time == true); Before The "for" function; but still it didnt work.

OK, Let Me Explain :

This Indicator Print Fractal Arrows after calculating the price; So, I want to start calculating and printing from 9AM (broker time) to 11PM.

I Mean, I want this indicator work just in this time range (9-23).

 

This code calculates buffers:

Forum on trading, automated trading systems and testing trading strategies

Set Limit Hours For An Indicator

p4rnak, 2023.12.06 18:30

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

  }

If you want to somehow use information about fractals (printing, for example), then analyze the indicator buffers after they are calculated. That is, after the code above and before "return(rates_total);"

 
p4rnak #:

Got u, But I Try To Use if(time == true); Before The "for" function; but still it didnt work.

OK, Let Me Explain :

This Indicator Print Fractal Arrows after calculating the price; So, I want to start calculating and printing from 9AM (broker time) to 11PM.

I Mean, I want this indicator work just in this time range (9-23).

Still don't understand what you are trying to do.

  • At the moment a new fractal appears, print something in the journal if the current time is in the range 09:00-23:00
  • Remove fractals from the chart whose bars do not belong to the range 09:00-23:00
  • ..?
[EDIT]
    p4rnak #:
    I want to start calculating and printing from 9AM (broker time) to 11PM
    This is a deliberately incorrect formulation of the problem. You cannot start or stop the calculation of indicator buffers. The idea of starting/stopping buffer calculations immediately leads you to a dead end. Because it is meaningless. Formulate the task more clearly what you want to get as a result. Take a look at my list above.
     
    Vladislav Boyko #:

    Still don't understand what you are trying to do.

    • At the moment a new fractal appears, print something in the journal if the current time is in the range 09:00-23:00
    • Remove fractals from the chart whose bars do not belong to the range 09:00-23:00
    • ..?

    yes, that's right sir.

    I Want To Show Fractal in an specific time range. (09:00 - 23:00).

     
    Vladislav Boyko #:

    This code calculates buffers:

    If you want to somehow use information about fractals (printing, for example), then analyze the indicator buffers after they are calculated. That is, after the code above and before "return(rates_total);"

    I think Vladislav has a very good point - perhaps the timing control should come after the exit from Oncalculate()

    If it is just a matter of printing, then the timing code should be elsewhere

     
    p4rnak #:

    yes, that's right sir.

    I Want To Show Fractal in an specific time range. (09:00 - 23:00).

    If I understand you correctly and you want to hide fractals whose bar time does not fall within the range you specify, then you need to analyze the time of the bars (on which the fractals are located), and not the current time


    [EDIT]

    Apply your algorithm (below) not to TimeCurrent(), but to time[i]

    (I did not check the correctness of the algorithm, and in this case it is better to remove the prints, otherwise you will get a million of them)

    Forum on trading, automated trading systems and testing trading strategies

    Set Limit Hours For An Indicator

    R4tna C, 2023.12.06 18:09

    //+------------------------------------------------------------------+
    //|                                                                  |
    //+------------------------------------------------------------------+
    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);
      }
    //+------------------------------------------------------------------+
    

    Reason: