I wrote a function so an indicator can send one pseudo-tick to chart if the market is closed, any way to improve it?

 

Hello,

This code appears to work great when dropping an indicator on a chart when the market is closed.  I've prevented recursive calls with a static variable.

Hoping an expert might be willing to evaluate this code for:

- Internal terminal behavior which makes this inadvisable?

- Some cool idea how I can only execute the pseudo-tick one time if loading a template with 5 indicators each using this function? (not critical but if there's a cool way I'd like to learn).

- Anything in my code which is more complicated than necessary? 

Thanks, much appreciated!


Function call from end of OnCalculate()

.....
   sendTickIfMktClosed();       
        
   return(rates_total);
  }

and the function:

//+------------------------------------------------------------------+
//                                                                   |
//+------------------------------------------------------------------+
void sendTickIfMktClosed()
   {
        // IF MARKET IS CLOSED, SEND A PSEUDO TICK TO UPDATE THE CHART
        datetime startTime=0;
        datetime endTime=0;
        MqlDateTime cTime, sTime, eTime;
        datetime compareTime=TimeCurrent();
        TimeToStruct(compareTime,cTime);
        
        // get the market hours:mins:seconds
        ENUM_DAY_OF_WEEK dayOfWeek;
        dayOfWeek=cTime.day_of_week;
        SymbolInfoSessionTrade(_Symbol,dayOfWeek,0,startTime,endTime);
        
        // convert market hours to year.mon.day of current time
        TimeToStruct(startTime,sTime);
        TimeToStruct(endTime,eTime);
        sTime.year=cTime.year;
        sTime.mon=cTime.mon;
        sTime.day=cTime.day;
        eTime.year=cTime.year;
        eTime.mon=cTime.mon;
        eTime.day=cTime.day;
        datetime openTime=StructToTime(sTime);
        datetime closeTime=StructToTime(eTime);
        
   // should print only once when market is open, and only twice when market is closed.
   // Print("Market Times detected for ",cTime.year,".",cTime.mon,".",cTime.day,": ",openTime," - ",closeTime);
   // -> Market Times detected for 2022.5.30: 2022.05.30 00:05:00 - 2022.05.30 23:55:00
   
   // make sure the pseudo tick only executes once
   static bool mkt_tickedOnce=false;
   
   // if market is closed all day OR current time is outside of market hours for the symbol
        if(   (startTime==D'1970.01.01' && endTime==D'1970.01.01') 
                 || (compareTime>closeTime && compareTime<openTime)
              )
           {
           if(mkt_tickedOnce==false)
              {
              mkt_tickedOnce=true;
              // send the pseudo tick, but only one time or each ChartSetSymbolPeriod() will cause it to execute again.
              ChartSetSymbolPeriod(0,NULL,0);
              }
           }
   }
//+------------------------------------------------------------------+
 
Christian Berrigan: This code appears to work great when dropping an indicator on a chart when the market is closed.  I've prevented recursive calls with a static variable.

When you “tick” the chart, the indicator is reloaded. Your static variable will be reset. (EAs are not reloaded.)

 
William Roeder #:

When you “tick” the chart, the indicator is reloaded. Your static variable will be reset. (EAs are not reloaded.)

Yeah, the fake tick doesn't reset the static variable but changing timeframes does.

Appears that the fake tick recalculates the indicators without re-executing OnInit(), but changing timeframes definitely reinitializes the indicator(s).  Probably better off having everything reinitialize when changing timeframes anyway.  Since it only affects charts when the market is closed, I should just accept that.  Thanks for looking!

 
William Roeder #:

When you “tick” the chart, the indicator is reloaded. Your static variable will be reset. (EAs are not reloaded.)

Do you happen to know if the indicator list is a reliable source for the order of execution of the indicators?

This way, I could only produce the fake tick with the last indicator on the chart list, and it would only execute once per TF change instead of once per indicator per TF change.

UPDATE:  Looks like the last indicator in the list is executed first when changing timeframes ... kind of a PITA for personal reasons.
 
Alternatively you could send a custom event prior to sending the chart reload tick.

This way you could "inform" all indicators with such a function, that they are not the "host".

Additionally I would add an "else" to the last, most inner if statement, resetting the tickedOnce variable.


 
Dominik Christian Egert #:
Alternatively you could send a custom event prior to sending the chart reload tick.

This way you could "inform" all indicators with such a function, that they are not the "host".

Additionally I would add an "else" to the last, most inner if statement, resetting the tickedOnce variable.


Thank you for the suggestion Dominik.

Reason: