How can I use isNewBar function correctly?

 
How can I use isNewBar function correctly?

I am coding for a structure that a certain order is entered only when a new bar appears with a predefined condition?
How can I use isNewBar function correctly in my coding?

My basic coding is below as and the expected ‘isNewBar’ function doesn’t seem to work. Which part I need to modify?

input static isNewBar(string() ,ENUM_TIMEFRAMES());
   
void OnTick()
  {
   double MA=iMA(NULL, PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE, 0)

   if (isNewBar () == && Close[1] > MA )
   {
   OrderSend("EURUSD",OP_BUY, Lotsize, Ask, Slippage, 0, 0, "1", MagicNo, 0, clrGreen);
   }

}

Best, 

M.G.

 
mygreensun:
How can I use isNewBar function correctly?

I am coding for a structure that a certain order is entered only when a new bar appears with a predefined condition?
How can I use isNewBar function correctly in my coding?

My basic coding is below as and the expected ‘isNewBar’ function doesn’t seem to work. Which part I need to modify?

input static isNewBar(string() ,ENUM_TIMEFRAMES());
   
void OnTick()
  {
   double MA=iMA(NULL, PERIOD_H1, 20, 0, MODE_SMA, PRICE_CLOSE, 0)

   if (isNewBar () == && Close[1] > MA )
   {
   OrderSend("EURUSD",OP_BUY, Lotsize, Ask, Slippage, 0, 0, "1", MagicNo, 0, clrGreen);
   }

}

Best, 

M.G.

Input is related to parameters for program, not for function return type- also why is the function static?

bool isNewBar(string() ,ENUM_TIMEFRAMES());

need to call IsNewBar with its parameters

 if (isNewBar (Symbol(),Period()) && Close[1] > MA )
 
Amir Yacoby:

Input is related to parameters for program, not for function return type- also why is the function static?

need to call IsNewBar with its parameters

void OnTick()
{
if(IsNewBar(Symbol(),_Period)) {
........
}
}
 

Thank you, guys. I will try to do it again. 


M.G

 
Amir Yacoby:

Input is related to parameters for program, not for function return type- also why is the function static?

need to call IsNewBar with its parameters

Hi Amir, 

I modified the part as you suggested. 

After compiling, there is an error message that 'IsNewBar' - function must have a body

Is there anything I need to add??

input double Lotsize=1.0;
input int Slippage=10;
input int Stoploss=30;
input int Takeprofit=30;

input int MAPeriod=20;
input int MAShift=0;
input ENUM_TIMEFRAMES MATimeFrame=PERIOD_M30;
input ENUM_MA_METHOD MAMethod=MODE_SMA;
input ENUM_APPLIED_PRICE MAPrice=PRICE_CLOSE;
bool IsNewBar (string symbol, ENUM_TIMEFRAMES timeframe);

void OnTick()
   {
   double MA=iMA(NULL, MATimeFrame, MAPeriod, MAShift, MAMethod, MAPrice, 0);
    
   if (IsNewBar (Symbol(), PERIOD_M30))
   {
   if (Close[1] > MA)
   {
   OrderSend(Symbol(),OP_BUY, Lotsize, Ask, Slippage, 0, 0, "1", 2, 0, clrGreen);
   }
   }
   }

Best, 

M.G.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
mygreensun:

Hi Amir, 

I modified the part as you suggested. 

After compiling, there is an error message that 'IsNewBar' - function must have a body

Is there anything I need to add??

input double Lotsize=1.0;
input int Slippage=10;
input int Stoploss=30;
input int Takeprofit=30;

input int MAPeriod=20;
input int MAShift=0;
input ENUM_TIMEFRAMES MATimeFrame=PERIOD_M30;
input ENUM_MA_METHOD MAMethod=MODE_SMA;
input ENUM_APPLIED_PRICE MAPrice=PRICE_CLOSE;
bool IsNewBar (string symbol, ENUM_TIMEFRAMES timeframe);

void OnTick()
   {
   double MA=iMA(NULL, MATimeFrame, MAPeriod, MAShift, MAMethod, MAPrice, 0);
    
   if (IsNewBar (Symbol(), PERIOD_M30))
   {
   if (Close[1] > MA)
   {
   OrderSend(Symbol(),OP_BUY, Lotsize, Ask, Slippage, 0, 0, "1", 2, 0, clrGreen);
   }
   }
   }

Best, 

M.G.

Aren't you calling IsNewBar(...), but not providing any code for it to action? 

It's not an inbuilt function like, say iLow, which returns a value without you having to do anything else

iLow(_Symbol,_Period,0) ;

It might have to look like this

bool IsNewBar (string symbol, ENUM_TIMEFRAMES timeframe){
//  code in here to
return true;  // or
return false;
}



 
andrew:

Aren't you calling IsNewBar(...), but not providing any code for it to action? 

There are likely dozens of definitions for IsNewBar() in this forum.

This is the one I use:

// Detects when a "new bar" occurs, which is the same as when the previous bar has completed.
bool IsNewBar(const string symbol, const ENUM_TIMEFRAMES period)
{
        bool isNewBar = false;
        static datetime priorBarOpenTime = NULL;

        // New Bar event handler -> per https://www.mql5.com/en/articles/159
        // SERIES_LASTBAR_DATE == Open time of the last bar of the symbol-period
        const datetime currentBarOpenTime = (datetime) SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);

        if( priorBarOpenTime != currentBarOpenTime )
        {
                // Don't want new bar just because EA started
                if ( priorBarOpenTime == NULL )
                {
                        isNewBar = false;
                }
                else
                {
                        isNewBar = true;
                }
                // isNewBar = ( priorBarOpenTime == NULL )?false:true;  // priorBarOpenTime is only NULL once

                // Regardless of new bar, update the held bar time
                priorBarOpenTime = currentBarOpenTime;
        }

        return isNewBar;
}
 
Anthony Garot:

There are likely dozens of definitions for IsNewBar() in this forum.

This is the one I use:

Yes, I was encouraging OP to create their own solution...;) 
 
andrew:
Yes, I was encouraging OP to create their own solution...;) 
Ah. I see that now.