Execute function at start time

 

Hello friends,

I am trying to run function at the time I need, but function ignoring the given time.


bool IsBelowMA()
{
   double Time_S = Hour() + Minute() / 100.0;
    // Ensure the function executes only after Time_Start
    if (Time_S >= Time_Start)
    {
        for (int i = 0; i < numCandles; i++)
        {
            // Calculate the moving average
            maprice = iMA(Symbol(), PERIOD_M60, MaPeriod, 0, MODE_SMA, PRICE_CLOSE, i);
            
            // Check if the current candle's closing price is above the moving average
            if (iClose(Symbol(), PERIOD_M60, i) > maprice)
            {
                // If any candle's closing price is above the moving average, return false
                return false;
            }
        }
    }
    
    // If all candles are below the moving average or if the function should not execute yet, return true
    return true;
}


Can we move this topic to MQL4, this question related to MQL4?

Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked, so we can't see your machine.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

    Always post all relevant code (using Code button) or attach the source file.

    We have no idea what S_Time is; only its form(HH.xxx). We have no idea what Start_Time is nor its value. We have no idea whether you are calling that function. What is that mystery PERIOD_M60? If you didn't define it, your code does not compile.

 

Thanks  for reply.  In order do not waste your time I will not include my code. Will confuse everyone.

Basically what I need is to find 10 consecutively bars which are below or above moving average starting from 12:00, so if one candle will go above moving average this will interrupt until get 10 bars.

I tried to do with chatgpt and searched internet at end tired to find solution.

 
 datetime time=StringToTime("12:00");
  
//---
int countup,coundwn;
if(time<TimeCurrent())
for(int i=0;i<Bars;i++)
  {
   
   double ma=iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE,i);
;
   if(Close[i]>ma)
   countup++;
   else countup=0;
   
   if(Close[i]<ma)
   coundwn++;
   else coundwn=0;
   
   if(countup>10)
   {
   Print("TEN UP AT i=",i);
   break;
   }
   
      if(coundwn>10)
   {
   Print("TEN DWN AT i=",i);
   break;
   }
   

  }
 

Hi

in the example from danielcioca - I think there should be also some normalization for times – StringToTime uses local date (If a string contains time with no date, the execution result is a current data of a local PC with a specified time) and it might be a different day than current time, so here you should first add:

datetime TimeCurr = StringToTime(TimeToString(TimeCurrent(),TIME_MINUTES));
if(time<TimeCurr)…
Have a nice weekend