Jean
Jean

Reputation: 137

Clicking refresh sometimes leads to different custom indicator results

I have a weird occurence that I cannot figure out. I developed a custom indicator that calculates an index following which I will make trade decisions. The indicator uses three time periods to check whether the trend is stable or not. The first is a one minute, second a 5 minute and lastly a 30 minute time period. I calculate the appropriate bar from which to check the larger time periods so that they correspond with the one minute bar.

My testing shows that the numbers are calculated correctly.

This indicator is attached to a one minute chart. A lot of times, I will right click and choose refresh and the indicator will change over the last 5 - 10 minutes! I cannot find a reason why it would do that. I tried clicking refresh after every one minute bar and sometimes it would change and other times not. However, even if I click after every minute on a chart, sometimes the changes will be 5 - 10 minutes back.

Can you please help me?

I include my code for your kind help:

int start()                         // Special function start()
  {
   int i,                        // Bar index                           
       iLook = 0,
       iCurrMinute = 0,
       iMinFloor = 0,
       iMinsAdd = 0,
       Counted_bars,                // Number of counted bars
       iCalcVal,
       iTrendConsistency;                    

   double EMA_1min_10_current, EMA_1min_10_prev,    
          EMA_5min_10_current, EMA_5min_10_prev,
          EMA_30min_10_current, EMA_30min_10_prev,
          EMA_1min_20_current, EMA_1min_20_prev,    
          EMA_5min_20_current, EMA_5min_20_prev,
          EMA_30min_20_current, EMA_30min_20_prev,
          dblMinRemain = 0;

   datetime dtBarTime;
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      iCalcVal = 0;
      iTrendConsistency = 0;
      dtBarTime = iTime(NULL,PERIOD_M1,i);

      //  Calculate trend consistency
      if (boolIncl1Min)
      {
         iLook = i;

         EMA_1min_10_current = iMA(NULL,PERIOD_M1,intMAShort,0,MODE_EMA,PRICE_CLOSE,iLook);
         EMA_1min_10_prev = iMA(NULL,PERIOD_M1,intMAShort,0,MODE_EMA,PRICE_CLOSE,iLook+1);    
         EMA_1min_20_current = iMA(NULL,PERIOD_M1,intMAMedium,0,MODE_EMA,PRICE_CLOSE,iLook);
         EMA_1min_20_prev = iMA(NULL,PERIOD_M1,intMAMedium,0,MODE_EMA,PRICE_CLOSE,iLook+1);

         if (EMA_1min_10_current > EMA_1min_20_current) iTrendConsistency++; 
         if (EMA_1min_10_current < EMA_1min_20_current) iTrendConsistency--;

         if (EMA_1min_10_current > EMA_1min_10_prev) iTrendConsistency++; 
         if (EMA_1min_10_current < EMA_1min_10_prev) iTrendConsistency--;

         if (EMA_1min_20_current > EMA_1min_20_prev) iTrendConsistency++; 
         if (EMA_1min_20_current < EMA_1min_20_prev) iTrendConsistency--;

         if (iClose(NULL,PERIOD_M1,iLook) > EMA_1min_10_current) iTrendConsistency++;
         if (iClose(NULL,PERIOD_M1,iLook) < EMA_1min_10_current) iTrendConsistency--;
      }

      if (boolIncl5Min) 
      {

         iLook = i;
         if (boolIncl1Min) iLook = iBarShift(NULL,PERIOD_M5,dtBarTime,true);

         EMA_5min_10_current = iMA(NULL,PERIOD_M5,intMAShort,0,MODE_EMA,PRICE_CLOSE,iLook);
         EMA_5min_10_prev = iMA(NULL,PERIOD_M5,intMAShort,0,MODE_EMA,PRICE_CLOSE,iLook+1);

         EMA_5min_20_current = iMA(NULL,PERIOD_M5,intMAMedium,0,MODE_EMA,PRICE_CLOSE,iLook);
         EMA_5min_20_prev = iMA(NULL,PERIOD_M5,intMAMedium,0,MODE_EMA,PRICE_CLOSE,iLook+1);

         if (EMA_5min_10_current > EMA_5min_20_current) iTrendConsistency++; 
         if (EMA_5min_10_current < EMA_5min_20_current) iTrendConsistency--;

         if (EMA_5min_10_current > EMA_5min_10_prev) iTrendConsistency++; 
         if (EMA_5min_10_current < EMA_5min_10_prev) iTrendConsistency--;

         if (EMA_5min_20_current > EMA_5min_20_prev) iTrendConsistency++; 
         if (EMA_5min_20_current < EMA_5min_20_prev) iTrendConsistency--;

         if (iClose(NULL,PERIOD_M5,iLook) > EMA_5min_10_current) iTrendConsistency++;
         if (iClose(NULL,PERIOD_M5,iLook) < EMA_5min_10_current) iTrendConsistency--;
      }

      if(boolIncl30Min)
      {  

         iLook = i;

         if (boolIncl1Min) iLook = iBarShift(NULL,PERIOD_M30,dtBarTime,true);

         EMA_30min_10_current = iMA(NULL,PERIOD_M30,intMAShort,0,MODE_EMA,PRICE_CLOSE,iLook);
         EMA_30min_10_prev = iMA(NULL,PERIOD_M30,intMAShort,0,MODE_EMA,PRICE_CLOSE,iLook+1);
         EMA_30min_20_current = iMA(NULL,PERIOD_M30,intMAMedium,0,MODE_EMA,PRICE_CLOSE,iLook);
         EMA_30min_20_prev = iMA(NULL,PERIOD_M30,intMAMedium,0,MODE_EMA,PRICE_CLOSE,iLook+1);

         if (EMA_30min_10_current > EMA_30min_20_current) iTrendConsistency++;
         if (EMA_30min_10_current < EMA_30min_20_current) iTrendConsistency--;

         if (EMA_30min_10_current > EMA_30min_10_prev) iTrendConsistency++; 
         if (EMA_30min_10_current < EMA_30min_10_prev) iTrendConsistency--;

         if (EMA_30min_20_current > EMA_30min_20_prev) iTrendConsistency++;
         if (EMA_30min_20_current < EMA_30min_20_prev) iTrendConsistency--;


         if (iClose(NULL,PERIOD_M30,iLook) > EMA_30min_10_current) iTrendConsistency++;
         if (iClose(NULL,PERIOD_M30,iLook) < EMA_30min_10_current) iTrendConsistency--;
      }



      Trend_Consistency[i]= iTrendConsistency;

      i--;                          // Calculating index of the next bar
     }

ps: boolIncl1Min is always true..

Upvotes: 0

Views: 483

Answers (1)

Jean
Jean

Reputation: 137

I have eventually figured out the answer which has eluded me for days! The condition for either or the 5 minutes or 30 minutes charts change during the most recent bar. The one minute bars record the 5 and 30 min conditions on a minute by minute basis. If I click refresh, the past bars are now firm and therefore can be finally calculated.

Code will be required to update past bars if these conditions change should one wish to have it.

Thanks for considering in helping me.

Upvotes: 0

Related Questions