Only want to run when new candle forms. Bug in my code.

 

I have an indicator which will send notifications when candle patterns are detected. On only want it to run when the code sees a new candle as formed.

I record the current candle open time to a file. So if the indicator is closed, and re-opened it knows where to pick up from.

Here are the functions I am using. The file storing functions are working fine so I won't clog up the post by putting those here.

 

void sendCandlePatternNotificationMsg(int timeframe) {
   //only send one notifcation per candle - data stored in file
   if(isNewCandle(Symbol(), timeframe)) {
      string notification_message = "";
      
      notification_message = buildCandlePatternNotificationMsg(timeframe);
      if(notification_message != 0) { broadcastNotificationMsg(notification_message); }
      
      writeCurrentCandleTime(Symbol(), timeframe);
   }   
}

bool isNewCandle(string symbol, int timeframe) {
   string   storedTime        = ReadStringFromFile("PABS"+symbol+timeframe);
   datetime lastRecordedTime  = StringToTime(storedTime);
   datetime currentCandleTime = iTime(symbol, timeframe, 0);
   
   //current candle open time is not the same as last recorded time and the current candle time is greater than stored time
   if((currentCandleTime != lastRecordedTime) && (currentCandleTime > lastRecordedTime)) { return(true); }  
   return(false);
}

bool writeCurrentCandleTime(string symbol, int timeframe) {
   datetime currentCandleTime = iTime(symbol, timeframe, 0);
   string   setTime           = TimeToStr(currentCandleTime); 
   
   if(StoreStringToFile(setTime, "PABS"+symbol+timeframe)) { return(true); }
   return(false);
}

 

I call the main function from within onCalculate which should take care of everything

  •  if a new candle is detected, send out the notification message and write the new candle time to a file
  •  if a new candle is not detected, don't do anything. 


sendCandlePatternNotificationMsg(Period()); 

 

Everything seems to be working, except when I flick through time frames. If I select a timeframe the indicator had not encountered before, it sends an alert if something is there.

I don't want this to happen, so to get around it, I am writing a new candle time inside the init code... 

int OnInit()
  {
    writeCurrentCandleTime(Symbol(), Period());
  }

 

This doesn't work for some reason.

When the indicator comes across a new timeframe, sends out the alert without waiting for a new candle to form.

It only fails on the very first time the code is exposed to a new timeframe, then it works ok.

Can anyone see what I am missing here. 

 
Obviously, most times when you change time frame, the current bar open time will not be the same as on the previous time frame
 

There is a separate file for each time frame. So there will be a file that stores the last daily, h4, and h1 last candle time.

Each function accepts a period parameter. 

What ever timeframe you call, it will create a file for it...

//the filename
"PABS"+symbol+timeframe

 

But I make sure I record the current candle time on init() so nothing should happen for the current candle...  

bool writeCurrentCandleTime(string symbol, int timeframe) {
   datetime currentCandleTime = iTime(symbol, timeframe, 0);
   string   setTime           = TimeToStr(currentCandleTime); 
   
   if(StoreStringToFile(setTime, "PABS"+symbol+timeframe)) { return(true); }
   return(false);
}

int OnInit()
  {
    writeCurrentCandleTime(Symbol(), Period());
  }
Reason: