!!!! How to get correct result on OnInit() or OnStart() such as function : BarsCalculated() , CopyBuffer()...

To add comments, please log in or register
Yu Zhang
389
Yu Zhang  

Sometimes we want the program to execute only once. So We use OnInit() or OnStart() . But I find that some code cannot be executed. 

Such as:

void OnStart() 
  { 
   int handle=iMA(_Symbol,_Period,15,0,MODE_SMA,PRICE_CLOSE);
   Print( BarsCalculated(handle) );
  }

The result is -1. 

Even I change it:

void OnStart() 
  { 
   int handle=iMA(_Symbol,_Period,15,0,MODE_SMA,PRICE_CLOSE);
   if(BarsCalculated(handle)<0)
     {
       Sleep(500);
       for(int i=0;i<100;i++)
         {
           if(BarsCalculated(handle)>0)
             Print(i);
           else
             Sleep(500);
         }
       
     }
    Print("Wrong");
  }

the result is "Wrong".

I want to get the correct result. How to do it? 

Please don't tell me use OnTick() or OnCalculate()!!! I just want to get correct result only in OnStart() or OnInit().

Thanks!!

Petr Nosek
1570
Petr Nosek  
Yu Zhang:

Sometimes we want the program to execute only once. So We use OnInit() or OnStart() . But I find that some code cannot be executed. 

Such as:

The result is -1. 

Even I change it:

the result is "Wrong".

I want to get the correct result. How to do it? 

Please don't tell me use OnTick() or OnCalculate()!!! I just want to get correct result only in OnStart() or OnInit().

Thanks!!

The OnStart() function is the Start event handler, which is automatically generated only for running scripts.

The BarsCalculated() returns the number of calculated data for the specified indicator.

Yu Zhang
389
Yu Zhang  
Petr Nosek:

The OnStart() function is the Start event handler, which is automatically generated only for running scripts.

The BarsCalculated() returns the number of calculated data for the specified indicator.

I know what your say. I want to execute someting about Indicator just only once. So I try to use it OnStart() or Oninit(). But any attempt will be failed. So I want to kown Is there any good idea with OnStart() or Oninit().

kypa
1038
kypa  
void OnStart() 
  { 
   Print(GetTickCount());
   int handle=iMA(_Symbol,_Period,15,0,MODE_SMA,PRICE_CLOSE);
   while(BarsCalculated(handle)<0) Sleep(500);
   Print(GetTickCount());
   if(BarsCalculated(handle)>0)
     {
      Print(BarsCalculated(handle));
      for(int i=0;i<100;i++)
       {
        if(BarsCalculated(handle)>0)
          Print(i);
        else
          Sleep(500);
       }
     }
   else Print("Wrong");
  }

GetTickCount() shows milliseconds since terminal start.

In indicators you can't use Sleep(), but a timeframe change reinitializes it.

Yu Zhang
389
Yu Zhang  
kypa:

GetTickCount() shows milliseconds since terminal start.

In indicators you can't use Sleep(), but a timeframe change reinitializes it.

Thank you very much.

kypa
1038
kypa  
You can also use OnChartEvent(), executing your code on pressed key or clicking something on the chart.
To add comments, please log in or register