Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2016.09.19 12:57

TypeToBytes - library for MetaTrader 5

| English 日本語 Deutsch Русский

votes: 15

This cross-platform library allows to conveniently perform byte-wise operation with structures and standard data types.


  • Byte-wise comparison (== and !=) of the structures and standard data types with each other (in MQL, the operators for comparing structures are absent by default).
  • Determining the byte shift of a structure filed by its name.
  • Reading the value of any standard type by the byte shift in the source data.
  • Writing structures and standard types by byte shift to the source data.
  • Converting structures and standard data types into a byte array.
Everything mentioned applies to simple structures.

A script with detailed comments is attached to demonstrate the capabilities of the library

// MQL4&5-code

#property strict

#include <TypeToBytes.mqh>

#define PRINT(A) ::Print(#A + " = " + (string)A);

void OnStart( void )
  MqlTick Tick;
  ::SymbolInfoTick(::Symbol(), Tick);

  MqlTick CloneTick = Tick;

// Operation with structures
  if (_R(Tick) == CloneTick)             // Now the structures can be compared

// Get the result of the required type by shift
  PRINT(_R(Tick)[(datetime)0])           // Check the 'datetime' value with the zero shift in the object of the MqlTick structure - Tick.time

// Get the shift of the structure field
  const int Offset = _OFFSET(Tick, bid); // Found the shift in bytes of the bid field in the MqlTick object

  PRINT(                        // Checked the value of
  _W(Tick, Offset, (double)1.23456);     // Wrote the (double)1.098765 value at the found shift
  PRINT(                        // Made sure that the is now equal to 1.098765

  PRINT(_R(Tick)[(double)Offset])        // Printed the 'double' value located at the shift Offset - it is again

  PRINT(_R(Tick).Bytes[8])               // Checked the value of byte with the shift of 8 in the object of the MqlTick structure
  PRINT(_R(Tick)[(uchar)8])              // The same, but using a different method

  PRINT(                   // Checked the value of
  _W(CloneTick, 0, Tick);                // Wrote the value of the Tick structure object to the CloneTick at the zero shift
  PRINT(                   // Made sure that ==

// Operation with the standard types
  color Color = C'241,248,255';

  PRINT(_R(Color)[(uchar)1])             // Green component of the color - 248

  _W(Color, 2, (uchar)230);              // Wrote the (uchar)230 value at the shift of 2.
  PRINT(Color)                           // Made sure that Color is now C'241,248,230'

// Simultaneous operation with mixed types
  if (_R(Tick) != Color)                 // It is even possible to compare structures with standard types
    ::Print("Not equal");



Translated from Russian by MetaQuotes Software Corp.
Original code:

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Average Directional Movement Index (ADX) Average Directional Movement Index (ADX)

The Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.

Modify All TP SL Modify All TP SL

The script modifies all orders (market and pending) on the symbol with specified Take Profit and Stop Loss.

iFractals 4all TF iFractals 4all TF

Buy or sell just by looking at the "arrow" as a signal. Very easy and simple.