Why doesn't this count correctly? Please help.

 

Still taking my time trying to learn as much as I can as I move along. Making a really simple indicator that should print the open of the current bar each time there is a new bar. It works correctly by checking each of the last 10 bars down to 0 but then at each tick it checks both bars 1 and 0 rather than just 1. What do I need to do different so it only prints info for the current new bar like I'm trying to do?

//+------------------------------------------------------------------+
//|                                                 _MA + IB Ind.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property indicator_chart_window

extern int iHistory = 10;                            //Bars to use
datetime dtCurrentTime = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

   if(GetNewBar() == true)
    {
     PrintOpen();
     SoundAlert();
    } 
     ErrorCheck();


   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {


   return(0);
  }

//--------------------------------------------------------------------


   void PrintOpen()
      {
         int    i, counted_bars;
         double dOpen, dClose;
   
         counted_bars = IndicatorCounted();
         i=Bars-counted_bars-1;
         if(i>iHistory) i = iHistory;
      
         
          while(i>=0)
            {
             dOpen = iOpen(NULL,0,i);
             dClose= iClose(NULL,0,i);
             
             Print("Checking bar ",i," Open = ",dOpen);
             i--;
            }
         
      }


   void ErrorCheck()
      {
       int err = GetLastError();
       if(err != 0)
         Alert("Error: ",err);
      }
      

bool GetNewBar()
{
   // Check for a new bar
   if (dtCurrentTime != Time[0])
   {
   dtCurrentTime = Time[0];
   return(true);
   }
   else return(false);
   
} // End Function GetNewBar()
      
   void SoundAlert()
      {
       PlaySound("alert.wav");
      }

Thanks.

 
try following code. not testet, but i think your error is:Bars-counted_bars

   void PrintOpen()
      {
         int    i, counted_bars;
         double dOpen, dClose;
   
         counted_bars = IndicatorCounted();
         i=Bars-counted_bars;
         if(i>iHistory) i = iHistory;
      
         
          while(i>=0)
            {
             dOpen = iOpen(NULL,0,i);
             dClose= iClose(NULL,0,i);
             
             Print("Checking bar ",i," Open = ",dOpen);
             i--;
            }
         
      }
 
zzuegg wrote >>
try following code. not testet, but i think your error is:Bars-counted_bars



Thanks. Just tried that. It ended up checking 2,1,0 at each tick so I figured out that if I change it to i=Bars-counted_bars-2 it ends up working. So I have to have something wrong. Any other suggestions?

 

I keep trying to move along with my learning of mql4 but seem to keep hitting the same problem each time that I don't want to move past without making sure I understand it correctly. I've tried this on both on MBtrading and Alpari now to see if that had anything to do with it. Have even gone back to some of the first tutorials I went through to see if I could figure this out. Started testing the RandomIndicator for an example and still, I notice that rather than checking just the last bar with each new tick, it checks the last 2 bars. Please help me understand why this happens? Where can I read about this and understand how to code this correctly so I have this part understood? I know Im close but could really use a few pointers so I can move on. Please help!!! Been working on this too long now. Any advice is much appreciated.

//+------------------------------------------------------------------+
//|                                              RandomIndicator.mq4 |
//|                                                     Antonuk Oleg |
//|                                                   banderass@i.ua |
//+------------------------------------------------------------------+
#property copyright "Antonuk Oleg"
#property link      "banderass@i.ua"

#property indicator_level1 800.0
#property indicator_level2 200.0
#property indicator_levelcolor LimeGreen
#property indicator_levelwidth 2
#property indicator_levelstyle STYLE_SOLID

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkOrchid



//---- input parameters
extern int       barsToProcess=100;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
//----

   MathSrand(TimeLocal());
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted(),
       limit;

   if(counted_bars>0)
      counted_bars--;
   
   limit=Bars-counted_bars;
   
   if(limit>barsToProcess)
      limit=barsToProcess;
  
   for(int i=0;i<limit;i++)
   {
      ExtMapBuffer1[i]=MathRand()%1001;
      Print("Checking bar ",i);
   }
   
   return(0);
}
//+------------------------------------------------------------------+
The only way that this does calculations for just the last bar like its supposed to is to change limit=Bars-counted_bars-1. The line before already decreased it by one so I'm essentially ydoing the same thing I mentioned before and subtracting 2 in order to make it work right...
Reason: