Dealing with static datetime function

 

Hi I tried to working on the code that is detecting new bar function. My code consist of static datetime in a NewBar() function and the problem of this is that if I use that function twice the second will never working. this is my code:

bool NewBar()
  {
   static datetime timeCur;
   datetime timePre = timeCur;
   timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar)
     {
      return true; // Once per bar
     }
   return false;
  }

//
double HeikenAshiOpenCurrentPeriod(int shift = 0)
  {
   double result = iCustom(Symbol(),PERIOD_CURRENT,"Heiken Ashi","Red","Lime","Red","Lime",2,shift);
   return result;
  }
//
double HeikenAshiCloseCurrentPeriod(int shift = 0)
  {
   double result = iCustom(Symbol(),PERIOD_CURRENT,"Heiken Ashi","Red","Lime","Red","Lime",3,shift);
   return result;
  }

bool HAGreenCandleCurrentPeriod(int shift = 0)
  {
   if(HeikenAshiOpenCurrentPeriod() < HeikenAshiCloseCurrentPeriod())
      return true;
   return false;
  }

from that code I want to call the function in this code:

   if(NewBar() && HAGreenCandleCurrentPeriod(1))
      Alert("New Bar & Green Candle");
   if(NewBar() && !HAGreenCandleCurrentPeriod(1))
      Alert("New Bar & Red Candle");

the second if will never be working because of the static datetime in the NewBar() Function. Is there a work around to deal with this?

Coding Forex: Forcing Code to Run Only Once Per Candle
Coding Forex: Forcing Code to Run Only Once Per Candle
  • 2017.09.15
  • www.youtube.com
Mql4 Progamming Tutorials and Forex Trading Training. https://learnmql4.comJimdandy Trade Management Tool.. https://www.mql5.com/en/market/product/11862Skill...
 
   static datetime preTime=0,curTime;
   bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0));
//---
   if(newBar && HAGreenCandleCurrentPeriod(1))
     { 
      Alert("New Bar & Green Candle");
      preTime=curTime;
     }
//---
   if(newBar && !HAGreenCandleCurrentPeriod(1))
     { 
      Alert("New Bar & Red Candle");
      preTime=curTime;
     }
//---
 
  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

  2. Your code
       bool isNewBar = timeCur != timePre;
       if(isNewBar)
         {
          return true; // Once per bar
         }
       return false;
    Simplified
       return  timeCur != timePre;

 
Hi I already use your suggestion Ernst and it works in green and red candle but instead working in every new bar it is working in every tick
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 
Luandre Ezra:
Hi I already use your suggestion Ernst and it works in green and red candle but instead working in every new bar it is working in every tick

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   static datetime preTime=iTime(Symbol(),Period(),0),curTime;
   bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0));
//---
   if(newBar && HAGreenCandleCurrentPeriod(1))
     { 
      Alert("New Bar & Green Candle");
      preTime=curTime;
     }
//---
   if(newBar && !HAGreenCandleCurrentPeriod(1))
     { 
      Alert("New Bar & Red Candle");
      preTime=curTime;
     }
//---
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double HeikenAshiOpenCurrentPeriod(int shift = 0)
  {
   return iCustom(Symbol(),PERIOD_CURRENT,"Heiken Ashi","Red","Lime","Red","Lime",2,shift);
  }
//---
double HeikenAshiCloseCurrentPeriod(int shift = 0)
  {
   return iCustom(Symbol(),PERIOD_CURRENT,"Heiken Ashi","Red","Lime","Red","Lime",3,shift);
  }
//---
bool HAGreenCandleCurrentPeriod(int shift = 0)
  {
   return (HeikenAshiOpenCurrentPeriod(shift) < HeikenAshiCloseCurrentPeriod(shift));
  }
//+------------------------------------------------------------------+

Working. Please show your code.

 
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

I never knew that function can only be called once per tick, thank you for the knowledge.

Working. Please show your code.

   static datetime preTime=iTime(Symbol(),Period(),0),curTime;
   bool newBar=preTime!=(curTime=iTime(Symbol(),Period(),0));
It's working know Ernst. I put the code above OnInit maybe that's why it doesn't work. Quick question, it seems that the code below doesn't works like I want. In the code I put the Heiken Ashi Candle shifted by 1, but every time the new bar coming the EA would return the candle color of the current candle. This code should be works when new bar is created then look for the finish bar color which is the previous candle. Is this happened because of the newBar variable?
if(newBar && HAGreenCandleCurrentPeriod(1))
     { 
      Alert("New Bar & Green Candle");
      preTime=curTime;
     }
//---
   if(newBar && !HAGreenCandleCurrentPeriod(1))
     { 
      Alert("New Bar & Red Candle");
      preTime=curTime;
 
Luandre Ezra:

I never knew that function can only be called once per tick, thank you for the knowledge.

It's working know Ernst. I put the code above OnInit maybe that's why it doesn't work. Quick question, it seems that the code below doesn't works like I want. In the code I put the Heiken Ashi Candle shifted by 1, but every time the new bar coming the EA would return the candle color of the current candle. This code should be works when new bar is created then look for the finish bar color which is the previous candle. Is this happened because of the newBar variable?

No, it happens because this:

bool HAGreenCandleCurrentPeriod(int shift = 0)
  {
   if(HeikenAshiOpenCurrentPeriod() < HeikenAshiCloseCurrentPeriod())
      return true;
   return false;
  }

should be like this:

//---
bool HAGreenCandleCurrentPeriod(int shift = 0)
  {
   return (HeikenAshiOpenCurrentPeriod(shift) < HeikenAshiCloseCurrentPeriod(shift));
  }
 
Thank you Ernst, I forgot to add the shift in the function. It is working properly now.
 
Luandre Ezra:

I never knew that function can only be called once per tick, thank you for the knowledge.

A function can be called multiple times in the same tick.

If a function such as IsNewBar() is called and returns true because it is the first tick of a new bar

if it is called again during the same tick it will return false.

So it can be called as many times as you like in a tick, but it can only ever return true the first time that it is called in the tick.

 
Keith Watford:

A function can be called multiple times in the same tick.

If a function such as IsNewBar() is called and returns true because it is the first tick of a new bar

if it is called again during the same tick it will return false.

So it can be called as many times as you like in a tick, but it can only ever return true the first time that it is called in the tick.

if(NewBar() && HAGreenCandleCurrentPeriod(1))
   Alert("New Bar & Green Candle");
if(NewBar() && !HAGreenCandleCurrentPeriod(1))
   Alert("New Bar & Red Candle");

hmm becoming a little bit confuse here. what do you mean by calling it again on the same tick? In my code, did this means calling it again?

 
Luandre Ezra:

hmm becoming a little bit confuse here. what do you mean by calling it again on the same tick? In my code, did this means calling it again?

Don't worry about it. Just don't use a function to check for a new bar.

Reason: