Extract forex trading data with python

Chew Lin Kiat
4 min readJul 6, 2022
A trader looking at forex charts

I have a friend, John who was curious about forex trading (i.e trading currency pairs). He wanted to know, if it possible to get forex data from the internet to analyse forex movement.

He knows that I had used python to scrape data and would like me to help him to get this forex data. However, he has a peculiar request. He only want intraday forex data timestamp only from 7.00pm to midnight. He told me, he only wanted to trade after work. He don’t want to trade after midnight, as he wants to get enough sleep in order to go to work the next day.

Forex trading is highly speculative. Statistics showed that many lose their money trading forex. I had cautioned John about it. John told me he wanted the data to analyse and is not going to trade any money, until he has enough data to know how to trade profitably.

With the assurance that he is not going to risk any money, I decided to help him with this task. In order to extract forex data, I went to search for forex trading companies that allows us to set up demo account. It turns out there are many companies offering forex trading. Some uses metatrader 4, while others uses metatrader 5. I decided to sign on a demo account with a forex broker who uses metatrader 5, Also, there is a python library that support metatrader 5. For those interested, please read the documentations here Working with Python — Developing programs — MetaEditor Help (metatrader5.com)

After installing metatrader 5, I am able to login to the broker account using python. Once I had login, I went on to check on the trading symbols in the broker platform. There are over 2000 trading symbols ranging from indices such as Dow Jones, precious metals, commodities to forex currency pairs. John wanted to analyse euro/usd data over a 5 minute interval period. A 5 minute interval means that over a 5 minute period, the data will be printed showing the open, high, low and close prices. This 5 minute data will be printed every 5 minutes from 7.00pm to midnight.

Apparently the broker does have these 5 minute data available for download. After writing a few lines of code, I am able to extract the data as shown below.

The time column shows epoch time. Using python datetime module, I am able to convert this to our human readable time. See below

As seen from the table above, the time recorded is in 5 minute interval. As the time is recorded is using broker server time, I would need to convert it to our local time and filter to the requested time period. See below for spreadsheet

Using a module called mplfinance. See link for documentation GitHub — matplotlib/mplfinance: Financial Markets Data Visualization using Matplotlib. Mplfinance allows charting for financial data and skip the time period for weekends. As such, I am able to draw the candle stick charts that are used by traders. I can also plot a moving averages in the chart (see below).

The chart above is the eur/usd chart with 10, 20 moving averages lines over a 5 minute time period. I showed this to John. He was impressed. If he decided to analyse another currency pair, he just need to modify the symbol in the python script. He can even specify the time period he wants, whether it is 1 minute or 1 hour time interval, so long the broker has the data, he can set up the time period and download the data.

Is it possble to analyse this with artifical intelligence? John asked.

It is certainly possible, I replied. However, this calls for a more understanding over the domain and the variables affecting the markets that you are interested. I replied.

For those who like see more of these updates. Do connect me at linkedin

--

--