简体   繁体   中英

How to stop Indicator from calculating on every tick, when I only need the indicator at bar close?

This is an indicator I downloaded from the internet but I have made some modifications. I noticed the indicator calculates the linear regression line and the upper and lower bands on EVERY tick.

I find that to be resource wasteful as I only need the line to be calculated at the close of each bar; ie when bar 0 ends and a new bar 0 is formed.

It should not calculate anything while bar 0 is still incomplete.

How I do make the necessary changes?

Thanks!!

//+------------------------------------------------------------------+
//|                                       Linear Regression Line.mq4 |
//|                                                      MQL Service |
//|                                           scripts@mqlservice.com |
//+------------------------------------------------------------------+
#property copyright "MQL Service"
#property link      "www.mqlservice.com"

#property indicator_chart_window
#property indicator_buffers   3

#property indicator_color1    White
#property indicator_width1    2

#property indicator_color2    Orange
#property indicator_width2    2

#property indicator_color3    Orange
#property indicator_width3    2

//---- input parameters
extern int LRLPeriod = 20;
extern int Number_SD = 2;

//---- buffers
double LRLBuffer[], LRLBuffer_Upper[], LRLBuffer_Lower[];

//int shift = 0;
int n = 0;
double sumx = 0;
double sumy = 0;
double sumxy = 0;
double sumx2 = 0;
double sumy2 = 0;
double yint = 0;
double r = 0;
double m = 0;

//+------------------------------------------------------------------+
//|                    INITIALIZATION FUNCTION                       |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, LRLBuffer);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(1, LRLBuffer_Upper);
SetIndexStyle(2, DRAW_LINE);
SetIndexBuffer(2, LRLBuffer_Lower);

IndicatorDigits(Digits);
if (LRLPeriod < 2) 
    LRLPeriod = 2;

IndicatorShortName("Linear Regression Line ("+LRLPeriod+")");
SetIndexDrawBegin(0, LRLPeriod+2);
IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS)+4);

return(0);
}

//+------------------------------------------------------------------+
//|                   DEINITIALIZATION FUNCTION                      |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//|                      ITERATION FUNCTION                          |
//+------------------------------------------------------------------+
int start()
{
int limit, j, Counted_bars;
int counted_bars = IndicatorCounted();

if (counted_bars < 0) 
    counted_bars = 0;

if (counted_bars > 0) 
    counted_bars--;

limit = Bars - counted_bars;

for (int shift=limit-1; shift >= 0; shift--)
{
    sumx = 0;
    sumy = 0;
    sumxy = 0;
    sumx2 = 0;
    sumy2 = 0;
    for (n = 0; n <= LRLPeriod-1; n++)
    { 
        sumx = sumx + n;
        sumy = sumy + Close[shift + n];
        sumxy = sumxy + n * Close[shift + n];
        sumx2 = sumx2 + n * n;
        sumy2 = sumy2 + Close[shift + n] * Close[shift + n]; 
    }
    double temp = LRLPeriod * sumx2 - sumx * sumx;
    if (temp == 0)
        temp = .0000001;
//      m = (LRLPeriod * sumxy - sumx * sumy) / (LRLPeriod * sumx2 - sumx * sumx); 
    m = (LRLPeriod * sumxy - sumx * sumy) / temp; 

    temp = LRLPeriod;
    if (temp == 0)
        temp = .0000001;
    yint = (sumy + m * sumx) / temp; // was LRLPeriod (obviously)

    temp = MathSqrt((LRLPeriod * sumx2 - sumx * sumx) * (LRLPeriod * sumy2 - sumy * sumy));
    if (temp == 0)
        temp = .0000001;
    r = (LRLPeriod * sumxy - sumx * sumy) / temp;


    LRLBuffer[shift] = yint - m * LRLPeriod;

    //Print (" "+shift+" "+LRLBuffer[shift]);
}

//----------Added Upper and Lower Bands--------------//

   int    nBARs   = 0;  
   double LRLBuffer_CPY[];                        
   ArraySetAsSeries(LRLBuffer_CPY,True);                   

   j  = Bars - Counted_bars - 1;         
   while( j >  0 )                              
   {      
    ArrayCopy( LRLBuffer_CPY, LRLBuffer, 0, j, WHOLE_ARRAY );

    double StDev  = iStdDevOnArray( LRLBuffer_CPY, nBARs, LRLPeriod, 0, MODE_SMA, 0 );  

   LRLBuffer_Upper[j] = LRLBuffer[j] + (Number_SD * StDev);                                    
   LRLBuffer_Lower[j] = LRLBuffer[j] - (Number_SD * StDev);                                     

   j--;
  }



return(0);
}
//+------------------------------------------------------------------+

The easiest way is to use standard OnCalculate(***) function and run the main cycle only if(rates_total>prev_calculated) .

Also, in the main cycle try to have for(int shift=limit-1;shift>0;shift--){ (be attentive), and by the way, are you sure that you need shift=limit-1 not just shift=limit ?

New - MQL4 syntax OnCalculate() will not help achieve the goal,
rather use this:

The OnCalculate() syntax is available, but the code-block actually remains outside-of-your control, when it gets launched and how many progressive steps will it be allowed to process.

The solution is to use the "old"-syntax and add "soft"-locking.

Using static variables helps keep interim values stored between calls and a just-HOT-end recalculations thus need not re-iterate from the whole depths, if shifting registers are used to just update from the just-frozen [1] Bar. The rest of the Bar [0] duration it does .NOP/JIT/RET and your code does not devastate fragility of a single-solo-thread performance, shared ( Yes! SHARED!! ) by ALL Custom Indicators ( a truly devilish anti-pattern in the recent New-MQL4 updates ... ):

int start(){
    static aCurrentTIME  = EMPTY;
    if (   aCurrentTIME == Time[0] ) return 0;   // .NOP/JIT/RET --^ 
           aCurrentTIME  = Time[0];              // .MOV/LOCK
 // -------------------------------------------- // .CALC:
    int limit = Bars - counted_bars;
    ...
    /* update just the "HOT"-end has just got into OHLCVT[1]-cells */
 // -------------------------------------------- // .FIN
}

depending on the case, you also could:

datetime calculatedBarTime;

void OnCalculate()
{
    if (  calculatedBarTime != Time[0] ) 
    {
          onBar();
    }
}

void onBar()
{
     calculatedBarTime  = Time[0];
  // on bar logic....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM