Modifying Sl/tp in an open position in MT5 with MetaTrader5 package and python

 

Hey guys. 

I hope you are doing well. I did actually searched a bit and tried a number of solutions but I didn't get any result. I want to modify sl/tp of an OPEN position with python, however, I am not successful! I use MetaTrader5 Package. Any suggestions? examples? documentations? Anything toward answer would be beneficial.

I have tried to use something similar to this

I have been through this

Also this   

this

even this

and I suppose the most relevant one, this


Any suggestions? 

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
shahabkabiri: I did actually searched a bit and tried a number of solutions but I didn't get any result. I want to modify sl/tp of an OPEN position with python, however, I am not successful! I use MetaTrader5 Package. Any suggestions? examples? documentations? Anything toward answer would be beneficial. I have tried to use something similar to thi, I have been through thi, Also this , this, even this, and I suppose the most relevant one, this. Any suggestions? 
Then please show your own code attempt so that we can see what you may be doing wrong. Showing other peoples code does not help identify what YOU are doing wrong compared to their code.
 
Fernando Carreiro #:
Then please show your own code attempt so that we can see what you may be doing wrong. Showing other peoples code does not help identify what YOU are doing wrong compared to their code.

I am sorry. My only intention was to show various solutions. Last thing I tried is like below: I only have one open position and it is a Buy position.

import MetaTrader5 as mt5
if not mt5.initialize():
    print("initialize() failed, error code =", mt5.last_error())
    quit()

symbol = "XAUUSD"
positions=mt5.positions_get(symbol=symbol)
position = positions[0]
request = {
    "action": mt5.TRADE_ACTION_SLTP,
    "position": position.ticket,
    "symbol" : position.symbol,
    "sl": 1800,
}
result_SL = mt5.order_send(request)

thanks in advance

 
shahabkabiri #: I am sorry. My only intention was to show various solutions. Last thing I tried is like below: I only have one open position and it is a Buy position. thanks in advance

Your code and your explanation are incomplete and it seems you forgot to set the Take Profit price in the code.

You say "I didn't get any result" and "I am not successful", but you don't provide any details.

What did NOT WORK! What was the results returned by the function? What was the error code and/or message.

Give more details of the current position being modified? What price is it open at and what is the previous stop-loss and take-profit before modification? What are the Stop Levels and Freeze levels for the symbol? Etc.

We cannot see your setup nor know what data you have, so help us "see", so we can help you.

 

Dear Fernando

Thanks for your reply and time. I was actually answering your questions, that suddenly I got curious about the variable type of the sl and tp, and surprisingly, changing the variables to Float type solved the problem! However, I do appreciate your patient response. 

Wish you the best. 


Ps: I have to mention that as you said, there should be a "tp" since it is removed if nothing is provided. Below is the code that works just fine: 

symbol = "CADJPY"
positions=mt5.positions_get(symbol=symbol)
position = positions[0]
request = {
    "action": mt5.TRADE_ACTION_SLTP,
    "position": position.ticket,
    "symbol" : position.symbol,
    "sl": float(180),
    "tp": float(60)
}
result_SL = mt5.order_send(request)

Also, I was not getting any errors before. The code just wasn't doing anything. 

Thanks again. 

Reason: