Backtesting not closing orders

Created at 22 Jul 2024, 00:51
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!
PA

patrickandrade2

Joined 22.07.2024

Backtesting not closing orders
22 Jul 2024, 00:51


I'm optimizing a custom Macd & RSI strategy but when I try to backtest some of them I keep getting the same error, some positions will open, will hit the SL but won't close. I'm having this error with almost every single strategy. I noticed the error is just, SL not being processed for some reason:
28/03/2024 17:31:00.090 | Trade | Executing Market Order to Sell 79 XAUUSD (SL: 14, TP: 81)
28/03/2024 17:31:00.090 | Trade | → Executing Market Order to Sell 79 XAUUSD (SL: 14, TP: 81) SUCCEEDED, Position PID26
Accessing the logs, everything seems fine, but using the visual mode I can clearly see the “SL” line in the chart is blank. The position just goes on forever and ever, eventually leading to:
09/04/2024 09:21:00.994 | Info | CBot instance [Macd & Rsi Strategy, XAUUSD, Hm7] aborted because account equity have dropped to 0.

I'm using the latest version of CTrader and I noticed that the error only occurs while I'm going some time back, using 01/04/2024 until today to backtest, everything is fine SL is working normally. If I go 10+ more days, regressing from 21/03/2024 the problems start to occur. I'm trying to do a full year backtest and there's a trade being created back in 08/2023 and it never closes. If I start from 09/2023, another one starts in 09/2023 and never closes and so on, not sure what's happening.

It's being impossible to backtest anything on the long run so far. 


@patrickandrade2
Replies

patrickandrade2
24 Jul 2024, 21:01 ( Updated at: 25 Jul 2024, 05:21 )

Found the problem

The thing is, the actual bot is misbehaving, it occurs when the bot is trying to set a stop loss and a sudden movement happens. It happened live just some moments ago. I have the setup for max 15 pips of stop loss on this strategy. When buying there was a massive movement to go down. Stop loss wasn't setted and now I'm in a huge loss. This is severe, please fix. 


@patrickandrade2

PanagiotisCharalampous
25 Jul 2024, 05:44

RE: Found the problem

patrickandrade2 said: 

The thing is, the actual bot is misbehaving, it occurs when the bot is trying to set a stop loss and a sudden movement happens. It happened live just some moments ago. I have the setup for max 15 pips of stop loss on this strategy. When buying there was a massive movement to go down. Stop loss wasn't setted and now I'm in a huge loss. This is severe, please fix. 

Hi there,

The stop loss is ignored when it falls within the spread since it is invalid. You need to handle this situation within your cBot code and detect when stop losses are not set due to invalid values.

Best regards,

Panagiotis


@PanagiotisCharalampous

patrickandrade2
25 Jul 2024, 09:18

RE: RE: Found the problem

PanagiotisCharalampous said: 

patrickandrade2 said: 

The thing is, the actual bot is misbehaving, it occurs when the bot is trying to set a stop loss and a sudden movement happens. It happened live just some moments ago. I have the setup for max 15 pips of stop loss on this strategy. When buying there was a massive movement to go down. Stop loss wasn't setted and now I'm in a huge loss. This is severe, please fix. 

Hi there,

The stop loss is ignored when it falls within the spread since it is invalid. You need to handle this situation within your cBot code and detect when stop losses are not set due to invalid values.

Best regards,

Panagiotis

And how is it possible to fix this issue? I'm trying to find any article in the support database that makes possible to implement a stop loss verification but can't find one.


@patrickandrade2

PanagiotisCharalampous
26 Jul 2024, 05:52

RE: RE: RE: Found the problem

patrickandrade2 said: 

PanagiotisCharalampous said: 

patrickandrade2 said: 

The thing is, the actual bot is misbehaving, it occurs when the bot is trying to set a stop loss and a sudden movement happens. It happened live just some moments ago. I have the setup for max 15 pips of stop loss on this strategy. When buying there was a massive movement to go down. Stop loss wasn't setted and now I'm in a huge loss. This is severe, please fix. 

Hi there,

The stop loss is ignored when it falls within the spread since it is invalid. You need to handle this situation within your cBot code and detect when stop losses are not set due to invalid values.

Best regards,

Panagiotis

And how is it possible to fix this issue? I'm trying to find any article in the support database that makes possible to implement a stop loss verification but can't find one.

Hi there,

You can check if a position has a stop loss using the Position.Opened event. See below an example

using System;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class CheckStopLoss : Robot
    {
        protected override void OnStart()
        {
            Positions.Opened += Positions_Opened;
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            if(obj.Position.StopLoss == null)
            {
                // Do something...
            }
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Best regards,

Panagiotis


@PanagiotisCharalampous