MetaTrader 5 Python User Group - how to use Python in Metatrader

 

We are preparing MetaTrader 5 module for Python.

As with the R package, we are testing on simple functions to extract data from a running copy of the terminal.

How you can test how it works:

  1. Install Python 3.7.6 x64 from https://www.python.org/downloads/windows/ with the %PATH% path included

  2. Get the matplotlib package for graph support
    pip install matplotlib
  3. Run the installation of MetaTrader5 package
    pip install MetaTrader5
    
  4. Your computer should be running MetaTrader 5 build 2007 or higher.

  5. Run the test script
    python metatrader5-test.py
  6. The MetaTrader 5 terminal will run in the background, the data will be extracted, shown in the console and a chart will be drawn



Test code:

from datetime import datetime
from MetaTrader5 import *

MT5Initialize()
MT5WaitForTerminal()

print(MT5TerminalInfo())
print(MT5Version())

ticks1 = MT5CopyTicksFrom("EURAUD", datetime(2019,1,28,13), 10000, MT5_COPY_TICKS_ALL)
ticks2 = MT5CopyTicksRange("AUDUSD", datetime(2019,1,27,13), datetime(2019,1,28,13,1), MT5_COPY_TICKS_ALL)

rates1 = MT5CopyRatesFrom("EURUSD", MT5_TIMEFRAME_M1, datetime(2019,1,28,13), 1000)
rates2 = MT5CopyRatesFromPos("EURGBP", MT5_TIMEFRAME_M1, 0, 1000)
rates3 = MT5CopyRatesRange("EURCAD", MT5_TIMEFRAME_M1, datetime(2019,1,27,13), datetime(2019,1,28,13))

MT5Shutdown()

#DATA
print('ticks1(', len(ticks1), ')')
for val in ticks1[:10]: print(val)
print('ticks2(', len(ticks2), ')')
for val in ticks2[:10]: print(val)
print('rates1(', len(rates1), ')')
for val in rates1[:10]: print(val)
print('rates2(', len(rates2), ')')
for val in rates2[:10]: print(val)
print('rates3(', len(rates3), ')')
for val in rates3[:10]: print(val)

#PLOTTING
x_time = [x.time for x in rates2]
y_open = [y.open for y in rates2]
y_close = [y.close for y in rates2]

import matplotlib.pyplot as plt

plt.plot(x_time, y_open, 'g-')
plt.plot(x_time, y_close, 'r-')

plt.show()


We will add more functions later and place the package in a public repository of Python packages, so you can install it normally.

Python Releases for Windows
Python Releases for Windows
  • www.python.org
The official home of the Python Programming Language
Files:
 

An example of a quick drawing of a correlation matrix:

from MetaTrader5 import *
from datetime import date
import pandas as pd 
import matplotlib.pyplot as plt 

# Initializing MT5 connection 
MT5Initialize()
MT5WaitForTerminal()

print(MT5TerminalInfo())
print(MT5Version())

# Create currency watchlist for which correlation matrix is to be plotted
sym = ['EURUSD','GBPUSD','USDJPY','USDCHF','AUDUSD','GBPJPY']

# Copying data to dataframe
d = pd.DataFrame()
for i in sym:
     rates = MT5CopyRatesFromPos(i, MT5_TIMEFRAME_M1, 0, 1000)
     d[i] = [y.close for y in rates]

# Deinitializing MT5 connection
MT5Shutdown()

# Compute Percentage Change
rets = d.pct_change()

# Compute Correlation
corr = rets.corr()

# Plot correlation matrix
plt.figure(figsize=(10, 10))
plt.imshow(corr, cmap='RdYlGn', interpolation='none', aspect='auto')
plt.colorbar()
plt.xticks(range(len(corr)), corr.columns, rotation='vertical')
plt.yticks(range(len(corr)), corr.columns);
plt.suptitle('FOREX Correlations Heat Map', fontsize=15, fontweight='bold')
plt.show()


Files:
 

When a history request comes from Python, is the corresponding hst and tkc generated in Terminal?


I would like to understand how it is possible to work with a thousand symbols simultaneously(AMPGlobalUSA-Demo), for each of which a hundred megabytes' worth of ticks are loaded.

 
If anyone knows, show me how to quickly create a similar MT5 chart from historical data in Python. I.e. interactive, not a static picture.
 
fxsaber:
If anyone knows, show me how to quickly create a semblance of MT5 chart from historical data in Python. I.e. interactive, not a static picture.

https://plot.ly/~pari/67/stock-chart-for-netflix-inc/#//

https://plot.ly/python/candlestick-charts/

plot.ly in general

 
Maxim Dmitrievsky:

https://plot.ly/~pari/67/stock-chart-for-netflix-inc/#//

https://plot.ly/python/candlestick-charts/

plot.ly in general.

I'm a complete zero in Python. I would like to get a motivational push that shows the simplicity and usability of MT5-Python. There will be an example of such a connection, please share the code.

It looks very cool. Would like to see a working code for MT5.
 
fxsaber:

I am a complete zero in Python. Would like to get a motivational push that sees the simplicity and usability of MT5-Python. There will be an example of such a connection, please share the code.

I'm for MO mostly, hardly ever visualise anything. Modern MO stuff is unrealistic to rewrite naturally, only use ready-made. I.e. the obvious motivation for me is this.

 
fxsaber:

It looks very cool. I would like to see a working code for MT5.

can be coded later... in fact there are ready examples in the links

 
Sometimes I need a quick view of the ticks in a more or less convenient form, for that I use the ZoomPrice indicator. But the visualisation is much cooler on the links. If it's very easy to transfer from MT5 to Python and get a cool tick chart - that's not a bad motivation for many people.
 
fxsaber:
Sometimes I need to quickly view the ticks in more or less handy form, I use ZoomPrice indicator for that. But visualization is much better on links. If it is very simple to transfer from MT5 to Python and get a cool tick chart - that's not a bad motivation for many people.

At the moment any info (other than quotes) can be sent/returned via real-time sockets. The only limitation is that they don't work in the tester yet.

 
Maxim Dmitrievsky:

At the moment any info (other than quotes) can be sent/returned via real-time sockets. The only limitation is that it doesn't work in the tester yet.

Unfortunately, without source codes, this feature is just words to me. Very much I don't know, and the start of self-study should probably be something practical to start with, which can be spun right away.

Reason: