Problems with WindowsPriceMax & WindowsPriceMin

 

Hi guys,

I have a cyclic indicator where the minimum and maximum values have been set to WindowsPriceMin and WindowsPriceMax.

...

double max = WindowPriceMax (0);

double min = WindowPriceMin (0);

...


The problem occurs when I open the chart because the indicator is not visible while everything is ok if I change time frame.

Can anyone tell me why?

 
Stefano Abbondanza :


Where is the question and description of the problem? Where is the indicator code?

 
Vladimir Karputov #:

Where is the question and description of the problem? Where is the indicator code?

The problem is highlighted when opening the chart because the indicator is not displayed, to see it I have to change the time frame.



//---- indicator parameters

extern int year=2022;

extern int month=1;

extern int day=1;

extern int hour=0;

extern int minute=0;

double max= WindowPriceMax();

double min= WindowPriceMin();

extern double cyclelength=0;

extern double trend=0;

extern int subcycles=2;



//---- indicator buffers

double Hurst[];



//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int init()

  {

   string short_name;

  

   IndicatorBuffers(1);

   SetIndexBuffer(0,Hurst); 

   SetIndexStyle(0,DRAW_LINE,2,1);

   SetIndexShift(0,cyclelength);

   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

   IndicatorShortName(WindowExpertName());

      

   

   switch(subcycles)

     {

      case 2: 

         short_name="BTP_2Tempi";   

         break;  

      case 3: 

         short_name="BTP_3Tempi";   

         break;   

      default:

         subcycles=2;

         short_name="BTP_2Tempi";

     }

   

      return(0);

  }

  

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int start()

{

   switch(subcycles)

   {

      case 2: 

         Battleplan_2T();

         break;  

      case 3: 

         Battleplan_3T();

   }

     

   return(0);

}

  

//+------------------------------------------------------------------+

//| Battleplan 2 tempi                                               |

//+------------------------------------------------------------------+

void Battleplan_2T()

{

   int i,phase;

   int flag, m=0;

   int counted_bars = IndicatorCounted();

   double a,w1,w2,w3,w4,cycle,x,grc,c,b,pi;

   if(counted_bars < 0) return (0);

  

   if (Bars!=counted_bars+1)

   {

      

      for( i=Bars; i>=0-cyclelength; i--) 

      {

         m=i-1+cyclelength;

         if (day==TimeDay(Time[i]) && month==TimeMonth(Time[i]) && year==TimeYear(Time[i])&& hour==TimeHour(Time[i]) && minute==TimeMinute(Time[i]))

         {

            flag=1;

         }

         if( flag==0)

         {

            Hurst[m]=EMPTY_VALUE;

         }

         else

         {

            a=a+1;

            x=(360/cyclelength)*a;

            phase=-90;

            pi=3.1415926535;

   

            w1=MathSin(((8*x+phase)*pi)/180);

            w2=2*MathSin(((4*x+phase)*pi)/180);

            w3=3*MathSin(((2*x+phase)*pi)/180);

            w4=4*MathSin(((x+phase)*pi)/180);

            cycle=(w1+w2+w3+w4);

            grc=0.01029;

            c=(grc*trend*1);

            b=b+c;

            Hurst[m]=(cycle+b+10)*((max-min)/15)+min;

            

         }

      }

   }

}

//+------------------------------------------------------------------+

//| Battleplan 3 tempi                                               |

//+------------------------------------------------------------------+

void Battleplan_3T()

{

   int i,phase;

   int flag, m=0;

   int counted_bars = IndicatorCounted();

   double a,w1,w2,w3,w4,cycle,x,grc,c,b,pi;

   if(counted_bars < 0)return (0);

 

  

   if (Bars!=counted_bars+1)

   {

      

      for( i=Bars; i>=0-cyclelength; i--) 

      {

         m=i-1+cyclelength;

         if (day==TimeDay(Time[i]) && month==TimeMonth(Time[i]) && year==TimeYear(Time[i])&& hour==TimeHour(Time[i]) && minute==TimeMinute(Time[i]))

         {

            flag=1;

         }

         if( flag==0)

         {

            Hurst[m]=EMPTY_VALUE;

         }

         else

         {

            a=a+1;

            x=(360/cyclelength)*a;

            phase=-90;

            pi=3.1415926535;

   

            w1=MathSin(((12*x+phase)*pi)/180);

            w2=2*MathSin(((6*x+phase)*pi)/180);

            w3=3*MathSin(((3*x+phase)*pi)/180);

            w4=4*MathSin(((x+phase)*pi)/180);

            cycle=(w1+w2+w3+w4);

            grc=0.01029;

            c=(grc*trend*1);

            b=b+c;

            Hurst[m]=(cycle+b+10)*((max-min)/17.5)+min;

            

         }

      }

   }

}



-------

 
double max= WindowPriceMax();

double min= WindowPriceMin();

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. 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)