Stop Loss at Break even not working during backtesting

Created at 16 Oct 2022, 18:02
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!
CA

capanna.paolo

Joined 04.07.2022

Stop Loss at Break even not working during backtesting
16 Oct 2022, 18:02


Dear all,

 

I have a bot that perfectly works when i trade live demo, and its used to move the stop loss at break even.

This code snippet is not working at all in some bots that i am backtesting. In the lost trades of the back test i clearly see that the stoploss was not moved at the break even even if the conditions were there.

This is the snippet:

 protected override void OnTick()
        {

            foreach (var pos in Positions)
            {
                var _tradeType = pos.TradeType;
                var entryPrice = pos.EntryPrice;
                
                var distance = _tradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;

                if (distance >= DistanceStoploss * Symbol.PipSize)
                {

                    var newSL = _tradeType == TradeType.Buy ? pos.EntryPrice + (13 * Symbol.PipSize) : pos.EntryPrice - (13 * Symbol.PipSize);
                    ModifyPosition(pos, newSL, pos.TakeProfit);
                    Print("Stop Loss just above/below Break Even set for position {0}", pos.Id);

                }
            }
        }

The rest of the functions are under the OnBar() function, which is below this snippet.

How is this possible?

 

Please i need support.

Thanks in advance.


@capanna.paolo
Replies

PanagiotisChar
17 Oct 2022, 10:56

Hi Paolo,

If you share the complete cBot code, I am happy to have a look and let you know what is wrong with your code.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

capanna.paolo
19 Oct 2022, 11:08

RE:

Panagiotis,

 

I think the problem is that in the backtest ticks are not available.

So for a backtest on a 1min candle timeframe, if within the same candle the price trigger the condition to bring the stoploss to break even, cTrader doesn't recognize it. It is recognized only if it happen in one of the next candles.

Does it sounds logic to you?

 

Thx,

Br

 

PanagiotisChar said:

Hi Paolo,

If you share the complete cBot code, I am happy to have a look and let you know what is wrong with your code.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

 


@capanna.paolo

PanagiotisChar
19 Oct 2022, 11:31

Hi Paolo,

Make sure you are using tick data as a data source. OnTick() logic works correctly only on tick data.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

capanna.paolo
19 Oct 2022, 15:27

RE:

Panagiotis,

Indeed, so stupid of me to not have seen before that the data source can be the tick data from server (i was using the candles option)

 

 

PanagiotisChar said:

Hi Paolo,

Make sure you are using tick data as a data source. OnTick() logic works correctly only on tick data.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@capanna.paolo

capanna.paolo
19 Oct 2022, 15:32 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

Panagiotis,

I'm testing this bot on a 3 years window of time, on the 5min chart. In the end of the backtest i see the equity/balance curve, but the trading statistics are not loading, i've tried several times but cTrader get stuck.

Should i log a ticket to the customer support? 3years of data (ticks from server, not just open candle) shouldnt be too much at all...i should be able to test it on 10 years.

 

What do you think?

 

Thx

BR

 

capanna.paolo said:

Panagiotis,

Indeed, so stupid of me to not have seen before that the data source can be the tick data from server (i was using the candles option)

 

 

PanagiotisChar said:

Hi Paolo,

Make sure you are using tick data as a data source. OnTick() logic works correctly only on tick data.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

 


@capanna.paolo

... Deleted by UFO ...

PanagiotisChar
19 Oct 2022, 15:37

Hi Paolo,

There is no customer support. If you share the complete cBot code, I can try this on my computer and let you know. Also the QA team can have a look and see what is the issue.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar