CPriceSeries::GetData: invalid buffer

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

//|                                              CSignalFxMedias.mqh | 
//|                                                 Fernando Ribeiro |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Fernando Ribeiro"
#property version   "1.00"


//+------------------------------------------------------------------+
//| include files                                                    |
//+------------------------------------------------------------------+

#include <Expert\ExpertSignal.mqh>


// wizard description start
//+------------------------------------------------------------------+
//| Description of the class                                         |
//| Title=Signal FxMedias                                            |
//| Type=SignalAdvanced                                              |
//| Name=CSignalFxMedias                                             |
//| Class=CSignalFxMedias                                            |
//| Page=                                                            |
//| Parameter=Expiration,int,60                                      |
//+------------------------------------------------------------------+
// wizard description end
//+------------------------------------------------------------------+
//| CSignalFxMedias class.                                           |
//| Purpose: Signal with Stochastic Oscillator and EMA               |
//+------------------------------------------------------------------+
class CSignalFxMedias : public CExpertSignal
  {
protected:
   CiClose              m_iclose;
   CiMA                 m_ima;
   CiStochastic         m_iStochastic;
   int                  m_stochastic_up_level;
   int                  m_stochastic_low_level;

   //--- Object initialization method
   bool               InitMovingAverage(CIndicators* indicators);
   bool               InitClose(CIndicators* indicators);
   bool               InitStochastic(CIndicators* indicators);
   int                CountOrders();
   int                CountPositions();

   double             MA(int index)                   { return(m_ima.Main(index));                 }
   double             Stochastic(int index)           { return(m_iStochastic.Main(index));         }
   double             Close(int index)                { return(m_iclose.GetData(index));           }
  


public:
                     CSignalFxMedias();
   void               Expiration(int value)           { m_expiration=value;                        }

   virtual bool       ValidationSettings();
   virtual bool       InitIndicators(CIndicators* indicators);
   virtual int        LongCondition();
   virtual int        ShortCondition();
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CSignalFxMedias::CSignalFxMedias()
  {
   m_stochastic_up_level = 75;
   m_stochastic_low_level = 25;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CSignalFxMedias::ValidationSettings()
  {
//--- Successful completion
   return(true);
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CSignalFxMedias::InitIndicators(CIndicators* indicators)
  {
//--- check pointer
   if(indicators==NULL)
      return(false);
      
//--- Initialization of the Close
   if(!InitClose(indicators))
      return(false);

//--- Initialization of the Moving Average
   if(!InitMovingAverage(indicators))
      return(false);

//--- Initialization of the Stochastic
   if(!InitStochastic(indicators))
      return(false);

//--- Successful completion
   return(true);
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CSignalFxMedias::InitClose(CIndicators *indicators)
  {

//--- add object to collection
   if(!indicators.Add(GetPointer(m_iclose)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }  

//--- initialize object
   if(!m_iclose.Create(m_symbol.Name(), m_period))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }
     
//--- ok
   return(true);
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CSignalFxMedias::InitMovingAverage(CIndicators *indicators)
  {
   int                  ma_period=25;                 // periodo MA
   int                  ma_shift=0;                   // shift
   ENUM_MA_METHOD       ma_method=MODE_EMA;           // método
   ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE;    // tipo de preço

//--- add object to collection
   if(!indicators.Add(GetPointer(m_ima)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_ima.Create(m_symbol.Name(), m_period, ma_period, 0, ma_method, applied_price))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }

//--- ok
   return(true);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CSignalFxMedias::InitStochastic(CIndicators *indicators)
  {

   int                  kperiod=10;                   // periodo k
   int                  dperiod=3;                    // periodo d
   int                  slowing=3;                    // slowing
   ENUM_MA_METHOD       ma_method=MODE_SMA;           // método
   ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE;    // tipo de preço
   ENUM_STO_PRICE       price_field=STO_LOWHIGH;      // Minimo/Maximo


//--- add object to collection
   if(!indicators.Add(GetPointer(m_iStochastic)))
     {
      printf(__FUNCTION__+": error adding object");
      return(false);
     }
//--- initialize object
   if(!m_iStochastic.Create(m_symbol.Name(), m_period, kperiod, dperiod, slowing, ma_method, price_field))
     {
      printf(__FUNCTION__+": error initializing object");
      return(false);
     }

//--- ok
   return(true);
  }


//+------------------------------------------------------------------+
//| Verifica sinal de compra                                                                   |
//+------------------------------------------------------------------+
int  CSignalFxMedias::LongCondition()
  {

   double m_iclose_prev = Close(1);
   double m_ma_now = MA(0);
   double m_stochastic = Stochastic(0);


   if(m_iclose_prev > m_ma_now)
      if(m_stochastic < m_stochastic_up_level)
         return(10);

   return(0);
  }

//+------------------------------------------------------------------+
//| Verifica sinal de venda                                                                   |
//+------------------------------------------------------------------+
int  CSignalFxMedias::ShortCondition()
  {

   double m_iclose_prev = Close(1);
   double m_ma_now = MA(0);
   double m_stochastic = Stochastic(0);

   if(m_iclose_prev < m_ma_now)
      if(m_stochastic > m_stochastic_low_level)
          return(10);

   return(0);
  }

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

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CSignalFxMedias::CountPositions()
  {
   int count=0;
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      ulong ticket=PositionGetTicket(i);
      if(ticket>0)
        {
         if(PositionGetInteger(POSITION_MAGIC)==m_magic)
           {
            count += 1;
           }
        }
     }
   return(count);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CSignalFxMedias::CountOrders()
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      ulong ticket=OrderGetTicket(i);
      if(ticket>0)
        {
         if(OrderGetInteger(ORDER_MAGIC)==m_magic)
           {
            count += 1;
           }
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+


Hi all,


I'm getting the error CPriceSeries::GetData: invalid buffer while calling Close(1).


How can I fix this?

Version: 5.00 (2748)


Best Regards,





 
ipsec:


Hi all,


I'm getting the error CPriceSeries::GetData: invalid buffer while calling Close(1).


How can I fix this?

Version: 5.00 (2748)


Best Regards,

Você sabe programar? ou apenas quer a solução fácil?

 
Flavio Jarabeck:

Você sabe programar? ou apenas quer a solução fácil?


Olá Flávio,

Sei programar sim.
Quando estou deputando o método GetData mostra os elementos no array (1024 por padrão), mas ele simplesmente não consegue obter o valor.

Já passou por isso?
 
ipsec:

Olá Flávio,

Sei programar sim.
Quando estou deputando o método GetData mostra os elementos no array (1024 por padrão), mas ele simplesmente não consegue obter o valor.

Já passou por isso?
Outro ponto interessante.

Eu chamo o Close(1) em dois locais (dentro do mesmo sinal) um no LongCondition e o outro no ShortCondition.

O que falha mais é o dentro do ShortCondition.