Array values ​​taken from other EAs

 

Good evening I have a problem going douring the demo with the same EA assigned to different charts (in particular stocks) on MT5. In particular both arrays I use in the EA (_mfi[] to collect the value of MFI and mated[] to collect Symbol's prices) are filled with values ​​of other symbols (so MFI and prices of an other random Symbol with the same EA). I tried to call the EAs with different names and put different Magicnumber but nothing changes.

//+------------------------------------------------------------------+
//|                                             CTrade_Sample_EA.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
#include<Trade\PositionInfo.mqh>
#property description "This Expert Advisor shows some examples of working  "
#property description "with CTrade class. Its functions are not called."

//--- object for performing trade operations
CTrade  trade;
CPositionInfo  position;

    
input int MagicNumber                =  123456;    

int MFI=0;

   
int OnInit()
  {
//--- set MagicNumber for your orders identification
   trade.SetExpertMagicNumber(MagicNumber);
//--- set available slippage in points when buying/selling
   int deviation=10;
   trade.SetDeviationInPoints(deviation);
//--- order execution mode
   trade.SetTypeFilling(ORDER_FILLING_RETURN);
//--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
   trade.LogLevel(1);
//--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
   trade.SetAsyncMode(true);
//---

   return(0);
  }



void OnDeinit(const int reason)
  {
//---
  }

void OnTick()
  {
    
 if(NewBar())
   {      
                      
    MFI=iMFI(_Symbol,PERIOD_CURRENT,14,VOLUME_TICK);
    double _mfi[];
    ArraySetAsSeries(_mfi, false);
    if (CopyBuffer(MFI,0,0,20,_mfi) < 0){Print("CopyBufferRSI error =",GetLastError());}

   MqlRates mated[];
   ArraySetAsSeries(mated, false);
   int mopied=CopyRates(Symbol(),0,0,5,mated);
   string format="%G";
  
      // all the system
  }
}

  
  bool NewBar()
     {
         static datetime lastbar;
         int start = 0; // bar index
         int count = 1; // number of bars
         datetime tm[]; // array storing the returned bar time
         //--- copy time 
         CopyTime(_Symbol,PERIOD_CURRENT,start,count,tm);
         datetime curbar = tm[0];
   
           if(lastbar!=curbar)
              {
                lastbar=curbar;
                return (true);    
              }
                    else
                        {
                         return(false);
                        }
     }
thanks in advance for the help

 

Remember once and for all: THE INDICATOR HANDLE SHOULD BE OBTAINED ONCE - IT IS DONE IN OnInit !!!

Receiving data from an indicator in an MQL5.

How to start with MQL5
How to start with MQL5
  • 2020.09.17
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Vladimir Karputov:

Remember once and for all: THE INDICATOR HANDLE SHOULD BE OBTAINED ONCE - IT IS DONE IN OnInit !!!

Receiving data from an indicator in an MQL5.

Thanks for the reply

Just like this?

//+------------------------------------------------------------------+
//|                                             CTrade_Sample_EA.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
#include<Trade\PositionInfo.mqh>
#property description "This Expert Advisor shows some examples of working  "
#property description "with CTrade class. Its functions are not called."

//--- object for performing trade operations
CTrade  trade;
CPositionInfo  position;

    
input int MagicNumber                =  123456;    

int MFI=0;

   
int OnInit()
  {
//--- set MagicNumber for your orders identification
   trade.SetExpertMagicNumber(MagicNumber);
//--- set available slippage in points when buying/selling
   int deviation=10;
   trade.SetDeviationInPoints(deviation);
//--- order execution mode
   trade.SetTypeFilling(ORDER_FILLING_RETURN);
//--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
   trade.LogLevel(1);
//--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
   trade.SetAsyncMode(true);
//---

   MFI=iMFI(_Symbol,PERIOD_CURRENT,14,VOLUME_TICK);
   
   return(0);
  }



void OnDeinit(const int reason)
  {
//---
  }

void OnTick()
  {
    
 if(NewBar())
   {      
                      
    
    double _mfi[];
    ArraySetAsSeries(_mfi, false);
    if (CopyBuffer(MFI,0,0,20,_mfi) < 0){Print("CopyBufferRSI error =",GetLastError());}

   MqlRates mated[];
   ArraySetAsSeries(mated, false);
   int mopied=CopyRates(Symbol(),0,0,5,mated);
   string format="%G";
  
      // all the system
  }
}

  
  bool NewBar()
     {
         static datetime lastbar;
         int start = 0; // bar index
         int count = 1; // number of bars
         datetime tm[]; // array storing the returned bar time
         //--- copy time 
         CopyTime(_Symbol,PERIOD_CURRENT,start,count,tm);
         datetime curbar = tm[0];
   
           if(lastbar!=curbar)
              {
                lastbar=curbar;
                return (true);    
              }
                    else
                        {
                         return(false);
                        }
     }

about the array mated[] do you have any advice? 

 
Francesco.ooz12 :

Thanks for the reply

Just like this?

about the array mated[] do you have any advice? 

I recommend following the link and carefully reading and watching the picture.

 
//+------------------------------------------------------------------+
//|                                             CTrade_Sample_EA.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
#include<Trade\PositionInfo.mqh>
#property description "This Expert Advisor shows some examples of working  "
#property description "with CTrade class. Its functions are not called."

//--- object for performing trade operations
CTrade  trade;
CPositionInfo  position;

    
input int MagicNumber                =  123456;    

int MFI=0;

   
int OnInit()
  {
//--- set MagicNumber for your orders identification
   trade.SetExpertMagicNumber(MagicNumber);
//--- set available slippage in points when buying/selling
   int deviation=10;
   trade.SetDeviationInPoints(deviation);
//--- order execution mode
   trade.SetTypeFilling(ORDER_FILLING_RETURN);
//--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
   trade.LogLevel(1);
//--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
   trade.SetAsyncMode(true);
//---

   MFI=iMFI(_Symbol,PERIOD_CURRENT,14,VOLUME_TICK);

    if(MFI==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMFI indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
    return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)
  {
//---
  }

void OnTick()
  {

 double _mfi[];
    ArraySetAsSeries(_mfi, false);
       int start_pos=0,count=20;
   if(!iGetArray(MFI,0,start_pos,count,_mfi))
      return;

   string text="";
   for(int i=0; i<count; i++)
   {
      text=text+IntegerToString(i)+": "+DoubleToString(_mfi[i],Digits()+1)+"\n"; 
    } 
      Comment(text);
      
    
 if(NewBar())
   {      
                      
    
    double _mfi[];
    ArraySetAsSeries(_mfi, false);
    if (CopyBuffer(MFI,0,0,20,_mfi) < 0){Print("CopyBufferRSI error =",GetLastError());}

   MqlRates mated[];
   ArraySetAsSeries(mated, false);
   int mopied=CopyRates(Symbol(),0,0,5,mated);
   string format="%G";
  
      // all the system
  }
}

//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
double iGetArray(const int handle,const int buffer,const int start_pos,const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      Print("This a no dynamic array!");
      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("Failed to copy data from the indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated 
      return(false);
     }
   return(result);
  }

  
  bool NewBar()
     {
         static datetime lastbar;
         int start = 0; // bar index
         int count = 1; // number of bars
         datetime tm[]; // array storing the returned bar time
         //--- copy time 
         CopyTime(_Symbol,PERIOD_CURRENT,start,count,tm);
         datetime curbar = tm[0];
   
           if(lastbar!=curbar)
              {
                lastbar=curbar;
                return (true);    
              }
                    else
                        {
                         return(false);
                        }
     }
Vladimir Karputov:

I recommend following the link and carefully reading and watching the picture.

Ok you re right, now i got it, i hope on monday it will be better,

about mated[]? here i don't have an handle

thanks

 
Francesco.ooz12 :

Ok you re right, now i got it, i hope on monday it will be better,

about mated[]? here i don't have an handle

thanks

Compile and remove errors:

 

 

Answer WHY are you doing 'false'?

      ArraySetAsSeries(_mfi, false);
***
      ArraySetAsSeries(mated, false);
 
//+------------------------------------------------------------------+
//|                                             CTrade_Sample_EA.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
#include<Trade\PositionInfo.mqh>
#property description "This Expert Advisor shows some examples of working  "
#property description "with CTrade class. Its functions are not called."

//--- object for performing trade operations
CTrade  trade;
CPositionInfo  position;

    
input int MagicNumber                =  123456;    

int MFI=0;

   
int OnInit()
  {
//--- set MagicNumber for your orders identification
   trade.SetExpertMagicNumber(MagicNumber);
//--- set available slippage in points when buying/selling
   int deviation=10;
   trade.SetDeviationInPoints(deviation);
//--- order execution mode
   trade.SetTypeFilling(ORDER_FILLING_RETURN);
//--- logging mode: it would be better not to declare this method at all, the class will set the best mode on its own
   trade.LogLevel(1);
//--- what function is to be used for trading: true - OrderSendAsync(), false - OrderSend()
   trade.SetAsyncMode(true);
//---

   MFI=iMFI(_Symbol,PERIOD_CURRENT,14,VOLUME_TICK);

    if(MFI==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMFI indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
    return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)
  {
//---
  }

void OnTick()
  {

 double _mfi[];
    ArraySetAsSeries(_mfi, false);
       int start_pos=0,count=20;
   if(!iGetArray(MFI,0,start_pos,count,_mfi))
      return;

   string text="";
   for(int i=0; i<count; i++)
   {
      text=text+IntegerToString(i)+": "+DoubleToString(_mfi[i],Digits()+1)+"\n"; 
    } 
      Comment(text);
      
    
 if(NewBar())
   {      
   MqlRates mated[];
   ArraySetAsSeries(mated, false);
   int mopied=CopyRates(Symbol(),0,0,5,mated);
   string format="%G";
  
      // all the system
  }
}

//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
double iGetArray(const int handle,const int buffer,const int start_pos,const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      Print("This a no dynamic array!");
      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("Failed to copy data from the indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated 
      return(false);
     }
   return(result);
  }

  
  bool NewBar()
     {
         static datetime lastbar;
         int start = 0; // bar index
         int count = 1; // number of bars
         datetime tm[]; // array storing the returned bar time
         //--- copy time 
         CopyTime(_Symbol,PERIOD_CURRENT,start,count,tm);
         datetime curbar = tm[0];
   
           if(lastbar!=curbar)
              {
                lastbar=curbar;
                return (true);    
              }
                    else
                        {
                         return(false);
                        }
     }
Vladimir Karputov:

Compile and remove errors:

 

of course

stupid mistake

 
Vladimir Karputov:

Answer WHY are you doing 'false'?

i structured the system in this way...
 
Francesco.ooz12:

Ok you re right, now i got it, i hope on monday it will be better,

about mated[]? here i don't have an handle

thanks

maybe i can put 

int OnInit()
  {
string Symbolvalue=Symbol();

and then

void OnTick()
 {
  if(NewBar())
   {      
   MqlRates mated[];
   ArraySetAsSeries(mated, false);
   int mopied=CopyRates(Symbolvalue,0,0,5,mated);
   string format="%G";
  
      // all the system
  }


?

 

Never use _Symbol and _Points -> always use Symbol () and Points ().

Never use '0' - always write Period ().

Correct your code. Set ArraySetAsSeries to 'true'.

Reason: