简体   繁体   English

通过 MetaTrader5 python 模块发送订单以打开 position 并且没有任何反应

[英]sending order to open a position via MetaTrader5 python module and nothing happens

I followed Metatrader5 python documentation and this answer in stack overflow我关注了 Metatrader5 python 文档和堆栈溢出中的这个答案

I try to open a sell position:我尝试开卖position:

 import MetaTrader5 as mt5 ea_magic_number = 9986989 # if you want to give every bot a unique identifier def get_info(symbol): '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5symbolinfo_py ''' # get symbol properties info=mt5.symbol_info(symbol) return info def open_trade(action, symbol, lot, sl_points, tp_points, deviation): '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py ''' # prepare the buy request structure symbol_info = get_info(symbol) if action == 'buy': trade_type = mt5.ORDER_TYPE_BUY price = mt5.symbol_info_tick(symbol).ask elif action =='sell': trade_type = mt5.ORDER_TYPE_SELL price = mt5.symbol_info_tick(symbol).bid point = mt5.symbol_info(symbol).point buy_request = { "action": mt5.TRADE_ACTION_DEAL, "symbol": symbol, "volume": lot, "type": trade_type, "price": price, "sl": price - sl_points * point, "tp": price + tp_points * point, "deviation": deviation, "magic": ea_magic_number, "comment": "sent by python", "type_time": mt5.ORDER_TIME_GTC, # good till cancelled "type_filling": mt5.ORDER_FILLING_RETURN, } # send a trading request result = mt5.order_send(buy_request) return result, buy_request def close_trade(action, buy_request, result, deviation): '''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py ''' # create a close request symbol = buy_request['symbol'] if action == 'buy': trade_type = mt5.ORDER_TYPE_BUY price = mt5.symbol_info_tick(symbol).ask elif action =='sell': trade_type = mt5.ORDER_TYPE_SELL price = mt5.symbol_info_tick(symbol).bid position_id=result.order lot = buy_request['volume'] close_request={ "action": mt5.TRADE_ACTION_DEAL, "symbol": symbol, "volume": lot, "type": mt5.ORDER_TYPE_SELL, "position": position_id, "price": price, "deviation": deviation, "magic": ea_magic_number, "comment": "python script close", "type_time": mt5.ORDER_TIME_GTC, # good till cancelled "type_filling": mt5.ORDER_FILLING_RETURN, } # send a close request result=mt5.order_send(close_request) # This is how I would execute the order result, buy_request = open_trade('buy', 'USDJPY', 0.1, 50, 50, 10) close_trade('sell', buy_request, result, 10)

Nothing happens and there is no reaction in applications terminal.没有任何反应,应用程序终端没有任何反应。 I also check Trade and History section in Metatrader5 to find some related information but I find out nothing.我还检查了 Metatrader5 中的交易和历史部分以查找一些相关信息,但我一无所获。

how can I monitor the logs in Metatrader5 to debug the code and resolve the problem?如何监控 Metatrader5 中的日志以调试代码并解决问题?

In MetaTrader algo trading must be ON in order to make a buy/sell position.在 MetaTrader 中,必须开启算法交易才能进行买入/卖出 position。

在此处输入图像描述

在此处输入图像描述

Just for people that reach this topic and for whom the Algo trading button was ON and nothing was happening when sending orders.仅适用于达到此主题并且 Algo 交易按钮已打开且在发送订单时没有发生任何事情的人。

In my case, it was because I was setting the 'volume' of the request as an integer, and it must be a float.就我而言,这是因为我将请求的“音量”设置为 integer,它必须是浮点数。 No error was being shown, no sign of life, order_send(...) returned None and that was it.没有显示错误,没有生命迹象, order_send(...) 返回 None 就是这样。 Just cast'ed the incoming value to float and it worked.刚刚将传入的值转换为浮动,它就起作用了。

This is not the case of the question author, but I hope it helps some venturing soul in the future.这不是问题作者的情况,但我希望它对将来有冒险精神的人有所帮助。

I have changed:我改变了:

"type_filling": mt5.ORDER_FILLING_RETURN

to:至:

"type_filling": mt5.ORDER_FILLING_IOC

and then It works for me.然后它对我有用。

I'm using python version 3.10我正在使用 python 版本 3.10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM