OnBarChange

 

Is there a way to get an indication to the Start function only when there is a new bar?

 
eyalsh:

Is there a way to get an indication to the Start function only when there is a new bar?


try this

if(newcandle!= Time[0]) //check for new candle
{
...put your code here

newcandle=Time[0];

}

 

you forgot to declare:

static datetime newcandle=0;


//z

edit: to make your code more secure you should also check:

if(high==open && high=close && low==open && low==close)

or

if(Volume==1)

 
zzuegg:

you forgot to declare:

static datetime newcandle=0;


//z

edit: to make your code more secure you should also check:

if(high==open && high=close && low==open && low==close)

or

if(Volume==1)


Thanks :)
 

Volume==1 is not reliable. Volume can skip numbers

high==open&& high=close... isn't reliable. Start is called when anything changes not just price, like spread, MarketInfo(Symbol(), MODE_STOPLEVEL), etc

Bars == bars.prev isn't reliable. Bars stops changing once max bars in chart is reached.

Time[0] is reliable

int start(){
    static datetime Time0;  bool    newBar  = Time0 < Time[0];
    if (!newBar) return(0);                   Time0 = Time[0];
 
WHRoeder:

Volume==1 is not reliable. Volume can skip numbers

high==open&& high=close... isn't reliable. Start is called when anything changes not just price, like spread, MarketInfo(Symbol(), MODE_STOPLEVEL), etc

Bars == bars.prev isn't reliable. Bars stops changing once max bars in chart is reached.

Time[0] is reliable


hi, time[0] alone is not reliable at all.

if connection drops and come back a few minutes later your EA will be take the first new tick as a new bar.

i never said only use volume or only use high==low eccc.

i ment:

static datetime Time.update=0; 
if(Time[0]!=Time.update){
  Time.update=Time[0];
  if(High[0]==Low[0]){
   //New Bar reliable
  }
}
 

addon:

the biggest problem with this solution is, that in fast moving markets and "slow" EA's there is the possibility that a bar is missed. To prevent that maybe a treshold could be inserted...lets lay 1pip


still far away from bulletproof, but nearer than everything else

 

You can use this function to check if it's a new bar or not:

bool NewBar ()

{

   static datetime LastTime = 0;

   if (Time[0] != LastTime) {
      LastTime = Time[0];     
      return (true);
   } else
      return (false);
}

And you can call it at any part of your program this way:


if(NewBar()) //do somthing
 

Hi. I tried so many ways but it couldn't make it work.

Should I put if ( NewBar () == true) before trading criteria?

Best regards.

 
lmrf:

Hi. I tried so many ways but it couldn't make it work.

Should I put if ( NewBar () == true) before trading criteria?

Best regards.


Should work
 

Hi. I am testing with a simple trading criteria like Stochastic Cross Signal line. Close short and open long position if a buy signal is generated, close long and open short position if a sell signal is generated. I tested in Metastock to see how many trades were generated and resulted in about 220 trades from August to 18th October in a 15min chart. In MetaTrader resulted less than 90 trades. I checked personally and there were many signals that didn't trigger an order. I put the "if(newbar() == true)" before trading criteria.

Best regards.

//------------------------------- 
bool newbar()

   {
    static datetime LastTime = 0;

    if (Time[0] != LastTime)
       {
        LastTime = Time[0];
        return (true);
       }
    else
      return (false);
   }
//-------------------------------    
int start()
  {
// initial data checks

   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);
     }
//-------------------------------
   StoCurrent=iStochastic(NULL,PERIOD_M15,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,0);
   StoPrevious=iStochastic(NULL,PERIOD_M15,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,1);
   SignalCurrent=iStochastic(NULL,PERIOD_M15,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_SIGNAL,0);
   SignalPrevious=iStochastic(NULL,PERIOD_M15,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_SIGNAL,1);

//------------------------------- 
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("No sufficient funds. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if(newbar() == true)
      if(StoCurrent>SignalCurrent && StoPrevious<SignalPrevious)    //TRADING CRITERIA
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"",MAGICMA,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }  
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if(StoCurrent<SignalCurrent && StoPrevious>SignalPrevious)    //TRADING CRITERIA
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"",MAGICMA,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
// --------  
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&       // check for opened position 
         OrderSymbol()==Symbol() &&    // check for symbol
         OrderMagicNumber() == MAGICMA) // check for magic number
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(newbar() == true)
            if(StoCurrent<SignalCurrent && StoPrevious>SignalPrevious)    //TRADING CRITERIA
              {
               OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position
               return(0); // exit
              }
           }
         else // go to short position
           {
            // should it be closed?
            if(newbar() == true)
            if(StoCurrent>SignalCurrent && StoPrevious<SignalPrevious)    //TRADING CRITERIA
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position
               return(0); // exit
              }
           }
        }
     }
   return(0);
  }



Reason: