why does this trailing stop doesn't work (python)

 

hi, I have a bot, and would like to make trailing stops, but for some reason, this script doesn't work. Can you please check it?


import MetaTrader5 as mt5

# connect to MT5 terminal
user=111111111
key="contraseña"
server="RoboForex-Pro"
        # Function to start Meta Trader 5 (MT5)
def start_mt5(username, password, server):
            # Ensure that all variables are the correct type
            uname = int(user) # Username must be an int
            pword = str(key) # Password must be a string
            trading_server = str(server) # Server must be a string
            filepath = str("C:/Program Files/MetaTrader 5/terminal64.exe") # Filepath must be a string

            # Attempt to start MT5
            if mt5.initialize(login=uname, password=pword, server=trading_server, path=filepath):
                # Login to MT5
                if mt5.login(login=uname, password=pword, server=trading_server):
                    return True
                else:
                    print("Login Fail")
                    quit()
                    return PermissionError
            else:
                print("MT5 Initialization Failed")
                quit()
                return ConnectionAbortedError
                    
start_mt5(user, key, server)

# define symbol and order parameters
symbols = ["USDJPY", "GBPJPY", "EURUSD", "GBPUSD", "USDCHF", "USDCAD","AUDCAD","AUDJPY", "AUDUSD", "AUDNZD", "AUDCHF", "CHFJPY", "EURGBP", "EURCHF", "EURNZD", "EURCAD", "EURAUD", "GBPCHF", "GBPJPY", "CADCHF"] 
trailing_stop = 0.0010 # trailing stop 10 pips below current market price
for symbol in symbols:
    # get list of open buy orders for the specified symbol
    orders = mt5.orders_get()#symbol=symbol, type=mt5.ORDER_TYPE_BUY, magic=0)
    print(orders)

    if not orders:
        print("no open buy orders found")
    else:
        for order in orders:
            order_id = order.ticket
            order_price = order.price_open
            order_stop_loss = order.sl
            order_take_profit = order.tp
            
            # calculate initial stop loss and take profit levels
            stop_loss = order_stop_loss#order_price - 0.0010 # initial stop loss 10 pips below entry price
            take_profit = order_take_profit#order_price + 0.0020 # take profit 20 pips above entry price
            
            # set initial stop loss and take profit levels
            modify_request = {
                "action": mt5.TRADE_ACTION_SLTP,
                "order": order_id,
                "sl": stop_loss,
                "tp": take_profit,
                "magic": order.magic,
                "comment": "Python trailing stop",
            }
            result = mt5.order_modify(modify_request)
            if result.retcode != mt5.TRADE_RETCODE_DONE:
                print("order_modify failed with error:", result.comment)
                continue
            
            while True:
                # get current market price
                market_price = mt5.symbol_info_tick(symbol).bid
                
                # set trailing stop
                new_stop_loss = market_price - trailing_stop
                modify_request = {
                    "action": mt5.TRADE_ACTION_SLTP,
                    "order": order_id,
                    "sl": new_stop_loss,
                    "tp": take_profit,
                    "magic": order.magic,
                    "comment": "Python trailing stop",
                }
                result = mt5.order_modify(modify_request)
                if result.retcode == mt5.TRADE_RETCODE_DONE:
                    print("trailing stop updated for order {}: {}".format(order_id, result))
                else:
                    print("order_modify failed with error:", result.comment)
                    break
                
                # wait for 5 seconds before checking again
                mt5.sleep(5000)
 
Javier Santiago Gaston De Iriarte Cabrera:

hi, I have a bot, and would like to make trailing stops, but for some reason, this script doesn't work. Can you please check it?


What that means "doesn't work" ?

If you want help, you should at least provide the relevant data to explain your issue. Screenshot, logs, etc...

Reason: