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

 

How do I send data from mt5 to a running python script and receive a response?

I guess I have to use stdin and stdout, and specify the process to pass it to.

Does anyone have any code where this is implemented?

p.s. This: https://www.mql5.com/ru/docs/integration/python_metatrader5 has read, and used, but it's not suitable for this task. The data needs to be transferred from the strategy tester as well.

Документация по MQL5: Интеграция / MetaTrader для Python
Документация по MQL5: Интеграция / MetaTrader для Python
  • www.mql5.com
MetaTrader для Python - Интеграция - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 

Problem with the "MetaTrader5 (5.0.35 of 28.07.21) library. After installing and trying to import it, I get the following error

(PressPurtEnv) C:\Users\User>python
Python 3.9.6 (default, Jul 30 2021, 11:42:22) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MetaTrader5 as mt5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\AppData\Local\R-MINI~1\envs\PressPurtEnv\lib\site-packages\MetaTrader5\__init__.py", line 257, in <module>
    from ._core import *
ModuleNotFoundError: No module named 'MetaTrader5._core'
>>> exit()

Returning to 5.0.33 works.

(PressPurtEnv) C:\Users\User>python
Python 3.9.6 (default, Jul 30 2021, 11:42:22) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>  import MetaTrader5 as mt5
  File "<stdin>", line 1
    import MetaTrader5 as mt5
IndentationError: unexpected indent
>>> import MetaTrader5 as mt5
>>> mt5.initialize()
True
>>> print(mt5.version())
(500, 3003, '22 Jul 2021')
>>> print(mt5.terminal_info())
TerminalInfo(community_account=True, community_connection=True, connected=True, dlls_allowed=True, trade_allowed=True, 
tradeapi_disabled=False, email_enabled=False, ftp_enabled=False, notifications_enabled=False, mqid=False, build=3003, 
maxbars=25000, codepage=1251, ping_last=51530, community_balance=1.1, retransmission=0.0, company='MetaQuotes Software Corp.', 
name='MetaTrader 5', language='Russian',path='C:\\Program Files\\MetaTrader 5', 
data_path='C:\\Users\\User\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075', 
commondata_path='C:\\Users\\User\\AppData\\Roaming\\MetaQuotes\\Terminal\\Common')
>>> mt5.shutdown()
True
>>> exit()

Does anybody have this error confirmed?

Checked with Python versions 3.7/3.8/3.9

 
Igor_Gagarin:

How do I send data from mt5 to a running python script and receive a response?

I guess I have to use stdin and stdout, and specify the process to pass it to.

Does anyone have any code where this is implemented?

p.s. This: https://www.mql5.com/ru/docs/integration/python_metatrader5 has read, and used, but it's not suitable for this task. The data needs to be passed from the strategy tester as well.

Python only sees the terminal in this implementation. It sends a request to it and gets an answer. It does not see the MKL program and cannot communicate with it. To interact with the MKL program, we must use additional features. I cannot say anything about interaction with the tester, I have not used it. But it is very likely that it is not possible.

Good luck

 
is not confirmed for me.
import os
import MetaTrader5 as mt5
print (mt5.__version__)
print (dir(mt5))
path_module=os.getcwd()
print ("Start path os.getcwd():",path_module)
ddir=path_module.split("\\")
print (ddir)
mt5.initialize()
print(mt5.version())
print(mt5.terminal_info())
#----------------------------
dir = os.path.dirname(__file__)
print ("Path os.path.dirname(__file__):",dir)
abs_path = os.path.join(dir)
print ("abs_path:",abs_path) 
##variable = open(abs_path) 
#print (variable)
#===============
#help("modules")
print ('end')
5.0.35
['ACCOUNT_MARGIN_MODE_EXCHANGE', 'ACCOUNT_MARGIN_MODE_RETAIL_HEDGING', 'ACCOUNT_MARGIN_MODE_RETAIL_NETTING', 'ACCOUNT_STOPOUT_MODE_MONEY', 'ACCOUNT_STOPOUT_MODE_PERCENT', 'ACCOUNT_TRADE_MODE_CONTEST', 'ACCOUNT_TRADE_MODE_DEMO', 'ACCOUNT_TRADE_MODE_REAL', 'AccountInfo', 'BOOK_TYPE_BUY', 'BOOK_TYPE_BUY_MARKET', 'BOOK_TYPE_SELL', 'BOOK_TYPE_SELL_MARKET', 'BookInfo', 'Buy', 'COPY_TICKS_ALL', 'COPY_TICKS_INFO', 'COPY_TICKS_TRADE', 'Close', 'DAY_OF_WEEK_FRIDAY', 'DAY_OF_WEEK_MONDAY', 'DAY_OF_WEEK_SATURDAY', 'DAY_OF_WEEK_SUNDAY', 'DAY_OF_WEEK_THURSDAY', 'DAY_OF_WEEK_TUESDAY', 'DAY_OF_WEEK_WEDNESDAY', 'DEAL_DIVIDEND', 'DEAL_DIVIDEND_FRANKED', 'DEAL_ENTRY_IN', 'DEAL_ENTRY_INOUT', 'DEAL_ENTRY_OUT', 'DEAL_ENTRY_OUT_BY', 'DEAL_REASON_CLIENT', 'DEAL_REASON_EXPERT', 'DEAL_REASON_MOBILE', 'DEAL_REASON_ROLLOVER', 'DEAL_REASON_SL', 'DEAL_REASON_SO', 'DEAL_REASON_SPLIT', 'DEAL_REASON_TP', 'DEAL_REASON_VMARGIN', 'DEAL_REASON_WEB', 'DEAL_TAX', 'DEAL_TYPE_BALANCE', 'DEAL_TYPE_BONUS', 'DEAL_TYPE_BUY', 'DEAL_TYPE_BUY_CANCELED', 'DEAL_TYPE_CHARGE', 'DEAL_TYPE_COMMISSION', 'DEAL_TYPE_COMMISSION_AGENT_DAILY', 'DEAL_TYPE_COMMISSION_AGENT_MONTHLY', 'DEAL_TYPE_COMMISSION_DAILY', 'DEAL_TYPE_COMMISSION_MONTHLY', 'DEAL_TYPE_CORRECTION', 'DEAL_TYPE_CREDIT', 'DEAL_TYPE_INTEREST', 'DEAL_TYPE_SELL', 'DEAL_TYPE_SELL_CANCELED', 'ORDER_FILLING_FOK', 'ORDER_FILLING_IOC', 'ORDER_FILLING_RETURN', 'ORDER_REASON_CLIENT', 'ORDER_REASON_EXPERT', 'ORDER_REASON_MOBILE', 'ORDER_REASON_SL', 'ORDER_REASON_SO', 'ORDER_REASON_TP', 'ORDER_REASON_WEB', 'ORDER_STATE_CANCELED', 'ORDER_STATE_EXPIRED', 'ORDER_STATE_FILLED', 'ORDER_STATE_PARTIAL', 'ORDER_STATE_PLACED', 'ORDER_STATE_REJECTED', 'ORDER_STATE_REQUEST_ADD', 'ORDER_STATE_REQUEST_CANCEL', 'ORDER_STATE_REQUEST_MODIFY', 'ORDER_STATE_STARTED', 'ORDER_TIME_DAY', 'ORDER_TIME_GTC', 'ORDER_TIME_SPECIFIED', 'ORDER_TIME_SPECIFIED_DAY', 'ORDER_TYPE_BUY', 'ORDER_TYPE_BUY_LIMIT', 'ORDER_TYPE_BUY_STOP', 'ORDER_TYPE_BUY_STOP_LIMIT', 'ORDER_TYPE_CLOSE_BY', 'ORDER_TYPE_SELL', 'ORDER_TYPE_SELL_LIMIT', 'ORDER_TYPE_SELL_STOP', 'ORDER_TYPE_SELL_STOP_LIMIT', 'OrderCheckResult', 'OrderSendResult', 'POSITION_REASON_CLIENT', 'POSITION_REASON_EXPERT', 'POSITION_REASON_MOBILE', 'POSITION_REASON_WEB', 'POSITION_TYPE_BUY', 'POSITION_TYPE_SELL', 'RES_E_AUTH_FAILED', 'RES_E_AUTO_TRADING_DISABLED', 'RES_E_FAIL', 'RES_E_INTERNAL_FAIL', 'RES_E_INTERNAL_FAIL_CONNECT', 'RES_E_INTERNAL_FAIL_INIT', 'RES_E_INTERNAL_FAIL_RECEIVE', 'RES_E_INTERNAL_FAIL_SEND', 'RES_E_INTERNAL_FAIL_TIMEOUT', 'RES_E_INVALID_PARAMS', 'RES_E_INVALID_VERSION', 'RES_E_NOT_FOUND', 'RES_E_NO_MEMORY', 'RES_E_UNSUPPORTED', 'RES_S_OK', 'SYMBOL_CALC_MODE_CFD', 'SYMBOL_CALC_MODE_CFDINDEX', 'SYMBOL_CALC_MODE_CFDLEVERAGE', 'SYMBOL_CALC_MODE_EXCH_BONDS', 'SYMBOL_CALC_MODE_EXCH_BONDS_MOEX', 'SYMBOL_CALC_MODE_EXCH_FUTURES', 'SYMBOL_CALC_MODE_EXCH_OPTIONS', 'SYMBOL_CALC_MODE_EXCH_OPTIONS_MARGIN', 'SYMBOL_CALC_MODE_EXCH_STOCKS', 'SYMBOL_CALC_MODE_EXCH_STOCKS_MOEX', 'SYMBOL_CALC_MODE_FOREX', 'SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE', 'SYMBOL_CALC_MODE_FUTURES', 'SYMBOL_CALC_MODE_SERV_COLLATERAL', 'SYMBOL_CHART_MODE_BID', 'SYMBOL_CHART_MODE_LAST', 'SYMBOL_OPTION_MODE_AMERICAN', 'SYMBOL_OPTION_MODE_EUROPEAN', 'SYMBOL_OPTION_RIGHT_CALL', 'SYMBOL_OPTION_RIGHT_PUT', 'SYMBOL_ORDERS_DAILY', 'SYMBOL_ORDERS_DAILY_NO_STOPS', 'SYMBOL_ORDERS_GTC', 'SYMBOL_SWAP_MODE_CURRENCY_DEPOSIT', 'SYMBOL_SWAP_MODE_CURRENCY_MARGIN', 'SYMBOL_SWAP_MODE_CURRENCY_SYMBOL', 'SYMBOL_SWAP_MODE_DISABLED', 'SYMBOL_SWAP_MODE_INTEREST_CURRENT', 'SYMBOL_SWAP_MODE_INTEREST_OPEN', 'SYMBOL_SWAP_MODE_POINTS', 'SYMBOL_SWAP_MODE_REOPEN_BID', 'SYMBOL_SWAP_MODE_REOPEN_CURRENT', 'SYMBOL_TRADE_EXECUTION_EXCHANGE', 'SYMBOL_TRADE_EXECUTION_INSTANT', 'SYMBOL_TRADE_EXECUTION_MARKET', 'SYMBOL_TRADE_EXECUTION_REQUEST', 'SYMBOL_TRADE_MODE_CLOSEONLY', 'SYMBOL_TRADE_MODE_DISABLED', 'SYMBOL_TRADE_MODE_FULL', 'SYMBOL_TRADE_MODE_LONGONLY', 'SYMBOL_TRADE_MODE_SHORTONLY', 'Sell', 'SymbolInfo', 'TICK_FLAG_ASK', 'TICK_FLAG_BID', 'TICK_FLAG_BUY', 'TICK_FLAG_LAST', 'TICK_FLAG_SELL', 'TICK_FLAG_VOLUME', 'TIMEFRAME_D1', 'TIMEFRAME_H1', 'TIMEFRAME_H12', 'TIMEFRAME_H2', 'TIMEFRAME_H3', 'TIMEFRAME_H4', 'TIMEFRAME_H6', 'TIMEFRAME_H8', 'TIMEFRAME_M1', 'TIMEFRAME_M10', 'TIMEFRAME_M12', 'TIMEFRAME_M15', 'TIMEFRAME_M2', 'TIMEFRAME_M20', 'TIMEFRAME_M3', 'TIMEFRAME_M30', 'TIMEFRAME_M4', 'TIMEFRAME_M5', 'TIMEFRAME_M6', 'TIMEFRAME_MN1', 'TIMEFRAME_W1', 'TRADE_ACTION_CLOSE_BY', 'TRADE_ACTION_DEAL', 'TRADE_ACTION_MODIFY', 'TRADE_ACTION_PENDING', 'TRADE_ACTION_REMOVE', 'TRADE_ACTION_SLTP', 'TRADE_RETCODE_CANCEL','TRADE_RETCODE_CLIENT_DISABLES_AT', 'TRADE_RETCODE_CLOSE_ONLY', 'TRADE_RETCODE_CLOSE_ORDER_EXIST', 'TRADE_RETCODE_CONNECTION', 'TRADE_RETCODE_DONE', 'TRADE_RETCODE_DONE_PARTIAL', 'TRADE_RETCODE_ERROR', 'TRADE_RETCODE_FIFO_CLOSE', 'TRADE_RETCODE_FROZEN', 'TRADE_RETCODE_INVALID', 'TRADE_RETCODE_INVALID_CLOSE_VOLUME', 'TRADE_RETCODE_INVALID_EXPIRATION', 'TRADE_RETCODE_INVALID_FILL', 'TRADE_RETCODE_INVALID_ORDER', 'TRADE_RETCODE_INVALID_PRICE', 'TRADE_RETCODE_INVALID_STOPS', 'TRADE_RETCODE_INVALID_VOLUME', 'TRADE_RETCODE_LIMIT_ORDERS', 'TRADE_RETCODE_LIMIT_POSITIONS', 'TRADE_RETCODE_LIMIT_VOLUME', 'TRADE_RETCODE_LOCKED', 'TRADE_RETCODE_LONG_ONLY', 'TRADE_RETCODE_MARKET_CLOSED',
'TRADE_RETCODE_NO_CHANGES', 'TRADE_RETCODE_NO_MONEY', 'TRADE_RETCODE_ONLY_REAL', 'TRADE_RETCODE_ORDER_CHANGED', 'TRADE_RETCODE_PLACED', 'TRADE_RETCODE_POSITION_CLOSED', 'TRADE_RETCODE_PRICE_CHANGED', 'TRADE_RETCODE_PRICE_OFF', 'TRADE_RETCODE_REJECT', 'TRADE_RETCODE_REJECT_CANCEL', 'TRADE_RETCODE_REQUOTE', 'TRADE_RETCODE_SERVER_DISABLES_AT', 'TRADE_RETCODE_SHORT_ONLY', 'TRADE_RETCODE_TIMEOUT', 'TRADE_RETCODE_TOO_MANY_REQUESTS', 'TRADE_RETCODE_TRADE_DISABLED', 'TerminalInfo', 'Tick', 'TradeDeal', 'TradeOrder', 'TradePosition', 'TradeRequest', '_RawOrder', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_core', 'account_info', 'copy_rates_from', 'copy_rates_from_pos', 'copy_rates_range', 'copy_ticks_from', 'copy_ticks_range', 'history_deals_get', 'history_deals_total', 'history_orders_get', 'history_orders_total', 'initialize', 'last_error', 'login', 'market_book_add', 'market_book_get', 'market_book_release', 'order_calc_margin', 'order_calc_profit', 'order_check', 'order_send', 'orders_get', 'orders_total', 'positions_get', 'positions_total', 'shutdown', 'symbol_info', 'symbol_info_tick', 'symbol_select', 'symbols_get', 'symbols_total', 'terminal_info', 'version']
Start path os.getcwd(): C:\Users\Alex\AppData\Roaming\MetaQuotes\Terminal\2BBE125AB050CAF856FF83F865B275C2\MQL5\Scripts\Python
['C:', 'Users', 'Alex', 'AppData', 'Roaming', 'MetaQuotes', 'Terminal', '2BBE125AB050CAF856FF83F865B275C2', 'MQL5', 'Scripts', 'Python']
(500, 3003, '22 Jul 2021')
TerminalInfo(community_account=True, community_connection=True, connected=True, dlls_allowed=False, trade_allowed=False, tradeapi_disabled=False, email_enabled=False, ftp_enabled=False, notifications_enabled=False, mqid=False, build=3003, maxbars=1000, codepage=1251, ping_last=124758, community_balance=3.69, retransmission=0.0, company='ICM Capital Ltd.', name='ICM Capital MetaTrader 5', language='Russian', path='C:\\Program Files\\\ICM Capital MetaTrader 5', ...)
Path os.path.dirname(__file__): c:\Users\Alex\AppData\Roaming\MetaQuotes\Terminal\2BBE125AB050CAF856FF83F865B275C2\MQL5\Scripts\Python
abs_path: c:\Users\Alex\AppData\Roaming\MetaQuotes\Terminal\2BBE125AB050CAF856FF83F865B275C2\MQL5\Scripts\Python
end


 
Vladimir Perervenko:

It does not see the MCL programme and cannot communicate with it. To communicate with the MCL program, you have to use additional features.

Anamed channel is fine by me, but some people do it on a socket. it's a matter of habit...

 
Vladimir Perervenko:

Problem with the "MetaTrader5 (5.0.35 of 28.07.21) library. After installing and trying to import it, I get the following error

Returning to 5.0.33 works.

Does anybody have this error confirmed?

Checked with Python versions 3.7/3.8/3.9

Upgraded to 5.0.35.

No errors.

Connected to pydev debugger (build 191.6605.12)
MetaTrader5 package author:  MetaQuotes Software Corp.
MetaTrader5 package version:  5.0.35
Backend TkAgg is interactive backend. Turning interactive mode on.
TerminalInfo(community_account=True, community_connection=True, connected=True, dlls_allowed=False, trade_allowed=False, trad...
(500, 3010, '02 Aug 2021')
 
Rashid Umarov:

Upgraded to 5.0.35.

No errors.

Latest beta of terminal 3007 (at least for me). Not the point, it's not important for the question.

Trying to update.

(r-reticulate) C:\Users\User>pip install --upgrade MetaTrader5
Requirement already satisfied: MetaTrader5 in c:\users\user\appdata\local\r-miniconda\envs\r-reticulate\lib\site-packages (5.0.34)
Collecting MetaTrader5
  Using cached MetaTrader5-5.0.35-cp38-cp38-win_amd64.whl (124 kB)
Requirement already satisfied: numpy>=1.7 in c:\users\user\appdata\local\r-miniconda\envs\r-reticulate\lib\site-packages 
(from MetaTrader5) (1.19.5)
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 
'c:\\users\\user\\appdata\\local\\r-miniconda\\envs\\r-reticulate\\lib\\site-packages\\numpy-1.19.5.dist-info\\METADATA'

Can you tell me what this means?

 
Vladimir Perervenko:

The latest beta of terminal 3007 (at least for me). Not the point, it's irrelevant to the question.

Trying to update

Can you tell me what this means?

According to your log - there is no such file or directory

ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 
'c:\\users\\user\\appdata\\local\\r-miniconda\\envs\\r-reticulate\\lib\\site-packages\\numpy-1.19.5.dist-info\\METADATA'
 
Rashid Umarov:

According to your log - there is no such file or directory

i.e. no numpy==1.19.5?

 
Vladimir Perervenko:

i.e. no numpy==1.9.5?

There doesn't seem to be a folder

"c:\users\\\user\\appdata\\local\r-miniconda\\\envs\\r-reticulate\\\lib\site-packages\numpy-1.19.5.dist-info\\\METADATA'