Download MetaTrader 5

what wrong with my While Loop

To add comments, please log in or register
Find out the latest MQL5.community news!
didatsd
60
didatsd 2016.01.25 14:02 

I want to loop the program until new candle occurs

 

  static datetime newbar;

 

        newbar = Time[0];

          while(newbar == Time[0])

               {

                Print("Still Old candle");

               }
          Print("Now new candle occurs");

 

every program run this While Operation always hang the meratrader.

Any  suggestion ?

Carl Schreiber
6591
Carl Schreiber 2016.01.25 15:14  
Try:  Sleep(500) within the loop!
didatsd
60
didatsd 2016.01.25 15:25  
gooly:
Try:  Sleep(500) within the loop!

I have try with Sleep(1000) 

even the  "Still Old candle" doesn't occurs in the expert tab

still the Meratrader hang and the CPU run 100% until I close force the metatrader

Carl Schreiber
6591
Carl Schreiber 2016.01.25 15:27  
Show the whole code including the Sleep(1000)!
didatsd
60
didatsd 2016.01.25 15:29  
//+------------------------------------------------------------------+
#property copyright "aka MikeZTN"
#property link      "ICQ 138092006"

#property indicator_chart_window

//+------------------------------------------------------------------+
color CbodyUp = C'81,0,81';
color CbodyDn = C'147,0,0';
color CshadUp = C'0,0,185';
color CshadDn = C'202,0,0';

static datetime waktu, ww;
static int cnt = 0;

int      numbar = 60;
int      i, x, tgl, mbar;
double   po, pc, ph, pl;
datetime to, tc;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init()
     {
      if(Period() < PERIOD_H4)
        {
         for(i=0; i<numbar; i++)
            {
             ObjectCreate("xxBodyTF"+i, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
             ObjectCreate("xxShadUp"+i, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
             ObjectCreate("xxShadDn"+i, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
             ObjectSet   ("xxBodyTF"+i, OBJPROP_BACK,   True);            
             ObjectSet   ("xxShadUp"+i, OBJPROP_BACK,   True);            
             ObjectSet   ("xxShadDn"+i, OBJPROP_BACK,   True);            
            }
        }
      if(Period() == PERIOD_M1) mbar = 1140;
      else if(Period() == PERIOD_M5) mbar = 288; 
           else if(Period() == PERIOD_M15) mbar = 96; 
                else if(Period() == PERIOD_M30) mbar = 48; 
                     else mbar = 24; 
      return;   
     }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit()
     {
      ObjectsDeleteAll(0,"xx*");
     }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() 
    {   
//    return(0);
     if(Period() > PERIOD_H1 || Bars <= mbar) return(0);

     if(cnt != Bars)
       {
        x = 0;
        tgl = TimeDay(Time[x]);
        while(tgl == TimeDay(Time[x])) x++;
        
        for(i=1; i<=numbar; i++)
           {
            if(x < Bars - mbar)
              {
               tgl = TimeDay(Time[x]);
               pc  = Close  [x];
               ph  = High   [x];
               pl  = Low    [x];
               tc  = Time   [x]; 
               while(tgl == TimeDay(Time[x]))
                    {
                     ph = MathMax(ph, High[x]);
                     pl = MathMin(pl, Low [x]);
                     po = Open[x]; 
                     to = Time[x];   
                     x++;
                    }
               docandles(i, to, tc, po, pc, ph, pl);
              }
           }
       }
       
     else
       {
          x = 0;
          tgl = TimeDay(Time[x]);
          pc  = Close[x];
          ph  = 0;
          pl  = 0;
          while(tgl == TimeDay(Time[x]))
               {
                ph = MathMax(ph, High[x]);
                pl = MathMin(pl, Low [x]);
                po = Open[x]; 
                to = Time[x];   
                x++;
               }
          tc = to + 1440 * 60;
          docandles(0, to, tc, po, pc, ph, pl);

          waktu = Time[0];
          
          while(waktu == Time[0])
               {
                pc = Close[0];
                ph = MathMax(ph, Close[0]);
                pl = MathMin(pl, Close[0]);
                docandles(0, to, tc, po, pc, ph, pl);
                Sleep(1000);
               }

       }

     cnt = Bars;
     return(0);
    }
//+------------------------------------------------------------------+
void docandles(int mi, datetime mto, datetime mtc,
               double mpo, double mpc, double mph, double mpl)
     {
      ObjectSet("xxBodyTF"+mi, OBJPROP_TIME1,  mto);
      ObjectSet("xxBodyTF"+mi, OBJPROP_PRICE1, mpo);
      ObjectSet("xxBodyTF"+mi, OBJPROP_TIME2,  mtc);
      ObjectSet("xxBodyTF"+mi, OBJPROP_PRICE2, mpc);
      if(mpo<mpc) ObjectSet("xxBodyTF"+mi, OBJPROP_COLOR, CbodyUp);
      else        ObjectSet("xxBodyTF"+mi, OBJPROP_COLOR, CbodyDn);

      ObjectSet("xxShadUp"+mi, OBJPROP_TIME1,  mto);
      ObjectSet("xxShadUp"+mi, OBJPROP_PRICE1, mph);
      ObjectSet("xxShadUp"+mi, OBJPROP_TIME2,  mtc);
      ObjectSet("xxShadUp"+mi, OBJPROP_PRICE2, MathMax(mpo,mpc));
      if(mpo<mpc) ObjectSet("xxShadUp"+mi, OBJPROP_COLOR, CshadUp);
      else        ObjectSet("xxShadUp"+mi, OBJPROP_COLOR, CshadDn);

      ObjectSet("xxShadDn"+mi, OBJPROP_TIME1,  mto);
      ObjectSet("xxShadDn"+mi, OBJPROP_PRICE1, mpl);
      ObjectSet("xxShadDn"+mi, OBJPROP_TIME2,  mtc);
      ObjectSet("xxShadDn"+mi, OBJPROP_PRICE2, MathMin(mpo,mpc));
      if(mpo<mpc) ObjectSet("xxShadDn"+mi, OBJPROP_COLOR, CshadUp);
      else        ObjectSet("xxShadDn"+mi, OBJPROP_COLOR, CshadDn);
      
     
      return;
     }
didatsd
60
didatsd 2016.01.25 15:35  

Here is the simple one

and same result

//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window


static datetime newbar;
//+------------------------------------------------------------------+
int start()
    {
     newbar = Time[0];
          
     while(newbar == Time[0])
          {
           Print("Still Old candle");
           Sleep(1000);
          }
     Print("Now new candle occurs");
     return(0);
    }
//+------------------------------------------------------------------+
Carl Schreiber
6591
Carl Schreiber 2016.01.25 15:52  

What about these two other loops?

               tgl = TimeDay(Time[x]);
               while(tgl == TimeDay(Time[x]))
                    {
                     ph = MathMax(ph, High[x]);
                     pl = MathMin(pl, Low [x]);
                     po = Open[x]; 
                     to = Time[x];   
                     x++;
                    }

There you are trapped until midnight and even here at 'full pc speed'!

didatsd
60
didatsd 2016.01.25 16:01  
gooly:

What about these two other loops?

There you are trapped until midnight and even here at 'full pc speed'!

Here I use from the candle history "Time[x]", so we don't wait the candle 

but with the "Sleep" one I use Candle Time[0] 

didatsd
60
didatsd 2016.01.25 16:05  
didatsd:

Here is the simple one

and same result

 

If we look second example.

I think I don't do mistake in the program, but the problem is in the syntax While(), but I do not know why.

Demos Stogios
189
Demos Stogios 2016.01.25 16:10  

hello,

 The problem bug lies in the Print function; i replaced print with Alert and it is working as expected. By the way, global variables are static by themselves

(in MQL that is) so there is no need to do

static datetime newbar ;

 

best regards 

Demos Stogios
189
Demos Stogios 2016.01.25 16:13  
Well, i was hurry on that one; Alert looks like it is working, but it does not actually
123
To add comments, please log in or register