簡體   English   中英

通過 MetaTrader5 python 模塊發送訂單以打開 position 並且沒有任何反應

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

我關注了 Metatrader5 python 文檔和堆棧溢出中的這個答案

我嘗試開賣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)

沒有任何反應,應用程序終端沒有任何反應。 我還檢查了 Metatrader5 中的交易和歷史部分以查找一些相關信息,但我一無所獲。

如何監控 Metatrader5 中的日志以調試代碼並解決問題?

在 MetaTrader 中,必須開啟算法交易才能進行買入/賣出 position。

在此處輸入圖像描述

在此處輸入圖像描述

僅適用於達到此主題並且 Algo 交易按鈕已打開且在發送訂單時沒有發生任何事情的人。

就我而言,這是因為我將請求的“音量”設置為 integer,它必須是浮點數。 沒有顯示錯誤,沒有生命跡象, order_send(...) 返回 None 就是這樣。 剛剛將傳入的值轉換為浮動,它就起作用了。

這不是問題作者的情況,但我希望它對將來有冒險精神的人有所幫助。

我改變了:

"type_filling": mt5.ORDER_FILLING_RETURN

至:

"type_filling": mt5.ORDER_FILLING_IOC

然后它對我有用。

我正在使用 python 版本 3.10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM