MetaTrader 5 Python用户组 - 如何在Metatrader中使用Python - 页 86

 

我如何从mt5发送数据到一个正在运行的python脚本并收到响应?

我想我必须使用stdin和stdout,并指定将其传递给哪个进程。

有谁有实现这一点的代码吗?

p.s. 这个:https://www.mql5.com/ru/docs/integration/python_metatrader5 读过,也用过,但不适合这个任务。数据也需要从策略测试器中转移。

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

关于 "MetaTrader5(5.0.35 of 28.07.21)库的问题。在安装并试图导入 后,我得到以下错误

(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()

返回到5.0.33的工作。

(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()

有没有人确认过这个错误?

用Python 3.7/3.8/3.9版本进行了检查

 
Igor_Gagarin:

我如何从mt5发送数据到一个正在运行的python脚本并收到响应?

我想我必须使用stdin和stdout,并指定将其传递给哪个进程。

有谁有实现这一点的代码吗?

p.s. 这个:https://www.mql5.com/ru/docs/integration/python_metatrader5 读过,也用过,但不适合这个任务。数据也需要从策略测试器中传递。

在这个实现中,Python只看到终端。它向它发出请求并得到答复。它没有看到MKL程序,不能与之通信。为了与MKL程序互动,我们必须使用额外的功能。我不能说什么与测试器的互动,我没有使用它。但这很可能是不可能的。

祝好运

 
对我来说还没有得到证实。
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', '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', '终端信息' 。'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','position_get', 'position_total', 'shutdown', 'symbol_info', 'symbol_info_tick', 'symbol_select', 'symbols_get', 'symbols_total', 'terminal_info', 'version' ]
开始路径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
结束


 
Vladimir Perervenko:

它看不到MCL程序,不能与之交流。为了与MCL程序通信,你必须使用额外的功能。

一个命名的通道 对我来说很好,但有些人在插座上做。这是一个习惯问题......

 
Vladimir Perervenko:

关于 "MetaTrader5(5.0.35 of 28.07.21)库的问题。在安装并试图导入 后,我得到以下错误

返回到5.0.33的工作。

有没有人确认过这个错误?

用Python 3.7/3.8/3.9版本进行了检查

已升级到5.0.35。

没有错误。

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:

已升级到5.0.35。

没有错误。

终端3007的最新测试版(至少对我而言)。不是重点,这对问题不重要。

试图更新。

(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'

你能告诉我这是什么意思吗?

 
Vladimir Perervenko:

终端3007的最新测试版(至少对我而言)。不是重点,这与问题无关。

试图更新

你能告诉我这是什么意思吗?

根据你的日志 - 没有这样的文件或目录

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:

根据你的日志 - 没有这样的文件或目录

即没有numpy==1.19.5?

 
Vladimir Perervenko:

即没有numpy==1.9.5?

似乎没有一个文件夹

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