Backtest : Orders executed when market is closed

Created at 27 Jun 2023, 23:54
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 : Orders executed when market is closed
27 Jun 2023, 23:54


Dear Spotware team,

I think you will agree that is not very realistic to successfully execute orders when market is closed.
I also don't understand why an order is executed when there is, clearly, not enough margin available.

See below.

Starting capital : 100 eur
Volume: 1000 lots

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MarketOrderTest : Robot
    {
        protected override void OnStart()
        {
            Timer.Start(1);
        }

        protected override void OnTimer()
        {
            if (!Symbol.MarketHours.IsOpened())
            {
                Print("Symbol.MarketHours.IsOpened() : ", Symbol.MarketHours.IsOpened());
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1000));

                Stop();
            }
        }
    }
}


@ncel01
Replies

firemyst
28 Jun 2023, 06:54

In regards to your second point, why aren't you testing margin levels in your code _before_ sending the execute order? Your margin levels change dynamically, and the order execution isn't always instantaneous.

For instance, you could have margin level of 50% when you submit the order, but then price in another open position of yours drops, lowering your margin level below 50%, which means your order can no longer execute even though it had enough margin when you submitted the order.

//You should check your account's margin levels before sending the order
//just to be a bit safer
if (Account.MarginLevel.HasValue && Account.MarginLevel >= xxx)
  //execute your order
}

 


@firemyst

ncel01
28 Jun 2023, 10:28

firemyst,

That doesn't answer to my question.

The above has nothing to do with my cBot code. It is only meant to report the issues described.

why aren't you testing margin levels in your code _before_ sending the execute order?

I could also always check if market is open before I place a trade, however that's not the point here.

The order execution is instantaneous when backtesting, but not in real time, while dynamics apply to both cases. However, in the last case, a position will never open if the calculated account margin level after it opens is <100% and, the same should apply to the backtesting.

The requirements to place an order, among other, when backtesting should be exactly the same as when you're running your code in real time. Uniformity between these cases is not something you must code.

In fact, margin and market hours are the most basic requirements in training when it comes to open a trade. Therefore, it gets hard to understand why these are dismissed when backtesting.


@ncel01

Spotware
28 Jun 2023, 16:04 ( Updated at: 29 Jun 2023, 08:03 )

Dear ncel01,

At the moment market hours and margin are not considered Both features will be added in a future release. In the meanwhile, please add manual checks before placing a trade.

Best regards,

cTrader Team


@Spotware

ncel01
28 Jun 2023, 16:39

RE:

Dear Spotware team,

That was not expected but it's clear.

Thanks for informing!

 

Spotware said:

Dear ncel01,

At the moment market hours and margin are not consider. Both features will be added in a future release. In the meanwhile, please add manual checks before placing a trade.

Best regards,

cTrader Team

 


@ncel01