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

 

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!!

 
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.

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

 
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.

 
kypa:

GetTickCount() shows milliseconds since terminal start.

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

Thank you very much.

 
You can also use OnChartEvent(), executing your code on pressed key or clicking something on the chart.
 


Dont really know why i am having errors with the ode below, please can anyone assist me. i am a newby.

Thanks



{

  int SL_dist=10;
  int TP_dist=30;
  double prt=0.35;
  string symb=Symbol();
  
  while(true)
  {
  int min_dist=MarketInfo(symb,Mode_STOPLEVEL);
  double min_lot=MarketInfo(symb,Mode_MINLOT);
  double one_lot=MarketInfo(symb,Mode_MARGINREQUIRED);
  double free=AccountFreeMargin();
  double step=MarketInfo(symb,Mode_LOTSTEP);
  double lot=MathFloor(free*prtone_lotstep)*step;
  
  if(lot &lt;min_lot)
  {
  Alert("Money below =",min_lot,"Lots");
  break;
  }
  double SL=bid-SL_dist*point;
  if(SL_dist &lt;min_dist)
  {
  SL_dist=min_dist;
  SL=bid-SL_dist*point;
  }
  double TP=bid+TP_dist*point;
  if(TP_dist &lt;min_dist)
  {
  TP_dist=min_dist;
  TP=bid+TP_dist*point;
  }
  
  int ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,2,TP,SL);
  if(ticket& gt;0);
  {
  Alert("opened order buy= ",ticket);
  break;
  }
  Error=GetLastError();
  
  switch(Error)
  {
  case 135:Alert("Price has change Refreshing.....");
  RefreshRate();
  continue;
  case 136:Alert("No price update. Waiting for the next tick");
  while(RefreshRate()==false)
  Sleep(1);
  continue;
  case 146:Alert("Trading subsystem busy Retrying....");
  Sleep(500);
  RefreshRate();
  continue;
  }
  
  switch(Error);
  {
  case 2:Alert("common error");
  break;
  case 5:Alert("outdated version of the client client terminal");
  break;
  case 64:Alert("Accont is blocked");
  break;
  case 133:Alert("Trading is forbidden");
  break;
  default:Alert("error=",Error,);
  }
  break;
  }
  
  
  
  
  return;
  }
 
alexandermcqueen:


...


Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button