indicator updates only half. what am i doing wrong?

 
double tickValue=MarketInfo(_Symbol,MODE_TICKVALUE);

string symbol = "EURUSD";
double ask    = SymbolInfoDouble(symbol,SYMBOL_ASK);

string name, length;
   bool DoAlert = false;
   int index = 0;
   
   if (WaitForClose) index = 1;
   
   int counted_bars = IndicatorCounted();
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   if (limit > 6000) limit = 6000;
   
     
   for (int i = 0; i <= limit; i++)

name = "Red-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
       length = DoubleToStr(((ask-iOpen("GBPUSD",0,i))*10000)0);

however if i change ask to iclose then length = DoubleToStr((((iClose("GBPUSD",0,i))-iOpen("GBPUSD",0,i))*10000) 
then the indicator works and update. so what do i do wrong? is it in the int limiter?

 
  1. double tickValue=MarketInfo(_Symbol,MODE_TICKVALUE);
    double ask    = SymbolInfoDouble(symbol,SYMBOL_ASK);

    Your posted code is without context. Are these lines inside a function or global? Why are other lines indented?
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    If they are outside, then those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  2.    for (int i = 0; i <= limit; i++)   
           name = "Red-" + TimeToStr(Time[i], TIME_DATE|TIME_MINUTES);
    
           length = DoubleToStr(((ask-iOpen("GBPUSD",0,i))*10000)0);
    

    Why are you repeatedly assigning to name? What do you expect length to be, when it is after the loop?

  3. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  4.    int counted_bars = IndicatorCounted();
       if (counted_bars > 0) counted_bars--;
       int limit = Bars - counted_bars;
       if (limit > 6000) limit = 6000;

    You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

    See How to do your lookbacks correctly #9#14 & #19. (2016)

 
William Roeder #:
  1. Your posted code is without context. Are these lines inside a function or global? Why are other lines indented?
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    If they are outside, then those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  2. Why are you repeatedly assigning to name? What do you expect length to be, when it is after the loop?

  3. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  4. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

    See How to do your lookbacks correctly #9#14 & #19. (2016)

thank you a lot for the reply. what is the reason why new event handlers are used? (oninit,ontick,oncalculate,ondeinit) ? that is confusing to me, but i am sure it must have a reason. 

what i expect length to be, is on the screen above the bar it should give a number that gets updated, but at the moment it is static. but as you explained above i understand it now, i basically assign a string to a text and it gets added, but it does not update. but why is that? should i otherwise upload the whole indicator so far i made? i just thought i use a small part of it. so it is easier to understand?

but when i do  

length = DoubleToStr(((ask-iOpen("GBPUSD",0,i))*10000)0);

it is static, but when i do

length = DoubleToStr((((iClose("GBPUSD",0,i))-iOpen("GBPUSD",0,i))*10000),0);

then it does update. 

but as i understand from your post that happens because i use

string symbol = "EURUSD";
double ask    = SymbolInfoDouble(symbol,SYMBOL_ASK);

so my question is how can i fix that? hopefully it is something small. otherwise i need to rewrite the indicator. thanks