[Solved] Logical error for plotting a line

 

I have a problem with the logic to plot a line in a certain condition.

A value was defined through the histogram, where a line would be plotted only greater than or equal to this value.

The problem that the histogram reaches this value and returns below the value, the line is moved, but does not return to the previous position, and position [i] did not end within the condition.

My difficulty is in keeping the previous position and making that line return if the histogram does not end at the specified value.

If I remove the indicator and add it again, it calculates and plots in the correct place.

I'll post the code and a printscreen with the errors.

Note: ChartRedraw () does not work to reposition the line in the previous position.

Summary: Move the line if it ends the current position within the condition.

#property copyright "Neto"
#property link      ""
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//| PROPRIEDADES DO INDICADOR (DIRETIVAS)                            |
//+------------------------------------------------------------------+
#property indicator_buffers 3
#property indicator_plots 1 
//---
#property indicator_label1 "HistogramBase" 
#property indicator_type1 DRAW_COLOR_HISTOGRAM 
#property indicator_style1 STYLE_SOLID 
#property indicator_color1 clrDodgerBlue,clrRed,clrWhite //histogram colors
#property indicator_width1 2 

//###############
double wBuffer[]; 
double wColor[]; 
double cl[];

int widthLineUp = 1;
int widthLineDown = 1;

string name;

int OnInit()
  {
//---
   SetIndexBuffer(0,wBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,wColor,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,cl,INDICATOR_DATA);
   
   IndicatorSetString( INDICATOR_SHORTNAME, "Wave Lines" );

   ChartSetInteger(0, CHART_SHOW_OBJECT_DESCR, true);
   
   name = "HighLineM1";
   ObjectCreate(0,name,OBJ_HLINE,0,0,0);  
   ObjectSetInteger(0,name,OBJPROP_COLOR,clrWhite);
   ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_DASH);
   ObjectSetInteger(0,name,OBJPROP_WIDTH,widthLineUp);
   ObjectSetString(0,name,OBJPROP_TEXT,"  Line UP");
        
   //Low
   name = "LowLineM1";
   ObjectCreate(0,name,OBJ_HLINE,0,0,0);           
   ObjectSetInteger(0,name,OBJPROP_COLOR,clrWhite);
   ObjectSetInteger(0,name,OBJPROP_STYLE,STYLE_DASH);
   ObjectSetString(0,name,OBJPROP_TEXT,"  Line DN");
   ObjectSetInteger(0,name,OBJPROP_WIDTH,widthLineDown);
   
   return(INIT_SUCCEEDED);
  }
  
  void OnDeinit(const int reason){
      ObjectDelete(0, "LowLineM1");
      ObjectDelete(0, "HighLineM1");
      ChartRedraw();
  }

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 i;
if(prev_calculated == 0) i = 2;
else i = prev_calculated-1;

while(i < rates_total){

   cl[i] = close[i];
   double vol = (double)volume[i];

   // ***** UP 
   if (cl[i] >= cl[i-1]) { 
      if (cl[i-1] >= cl[i-2]) {
         wBuffer[i] = wBuffer[i-1] + vol;
         wColor[i] = 0; //Color DodgerBlue
      } 
      else{
         wBuffer[i] = vol;
         wColor[i] = 0;
      }
   } 
   // ***** DOWN   
   else{                          
      if (cl[i-1] < cl[i-2]) {
         wBuffer[i] = wBuffer[i-1] - vol;
         wColor[i] = 1; //Color Red
      }
      else{
         wBuffer[i] = -vol;
         wColor[i] = 1;
      }
   }
        // #########  condition for plotting the line
        if(Period() == PERIOD_M1){
            if(wBuffer[i] >= 91500){
              wColor[i] = 2; //Color White
              ObjectMove(0,"HighLineM1",0,TimeCurrent(),high[i]);
              }
            if(wBuffer[i] <= -91500){
               wColor[i] = 2; 
               ObjectMove(0,"LowLineM1",0,TimeCurrent(),low[i]);
               //ChartRedraw();
            }
        }
        // #####################################
     
   i++;
  }
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
 

Solved

The code:


#property copyright "Neto"
#property link      ""
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#property indicator_buffers 4
#property indicator_plots 1 
//---
#property indicator_label1 "HistogramLines" 
#property indicator_type1 DRAW_COLOR_HISTOGRAM 
#property indicator_style1 STYLE_SOLID 
#property indicator_color1 clrDodgerBlue,clrRed,clrWhite //histogram colors
#property indicator_width1 2 

//###############
double waveBuffer[]; 
double waveColor[]; 
double cl[];
double bufferAux[];

int widthLineUp = 1;
int widthLineDown = 1;


