从Python获得的MT5订单有多接近?
我试图通过MT5Python API将命令从python脚本发送到我的MT5终端 我可以打开一个位置,但如何从Python mt5 API关闭它 我打开一张“买”票,它是这样的:从Python获得的MT5订单有多接近?,python,mql5,metatrader5,Python,Mql5,Metatrader5,我试图通过MT5Python API将命令从python脚本发送到我的MT5终端 我可以打开一个位置,但如何从Python mt5 API关闭它 我打开一张“买”票,它是这样的: import MetaTrader5 as mt5 lot = 0.1 request = { "action": mt5.TRADE_ACTION_DEAL, "symbol": symbol, "volume": lot, "type": mt5.ORDER_TYPE_BUY, "price": price, .
import MetaTrader5 as mt5
lot = 0.1
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": mt5.ORDER_TYPE_BUY,
"price": price,
.....
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
enter copoint = mt5.symbol_info(symbol).point
但是什么命令和形式来发送关闭
查找更多信息:
非常感谢 顺序的结构是相同的,只是在关闭期间,必须指定从第一个顺序返回的结果对象中获得的位置id。很明显,如果成交一个买入订单,那么成交请求就是一个卖出订单。 下面是您可以用来进行买卖的代码
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": trade_type,
"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)
顺序的结构是相同的,只是在关闭期间,必须指定从第一个顺序返回的结果对象中获得的位置\u id。很明显,如果成交一个买入订单,那么成交请求就是一个卖出订单。 下面是您可以用来进行买卖的代码
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": trade_type,
"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)