Python Integration

Muhammad Hammad Maqsood  

I'm trying to integrate Python into MetaEditor5 following the exact same steps from the documentation https://www.mql5.com/en/docs/integration/python_metatrader5.
I've tried with Python v3.9 and v3.7. 
All the pip install commands execute successfully.

However, when i try to execute the following script from the documentation, within a New Python Script option in MetaEditor5, it fails.
I've integrated the compiler in the MetaEditor5 options by copying the address of the python executable file.

On compilation it doesn't give any errors, However it prints the following in the journals tab

Python "C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.7\python.exe" -u "C:\Users\username\AppData\Roaming\MetaQuotes\Terminal\terminalnumber\MQL5\Scripts\Python Script.py" EURUSD 5 failed

But still creates a .py file.

Running this script in MT5 gives the following message in Journals Tab

"python process thread create error [The system cannot find the file specified. (2)]"
and i have to forcible terminate the execution of the script.

Any help on what could be causing the error!


from datetime import datetime
import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import MetaTrader5 as mt5
 
# connect to MetaTrader 5
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()
 
# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())
 
# request 1000 ticks from EURAUD
euraud_ticks = mt5.copy_ticks_from("EURAUD", datetime(2020,1,28,13), 1000, mt5.COPY_TICKS_ALL)
# request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(2020,1,27,13), datetime(2020,1,28,13), mt5.COPY_TICKS_ALL)
 
# get bars from different symbols in a number of ways
eurusd_rates = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M1, datetime(2020,1,28,13), 1000)
eurgbp_rates = mt5.copy_rates_from_pos("EURGBP", mt5.TIMEFRAME_M1, 0, 1000)
eurcad_rates = mt5.copy_rates_range("EURCAD", mt5.TIMEFRAME_M1, datetime(2020,1,27,13), datetime(2020,1,28,13))
 
# shut down connection to MetaTrader 5
mt5.shutdown()
 
#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('eurgbp_rates(', len(eurgbp_rates), ')')
for val in eurgbp_rates[:10]: print(val)
 
print('eurcad_rates(', len(eurcad_rates), ')')
for val in eurcad_rates[:10]: print(val)
 
#PLOT
# create DataFrame out of the obtained data
ticks_frame = pd.DataFrame(euraud_ticks)
# convert time in seconds into the datetime format
ticks_frame['time']=pd.to_datetime(ticks_frame['time'], unit='s')
# display ticks on the chart
plt.plot(ticks_frame['time'], ticks_frame['ask'], 'r-', label='ask')
plt.plot(ticks_frame['time'], ticks_frame['bid'], 'b-', label='bid')
 
# display the legends
plt.legend(loc='upper left')
 
# add the header
plt.title('EURAUD ticks')
 
# display the chart
plt.show()
Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
  • www.mql5.com
MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
primehaxor  
I am also having the same issue. No errors during the compilation, but when I tried to attach to the chart I see the same message above.
Muhammad Hammad Maqsood  
primehaxor #:
I am also having the same issue. No errors during the compilation, but when I tried to attach to the chart I see the same message above.
Make the script in some python ide such as spyder. Then it gives proper error messages.
Switching to spyder helped me get it working as all errors are clearly given in spyder.

The Mql5 IDE does not give python errors.
Muhammad Hammad Maqsood  
primehaxor #:
I just solved it, basically what I was doing wrong was providing the Python.exe inside the compiler option, just leave the basedir and it is enough, btw I'm using Python 3.8.0, working like a charm now!
Okay.
The Messiah  
primehaxor #:
I just solved it, basically what I was doing wrong was providing the Python.exe inside the compiler option, just leave the basedir and it is enough, btw I'm using Python 3.8.0, working like a charm now!
Hi. I'm on python 3.10. My scripts are not running on MT5 once I've dragged them onto the chart. The steps I took are - 1. Create new document.
2. Python script
3.name it
4.tick MetaTrader5 library 
5. Finish - I attached the scripts from the mql5 website and when I drag the script to the chart it doesn't work. "Python process thread create error".( system could not find the file specified. (2)) What am I doing wrong?
Fernando Carreiro  
@The Messiah #:
Hi. I'm on python 3.10. My scripts are not running on MT5 once I've dragged them onto the chart. The steps I took are - 1. Create new document.
2. Python script
3.name it
4.tick MetaTrader5 library 
5. Finish - I attached the scripts from the mql5 website and when I drag the script to the chart it doesn't work. "Python process thread create error".( system could not find the file specified. (2)) What am I doing wrong?
You can't run Python scripts on a MetaTrader chart. It runs externally and "calls" MetaTrader (like a library or service) to have it process the requests. It does not run on the MetaTrader terminal. That is reserved only for MQL programs.