How to visualize high frequency financial data using Plotly and R | by Pedro Lealdino Filho | Towards Data Science

In this article I show how to use the Plotly package to visualize financial data in high frequency using R. To perform analysis and develop trading algorithms, it is necessary to obtain data in very high frequencies to be able to take quick and accurate actions to maximize the profits earned in the trades.
Today, more than 40% of the operations carried out on American stock exchanges are carried out by robots programmed to analyze the market and buy or sell according to market indicators. Today we are going to learn how to create a candlestick chart from a Brazilian asset data called “mini-index,” widely used by daytraders.
There are several ways to collect high-frequency data from the exchange. But today, since we will not analyze the data in real-time, we will collect the data using Metatrader, a tool for conducting free trades, which enables the purchase and sale of assets.
The first step is to open the tool and choose the asset from which we want to collect it. In our case, it is June, and the mini-index asset for that month is WINM20.
After selecting the asset, we choose the chart’s time frame. Please select the time of one minute so that we can collect the data in seconds.
After, click on file and save. The asset data will be saved in .csv format.Ready, data collection is done! Let’s go to the data processing!
I use R, a powerful and free statistical programming language, along with some graphic libraries to perform my analysis. After opening RStudio, I create a. RScript file.
We started by importing the necessary libraries
library(plotly) #para plotar os gráficoslibrary(xts) #para trabalhar com dados de séries temporaislibrary(tidyverse) #jeito magnífico de trabalhar com dados
Afterwards, we import the data of our asset that is named “WINM20M1.csv”
data <- read.csv("WINM20M1.csv", header = FALSE, sep = ",", fileEncoding = "UTF-16LE", dec = ".")head(data)
We see seven variables that have no name yet. Thus, we create a vector with the name of the variables using the OHLC standard (Open, High, Low, Close) and assign this vector to the name of the data.frame columns.
colnames_ <- c("Date", "Open", "High", "Low", "Close", "Tick", "Volume")
colnames(data) <- colnames_head(data)
Now that we have our data appropriately renamed, we need to change the type of the variable, Date, to type Date. But before changing it, we must make a change to the string to replace the “.” per “-” .
data$Date <- gsub("\\.", "-", data$Date)data$Date <- as.POSIXct(data$Date)head(data)
Okay, now for the data visualization.
Finally, to visualize the data, we use the Plotly library, perfect for this type of task. First, we create a fig variable to store our graph. I will only use the last 20 minutes so that the visualization is not too dense
fig <- tail(data, 20) %>%  plot_ly(x = ~Date, type = "candlestick",          open = ~Open, close = ~Close,          high = ~High, low = ~Low)fig <- fig %>% layout(title = "Candles de Mini Ídice")fig
Voilà!
Questions or comments, please contact! Hug! Don’t forget to subscribe to our newsletter to receive weekly content on data science, mathematics, and finance.