Backtest (visual vs non-visual mode): different results

Created at 14 Jun 2023, 09:44
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!
NC

ncel01

Joined 19.03.2020

Backtest (visual vs non-visual mode): different results
14 Jun 2023, 09:44


Hello,

I've noticed that I am getting different backtest results just by selecting/unselecting the visual mode. Nothing else has been changed.

Any ideas?

Thanks.


@ncel01
Replies

PanagiotisChar
15 Jun 2023, 08:59

Hi ncel01,

Hard to tell without being able to reproduce.

Aieden Technologies

Need help? Join us on Telegram

 


@PanagiotisChar

ncel01
15 Jun 2023, 15:09 ( Updated at: 15 Jun 2023, 15:10 )

Hi Panagiotis,

Yes, I understand.

I am still trying to figure out what can be the problem but it should not be related to my code.

I suspect this is related to the way the trading events are handled.

Are you aware how priority on execution is assigned to the events in case these are triggered at the exact same moment? For instance: OnBar(), Positions_Opened(), Positions_Closed(), etc.

I still can try to create a bot sample that reproduces this behaviour so this can be further checked.

Thank you!


@ncel01

ncel01
15 Jun 2023, 20:35

Panagiotis,

After investigating further what could be the reason for this issue, I was able to generate a simple cBot sampe that will allow anyone to reproduce it, I believe (comments included):

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class SamplecBot : Robot
    {
        // --> STEP NOT WORKING PROPERLY ( A step of 0.1 instead of 0.01 is being applied to this parameter ! )
        [Parameter("Volume in lots :", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double VolumeInLots { get; set; }

        protected override void OnStart()
        {
            Positions.Opened += Positions_Opened;
            PlacePendingOrder();
        }

        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            if (Positions.Count < 10)
                PlacePendingOrder();
        }

        private void PlacePendingOrder()
        {
            var direction = TradeType.Buy;
            var volume = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            var distanceInPips = 50;
            var entryPrice = direction == TradeType.Buy ? Symbol.Ask - distanceInPips * Symbol.PipSize : Symbol.Bid + distanceInPips * Symbol.PipSize;
            var stopLossInPips = 50;
            var takeProfitInPips = 50;

            PlaceLimitOrder(direction, SymbolName, volume, entryPrice, null, stopLossInPips, takeProfitInPips);
        }

        protected override void OnTick()
        {
            // Below is the reason why visual and non-visual mode deliver different results when backtesting
            // It seems like there is an overlap of the trading events ( Positions_Opened() and OnTick() ) when not in visual mode
            // It also seems that OnTick() is called multiple times and that this value is increasing by 1 each time 
            // Multiple pending orders are being placed : the number of "same" positions is getting cumulative ( see Entry Time in history tab )
            // Entry Time A: 1 position is opened
            // Entry Time B: 2 positions are opened
            // Entry Time C: 3 positions are opened
            // Entry Time D: 4 positions are opened
            // Etc.
            if (PendingOrders.Count == 0)
                PlacePendingOrder();
        }
    }
}

 


@ncel01

ncel01
16 Jun 2023, 14:07

Dear cTrader team,

Any feedback on this issue?


@ncel01

Spotware
16 Jun 2023, 15:02

Dear ncel01,

Our team is investigating this issue and it will be resolved in an upcoming update.

Best regards,

cTrader Team


@Spotware

ncel01
19 Jun 2023, 10:17

Dear Spotware team,

You're welcome!

Please be aware that, most probably, this is not the only case for which visual and non-visual backtest are providing different results.

Question:

What priority is given, by the the API, to the events when this occur at the exact same moment?

In my case, I need to be aware of this so that the correct cBot logic can be implemented.

Thanks.


@ncel01

ncel01
23 Jun 2023, 09:23

Dear Spotware team,

Kind request to advise on the above or, alternatively, to share any available documentation regarding this.

Thank you.


@ncel01

ncel01
25 Jul 2023, 05:22

Dear Spotware team,

Thanks for clarifying the above or, alternatively, for sharing any available documentation regarding this.


@ncel01

Spotware
25 Jul 2023, 06:41

Dear ncel01,

