CopyBuffer Error

To add comments, please log in or register
Kemixe
18
Kemixe  

I'm trying to import an indicator value but I'm not getting it. The print on the indicator has the right value but when I pull the expert, only 0.0 appears

//+------------------------------------------------------------------+
//|                                                          MHI.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

//--- funçoes proprias
#import "funcoesTiagoEA.ex5"
int CorCandle (const double open, const double close);
#import
//-----------------------

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot wins
#property indicator_label1  "wins"
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      velas=120;
//--- indicator buffers
double         winsBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,winsBuffer,INDICATOR_DATA);
//---
   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[])
  {
//---
   if( prev_calculated == 0 )
     {
         ArraySetAsSeries(time,true);
         ArraySetAsSeries(open,true);  
         ArraySetAsSeries(close,true);
              
         for(int i=1;i<= velas;i++){
            MqlDateTime tempo;
            TimeToStruct(time[i],tempo);
            
            if( tempo.min % 5 == 0){
               
               int vt = CorCandle( open[i], close[i] );
               
               int v0 = CorCandle( open[i+1], close[i+1] );
               int v1 = CorCandle( open[i+2], close[i+2] );
               int v2 = CorCandle( open[i+3], close[i+3] );
               
               int valor = v0 + v1 + v2;
            
               if (valor > 0 && vt < 0) {
                  winsBuffer[0] = winsBuffer[0] + 1.0;
               }else{
                  winsBuffer[0] = winsBuffer[0] - 1.0;
               }
             
            }   
             
         }  
     } 
     printf("WINS: %f",winsBuffer[0]); 
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

2020.07.29 11:39:23.968 MHI (EURUSD,M1) WINS: -16.000000

//+------------------------------------------------------------------+
//|                                                     EX_tiago.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"



int cus_Handle;
double cus_Buffer[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   
    
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   cus_Handle = iCustom(_Symbol,_Period,"MHI",120);
   CopyBuffer(cus_Handle,0,0,1,cus_Buffer);
   printf(DoubleToString( cus_Buffer[0]));
  }


2020.07.29 11:40:36.847 EX_tiago (EURUSD,M1) 0.00000000

Vladimir Karputov
Moderator
207336
Vladimir Karputov  

Why don't you process incoming quotes online?

You only fill the buffer once:

  if( prev_calculated == 0 )
     {

Where is the processing condition

int limit=prev_calculated-1;
if(prev_calculated==0)
  limit=0;

for(int i=limit; i<rates_total; i++)
    {

?

Kemixe
18
Kemixe  
//+------------------------------------------------------------------+
//|                                                          MHI.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

//--- funçoes proprias
#import "funcoesTiagoEA.ex5"
int CorCandle (const double open, const double close);
#import
//-----------------------

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot wins
#property indicator_label1  "wins"
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      velas=120;
//--- indicator buffers
double         winsBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,winsBuffer,INDICATOR_DATA);
//---
   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[])
  {
//---
     winsBuffer[0] = 99.0; 

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

I just want to pass the value on to the expert and I'm not getting it. Then I changed the code to the simplest possible to try

Vladimir Karputov
Moderator
207336
Vladimir Karputov  

ERROR^

//--- funçoes proprias
#import "funcoesTiagoEA.ex5"
int CorCandle (const double open, const double close);
#import
Kemixe
18
Kemixe  
//+------------------------------------------------------------------+
//|                                               funcoesTiagoEA.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property library
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

int CorCandle( const double open, const double close ) export
{
   if( open < close ) //verde
     {
         return 1; 
     }
   else if( open > close ) //vermelho
    {
         return -1;
    }
   else
     {
         return 0; //empate
     }
   
}

funcoesTiagoEA.ex5
Vladimir Karputov
Moderator
207336
Vladimir Karputov  

Use right code.

Indicator: (located in the MQL5 \ Indicators \ Test folder)

//+------------------------------------------------------------------+
//|                                                          MHI.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.000"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot wins
#property indicator_label1  "wins"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      velas=120;
//--- indicator buffers
double   winsBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,winsBuffer,INDICATOR_DATA);
//---
   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=prev_calculated-1;
   if(prev_calculated==0)
      limit=0;
   for(int i=limit; i<rates_total; i++)
     {
      winsBuffer[i] = i;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+


Expert: (located in the MQL5 \ Experts \ Tests folder)

//+------------------------------------------------------------------+
//|                                                       MHI EA.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- input parameters
input int      Input1=9;
//---
int      handle_iCustom;                        // variable for storing the handle of the iCustom indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iCustom
   handle_iCustom=iCustom(Symbol(),Period(),"Test\\MHI");
//--- if the handle is not created
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   string text="";
   double mhi[];
   ArraySetAsSeries(mhi,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iCustom,0,start_pos,count,mhi))
     {
      return;
     }
   for(int i=count-1; i>=0; i--)
     {
      text=text+"MHI"+"["+(string)i+"]"+" "+DoubleToString(mhi[i],2)+" | "+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+


Result:


Files:
MHI.mq5 3 kb
MHI_EA.mq5 4 kb
To add comments, please log in or register