horizontal lines based on zigzag keep disappearing

 

@Vladimir Karputov Alain Verleyen  William Roeder

I have zigzag based indicator. The code makes sense to me but the objects(HLINE) drawn on screen keep disappearing, please help

//+------------------------------------------------------------------+
//|                                                        HR2.2.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots   1
#property indicator_buffers 2
//+------------------------------------------------------------------+
//| ZigZag Inputs                                                    |
//+------------------------------------------------------------------+
input int inpDepth = 8; // Depth
input int inpDeviation = 5; // Deviation
input int inpBackstep = 4; //Backstep
sinput group ""

//+------------------------------------------------------------------+
//| Peak Analysis input                                              |
//+------------------------------------------------------------------+
input int InpGapPoints= 200000; //Minimum Gap peaks in points
input int InpSensitivity = 2; //Peak Sensitivity
input int InpLookBack = 100; //LookBack

//+------------------------------------------------------------------+
//| Drawing Inputs                                                   |
//+------------------------------------------------------------------+
string InpPrefix = "SRLEvel_"; // OBject Name Prefix
color InpLineColour= clrLightSteelBlue; //Line Colour
int InpLineWeight= 6;// Line Weight

//For Levels
double SRLevel[];

//ZigZag GET fron the indicator
double Buffer[];
int Handle;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Zigzag
   Handle = iCustom(_Symbol,PERIOD_H4,"Examples\\ZigZag",inpDepth,inpDeviation,inpBackstep);

   if(Handle == INVALID_HANDLE)
     {
      Print("Could not create a handle to the zizag indicator");
      return(INIT_FAILED);
     }
   ArraySetAsSeries(Buffer, true);


//Clean up Any SRLEVELS on the chart if any exist
   ObjectsDeleteAll(0, InpPrefix, 0, OBJ_HLINE);

   ChartRedraw(0);
   ArrayResize(SRLevel,InpLookBack);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(Handle);
//Clean up Any SRLEVELS on the chart if any exist

   ObjectsDeleteAll(0, InpPrefix, 0, OBJ_HLINE);
   ChartRedraw(0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---One Time convert points to a price gap
   static double levelGap = InpGapPoints* SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   DrawLevels();

   if(rates_total == prev_calculated)
      return(rates_total);

//Get the most recent <lookback> peaks
   double zz = 0;
   double zzPeaks[];
   int zzCount = 0;

   ArrayResize(zzPeaks, InpLookBack);
   ArrayInitialize(zzPeaks,0.0);

   int count = CopyBuffer(Handle, 0, 0,rates_total, Buffer);

   if(count < 0)
     {
      int err = GetLastError();
      return(0);
     }

   for(int i = 1; i < rates_total && zzCount < InpLookBack; i++)

     {
      zz = Buffer[i];

      if(zz != 0&& zz != EMPTY_VALUE)
        {
         zzPeaks[zzCount] = zz;
         zzCount++;
        }
     }

   ArraySort(zzPeaks);

//Search for groupings and set levels
   int srCounter= 0;
   double price = 0;
   int priceCount = 0;
   ArrayInitialize(SRLevel, 0.0);

   for(int i = InpLookBack-1; i>=0; i--)
     {
      price+=zzPeaks[i];
      priceCount++;

      if(i==0 || (zzPeaks[i] - zzPeaks[i-1]) >levelGap)
        {
         if(priceCount >= InpSensitivity)
           {
            price =price/priceCount;
            SRLevel[srCounter] = price;
            srCounter++;



           }
         price = 0;
         priceCount = 0;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
void DrawLevels()
  {

   for(int i = 0; i<InpLookBack; i++)
     {
      string name = InpPrefix + IntegerToString(i);
      if(SRLevel[i] == 0)
        {
         ObjectDelete(0, name);
         continue;
        }
      if(ObjectFind(0, name)<0)
        {
         ObjectCreate(0,name,OBJ_HLINE, 0, 0, SRLevel[i]);
         ObjectSetInteger(0, name, OBJPROP_COLOR,InpLineColour);
         ObjectSetInteger(0, name, OBJPROP_WIDTH,InpLineWeight);
         ObjectSetInteger(0,name,OBJPROP_BACK,true);

        }
      else
        {
         ObjectSetDouble(0,name,OBJPROP_PRICE,SRLevel[i]);
        }
     }
   ChartRedraw(0);


  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
 

You calling DrawLevels, before populating SRlevel.

 
William Roeder #:

You calling DrawLevels, before populating SRlevel.

Thanks

void DrawLevels(double price,int count)
  {

   string name = InpPrefix + IntegerToString(count);
   if(price == 0)
     {
      ObjectDelete(0, name);
     }

   if(ObjectFind(0, name)<0)
     {
      ObjectCreate(0,name,OBJ_HLINE, 0, 0, price);
      ObjectSetInteger(0, name, OBJPROP_COLOR,InpLineColour);
      ObjectSetInteger(0, name, OBJPROP_WIDTH,InpLineWeight);
      ObjectSetInteger(0,name,OBJPROP_BACK,true);
     }
   else
     {
      ObjectSetDouble(0,name,OBJPROP_PRICE,price);
     }
   ChartRedraw(0);

  }
 
William Roeder #:

You calling DrawLevels, before populating SRlevel.

They are not showing anywhere in an expert advisor

 
Sphelele Sphesihle Lubanyana #:

They are not showing anywhere in an expert advisor

if you want to see objects created by an Indicator within an EA, you will have to manually attach the indicator to the chart using  ChartIndicatorAdd()  in the OnInit() of the EA. Then you need to do IndicatorRelease(handle) in OnDeInit of the expert advisor.