MQL5 ReferenceIntegrationMetaTrader for Python 

MetaTrader module for integration with Python

Python is a modern high-level programming language for developing scripts and applications. It contains multiple libraries for machine learning, process automation, as well as data analysis and visualization.

MetaTrader package for Python is designed for convenient and fast obtaining of exchange data via interprocessor communication directly from the MetaTrader 5 terminal. The data received this way can be further used for statistical calculations and machine learning.

Functions for integrating MetaTrader 5 and Python

Function

Action

MT5Initialize

Establish a connection with the MetaTrader 5 terminal

MT5Shutdown

Close the previously established connection to the MetaTrader 5 terminal

MT5TerminalInfo

Get status and parameters of the connected MetaTrader 5 terminal

MT5Version

Return the MetaTrader 5 terminal version

MT5WaitForTerminal

Wait till the MetaTrader 5 terminal connects to the trade server

MT5CopyRatesFrom

Get bars from the MetaTrader 5 terminal starting from the specified date

MT5CopyRatesFromPos

Get bars from the MetaTrader 5 terminal starting from the specified index

MT5CopyRatesRange

Get bars in the specified date range from the MetaTrader 5 terminal

MT5CopyTicksFrom

Get ticks from the MetaTrader 5 terminal starting from the specified date

MT5CopyTicksRange

Get ticks for the specified date range from the MetaTrader 5 terminal

Example of connecting Python to MetaTrader 5

  1. Download the latest version of Python 3.7 from https://www.python.org/downloads/windows
  2. When installing Python, check "Add Python 3.7 to PATH%" to be able to run Python scripts from the command line.
  3. Install the MetaTrader 5 module from the command line

  pip install MetaTrader5

  1. Add matplotlib and pytz packages

  pip install matplotlib
  pip install pytz

  1. Launch the test script

   from datetime import datetime
   from MetaTrader5 import *
   from pytz import timezone
   import matplotlib.pyplot as plt
   utc_tz = timezone('UTC')
 
 
   # connect to MetaTrader 5
   MT5Initialize()
   # wait till MetaTrader 5 establishes connection to the trade server and synchronizes the environment
   MT5WaitForTerminal()
 
   # request connection status and parameters
   print(MT5TerminalInfo())
   # get data on MetaTrader 5 version
   print(MT5Version())
 
   # request 1000 ticks from EURAUD 
   euraud_ticks = MT5CopyTicksFrom("EURAUD"datetime(2019,4,1,0), 1000MT5_COPY_TICKS_ALL)
   # request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00 
   audusd_ticks = MT5CopyTicksRange("AUDUSD"datetime(2019,4,1,13), datetime(2019,4,2,13), MT5_COPY_TICKS_ALL)
 
   # get bars from different symbols in a number of ways
   eurusd_rates = MT5CopyRatesFrom("EURUSD"MT5_TIMEFRAME_M1datetime(2019,4,5,15), 1000)
   eurrub_rates = MT5CopyRatesFromPos("EURRUB"MT5_TIMEFRAME_M101000)
   eurjpy_rates = MT5CopyRatesRange("EURJPY"MT5_TIMEFRAME_M1datetime(2019,4,1,13), datetime(2019,4,2,13))
   # shut down connection to MetaTrader 5
   MT5Shutdown()
 
   #DATA
   print('euraud_ticks(', len(euraud_ticks), ')')
   for val in euraud_ticks[:10]: print(val)
   print('audusd_ticks(', len(audusd_ticks), ')')
   for val in audusd_ticks[:10]: print(val)
   print('eurusd_rates(', len(eurusd_rates), ')')
   for val in eurusd_rates[:10]: print(val)
   print('eurrub_rates(', len(eurrub_rates), ')')
   for val in eurrub_rates[:10]: print(val)
   print('eurjpy_rates(', len(eurjpy_rates), ')')
   for val in eurjpy_rates[:10]: print(val)
 
   #PLOTTING
   x_time = [x.time.astimezone(utc_tz) for x in euraud_ticks]
   # prepare Bid and Ask arrays
   bid = [y.bid for y in euraud_ticks]
   ask = [y.ask for y in euraud_ticks]
 
   # draw ticks on the chart
   plt.plot(x_timeask,'r-', label='ask')
   plt.plot(x_timebid,'g-', label='bid')
   # display legends 
   plt.legend(loc='upper left')
   # display header 
   plt.title('EURAUD ticks')
   # display the chart
   plt.show()

  1. Get data and chart
    python_script_chart

[2, 'MetaQuotes-Demo', '16167573']
[500, 2009, '15 Mar 2019']
 
euraud_ticks( 1000 )
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 3, 512000), bid=1.5764200000000002, ask=1.57837, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 8, 70000), bid=1.57643, ask=1.57837, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 26, 142000), bid=1.57649, ask=1.57837, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 26, 260000), bid=1.5765500000000001, ask=1.57837, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 26, 365000), bid=1.5765500000000001, ask=1.5785900000000002, last=0.0, volume=0.0, flags=4)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 26, 410000), bid=1.5765500000000001, ask=1.57837, last=0.0, volume=0.0, flags=4)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 26, 636000), bid=1.57663, ask=1.57837, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 30, 72000), bid=1.57659, ask=1.57837, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 34, 320000), bid=1.57656, ask=1.57837, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 3, 2, 35, 61000), bid=1.57653, ask=1.57837, last=0.0, volume=0.0, flags=130)
 
audusd_ticks( 61336 )
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 1, 410000), bid=0.71262, ask=0.7128, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 2, 380000), bid=0.71263, ask=0.71282, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 2, 753000), bid=0.71262, ask=0.71282, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 2, 842000), bid=0.71262, ask=0.71281, last=0.0, volume=0.0, flags=4)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 3, 428000), bid=0.71261, ask=0.71279, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 3, 771000), bid=0.71261, ask=0.7128, last=0.0, volume=0.0, flags=4)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 3, 936000), bid=0.71261, ask=0.71279, last=0.0, volume=0.0, flags=4)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 4, 2000), bid=0.7126, ask=0.71279, last=0.0, volume=0.0, flags=130)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 4, 108000), bid=0.71258, ask=0.71276, last=0.0, volume=0.0, flags=134)
MT5Tick(time=datetime.datetime(2019, 4, 1, 13, 0, 4, 186000), bid=0.71257, ask=0.71275, last=0.0, volume=0.0, flags=134)
 
eurusd_rates( 1000 )
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 20), open=1.12162, low=1.12171, high=1.12152, close=1.12153, tick_volume=22, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 21), open=1.12153, low=1.1217, high=1.12153, close=1.12169, tick_volume=35, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 22), open=1.1217, low=1.12179, high=1.1217, close=1.12179, tick_volume=49, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 23), open=1.12178, low=1.12189, high=1.12178, close=1.12187, tick_volume=39, spread=9, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 24), open=1.12188, low=1.12188, high=1.12181, close=1.12185, tick_volume=59, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 25), open=1.12183, low=1.12187, high=1.12173, close=1.12173, tick_volume=47, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 26), open=1.12174, low=1.12179, high=1.12173, close=1.12178, tick_volume=39, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 27), open=1.12178, low=1.12179, high=1.12171, close=1.12171, tick_volume=28, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 28), open=1.12171, low=1.12171, high=1.12166, close=1.12169, tick_volume=39, spread=8, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 4, 22, 29), open=1.12169, low=1.12169, high=1.1215600000000001, close=1.12158, tick_volume=43, spread=8, real_volume=0)
 
eurrub_rates( 1000 )
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 49), open=73.369, low=73.371, high=73.346, close=73.353, tick_volume=96, spread=25, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 50), open=73.353, low=73.367, high=73.352, close=73.367, tick_volume=46, spread=26, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 51), open=73.367, low=73.367, high=73.347, close=73.355, tick_volume=50, spread=21, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 52), open=73.356, low=73.358, high=73.341, close=73.347, tick_volume=51, spread=24, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 53), open=73.347, low=73.35, high=73.334, close=73.334, tick_volume=43, spread=23, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 54), open=73.333, low=73.337, high=73.316, close=73.32, tick_volume=47, spread=25, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 55), open=73.32, low=73.33, high=73.32, close=73.328, tick_volume=45, spread=25, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 56), open=73.328, low=73.334, high=73.322, close=73.326, tick_volume=52, spread=24, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 57), open=73.326, low=73.327, high=73.319, close=73.32, tick_volume=41, spread=26, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 3, 18, 58), open=73.32, low=73.331, high=73.319, close=73.326, tick_volume=39, spread=25, real_volume=0)
 
eurjpy_rates( 1441 )
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 0), open=124.763, low=124.785, high=124.737, close=124.749, tick_volume=236, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 1), open=124.748, low=124.754, high=124.735, close=124.752, tick_volume=165, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 2), open=124.752, low=124.768, high=124.735, close=124.752, tick_volume=162, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 3), open=124.752, low=124.756, high=124.736, close=124.739, tick_volume=122, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 4), open=124.738, low=124.76, high=124.734, close=124.757, tick_volume=164, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 5), open=124.756, low=124.773, high=124.752, close=124.772, tick_volume=150, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 6), open=124.773, low=124.802, high=124.771, close=124.779, tick_volume=180, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 7), open=124.777, low=124.781, high=124.758, close=124.775, tick_volume=201, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 8), open=124.776, low=124.79, high=124.768, close=124.777, tick_volume=189, spread=15, real_volume=0)
MT5Rate(time=datetime.datetime(2019, 4, 1, 13, 9), open=124.777, low=124.803, high=124.774, close=124.794, tick_volume=128, spread=16, real_volume=0)