int OnInit()
  {
//---
   SetIndexBuffer(0,waveBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,waveColor,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,cl,INDICATOR_DATA);
   SetIndexBuffer(3,bufferAux,INDICATOR_DATA);

   IndicatorSetString( INDICATOR_SHORTNAME, "Wave Lines" );

   ChartSetInteger(0, CHART_SHOW_OBJECT_DESCR, true);
   
   CreateHline(0,"HighLineM1",clrWhite,STYLE_DASH,widthLineUp,"  Line UP");
   CreateHline(0,"LowLineM1",clrWhite,STYLE_DASH,widthLineDown,"  Line DN");
   CreateHline(0,"HighLineM2",clrWhite,STYLE_DASH,widthLineUp,"  Line UP");
   CreateHline(0,"LowLineM2",clrWhite,STYLE_DASH,widthLineDown,"  Line DN");
   CreateHline(0,"HighLineM5",clrWhite,STYLE_DASH,widthLineUp,"  Line UP");
   CreateHline(0,"LowLineM5",clrWhite,STYLE_DASH,widthLineDown,"  Line DN");
   
   return(INIT_SUCCEEDED);
  }
  
  void OnDeinit(const int reason){
      ObjectDelete(0, "LowLineM1");
      ObjectDelete(0, "HighLineM1");
      ObjectDelete(0, "LowLineM2");
      ObjectDelete(0, "HighLineM2");
      ObjectDelete(0, "LowLineM5");
      ObjectDelete(0, "HighLineM5");
      ChartRedraw();
  }

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 i;
if(prev_calculated == 0) i = 2;
else i = prev_calculated-1;

int pos;

while(i < rates_total){

   double value = 0;
   cl[i] = close[i];
   double vol = (double)volume[i];
   
   // ***** UP 
   if (cl[i] >= cl[i-1]) { 
      if (cl[i-1] >= cl[i-2]) {
         waveBuffer[i] = waveBuffer[i-1] + vol;
         waveColor[i] = 0; //Color DodgerBlue
      } 
      else{
         waveBuffer[i] = vol;
         waveColor[i] = 0;
      }
   } 
   // ***** DOWN   
   else{                          
      if (cl[i-1] < cl[i-2]) {
         waveBuffer[i] = waveBuffer[i-1] - vol;
         waveColor[i] = 1; //Color Red
      }
      else{
         waveBuffer[i] = -vol;
         waveColor[i] = 1;
      }
   }
         
    pos = i;
    i++;

    if(Period() == PERIOD_M1){
      value = 91500;  
      bufferAux[pos] = buff(pos,waveBuffer,value); 
         if(bufferAux[pos-1] >= value) {waveColor[pos-1] = 2; ObjectMove(0,"HighLineM1",0,TimeCurrent(),high[pos-1]);} //If condition, color white, move line
         else if(bufferAux[pos-1] <= -value) {waveColor[pos-1] = 2; ObjectMove(0,"LowLineM1",0,TimeCurrent(),low[pos-1]);}
    }
    if(Period() == PERIOD_M2){
       value = 250000;
       bufferAux[pos] = buff(pos,waveBuffer,value);
         if(bufferAux[pos-1] >= value) {waveColor[pos-1] = 2; ObjectMove(0,"HighLineM2",0,TimeCurrent(),high[pos-1]);}
         else if(bufferAux[pos-1] <= -value) {waveColor[pos-1] = 2; ObjectMove(0,"LowLineM2",0,TimeCurrent(),low[pos-1]);}
    }
    if(Period() == PERIOD_M5){
       value = 720000;
       bufferAux[pos] = buff(pos,waveBuffer,value);
         if(bufferAux[pos-1] >= value) {waveColor[pos-1] = 2; ObjectMove(0,"HighLineM5",0,TimeCurrent(),high[pos-1]);}
         else if(bufferAux[pos-1] <= -value) {waveColor[pos-1] = 2; ObjectMove(0,"LowLineM5",0,TimeCurrent(),low[pos-1]);}
    }
 }

   ChartRedraw();
   return(rates_total);
  }
//+------------------------------------------------------------------+
// Function to define line plotting through TimeFrame
double buff(int pos, double &buf[], double value){
   double res = 0;
   int h = 0;
   int l = 0;
      for(int j = 0; j < 1; j++){
         if(buf[pos] >= value){h = 1;}
         else{h = 0;}
         
         if(buf[pos] <= -value){l = 2;}
         else{l = 0;}
      }
      if((h == 1) || l == 2) res = buf[pos];
      else{res = 0;}
 
   return res;
}

void CreateHline(long   chart_id,      // chart ID
                 string name,          // object name
                 color  Color,         // line color
                 int    style,         // line style
                 int    width,         // line width
                 string text           // text
                 )
  {
//----
   ObjectCreate(chart_id,name,OBJ_HLINE,0,0,0);
   ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
   ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
   ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
   ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
//----
  }
Reason: