
When MetaTrader 5 introduced the MetaTrader5 Python Library, it was a game-changer. Finally, the world’s most popular and widely used programming language could be easily integrated into one of the world’s largest retail trading platforms.
The extensive range of Python libraries and widespread availability of powerful computing platforms meant that everyday traders could integrate powerful machine learning libraries and concepts into their day-trading activities. In fact, my company Creative Appnologies, uses it extensively in our analysis programs.
These integrations mean that sometimes you need to modify an open position.
To modify an open position, you need the following:
The order number of the position
Symbol
New Stop Loss value
New Take Profit value
Here’s the code to modify an open position:
# Function to modify an open position
def modify_position(order_number, symbol, new_stop_loss, new_take_profit):
# Create the request
request = {
"action": MetaTrader5.TRADE_ACTION_SLTP,
"symbol": symbol,
"sl": new_stop_loss,
"tp": new_take_profit,
"position": order_number
}
# Send order to MT5
order_result = MetaTrader5.order_send(request)
if order_result[0] == 10009:
return True
else:
return False
No comments yet