using python on metatrader 5

 

Hi dear friends,

I wrote a python script, as far as I know , this kind of script gets some data as input like from_date to to_date.

suppose I want analyses some data between two dates, It will work correctly .

Just I have some question :

1.How can I import my script into Metatrader 5 


2.for instance , If I use the the below code, the script in Metatrader 5 works well or I should run this script in among a loop?

I mean that , the metatrader run my script base on this date or not?

data = pd.DataFrame(mt.copy_rates_range("EURUSD", mt.TIMEFRAME_H4,datetime(2022,1,12),datetime.now()))

I really confuse, is there any one here to help me please..

Documentation on MQL5: Integration / MetaTrader for Python / copy_rates_range
Documentation on MQL5: Integration / MetaTrader for Python / copy_rates_range
  • www.mql5.com
copy_rates_range - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
contactsaeed: I wrote a python script, as far as I know , this kind of script gets some data as input like from_date to to_date. suppose I want analyses some data between two dates, It will work correctly. Just I have some question :

1.How can I import my script into Metatrader 5 

2.for instance , If I use the the below code, the script in Metatrader 5 works well or I should run this script in among a loop?

I mean that , the metatrader run my script base on this date or not? data = pd.DataFrame(mt.copy_rates_range("EURUSD", mt.TIMEFRAME_H4,datetime(2022,1,12),datetime.now())) I really confuse, is there any one here to help me please..

MetaTrader only runs compiled MQL5 code, not Python. You don't import Python code into MetaTrader 5. The Python code runs on its own outside of MetaTrader and uses the API to access the MT5 terminal data and interact with it.

 
Please also read the following forum topic: How to embed python in mql5
MQL5 forum
MQL5 forum
  • www.mql5.com
MQL5: Forum on automated trading systems and strategy testing
 
Fernando Carreiro #:

MetaTrader only runs compiled MQL5 code, not Python. You don't import Python code into MetaTrader 5. The Python code runs on its own outside of MetaTrader and uses the API to access the MT5 terminal data and interact with it.

Thanks for your reply.

But I visit some youtube chanel that run the script python import to script metatrader.

My problem is, I don't know how meta trader run this kind of script. for instance I don't know is meta trader run this script for once or it runs for several times?

 
contactsaeed #: Thanks for your reply. But I visit some youtube chanel that run the script python import to script metatrader. My problem is, I don't know how meta trader run this kind of script. for instance I don't know is meta trader run this script for once or it runs for several times?

I repeat — there is no MetaTrader "import" of Python scripts. The python script runs in Python and it accesses the MT5 terminal via its integration package.

Read the documentation: MetaTrader module for integration with Python

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.
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
 

I have no experience to run python script inside MQL5.  But I am willing to share my experience to run python/Metatrader5.  Here is my python setup: VS Code is my python IDE, MetaTrader5 package is installed.  Most importantly, MT5 terminal has to be up running with successful login

First of all, we have to initialize mt5 for python usage.  Modify my code accordingly:

import MetaTrader5 as mt5

def init_mt5(msg=True):
    path = r'C:\Program Files\MetaTrader 5'
    TERMINAL = r'\terminal64.exe'
    terminal = path + TERMINAL
    if not mt5.initialize(terminal):
        print("initialize() failed, error code =",mt5.last_error())
        quit()
    acct_info = mt5.account_info()._asdict()
    term_info = mt5.terminal_info()._asdict()
    if msg == True:
        print('**********   Summary of Login/Terminal Information  *************')
        print('Login Account: ', acct_info['login'])
        print('Account Name: ', acct_info['name'])
        print('Login Server: ', acct_info['server'])
        print('Account Leverage: ', acct_info['leverage'])
        print('Account Balance: ', acct_info['balance'])
        print('Account Buying Power in Million: ', round(acct_info['leverage'] * acct_info['balance'] / 1_000_000, 3))
        print('Terminal: ', term_info['name'])
        print('Terminal Connected: ', term_info['connected'])
        print('Trade Allowed: ', term_info['trade_allowed'])
        print('Trade API Disabled: ', term_info['tradeapi_disabled'])
        print('************   End of Login/Terminal Information  ***************')

init_mt5()
mt5.shutdown()

Below is the screenshot of output.  After successful initialization, you can do whatever described in MQL5 python reference such as copy_rates.


 

 

Fernando Carreiro #:

I repeat — there is no MetaTrader "import" of Python scripts. The python script runs in Python and it accesses the MT5 terminal via its integration package.

Read the documentation: MetaTrader module for integration with Python


Hi,

On clicking the link, it says "404. The page does not exist"
Could you please confirm the link.


Thanks.

 
@Abhishek Jiyani #: On clicking the link, it says "404. The page does not exist". Could you please confirm the link.
MetaQuotes has changed their documentation. Here is the new link ... Documentation on MQL5: Python Integration
Documentation on MQL5: Python Integration
Documentation on MQL5: Python Integration
  • www.mql5.com
Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:
MetaQuotes has changed their documentation. Here is the new link ... Documentation on MQL5: Python Integration


Could you please help me on How to load an EA from Python script?

 
@Abhishek Jiyani #: Could you please help me on How to load an EA from Python script?

The Python API and MQL5 EAs are two completely different things. The API is not for "loading" EAs. It for coding algorithmic programs equivalent to an EA but in Python.

To have MetaTrader startup with a specific EA, use the normal command line with a configuration — Platform Start - For Advanced Users - Getting Started - MetaTrader 5 Help

 

@Fernando Carreiro

Please help me. I always face this error when compiling the Python script in MT5 terminal. Error after compile.  "import MetaTrader5 as mt5 Python2.py,   from ._core import * __init__.py,   ModuleNotFoundError: No module named 'MetaTrader5._core' __init__.py". Even I have already installed by this command "pip install MetaTrader5". Still have this error.
Fernando Carreiro
Fernando Carreiro
  • 2023.10.01
  • www.mql5.com
Trader's profile
Reason: