Indicator Miscellaneous Questions - page 15

 

OnCalculate

The OnCalculate() function is called only in custom indicators when it's necessary to calculate the indicator values by the Calculate event.

This usually happens when a new tick is received for the symbol, for which the indicator is calculated.

This indicator is not required to be attached to any price chart of this symbol.

The OnCalculate() function must have a return type int. There are two possible definitions. Within one indicator you cannot use both versions of the function.

The first form is intended for those indicators that can be calculated on a single data buffer. An example of such an indicator is Custom Moving Average.

int OnCalculate (const int rates_total,      // size of the price[] array
                 const int prev_calculated,  // bars handled on a previous call
                 const int begin,            // where the significant data start from
                 const double& price[]       // array to calculate
   );

 
Marco vd Heijden:

OnCalculate

The OnCalculate() function is called only in custom indicators when it's necessary to calculate the indicator values by the Calculate event.

This usually happens when a new tick is received for the symbol, for which the indicator is calculated.

This indicator is not required to be attached to any price chart of this symbol.

The OnCalculate() function must have a return type int. There are two possible definitions. Within one indicator you cannot use both versions of the function.

The first form is intended for those indicators that can be calculated on a single data buffer. An example of such an indicator is Custom Moving Average.

int OnCalculate (const int rates_total,      // size of the price[] array
                 const int prev_calculated,  // bars handled on a previous call
                 const int begin,            // where the significant data start from
                 const double& price[]       // array to calculate
   );

Perfect, Mr. Marco, thanks so much more.
 

 ( I use word " Lag " it means delays price action, order open, close, just one word takes down my MT4 platform ) 

I use below function for my custom indicator.

int OnCalculate(const int rates_total, const int prev_calculated, const datetime & time[], const double & open[], const double & high[], const double & low[], const double & close[], const long & tick_volume[], const long & volume[], const int & spread[]){...}

When I write below return code to my custom indicator then my custom indicator starts to lag. But works correct, which one I want.

return(0); // starts to lag...

And when I write below return code to my custom indicator then my custom indicator does not work correctly which one I want. I mean while fake MA cross " Arrow " does not go back to previous MA cross point.

return rates_total-1; // does not go back to previous MA cross point
return(rates_total-1); // and same thing here

Q:  What can I do for this situation, please?

Thanks in advance.

 
Max Enrik: And when I write below return code to my custom indicator then my custom indicator does not work correctly which one I want. I mean while fake MA cross " Arrow " does not go back to previous MA cross point.
Post all of your code. That is meaningless without context.
 
whroeder1:
Post all of your code. That is meaningless without context.

Here you go.

Thanks in advance.

Indicator Example Chart 06

#property strict
#property indicator_chart_window
#property indicator_buffers 2

string arrowIcon="delete arrow icon";

int i;
double arrowLow,arrowHigh,priceOne,priceTwo,priceOne_pre,priceTwo_pre,bufferOne[],bufferTwo[];
color clrup=clrBlue,clrdown=clrRed;
datetime arrowTime;
//---------------------------------------------------------
// OnDeinit
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0,"delete");
   return;
  }
// OnInit
int OnInit()
  {
   IndicatorDigits(Digits);
// line
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clrdown);
   SetIndexBuffer(0,bufferOne);

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,clrup);
   SetIndexBuffer(1,bufferTwo);
//---
   return(0);
  }
// OnCalculate
int OnCalculate(const int rates_total,const int prev_calculated,
                const datetime  &time[],const double  &open[],const double  &high[],
                const double  &low[],const double  &close[],const long  &tick_volume[],
                const long  &volume[],const int  &spread[])
  {
//---
   int lookback=30; // I do not know what could I write here.
   for(i=Bars-1-MathMax(lookback,prev_calculated); i>=0; --i)
     {
      bufferOne[i]=iMA(Symbol(),0,lookback/2,0,MODE_EMA,PRICE_CLOSE,i);
      bufferTwo[i]=iMA(Symbol(),0,lookback,0,MODE_EMA,PRICE_CLOSE,i);

      priceOne = bufferOne[i];
      priceTwo = bufferTwo[i];
      priceOne_pre = bufferOne[i+5];
      priceTwo_pre = bufferTwo[i+5];

      // buy
      if(priceOne>priceTwo && priceOne_pre<=priceTwo_pre)
        {
         arrowTime=Time[i];
         //arrowLow  = iLow( Symbol(), 0, i );
         arrowLow=Low[i];

         objArrow();
         if(ObjectFind(0,arrowIcon)>=0)
           {
            ObjectMove(0,arrowIcon,0,arrowTime,arrowLow-5*Point);
            ObjectSetInteger(0,arrowIcon,OBJPROP_COLOR,clrup);
            ObjectSetInteger(0,arrowIcon,OBJPROP_ANCHOR,ANCHOR_TOP);
           }
        }
      // sell
      if(bufferOne[i]<bufferTwo[i] && priceOne_pre>=priceTwo_pre)
        {
         arrowTime = Time[i];
         arrowHigh = High[i];

         objArrow();
         if(ObjectFind(0,arrowIcon)>=0)
           {
            ObjectMove(0,arrowIcon,0,arrowTime,arrowHigh+5*Point);
            ObjectSetInteger(0,arrowIcon,OBJPROP_COLOR,clrdown);
            ObjectSetInteger(0,arrowIcon,OBJPROP_ANCHOR,ANCHOR_BOTTOM);
           }
        }
     }
   Print("Time: ",arrowTime,"  Low: ",arrowLow);
//    Print( "priceOne ", DoubleToString( priceOne, Digits ), "  priceTwo ", DoubleToString( priceTwo, Digits ) );
//---
//return(0); // works correct but cause lags
   return(rates_total - 1); // no lags but does not go to previous ma cross after fake ma cross
  }
// objects
void objArrow()
  {
   if(ObjectFind(0,arrowIcon)<0)
     {
      ObjectCreate(0,arrowIcon,OBJ_ARROW,0,arrowTime,arrowLow);
      ObjectSetInteger(0,arrowIcon,OBJPROP_ARROWCODE,159);
      ObjectSetInteger(0,arrowIcon,OBJPROP_WIDTH,2);
     }
//---
   return;
  }
//+------------------------------------------------------------------+
 
Max Enrik: I mean while fake MA cross " Arrow " does not go back to previous MA cross point.
You don't test for that and move it back.
 
whroeder1:
You don't test for that and move it back.

OK! Thanks!
 

I still need more clearly answer.

Thanks in advance.

 

 Answer is:  Check Both Current MA Cross and Previous MA Cross.

#Buffer ( array out of range in ) - Closed
#Custom Indicator Lagging - Closed

 

#First result of Loop - Open

string counts;
void _a()
{
    counts += "a - " + IntegerToString( ObjectsTotal( OBJ_ARROW ) );
    //---
    return;
}
void _b()
{
    counts += " - b - " + IntegerToString( ObjectsTotal( OBJ_ARROW ) );
    //---
    return;
}
Print( counts );
// while 1st loop: a - 2 - b - 4
// while 2nd loop: a - 2 - b - 4a - 2 - b - 4
// while 3rd loop: a - 2 - b - 4a - 2 - b - 4a - 2 - b - 4a - 2 - b - 4
// and so on...
// But I like only 1st loop results even that loop runs 10 thousand times

Thanks in advance.

Reason: