Extending MqlTick

 

Hello,

I need to extend MqlTick struct to include additional info such as symbol and AccountPlatform.

How can it be done?

 
Hannan Nussbaum: I need to extend MqlTick struct to include additional info such as symbol and AccountPlatform. How can it be done?

You can't "extend" a structure, especially not one used internally by the built-in functions. You can however define your own "similar" structures for your own functionality.

 
Fernando Carreiro:

You can't "extend" a structure, especially not one used internally by the built-in functions. You can however define your own "similar" structures for your own functionality.

Yes you can inherit from a structure.

struct CustomTick:MqlTick
  {
   string   symbol;
   int      account;   
  };

However with MqlTick, you will not be able to use it in function like SymbolInfoTick().

 
  1. Hannan Nussbaum: How can it be done?

    You just do it. Structs can only extend structs and classes only classes.

  2. Alain Verleyen: However you will not be able to use it in function like SymbolInfoTick().
    Sure you can, just like you can pass a subclass object to a function taking the superclass.
 
whroeder1:

  1. Sure you can, just like you can pass a subclass object to a function taking the superclass.

Right it works for SymbolInfoTick().

But it doesn't with MqlRates and CopyRates() (compiles but gives runtime error 4029: Invalid Array). So I would suggest anyone to check before relying on such solution for internal mql structure.

 

I am using this code to save and load tick to file memory. It expects MqlTick and throws parameter conversion error with the custom struct. Any solution?







#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property strict
#include <MemMapLib.mqh>
#include <TypeToBytes.mqh>

template <typename T>

class EXCHANGE_DATA
{
private:
  CMemMapFile* FileMemory;

public:  
  EXCHANGE_DATA( const int Amount, const bool ModeCreate = false, string FileName = "Local\\test" )
  {
//    FileName += _Symbol;
    
    this.FileMemory = new CMemMapFile;
      
    if (this.FileMemory.Open(FileName, sizeof(T) * Amount + sizeof(int) + HEAD_MEM, ModeCreate ? modeCreate : modeOpen) != 0)
    {
      Alert("FileMemory.Open - ERROR!");
      
      delete &this;
    }
  }
  
  ~EXCHANGE_DATA( void )
  {
    this.FileMemory.Close();
    
    delete this.FileMemory;
  }

  void DataSave( const T &Data[], const bool FromBegin = true  ) const
  {
    const int Size = ::ArraySize(Data) * sizeof(T);
    uchar Bytes[];
    
    _ArrayCopy(Bytes, _R(Size).Bytes);              // Записали количество  
    _ArrayCopy(Bytes, _R(Data).Bytes, sizeof(int)); // Записали данные
  
        if (FromBegin)
          this.FileMemory.Seek(0, SEEK_SET);
  
        this.FileMemory.Write(Bytes, ::ArraySize(Bytes)); // Сбросили все в память
    
    return;
  }
  
  int DataLoad( T &Data[], const bool FromBegin = true ) const
  {
        if (FromBegin)
          this.FileMemory.Seek(0, SEEK_SET);
  
        uchar Bytes[];
          
        this.FileMemory.Read(Bytes, sizeof(int));  // Прочли из памяти количество данных        
        this.FileMemory.Read(Bytes, _R(Bytes)[0]); // Получили сами данные
  
        _ArrayCopy(Data, Bytes);              // Сбросили данные в массив
    
    return(::ArraySize(Data));
  }  
};






Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
Choose a suitable trading strategy and subscribe to it with a few clicks. All Signals are provided with detailed statistics and informative charts. Become a trading signal provider and sell subscriptions to thousands of traders around the world. With the Signals service, your successful strategy can generate income with a small start-up budget...
 
 
Alain Verleyen:

Yes you can inherit from a structure.

However with MqlTick, you will not be able to use it in function like SymbolInfoTick().

Thanks! Did not know that!
 
whroeder1:
  1. You just do it. Structs can only extend structs and classes only classes.

  2. Sure you can, just like you can pass a subclass object to a function taking the superclass.
Thanks! Did not know that!
 

static CustomTick Ticks[1];

ExchangeTicks.DataSave(Ticks, true);

Error: 'Ticks' - parameter conversion not allowed

What can be done to solve this? When using the standard MqlTick is works fine.

(DataSave method see above code in class EXCHANGE_DATA)

 
You can derive from a structure just fine. You can't use an array of the derived in place of an array of the base. You'll have to copy each element yourself.
Reason: