Download MetaTrader 5

Help Please - indicator does not refresh on offline chart

To add comments, please log in or register
wieb
113
wieb  

Hello,

This indicator doesn't refresh on offline chart (range bars).

I really don't have a clue how to fix it. 


//+------------------------------------------------------------------+
//|                                     LastManStandingIndicator.mq4 |
//|                                        Copyright 2016, Jay Davis |
//|                                         https://www.tidyneat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Jay Davis"
#property link      "https://www.tidyneat.com"
#property version   "1.1"
#property strict
#property indicator_chart_window
#property indicator_buffers 5


extern color MajorSwingColor=clrPurple;
extern int MajorSwingSize=3;
extern int PeriodsInMajorSwing=13;

extern color MinorSwingColor=clrCornflowerBlue;
extern int MinorSwingSize=1;
extern int PeriodsInMinorSwing=5;

extern ENUM_MA_METHOD MovingAveragMethod=MODE_EMA;
extern int MovingAveragePeriods= 55;
extern color MovingAvergeColor = clrDarkGoldenrod;

extern color fiftyPercentLineColor=clrAliceBlue;

int lookBack=PeriodsInMajorSwing*2;

double majorSwingHigh[];
double minorSwingHigh[];
double majorSwingLow[];
double minorSwingLow[];
double EMA[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,majorSwingHigh);  //associates array with buffer
   SetIndexStyle(0,DRAW_ARROW,EMPTY,MajorSwingSize,MajorSwingColor);
   SetIndexArrow(0,108); // drawing wingding 108
   SetIndexLabel(0,"Major Swing High");

   SetIndexBuffer(1,minorSwingHigh);  //associates array with buffer
   SetIndexStyle(1,DRAW_ARROW,EMPTY,MinorSwingSize,MinorSwingColor);
   SetIndexArrow(1,108); // drawing wingding 108
   SetIndexLabel(1,"Minor Swing High");

   SetIndexBuffer(2,majorSwingLow);  //associates array with buffer
   SetIndexStyle(2,DRAW_ARROW,EMPTY,MajorSwingSize,MajorSwingColor);
   SetIndexArrow(2,108); // drawing wingding 108
   SetIndexLabel(2,"Major Swing Low");

   SetIndexBuffer(3,minorSwingLow);  //associates array with buffer
   SetIndexStyle(3,DRAW_ARROW,EMPTY,MinorSwingSize,MinorSwingColor);
   SetIndexArrow(3,108); // drawing wingding 108
   SetIndexLabel(3,"Minor Swing Low");

   SetIndexBuffer(4,EMA);  //associates array with buffer
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,EMPTY,MovingAvergeColor);
   SetIndexLabel(4,"Moving Average");


//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
//if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
// First Run Through Rule
   if(counted_bars==0)
     {
      if(lookBack>=MovingAveragePeriods)
        {
         limit-=lookBack;
        }
      else
        {
         limit-=MovingAveragePeriods;
        }
     }


//---
   for(int i=1; i<limit; i++)
     {
      // Draw Moving Average
      EMA[i]=iMA(NULL,0,MovingAveragePeriods,0,MovingAveragMethod,PRICE_CLOSE,i);

      // Minor Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMinorSwing*2,i)==i+PeriodsInMinorSwing)
        {
         minorSwingHigh[i+PeriodsInMinorSwing]=High[i+PeriodsInMinorSwing];
        }

      // Major Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
        {
         majorSwingHigh[i+PeriodsInMajorSwing]=High[i+PeriodsInMajorSwing];
        }

      // Minor Swing Low Logic
      if(iLowest(NULL,0,MODE_LOW,PeriodsInMinorSwing*2,i)==i+PeriodsInMinorSwing)
        {
         minorSwingLow[i+PeriodsInMinorSwing]=Low[i+PeriodsInMinorSwing];
        }

      // Major Swing Low Logic
      if(iLowest(NULL,0,MODE_LOW,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
        {
         majorSwingLow[i+PeriodsInMajorSwing]=Low[i+PeriodsInMajorSwing];
        }

     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

 

 

Please help, thank you. 

whroeder1
16572
whroeder1  
wieb: This indicator doesn't refresh on offline chart (range bars). I really don't have a clue how to fix it.
  1. There is nothing to fix in the moving average or your indicator.
  2. If your offline generator isn't running, or isn't sending update messages, nothing will change.
wieb
113
wieb  
whroeder1:
  1. There is nothing to fix in the moving average or your indicator.
  2. If your offline generator isn't running, or isn't sending update messages, nothing will change.

 Hello  @whroeder1

Thanks a lot for the replied. 

Nothing wrong with offline generator, it is running but the indicator does not refresh/redraw.

Please see short video bellow to have better understanding about this issue. 

Please help, thank you.

 

 

honest_knave
Moderator
2293
honest_knave  
//+------------------------------------------------------------------+
//|                                     LastManStandingIndicator.mq4 |
//|                                        Copyright 2016, Jay Davis |
//|                                         https://www.tidyneat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Jay Davis"
#property link      "https://www.tidyneat.com"
#property version   "1.1"
#property strict
#property indicator_chart_window
#property indicator_buffers 5


extern color MajorSwingColor=clrPurple;
extern int MajorSwingSize=3;
extern int PeriodsInMajorSwing=13;

extern color MinorSwingColor=clrCornflowerBlue;
extern int MinorSwingSize=1;
extern int PeriodsInMinorSwing=5;

extern ENUM_MA_METHOD MovingAveragMethod=MODE_EMA;
extern int MovingAveragePeriods= 55;
extern color MovingAvergeColor = clrDarkGoldenrod;

extern color fiftyPercentLineColor=clrAliceBlue;

int lookBack=PeriodsInMajorSwing*2;

double majorSwingHigh[];
double minorSwingHigh[];
double majorSwingLow[];
double minorSwingLow[];
double EMA[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,majorSwingHigh);  //associates array with buffer
   SetIndexStyle(0,DRAW_ARROW,EMPTY,MajorSwingSize,MajorSwingColor);
   SetIndexArrow(0,108); // drawing wingding 108
   SetIndexLabel(0,"Major Swing High");

   SetIndexBuffer(1,minorSwingHigh);  //associates array with buffer
   SetIndexStyle(1,DRAW_ARROW,EMPTY,MinorSwingSize,MinorSwingColor);
   SetIndexArrow(1,108); // drawing wingding 108
   SetIndexLabel(1,"Minor Swing High");

   SetIndexBuffer(2,majorSwingLow);  //associates array with buffer
   SetIndexStyle(2,DRAW_ARROW,EMPTY,MajorSwingSize,MajorSwingColor);
   SetIndexArrow(2,108); // drawing wingding 108
   SetIndexLabel(2,"Major Swing Low");

   SetIndexBuffer(3,minorSwingLow);  //associates array with buffer
   SetIndexStyle(3,DRAW_ARROW,EMPTY,MinorSwingSize,MinorSwingColor);
   SetIndexArrow(3,108); // drawing wingding 108
   SetIndexLabel(3,"Minor Swing Low");

   SetIndexBuffer(4,EMA);  //associates array with buffer
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,EMPTY,MovingAvergeColor);
   SetIndexLabel(4,"Moving Average");


//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
//if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
// First Run Through Rule
   if(counted_bars==0)
     {
      if(lookBack>=MovingAveragePeriods)
        {
         limit-=lookBack;
        }
      else
        {
         limit-=MovingAveragePeriods;
        }
     }


//---
   for(int i=1; i<limit; i++)
     {
      // Draw Moving Average
      EMA[i]=iMA(NULL,0,MovingAveragePeriods,0,MovingAveragMethod,PRICE_CLOSE,i);

      // Minor Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMinorSwing*2,i)==i+PeriodsInMinorSwing)
        {
         minorSwingHigh[i+PeriodsInMinorSwing]=High[i+PeriodsInMinorSwing];
        }
      else minorSwingHigh[i+PeriodsInMinorSwing]=EMPTY_VALUE;

      // Major Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
        {
         majorSwingHigh[i+PeriodsInMajorSwing]=High[i+PeriodsInMajorSwing];
        }
      else majorSwingHigh[i+PeriodsInMajorSwing]=EMPTY_VALUE;

      // Minor Swing Low Logic
      if(iLowest(NULL,0,MODE_LOW,PeriodsInMinorSwing*2,i)==i+PeriodsInMinorSwing)
        {
         minorSwingLow[i+PeriodsInMinorSwing]=Low[i+PeriodsInMinorSwing];
        }
      else minorSwingLow[i+PeriodsInMinorSwing]=EMPTY_VALUE;

      // Major Swing Low Logic
      if(iLowest(NULL,0,MODE_LOW,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
        {
         majorSwingLow[i+PeriodsInMajorSwing]=Low[i+PeriodsInMajorSwing];
        }
      else majorSwingLow[i+PeriodsInMajorSwing]=EMPTY_VALUE;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Uncompiled / untested.

You basically need to "clear our the old values" as well as setting the new ones. A few ways to achieve this, but the above method is probably the most descriptive.

HTH 

whroeder1
16572
whroeder1  
wieb: Nothing wrong with offline generator, it is running but the indicator does not refresh/redraw.
  1. I see the EMA drew to bar zero. So it's working as written.
  2. It counts up, so it is repainting and therefor useless. Always count down.
wieb
113
wieb  
honest_knave:

//---
   for(int i=1; i<limit; i++)
     {
      // Draw Moving Average
      EMA[i]=iMA(NULL,0,MovingAveragePeriods,0,MovingAveragMethod,PRICE_CLOSE,i);

      // Minor Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMinorSwing*2,i)==i+PeriodsInMinorSwing)
        {
         minorSwingHigh[i+PeriodsInMinorSwing]=High[i+PeriodsInMinorSwing];
        }
      else minorSwingHigh[i+PeriodsInMinorSwing]=EMPTY_VALUE;

      // Major Swing High Logic
      if(iHighest(NULL,0,MODE_HIGH,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
        {
         majorSwingHigh[i+PeriodsInMajorSwing]=High[i+PeriodsInMajorSwing];
        }
      else majorSwingHigh[i+PeriodsInMajorSwing]=EMPTY_VALUE;

      // Minor Swing Low Logic
      if(iLowest(NULL,0,MODE_LOW,PeriodsInMinorSwing*2,i)==i+PeriodsInMinorSwing)
        {
         minorSwingLow[i+PeriodsInMinorSwing]=Low[i+PeriodsInMinorSwing];
        }
      else minorSwingLow[i+PeriodsInMinorSwing]=EMPTY_VALUE;

      // Major Swing Low Logic
      if(iLowest(NULL,0,MODE_LOW,PeriodsInMajorSwing*2,i)==i+PeriodsInMajorSwing)
        {
         majorSwingLow[i+PeriodsInMajorSwing]=Low[i+PeriodsInMajorSwing];
        }
      else majorSwingLow[i+PeriodsInMajorSwing]=EMPTY_VALUE;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Uncompiled / untested.

You basically need to "clear our the old values" as well as setting the new ones. A few ways to achieve this, but the above method is probably the most descriptive.

HTH 

 

Thanks a lot for the help @honest_knave

Solved the issue, now the indicator works perfectly.

To add comments, please log in or register