This tutorial explains how to import high-frequency trading data and visualize it using the Plotly library
Import the necessary libraries that you intend to use for your project.
There are packages that allow real-time trading data import which could come handy for algo trading.
I have used the following packages for my codethrough.
library(plotly) # Graphs
library(xts) # Time-series constructor
library(tidyverse) #Ease of use
I have used the Metatrader application to download USDEUR Forex trade data as a csv file.
Many websites provide us with trade data in various formats.
CSV would be suited for use in our case. I have downloaded the EURUSD forex trade data from Metarader application.
You can find the application in the following link.
I have uploaded the file onto my Github account for ease of access.
# Importing trading data from the csv file downloaded from Metatrade ----
library (readr)
data <- read.csv("https://raw.githubusercontent.com/kkarthi6/CPP526_Kavin/master/EURUSDM1.csv",
header = FALSE, sep = ",", fileEncoding = "UTF-16LE", dec = ".")
head(data)
## V1 V2 V3 V4 V5 V6 V7
## 1 2020.03.20 13:04 1.07302 1.07322 1.07276 1.07299 169 0
## 2 2020.03.20 13:05 1.07299 1.07320 1.07246 1.07298 224 0
## 3 2020.03.20 13:06 1.07297 1.07306 1.07271 1.07289 180 0
## 4 2020.03.20 13:07 1.07289 1.07298 1.07272 1.07285 143 0
## 5 2020.03.20 13:08 1.07285 1.07324 1.07272 1.07291 160 0
## 6 2020.03.20 13:09 1.07291 1.07298 1.07241 1.07247 185 0
Data that is available online would not always meet our requirements.
We will need to make a few changes to the way data is stored and make sure it is suited for the type of analysis that we intend to carry out.
# Renaming the columns using the column name vector
colnames_ <- c("Date", "Open", "High", "Low", "Close", "Tick", "Volume")
colnames(data) <- colnames_
head(data)
## Date Open High Low Close Tick Volume
## 1 2020.03.20 13:04 1.07302 1.07322 1.07276 1.07299 169 0
## 2 2020.03.20 13:05 1.07299 1.07320 1.07246 1.07298 224 0
## 3 2020.03.20 13:06 1.07297 1.07306 1.07271 1.07289 180 0
## 4 2020.03.20 13:07 1.07289 1.07298 1.07272 1.07285 143 0
## 5 2020.03.20 13:08 1.07285 1.07324 1.07272 1.07291 160 0
## 6 2020.03.20 13:09 1.07291 1.07298 1.07241 1.07247 185 0
# Formatting the date to standard form YYYY-MM-DD
data$Date <- gsub("\\.", "-", data$Date)
data$Date <- as.POSIXct(data$Date)
head(data)
## Date Open High Low Close Tick Volume
## 1 2020-03-20 13:04:00 1.07302 1.07322 1.07276 1.07299 169 0
## 2 2020-03-20 13:05:00 1.07299 1.07320 1.07246 1.07298 224 0
## 3 2020-03-20 13:06:00 1.07297 1.07306 1.07271 1.07289 180 0
## 4 2020-03-20 13:07:00 1.07289 1.07298 1.07272 1.07285 143 0
## 5 2020-03-20 13:08:00 1.07285 1.07324 1.07272 1.07291 160 0
## 6 2020-03-20 13:09:00 1.07291 1.07298 1.07241 1.07247 185 0
The plotly package is used here for visualization.
Since trading data requires special plotting methods, we have used plotly.
The last 20 minutes of data alone is included in our visualization to improve graph readability.
# Using plotly to visualize the last 20 min of trading data
fig <- tail(data, 20) %>%
plot_ly(x = ~Date, type = "candlestick",
open = ~Open, close = ~Close,
high = ~High, low = ~Low)
fig <- fig %>% layout(title = "USDEUR trading data ")
fig
tidyverse package documentation
https://cran.r-project.org/web/packages/tidyverse/tidyverse.pdf
plotly package documentation
https://cran.r-project.org/web/packages/plotly/plotly.pdf
This interesting article of visualizing high-frequency trading data in R