The fix will be released in the next update.

Best regards,

cTrader Team


@Spotware

ncel01
25 Jul 2023, 08:39

Dear cTrader Team,

Thanks for informing.

However, what I really wanted to know is how priority on execution is assigned (by the API) to the price related events, in case these are triggered at the exact same moment.

For instance: 

  1. OnTick()
  2. OnBar()
  3. PendingOrders_Filled()
  4. Positions_Opened()
  5. Positions_Closed() 
  6. Positions_Modified()
  7. Positions_Cancelled()
  8. PendingOrders_Created()
  9. PendingOrders_Modified()
  10. PendingOrders_Cancelled()
  11. Etc.

Callback functions: will these be triggered right ater their respective event or, only after all the main events have been called?

Please note that it is important to be aware of this to properly manage execution, especially when using asynchronous operations.

Thank you!


@ncel01

Spotware
26 Jul 2023, 05:04

Dear ncel01,

In your case OnTick() should be executed before Position.Opened event.

Best regards,

cTrader Team


@Spotware

ncel01
27 Jul 2023, 08:24

Dear cTrader Team,

There is no “my case”.

As you might have noticed, the code sample provided above was only intended to report an issue.

My question was obviously generic.

Thanks for clarifying the community on this by providing an effective answer. It would be also of value to see documentation on this available.


@ncel01

alexandre.abconcept
05 Feb 2024, 09:04 ( Updated at: 05 Feb 2024, 11:56 )

RE: Backtest (visual vs non-visual mode): different results

ncel01 said: 

Dear cTrader Team,

There is no “my case”.

As you might have noticed, the code sample provided above was only intended to report an issue.

My question was obviously generic.

Thanks for clarifying the community on this by providing an effective answer. It would be also of value to see documentation on this available.

 

 

Hello is there news on this ? I have the same issue.. When I backtest in Visual mode it is not the same results than in not visual..


@alexandre.abconcept

PanagiotisCharalampous
05 Feb 2024, 11:59

RE: RE: Backtest (visual vs non-visual mode): different results

alexandre.abconcept said: 

ncel01 said: 

Dear cTrader Team,

There is no “my case”.

As you might have noticed, the code sample provided above was only intended to report an issue.

My question was obviously generic.

Thanks for clarifying the community on this by providing an effective answer. It would be also of value to see documentation on this available.

 

 

Hello is there news on this ? I have the same issue.. When I backtest in Visual mode it is not the same results than in not visual..

Hi there,

The above issue is resolved. If you still face such an issue, please create a separate thread and provide information and exact steps to reproduce.

Best regards,

Panagiotis


@PanagiotisCharalampous

gyoy
28 Apr 2024, 05:09

RE: RE: Backtest (visual vs non-visual mode): different results

alexandre.abconcept said: 

ncel01 said: 

Dear cTrader Team,

There is no “my case”.

As you might have noticed, the code sample provided above was only intended to report an issue.

My question was obviously generic.

Thanks for clarifying the community on this by providing an effective answer. It would be also of value to see documentation on this available.

 

 

Hello is there news on this ? I have the same issue.. When I backtest in Visual mode it is not the same results than in not visual..

Hi, any news? I have the same issue. I don't know how reliable my code is. The visual mode shows a fairly large negative value, while the silent mode shows positive value of the same magnitude.


@gyoy

PanagiotisCharalampous
28 Apr 2024, 15:40

RE: RE: RE: Backtest (visual vs non-visual mode): different results

gyoy said: 

alexandre.abconcept said: 

ncel01 said: 

Dear cTrader Team,

There is no “my case”.

As you might have noticed, the code sample provided above was only intended to report an issue.

My question was obviously generic.

Thanks for clarifying the community on this by providing an effective answer. It would be also of value to see documentation on this available.

 

 

Hello is there news on this ? I have the same issue.. When I backtest in Visual mode it is not the same results than in not visual..

Hi, any news? I have the same issue. I don't know how reliable my code is. The visual mode shows a fairly large negative value, while the silent mode shows positive value of the same magnitude.

Hi there,

See my response above.

Best regards,

Panagiotis


@PanagiotisCharalampous