While Loop doesn't seem to work as expected in OnTick() function

Created at 24 Mar 2022, 15:08
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!
LO

lokelucerne

Joined 24.03.2022

While Loop doesn't seem to work as expected in OnTick() function
24 Mar 2022, 15:08


Hi, here is the sample code I am using to test this functionality out.

protected override void OnTick()
{
    TradeOperation op = ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, 1);
    Print("TradeOperation op = ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, 1);");
   
    while (op.IsExecuting)
    {
        Executed = op.IsExecuting;
        Print("Executed = op.IsExecuting;");
    }
   
    Print("ORDER OPENED");

}

The main reason why I chose to use async orders over synchronous orders is so I can open as many orders as close to the required price as possible. (e.g. For asynchronous orders, I can execute multiple orders and check their status later on, whereas for synchronous orders, I have to wait for 1 order to execute before I can execute another order, which allows for more variation in price movement).

However, I am facing an issue with the while loop not terminating, and I am not able to find out what the issue is. When I stop backtesting, the error I get is "Backtesting was terminated due to timeout". I suspect the issue is that the TradeOperation is only update before/after OnTick is executed, and if so, are there any workarounds for me to update the value of TradeOperation inside of OnTick?


@lokelucerne