High-Frequency Trading with mt5R

It’s an pretty naive example to check the viability of high-frequency trading.

How we will test it

We will run 100 times the function MT5.Quick_GetSymbol() too check how long will take every loop, and will calculate how many times this function will be used in 1 second interval.

Sending order > MT5 fetching data > Receiving data, for every loop.

First let’s load mt5R.

library(mt5R)

Run the loop.

iCnt <- 0; dtVec <- as.integer()

while(iCnt < 100)
{
  TableEx <- MT5.Quick_GetSymbol("EURUSD", iTF = 1, iRows = 10)
  dtVec[iCnt <<- iCnt + 1] <- Sys.time()
}

What is inside of TableEx?

print(TableEx)
#>    Year Month Day Hour Minute    Open    High     Low   Close Volume
#> 1  2021     3  19   23     50 1.19015 1.19021 1.19014 1.19016     15
#> 2  2021     3  19   23     51 1.19015 1.19019 1.19012 1.19017     16
#> 3  2021     3  19   23     52 1.19019 1.19019 1.19012 1.19012     31
#> 4  2021     3  19   23     53 1.19012 1.19028 1.19012 1.19026     31
#> 5  2021     3  19   23     54 1.19028 1.19048 1.19028 1.19033     42
#> 6  2021     3  19   23     55 1.19035 1.19055 1.19031 1.19036     47
#> 7  2021     3  19   23     56 1.19036 1.19051 1.19036 1.19046     21
#> 8  2021     3  19   23     57 1.19048 1.19049 1.19036 1.19042     68
#> 9  2021     3  19   23     58 1.19033 1.19035 1.19028 1.19032     38
#> 10 2021     3  19   23     59 1.19032 1.19036 1.19022 1.19024     49

Now we will check the results.

Dates <- as.POSIXct(dtVec, origin = "1970-01-01")
Secs <- as.integer(strftime(Dates, format = "%S")) ##Interested only on seconds
table(Secs)
#> Secs
#> 37 38 39 
#> 31 53 16

Only one second interval was fully used - the middle one.

Answering the first question, how many times inside of 1 second the function MT5.Quick_GetSymbol() was used?

InterestedNumber <- table(Secs)[2]
print(InterestedNumber[[1]]) ##only the number
#> [1] 53

How long, in average, MT5.Quick_GetSymbol() takes in milliseconds every run?

1000/InterestedNumber[[1]]
#> [1] 18.86792

The result is relative for every spec and parameters.

This was a naive example, focused in fetch data. Expert Advisors use a lot of computation, functions and every kind of models. The result demonstrate how powerful its the socket connection.