Smaller stop loss than expected

Created at 04 Mar 2024, 17:17
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
FL

flappiyt

Joined 04.03.2024

Smaller stop loss than expected
04 Mar 2024, 17:17


Hello, I have a problem: my bot is setting smaller stop losses than expected. For example, the stop loss should be 12 pips below the close of a candle, but the bot sets it at 3.4 pips below. I have no idea what the problem is, but I imagine it has to do with when to use the addition (+) and subtraction (-) in the calculation of a stop loss.

 

[Parameter("Initial Stop Loss", DefaultValue = 10)]
        public double SL { get; set; }

protected override void OnBarClosed()
        {
            
            MarketSeries series = MarketData.GetSeries(TimeFrame);
            
            // Uzyskaj dane o ostatniej świeczce
                double open = series.Open.LastValue;
                double close = series.Close.LastValue;
                double low = series.Low.LastValue;
                double bodySize = Math.Abs(open - close);

                // Sprawdź kolor świeczki
                bool isBullish = close > open;

                // Ustaw stop loss zgodnie z warunkiem
                double stopLoss;

                double candleBodySize = Math.Abs(MarketSeries.Close[index] - MarketSeries.Open[index]);

                if (isBullish)
                {
                    if (bodySize < 10 * Symbol.PipSize)
                    {
                        stopLoss = close - SL * Symbol.PipSize;
                    }
                    else
                    {
                        stopLoss = open - SL * Symbol.PipSize;
                    }
                }
                else
                {
                    if (low - close <= 10 * Symbol.PipSize)
                    {
                        stopLoss = close + SL * Symbol.PipSize;
                    }
                    else
                    {
                        stopLoss = low - 10 * Symbol.PipSize + SL * Symbol.PipSize;
                    }
                }

                // Execute market order only when the conditions are met
                ExecuteMarketOrder(TradeType.Buy, Symbol, InitialVolume, "Frann", SL, TP);
                isAlreadyInTrade = true;
            
        }


@flappiyt
Replies

firemyst
06 Mar 2024, 09:58 ( Updated at: 06 Mar 2024, 14:00 )

Of course it's doing that. That's exactly what you told it to do.

Look at your logic. For starters:

 if (bodySize < 10 * Symbol.PipSize)
                    {   //Why are you setting the SL from the close price here
                        stopLoss = close - SL * Symbol.PipSize;
                    }
                    else
                    {   //and setting the SL from the open price here?
                        stopLoss = open - SL * Symbol.PipSize;
                    }
 //if the overall bar is "bullish", don't you want them both calculated from the open price?

@firemyst