Now, we will work with the MetaTrader5 library to connect to the platform and to receive historical data. MetaTrader5 has a method called
initialize()
, which will run the MetaTrader5 Client. After initializing the platform, we will then have to log in to a trading account that will have access to the server data provided by the broker. For that, we will use the method
login()
.
In the example below, we will use an MT5 demo account provided by the broker VantageFX. If you do not have your own MT5 account, you can quickly get one by signing up
here
or use the example credentials.
login = 123
password = 'password'
server = 'server'
# runs MetaTrader5 client
# MetaTrader5 platform must be installed
mt.initialize()
# log in to trading account
mt.login(login, password, server)
Once you execute the code, your MetaTrader5 platform should be running and logged into the trading account based on the credentials provided.
Requesting OHLC Data
Now we can interact with the platform, specifically for our case we are now able to request historical bars. The MetaTrader5 method we will use is
copy_rates_from_pos()
.
symbol = 'EURUSD'
timeframe = 1 # integer value representing minutes
start_bar = 0 # initial position of first bar
num_bars = 1000 # number of bars
bars = mt.copy_rates_from_pos(symbol, timeframe, start_bar, num_bars)
The method
copy_rates_from_pos()
takes 4 arguments. You pass in the symbol name (which you can find in the platform) as a string, the timeframe of your OHLC data in minutes, the start index of the bar, and the number of bars.
Creating a Pandas DataFrame
We are almost there. Let’s print our variable
bars
and see what we have.
I hope that you liked this article and make sure to subscribe to my mailing list if you wish to receive updates regarding new articles!