Implementing trailingstop

 
Hello guys, I really need help in implementing trailingstop in my bot. I want when the trade is opened, if it is a buy trade it follow the price by 45 pips and if it is a sell it; there is no error in my code but for some reason the strategy is not implementing in the tester. See my code below, i will really appreciate every input on this:

    if (PositionSelect(_Symbol) == false)
    {
        if (shouldBuy && Ask > MAAvalue)
        {
            double entryPrice = NormalizeDouble(fibLevel1, _Digits);
            double stopLoss = NormalizeDouble(fibLevel2, _Digits);

            // Place buy order and handle any errors or further management
            int buyResult = trade.Buy(LotSize, Symbol(), entryPrice, stopLoss, 0, "Bought");
            if (buyResult == TRADE_RETCODE_DONE)
            {
                // The buy order was executed successfully
                // You can add further actions or logging here
                Print("Buy trade successfull");

                // Set trailing stop
                trailingStop = entryPrice - TrailingStopDistance * _Point;
            }
            else
            {
                // Handle any errors that occurred during the buy order placement
                Print("Error placing order: ", GetLastError());
            }
        }
        else if (shouldSell && Bid < MAAvalue)
        {
            double entryPrice = NormalizeDouble(fibLevel1, _Digits);
            double stopLoss = NormalizeDouble(fibLevel2, _Digits);

            // Place sell order and handle any errors or further management
            int sellResult = trade.Sell(LotSize, Symbol(), entryPrice, stopLoss, 0, "Sold");
            if (sellResult == TRADE_RETCODE_DONE)
            {
                // The sell order was executed successfully
                Print("Sell trade successfull");
                // You can add further actions or logging here

                // Set trailing stop
                trailingStop = entryPrice + TrailingStopDistance * _Point;
            }
            else
            {
                // Handle any errors that occurred during the sell order placement
                Print("Error placing order: ", GetLastError());
            }
        }
    }
    else
    {
        ulong posTicket = trade.ResultOrder();

        // Manage the trailing stop if a position is open
        if (POSITION_TYPE_BUY)
        {
            // Update the stop loss level for a buy position
            if (Bid > trailingStop)
            {
                trailingStop = Bid - TrailingStopDistance * _Point;
                int modifyResult = trade.PositionModify(posTicket, trailingStop, 0);

                if (modifyResult == TRADE_RETCODE_DONE)
                {
                    Print("Trailing stop for Buy position modified successfully");
                }
                else
                {
                    Print("Error modifying trailing stop for Buy position: ", GetLastError());
                }
            }
            else if (POSITION_TYPE_SELL)
            {
                // Update the stop loss level for a sell position
                if (Ask < trailingStop)
                {
                    trailingStop = Ask + TrailingStopDistance * _Point;
                    int modifyResult = trade.PositionModify(posTicket, trailingStop, 0);

                    if (modifyResult == TRADE_RETCODE_DONE)
                    {
                        Print("Trailing stop for Sell position modified successfully");
                    }
                    else
                    {
                        Print("Error modifying trailing stop for Sell position: ", GetLastError());
                    }
                }
            }
        }
        
        }
 
David Udoh: TRADE_RETCODE_DONE

Where in the documentation does it state that the CTrade methods Buy or Sell return an enumeration with a possible value of TRADE_RETCODE_DONE?

It doesn't! Both of those methods return a "bool", a true or a false. The same applies to PositionModify.

Also, POSITION_TYPE_BUY and POSITION_TYPE_SELL are constant values from an the enumeration ENUM_POSITION_TYPE. They are not functions.

You need to seriously re-evaluate your code by referencing the documentation and doing more research.

I highly suggest reading the following ...

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
Fernando Carreiro #:

Where in the documentation does it state that the CTrade methods Buy or Sell return an enumeration with a possible value of TRADE_RETCODE_DONE?

It doesn't! Both of those methods return a "bool", a true or a false. The same applies to PositionModify.

Also, POSITION_TYPE_BUY and POSITION_TYPE_SELL are constant values from an the enumeration ENUM_POSITION_TYPE. They are not functions.

You need to seriously re-evaluate your code by referencing the documentation and doing more research.

I highly suggest reading the following ...

I am new to the mq5 language so I didn't really know this. Thanks🙏🏼.

Reason: