Indicador usando ZigZag

 

Boa tarde Pessoal,


estou criando um indicador usando o ZigZag para marcar pivos, chamo o ZigZig através do iCustom(), e copio os buffers dele , porém ao fazer um plot simples percebi que pontos que o zigzag havia marcado e depois recalculou ainda permanecem marcados alguém tem uma ideia de como resolver isso?

mandei uma imagem junto as bolinhas são do meu indicador o zigzag é do proprio zigzag coloquei para facilitar o entendimento.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrIndianRed
#property indicator_width1  1
#property indicator_label1  "Suporte"

#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrDarkGreen
#property indicator_width2  1
#property indicator_label2 "Resistência"


//buffers de indicador
double supBuffer[];
double resBuffer[];
//handle
int ZigHandle;

int OnInit()
  {

 ZigHandle = iCustom(NULL,_Period,"Examples\\ZigZag",12,5,3);
 
 if(ZigHandle==INVALID_HANDLE || ZigHandle <= 0)
     {
      Print(" Falha ao obter o identificador do indicador");
      return(INIT_FAILED);
     }
  //----------Suporte------------
  SetIndexBuffer(0,supBuffer,INDICATOR_DATA);
  PlotIndexSetString(0,PLOT_LABEL,"Suporte");
  PlotIndexSetInteger(0,PLOT_ARROW,159);
  
  
  //----------Resistencia------------
  SetIndexBuffer(1,resBuffer,INDICATOR_DATA);
  PlotIndexSetString(1,PLOT_LABEL,"Resistencia");
  PlotIndexSetInteger(1,PLOT_ARROW,159);
 
  
  IndicatorSetString(INDICATOR_SHORTNAME,"Suporte&Resistencia");
  IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   
//---
   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[])
  {

  double resarray[],suparray[];
  CopyBuffer(ZigHandle,1,0,rates_total,resarray);
  CopyBuffer(ZigHandle,2,0,rates_total,suparray);
 


for(int i=0;i<rates_total;i++)
  {
    
    resBuffer[i] = resarray[i];
    supBuffer[i] = suparray[i]; 

       
  }
       
    
   

 
   return(rates_total);
  }
//+------------------------------------------------------------------+
Arquivos anexados:
 
gustavo.alonso:

....

Eu faço assim geralmente. 

Acho que haja maneiras mais triviais de se fazer isso, mas atende perfeitamente a copia dos valores do zig zag.


#include <Trade\SymbolInfo.mqh>
CSymbolInfo    m_symbol;
int           handle;
datetime m_start_time;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   if(!m_symbol.Name(Symbol())) // sets symbol name
      return(INIT_FAILED);

   m_start_time=D'2020.01.01 00:00:00';
   handle=iCustom(Symbol(),Period(),"Examples\\ZigZag");


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double high_0  = 0.0;
   double high_1  = 0.0;
   double low_0   = 0.0;
   double low_1   = 0.0;

   if(!SearchExtremes(high_0,low_0,high_1,low_1, 0))
      return;


   Print("high_0: ", high_0, " low_0: ", low_0, " high_1: ", high_1, " low_1: ", low_1);

  }
//+------------------------------------------------------------------+
bool SearchExtremes(double &high_0,double &low_0,double &high_1,double &low_1, int index)
  {
   if(BarsCalculated(handle)==-1)
      return(false);

   double zigzag[];
   ArraySetAsSeries(zigzag,true);
   int copied=CopyBuffer(handle, index, TimeCurrent(), m_start_time,zigzag);
   if(copied==-1)
      return(false);
   int size=ArraySize(zigzag);
   double vertex_0=0.0,vertex_1=0.0,vertex_2=0.0,vertex_3=0.0;

   for(int i=0; i<size; i++)
     {
      if(zigzag[i]!=0)
        {
         if(vertex_0==0.0)
           {
            vertex_0=zigzag[i];
            continue;
           }
         if(vertex_1==0.0)
           {
            vertex_1=zigzag[i];
            continue;
           }
         if(vertex_2==0.0)
           {
            vertex_2=zigzag[i];
            continue;
           }
         if(vertex_3==0.0)
           {
            vertex_3=zigzag[i];
            //--- save memory: remember the number of copied items
            datetime temp=iTime(m_symbol.Name(),Period(),i);
            if(temp==D'1970.01.01 00:00:00')
               return(false);
            m_start_time=temp;
            break;
           }
        }
     }
//---
   if(vertex_0!=0.0 && vertex_1!=0.0 && vertex_2!=0.0 && vertex_3!=0.0)
     {
      high_0=0.0;
      low_0=0.0;
      high_1=0.0;
      low_1=0.0;
      if(vertex_0>vertex_1)
        {
         high_0   = vertex_0;
         low_0    = vertex_1;
         high_1   = vertex_2;
         low_1    = vertex_3;
         int d=0;
         return(true);
        }
      else
        {
         low_0    = vertex_0;
         high_0   = vertex_1;
         low_1    = vertex_2;
         high_1   = vertex_3;
         int d=0;
         return(true);
        }
     }
//---
   m_start_time=m_start_time-100;
   return(false);
  }
 
Jonathan Pereira:

Eu faço assim geralmente. 

Acho que haja maneiras mais triviais de se fazer isso, mas atende perfeitamente a copia dos valores do zig zag.


muito obrigado vou estudar esse codigo, vlwww