NameError: name 'MT5Initialize' is not defined - Python integration?

 

Greetings,

Hope this is is correct section to post. I am trying to get the Python integration to work with Metatrader 5, however I am getting this error when execute the pythonscript-

NameError: name 'MT5Initialize' is not defined


Have made exactly like the tutorial fond here - https://www.mql5.com/en/docs/integration/python_metatrader5

Even tried to add path to MetaTrader5 like this - 

# connect to MetaTrader 5
MT5Initialize(path="C:\\Program Files\\MetaTrader 5\\terminal64.exe")


Anyone who can help me out? I am jumping up and down of excitement when I found the Python pip package...

Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
  • www.mql5.com
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...
 
Someone please help on this. Previously was okay. I just update the package today and it show the same error as fxchampion888 stated.
 
Tried with version 5.0.14 up to 5.0.16. All shown MT5Initialize is not defined.
Tried with version 5.0.11 it show IPC recv failed.
 

I found this announcement from MetaQuotes yesterday (I am not sure how it may be related to this error ... but just in case) -

Forum on trading, automated trading systems and testing trading strategies

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

Renat Fatkhullin , 2020/02/12 09:20

Since version 5.0.15, the Python library works in a new format that is not compatible with the old format and requires the latest beta terminal.

We’ll publish updated documentation and examples soon.


 
Sergey Golubev:

I found this announcement from MetaQuotes yesterday (I am not sure how it may be related to this error ... but just in case) -


Thanks Sergey. 

Tried to find from the package folder. Found the new function naming. :)

 
Guan Chuan Lee:

Thanks Sergey. 

Tried to find from the package folder. Found the new function naming. :)

Thanks for sharing! Where exactly did you found new naming?
 

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

MetaTrader 5 Python User Group - как использовать Python в Метатрейдере

Renat Fatkhullin, 2020.02.13 17:21

Новая версия MetaTrader 5 for Python 5.0.18 и бета версия MetaTrader 5 build 2319:

  • бета МТ5 скачивается через Справка -> Проверить бета версию
  • питон библиотека:
    pip install --upgrade metatrader5
    

Так как все апи изменилось, то старые примеры больше не работают.

Вот новый набор функций:

initialize(path=None)                              Establish connection with the MetaTrader 5 Terminal
wait()                                             Wait for the MetaTrader 5 Terminal to connect to a broker's server
shutdown()                                         Disconnect from the MetaTrader 5 Terminal

version()                                          Get the MetaTrader 5 Terminal version
terminal_info()                                    Get the parameters of the MetaTrader 5 terminal
account_info()                                     Returns information of current account

copy_ticks_from(symbol, from, count, flags)                Get ticks starting from the specific date
copy_ticks_range(symbol, from, to, flags)                  Get ticks from the specified period
copy_rates_from(symbol, timeframe, from, count)            Get bars starting from the specific date
copy_rates_from_pos(symbol, timeframe, start_pos, count)   Get bars starting from the specified position
copy_rates_range(symbol, timeframe, date_from, date_to)    Get bars from the specified period

positions_total()                                          Returns the number of open positions
positions_get([symbol=\"SYMBOL\"],[ticket=TICKET])         Returns all open positions, can be filtered by symbol or ticket

orders_total()                                             Returns the number of orders
orders_get([symbol=\"SYMBOL\"],[ticket=TICKET])            Returns all orders, can be filtered by symbol or ticket

history_orders_total(from, to)                             Returns the number of orders in selected range from the history
history_orders_get(from, to)                               Returns orders in selected range from the history or filtered by position id, ticket

history_deals_total(from, to)                              Returns the number of deals in selected range from the history
history_deals_get(from, to)                                Returns deals in selected range from the history or filtered by position id, ticket

order_check(request)                                                Checks if there are enough funds to execute the required trade operation
order_send(request)                                                 Sends trade requests to a server
order_calc_margin(action, symbol, volume, price)                    Calculates the margin required for the specified order
order_calc_profit(action, symbol, volume, price_open, price_close)  Calculates the profit for the current account, in the current market conditions, based on the parameters passed

symbol_info(symbol)                                        Returns full information for a specified symbol
symbol_info_tick(symbol)                                   Returns current prices of a specified symbol
symbol_select(symbol,[enable])                             Selects a symbol in the Market Watch window or removes a symbol from the window

Пример:

import MetaTrader5 as mt5
import time

mt5.initialize()
mt5.wait()

dev = 0.00010;
symbol = "EURUSD"
buy_price = 0

mt5.symbol_select(symbol)

time.sleep(1)
p = mt5.symbol_info_tick(symbol)
prev_price = p.ask

while True:
    p = mt5.symbol_info_tick(symbol)
    print(p.bid,'/',p.ask)
    
    if p.ask > prev_price and buy_price == 0:
            print("Buy ", p.ask)
            r = mt5.Buy(symbol, 0.01)
            if r.retcode == mt5.TRADE_RETCODE_DONE:
                buy_price = p.ask;
    elif buy_price > 0 and p.ask + dev < buy_price:
        print("Buy(close) ", p.bid)
        mt5.Close(symbol)
        buy_price = 0

    prev_price = p.ask
    time.sleep(1)

mt5.shutdown()

 
Guan Chuan Lee:
Tried with version 5.0.14 up to 5.0.16. All shown MT5Initialize is not defined.
Tried with version 5.0.11 it show IPC recv failed.

Hi,I've same issue of "IPC recv failed".

Already tried the new method initialize(), it doesn't work.

Would you mind to share with me the solution for this issue?


Thanks

 
traderyinfengxu:

Hi,I've same issue of "IPC recv failed".

Already tried the new method initialize(), it doesn't work.

Would you mind to share with me the solution for this issue?


Thanks

to address the "IPC recv failed" I install the beta version of metatrader5:

  • beta MT5 is downloaded via Help -> Check beta version
  • python library:


it worked for me

 
fxchampion888:

Greetings,

Hope this is is correct section to post. I am trying to get the Python integration to work with Metatrader 5, however I am getting this error when execute the pythonscript-


Have made exactly like the tutorial fond here - https://www.mql5.com/en/docs/integration/python_metatrader5

Even tried to add path to MetaTrader5 like this - 


Anyone who can help me out? I am jumping up and down of excitement when I found the Python pip package...

Hi,

Here are the new docs: https://www.mql5.com/en/docs/integration/python_metatrader5


Best regards

Documentation on MQL5: Integration / MetaTrader for Python
Documentation on MQL5: Integration / MetaTrader for Python
  • www.mql5.com
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...
 
fxchampion888:

NameError: name 'MT5Initialize' is not defined


Python knows the purposes of certain names (ex. built-in functions ). Other names are defined within the program (ex. variables). If Python encounters a name that it doesn't recognize, you'll probably get NameError: global name 'xx' is not defined error. In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.

Reason: