Function Sleep() can't be called for Custom Indicator. Is there any workaround ?

 

Hi,

The Sleep() function isn't allowed for a custom indicator.  Is there any workaround ?

I wrote the following code but it doesnt work fine when I change timeframes of charts.

But when I did the same thing with Expert Adviser program, it works fine by using the Sleep() function.

How can I do that with Custom Indicator ?

int OnInit()
   {
    int x=0,y=0;
    ChartTimePriceToXY(0,0,D'2013.10.01',1,x,y);     //Always wrong value
    return(INIT_SUCCEEDED);
   }

or

int OnInit()
   {
    if(ChartNavigate(0,CHART_CURRENT_POS,100)==false) Print("Error!!!");    //Sometimes error occurs
    return(INIT_SUCCEEDED);
   }

 

 Thank you =)


Documentation on MQL5: Common Functions / Sleep
Documentation on MQL5: Common Functions / Sleep
  • www.mql5.com
Common Functions / Sleep - Documentation on MQL5
 
stitchtrader:

Hi,

The Sleep() function isn't allowed for a custom indicator.  Is there any workaround ?

I wrote the following code but it doesnt work fine when I change timeframes of charts.

But when I did the same thing with Expert Adviser program, it works fine by using the Sleep() function.

How can I do that with Custom Indicator ?

or

 

 Thank you =)


It's probably not a good idea to put this code in OnInit(), your examples don't make a lot of sense.
 
angevoyageur:
It's probably not a good idea to put this code in OnInit(), your examples don't make a lot of sense.
Hi,
Thank you
Where should I put this if I want it to be executed once when the program is loaded ?

 
stitchtrader:
Hi,
Thank you
Where should I put this if I want it to be executed once when the program is loaded ?

In OnTick(), for example :

void OnTick()
  {
   static bool firstCall=true;
   
   if(firstCall)
   {
      //-- Your code ...
      firstCall=false;
   }
...
 
angevoyageur:

In OnTick(), for example :

Thank you so much!

I'll try that :)

 

Hi, 

It almost works fine in the way that you told me. Thanks (=^・^=)

But when I change timeframe of chart, 'Wating For Update' pending sometimes occurs for seconds.

 

In that case, the function still returns false.

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[]
                )
 {
  static bool firstCall=true;
  if(firstCall)
   {
    if(ChartNavigate(0,CHART_CURRENT_POS,100)==false)
      Print("ChartNavigate Error!!!");
    firstCall=false;
   }
 }

 I know it would work well when the next tick is received, by writing as blow.

 if(ChartNavigate(0,CHART_CURRENT_POS,100)==false)
   Print("ChartNavigate Error");
 else
   firstCall=false;

 But it doesnt work on weekend, when markets are closed.

 Do you have any recommendations to do well any day ?

 
stitchtrader:

Hi, 

It almost works fine in the way that you told me. Thanks (=^・^=)

But when I change timeframe of chart, 'Wating For Update' pending sometimes occurs for seconds.

 

In that case, the function still returns false.

 I know it would work well when the next tick is received, by writing as blow.

 But it doesnt work on weekend, when markets are closed.

 Do you have any recommendations to do well any day ?

Sorry but I don't understand well your issue, what "doesn't work on weekend" ?
 
angevoyageur:
Sorry but I don't understand well your issue, what "doesn't work on weekend" ?

 Sorry about my bad English :'(

 

if(ChartNavigate(0,CHART_CURRENT_POS,100)==false)
  Print("ChartNavigate Error");
else
  firstCall=false;

Even though the ChartNavigate function fails in firstCall, it is called by every tick before becoming successful on weekdays.

But on weekend, it doesnt work fine because the ChartNavigate function is called only once,

the second never be called.

So I'm strugging to make it work any day.

 

I wanted to do like below with Custom Indicators.

for(int i=0;i<100;i++)
 {
   if(ChartNavigate(0,CHART_CURRENT_POS,100)==false)
     Sleep(50);
   else
    {
      firstCall=false;
      break;
    }
 }
 
stitchtrader:

 Sorry about my bad English :'(

 

Even though the ChartNavigate function fails in firstCall, it is called by every tick before becoming successful on weekdays.

But on weekend, it doesnt work fine because the ChartNavigate function is called only once,

the second never be called.

So I'm strugging to make it work any day.

 

I wanted to do like below with Custom Indicators.

Your English is just as good as mine

Ok, I got it now. On week-end OnCalculate() is only call once as there is no tick. Isn't your last posted code working ? Maybe you can try a while() loop.

 
angevoyageur:

Your English is just as good as mine

Ok, I got it now. On week-end OnCalculate() is only call once as there is no tick. Isn't your last posted code working ? Maybe you can try a while() loop.

I feel some relief about my English :-)

The last posted code works fine only as an Expert Adviser program that allows the Sleep() function.

But it doesnt as a Custom Indicator that doesnt allow the Sleep() function sadly. 

So I appreciate if you could tell me any idea or any workaround !

 

Thanks for your time (^_^)

Documentation on MQL5: Common Functions / Sleep
Documentation on MQL5: Common Functions / Sleep
  • www.mql5.com
Common Functions / Sleep - Documentation on MQL5
Reason: