MetaTrader R package - the summary

To add comments, please log in or register
Sergey Golubev
Moderator
109226
Sergey Golubev  

The Main Study

----------------

The Articles

----------------

The Video

  • An Introduction to R - A Brief Tutorial for R - the video on post #110 
  • A Brief Tutorial for R - the video on post #72 (in Russian) 
  • R tutorials - introduction to R Studio - the video on post #111 
  • Biostatistics in R - introduction to meta analysis in R - the video on post #112 
  • Writing Scripts in R - the video on post #113 
  • How to Install Packages in R - the video on post #114 
  • Export Data from R (csv , txt and other formats) - the video on post #115  
----------------

The Forum

----------------

more to follow ..

Sergey Golubev
Moderator
109226
Sergey Golubev  

The first version of integration (first post of this thread) - 

Forum on trading, automated trading systems and testing trading strategies

New MetaTrader 5 Platform build 2005: Economic Calendar, MQL5 applications as services and R language API

MetaQuotes Software Corp., 2019.02.20 17:20

The MetaTrader 5 platform update will be released on February 21, 2019. The new version features the following changes:

  1. ...
  2. ...
  3. ...
  4. ...
  5. Terminal: Added new API enabling request of MetaTrader 5 terminal data through applications using the R language.

    We have prepared a special MetaTrader package. It contains DLL for interactions between R and the MetaTrader 5 terminal, documentation and auxiliary r files. We are completing the package registration in the CRAN repository, after which it will be available for download and installation.




    The package can be installed using a special command:
    R CMD INSTALL --build MetaTrader

    The following commands related to data request are available:

    • MT5Initialize initializes and establishes connection with the MetaTrader 5 terminal. If necessary, the terminal is launched during command execution.
    • MT5Shutdown de-initializes and disconnects from MetaTrader 5.
    • MT5Version gets the MetaTrader 5 terminal version.
    • MT5TerminalInfo gets the state and parameters of terminal connection to a broker's server (account number and server address).
    • MT5WaitTerminal waits for the MetaTrader 5 terminal to connect to a broker's server.
    • MT5CopyTicksFrom(symbol, from, count, flags) copies the specified number of ticks starting from the specified date. The date is specified in milliseconds since 01.01.1970.
    • MT5CopyTicksRange(symbol, from, to, flags) copies ticks from within the specified period. The dates are specified in milliseconds since 01.01.1970.
    • MT5CopyRatesFrom(symbol, timeframe, from, count) copies the specified number of one-minute bars starting from the specified date. The date is specified in seconds since 01.01.1970.
    • MT5CopyRatesFromPos(symbol, timeframe, start_pos, count) copies one-minute bars from the specified position relative to the last bar.
    • MT5CopyRatesFromRange(symbol, timeframe, date_from, date_to) copies bars from within the specified period. The dates are specified in seconds since 01.01.1970.

    The list of supported commands will be further expanded.

    ...

Sergey Golubev
Moderator
109226
Sergey Golubev  

Forum on trading, automated trading systems and testing trading strategies

MetaTrader R package: how to get direct access to data in MetaTrader 5 from R?

Renat Fatkhullin , 2019.02.22 02:18

In the 2005 build of MetaTrader 5, we implemented the first version of integration with external systems such as the analytical package R.

Now analysts using R can directly pull large amounts of historical data from MetaTrader 5 instead of torment with exporting / importing data.

So far this is the first version with a limited set of data extraction functions, and later we will expand the functionality.


What you need to do to check:

  1. Update MetaTrader 5 to 2005 build through opening a demo account on MetaQuotes-Demo or wait for updates on your broker

  2. Download and install the R package: https://cran.r-project.org/bin/windows/base/ (now version 3.5.2, install the 64 bit version)

  3. Write the path to the executable files R in the PATH, for example E: \ Programs \ R \ bin \ x64, so that it can be easily run from the command line

  4. Download the MetaTrader_1.0.1.zip package (compiled for the minimal version R 3.5.0) from the attachment and install it with the command
    C:\Downloads> R CMD INSTALL C:\Downloads\MetaTrader_1.0.3.zip
    In R CMD INSTALL
    * installing to library 'E:/Programs/R/library'
    пакет 'MetaTrader' удачно распакован, MD5 sums проверены
  5. Download IDE R-Studio https://www.rstudio.com/products/rstudio/download/ for the convenience of work, although there is a regular Rgui.exe in the R package

  6. Launch R-Studio or Rgui.exe and load the MetaTrader library into
     library (MetaTrader)
    R-Studio with MetaTrader

    Documentation is available in the help window, where it is enough to enter the name of the MetaTrader package.


  7. Copy test script and press Enter
    MT5Initialize();
    
    if (exists( "ver" )) rm(ver)
    if (exists( "info" )) rm(info)
    if (exists( "ticks1" )) rm(ticks1)
    if (exists( "ticks2" )) rm(ticks2)
    if (exists( "rates1" )) rm(rates1)
    if (exists( "rates2" )) rm(rates2)
    
    ver <- MT5Version()
    info <- MT5TerminalInfo()
    
    ver
    info
    
    MT5WaitForTerminal()
    
    ticks1 <- MT5CopyTicksFrom( "EURAUD" , as .POSIXct( "2019-01-28 13:00:00" ), 10000 , MT5_COPY_TICKS_ALL)
    ticks2 <- MT5CopyTicksRange( "AUDUSD" , as .POSIXct( "2019-01-28 13:00:00" ), as .POSIXct( "2019-01-28 13:01:00" ), MT5_COPY_TICKS_ALL)
    
    rates1 <- MT5CopyRatesFrom( "EURUSD" , MT5_TIMEFRAME_M1, as .POSIXct( "2019-01-28 13:00:00" ), 1000 )
    rates2 <- MT5CopyRatesFromPos( "EURUSD" , MT5_TIMEFRAME_M1, 0 , 1000 )
    
    MT5Shutdown();
    
    #COUNT
     length(ticks1[, 1 ])
    length(ticks2[, 1 ])
    length(rates1[, 1 ])
    length(rates2[, 1 ])
    
    #time  correcting
    ticks1[, 1 ] = ticks1[, 1 ] / 1000
    ticks2[, 1 ] = ticks2[, 1 ] / 1000
    
    #DATA
     ticks1[ 1 : 10 ,]
    ticks2[ 1 : 10 ,]
    rates1[ 1 : 10 ,]
    rates2[ 1 : 10 ,]
    

    The last active terminal will be automatically found, launched with waiting for initialization and the following requests will be processed:
    > 
    > MT5Initialize();
    [ 1 ] TRUE
    > 
    > if (exists( "ver" )) rm(ver)
    > if (exists( "info" )) rm(info)
    > if (exists( "ticks1" )) rm(ticks1)
    > if (exists( "ticks2" )) rm(ticks2)
    > if (exists( "rates1" )) rm(rates1)
    > if (exists( "rates2" )) rm(rates2)
    > 
    > ver <- MT5Version()
    > info <- MT5TerminalInfo()
    > 
    > ver
    [[ 1 ]]
    [ 1 ] 500
    
    [[ 2 ]]
    [ 1 ] 2005
    
    [[ 3 ]]
    [ 1 ] "21 Feb 2019"
    
    > info
    [[ 1 ]]
    [ 1 ] 2
    
    [[ 2 ]]
    [ 1 ] "MetaQuotes-Demo"
    
    [[ 3 ]]
    [ 1 ] "12652429"
    
    > 
    > MT5WaitForTerminal()
    [ 1 ] TRUE
    > 
    > ticks1 <- MT5CopyTicksFrom( "EURAUD" , as .POSIXct( "2019-01-28 13:00:00" ), 10000 , MT5_COPY_TICKS_ALL)
    > ticks2 <- MT5CopyTicksRange( "AUDUSD" , as .POSIXct( "2019-01-28 13:00:00" ), as .POSIXct( "2019-01-28 13:01:00" ), MT5_COPY_TICKS_ALL)
    > 
    > rates1 <- MT5CopyRatesFrom( "EURUSD" , MT5_TIMEFRAME_M1, as .POSIXct( "2019-01-28 13:00:00" ), 1000 )
    > rates2 <- MT5CopyRatesFromPos( "EURUSD" , MT5_TIMEFRAME_M1, 0 , 1000 )
    > 
    > MT5Shutdown();
    [ 1 ] TRUE
    > 
    > #COUNT
     > length(ticks1[, 1 ])
    [ 1 ] 10000
    > length(ticks2[, 1 ])
    [ 1 ] 101
    > length(rates1[, 1 ])
    [ 1 ] 1000
    > length(rates2[, 1 ])
    [ 1 ] 1000
    > 
    > #time  correcting
    > ticks1[, 1 ] = ticks1[, 1 ] / 1000
    > ticks2[, 1 ] = ticks2[, 1 ] / 1000
    > 
    > #DATA
     > ticks1[ 1 : 10 ,]
                time     bid     ask last volume flags
     [ 1 ,] 1548669600 1.58697 1.58729      0        0    134
     [ 2 ,] 1548669600 1.58698 1.58729      0        0    130
     [ 3 ,] 1548669600 1.58696 1.58728      0        0    134
     [ 4 ,] 1548669601 1.58695 1.58727      0        0    134
     [ 5 ,] 1548669601 1.58696 1.58727      0        0    130
     [ 6 ,] 1548669601 1.58697 1.58728      0        0    134
     [ 7 ,] 1548669601 1.58696 1.58728      0        0    130
     [ 8 ,] 1548669601 1.58696 1.58725      0        0      4
     [ 9 ,] 1548669601 1.58692 1.58723      0        0    134
    [ 10 ,] 1548669601 1.58692 1.58724      0        0      4
    > ticks2[ 1 : 10 ,]
                time     bid     ask last volume flags
     [ 1 ,] 1548669600 0.71857 0.71876      0        0    134
     [ 2 ,] 1548669601 0.71856 0.71876      0        0    130
     [ 3 ,] 1548669601 0.71856 0.71875      0        0      4
     [ 4 ,] 1548669601 0.71855 0.71875      0        0    130
     [ 5 ,] 1548669601 0.71855 0.71874      0        0      4
     [ 6 ,] 1548669602 0.71855 0.71875      0        0      4
     [ 7 ,] 1548669602 0.71855 0.71874      0        0      4
     [ 8 ,] 1548669602 0.71855 0.71873      0        0      4
     [ 9 ,] 1548669602 0.71853 0.71872      0        0    134
    [ 10 ,] 1548669602 0.71853 0.71873      0        0      4
    > rates1[ 1 : 10 ,]
                time    open    high     low   close tick_volume spread real_volume
     [ 1 ,] 1548436560 1.13825 1.13840 1.13822 1.13823          102        8            0
     [ 2 ,] 1548436620 1.13824 1.13879 1.13824 1.13872          142        8            0
     [ 3 ,] 1548436680 1.13873 1.13934 1.13862 1.13918          223        8            0
     [ 4 ,] 1548436740 1.13917 1.13930 1.13913 1.13916          180        8            0
     [ 5 ,] 1548436800 1.13917 1.13949 1.13912 1.13941          194        8            0
     [ 6 ,] 1548436860 1.13940 1.13942 1.13925 1.13941            93        8            0
     [ 7 ,] 1548436920 1.13942 1.13964 1.13942 1.13964          121        8            0
     [ 8 ,] 1548436980 1.13964 1.13988 1.13956 1.13988          175        8            0
     [ 9 ,] 1548437040 1.13989 1.14038 1.13989 1.14021          218        8            0
    [ 10 ,] 1548437100 1.14021 1.14036 1.14013 1.14026          127        8            0
    > rates2[ 1 : 10 ,]
                time    open    high     low   close tick_volume spread real_volume
     [ 1 ,] 1550745060 1.13551 1.13629 1.13551 1.13559          189        8            0
     [ 2 ,] 1550745120 1.13559 1.13565 1.13532 1.13543          127        8            0
     [ 3 ,] 1550745180 1.13543 1.13549 1.13505 1.13518          116        8            0
     [ 4 ,] 1550745240 1.13518 1.13550 1.13514 1.13528          154        8            0
     [ 5 ,] 1550745300 1.13528 1.13578 1.13525 1.13565          150        8            0
     [ 6 ,] 1550745360 1.13565 1.13569 1.13547 1.13559            99        8            0
     [ 7 ,] 1550745420 1.13558 1.13566 1.13546 1.13558          106        8            0
     [ 8 ,] 1550745480 1.13558 1.13562 1.13547 1.13560            83        8            0
     [ 9 ,] 1550745540 1.13560 1.13567 1.13548 1.13558          142        8            0
    [ 10 ,] 1550745600 1.13559 1.13559 1.13547 1.13551            95        8            0 
  8. You can visualize arrays
    plot(ticks1)



Current feature set:

MT5Initialize           Establish connection with the MetaTrader 5 Terminal
MT5Shutdown             Disconnect from the MetaTrader 5 Terminal

MT5TerminalInfo         Get the state and parameters of the MetaTrader 5 terminal connection
MT5Version              Get the MetaTrader 5 Terminal version
MT5WaitForTerminal      Wait for the MetaTrader 5 Terminal to connect to a broker's server


MT5CopyRatesFrom        Get bars starting from the specific date
MT5CopyRatesFromPos     Get bars starting from the specified position
MT5CopyRatesRange       Get bars from the specified period
MT5CopyTicksFrom        Get ticks starting from the specific date
MT5CopyTicksRange       Get ticks from the specified period
Files:
Sergey Golubev
Moderator
109226
Sergey Golubev  

Forum on trading, automated trading systems and testing trading strategies

MetaTrader R package: how from R to get direct access to data in MetaTrader 5?

Vladimir Perervenko , 2019.02.22 09:15

Thanks for the tip.

Installing the package can be easier.

1. Install and open Rstudio

2. Download the MetaTrader_1.0.3.zip package (compiled for the minimal version R 3.5.0)

3. In the Packages -> install tab and specify the path where the MetaTrader_1.0.3.zip file is located .

Is done.

Now I’ll check how all the functions work.


Sergey Golubev
Moderator
109226
Sergey Golubev  

Forum on trading, automated trading systems and testing trading strategies

R_UserGroup

Vladimir Perervenko , 2019.02.24 11:33

The thread is described in some details how to install the package. I will repeat the sequence by slightly modifying:
What you need to do to check:

  1. Update MetaTrader 5 to 2005 build through opening a demo account on MetaQuotes-Demo or wait for updates on your broker
  2. Download and install R: https://cran.r-project.org/bin/windows/base/ (now version 3.5.2, install the 64 bit version)
  3. Write the path to the executable files R in the PATH, for example E: \ Programs \ R \ bin \ x64, so that it can be easily run from the command line
  4. Download the MetaTrader_1.0.3.zip package (compiled for the minimal version R 3.5.0) from the attachment and install it with a command. The file is on the thread .
  5. Download IDE R-Studio https://www.rstudio.com/products/rstudio/download/ for convenience of work, although there is a regular Rgui.exe as part of R
  6. Launch Rstudio. In the Packages tab, click install and specify the path to the place where the MetaTrader_1.0.3.zip file is located

Is done. In the next post we will check the work on simple examples.
Sergey Golubev
Moderator
109226
Sergey Golubev  

Forum on trading, automated trading systems and testing trading strategies

Something Interesting to Read

Sergey Golubev, 2019.02.27 08:48

R for Data Science: Import, Tidy, Transform, Visualize, and Model Data

R for Data Science

Learn how to use R to turn raw data into insight, knowledge, and understanding. This book introduces you to R, RStudio, and the tidyverse, a collection of R packages designed to work together to make data science fast, fluent, and fun. Suitable for readers with no previous programming experience, R for Data Science is designed to get you doing data science as quickly as possible.

Authors Hadley Wickham and Garrett Grolemund guide you through the steps of importing, wrangling, exploring, and modeling your data and communicating the results. You’ll get a complete, big-picture understanding of the data science cycle, along with basic tools you need to manage the details. Each section of the book is paired with exercises to help you practice what you’ve learned along the way.

Sergey Golubev
Moderator
109226
Sergey Golubev  

About MetaTrader R (to make it shorter) - 

----------------

Forum on trading, automated trading systems and testing trading strategies

MetaTrader R package: how from R to get direct access to data in MetaTrader 5?

Renat Fatkhullin , 2019.02.26 15:47

I repeat once again - this is a library that simplifies access to the initial data of the market environment .

Neither to charts, nor to indicators, nor to MQL5 with robots, namely to the initial data of ticks and charts.


Do you want to do analysis in R?

We have solved one of the main problems with the delivery of market data. You need to concentrate on your work if you prefer analytics in R.

The MQL5 engine and functionality are not going to be transferred to R.
Sergey Golubev
Moderator
109226
Sergey Golubev
Moderator
109226
Sergey Golubev  
To add comments, please log in or register