how to check if an async order is still executing

Created at 24 Feb 2021, 10:37
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!
xabbu's avatar

xabbu

Joined 20.07.2020

how to check if an async order is still executing
24 Feb 2021, 10:37


Dear Panagiotis,

I have coverted my cBot to async ordering. Everything works fine, BUT:

Before I have checked if a position already exist, and if yes, I have not executed another order.

Now, with switching to asyncronous mode, the first order is still executing and has not ended, so the second trade is executed, what is not wanted.

How can I check if everywhere and anytime in my cBot, if there is a trade in async execution, which is not finished jet?

I tried "operation.IsExecuting" but did not succed. 

Kindest regards and thx for your support,

 


@xabbu
Replies

PanagiotisCharalampous
24 Feb 2021, 10:47

Hi xabbu,

You can use a callback if you want to be notified when an order has been executed. See below

public TradeOperation ExecuteMarketOrderAsync(TradeType tradeType, string symbolName, double volume, [optional] Action callback)

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

xabbu
24 Feb 2021, 10:51

Dear Panagiotis,

yes thank you very much, I have done so and I get the correct messages, if an operation is ended successful or with an error. Works like a charm!

But I`m adressing the meantime, BEFORE the callback comes back with a result. I need to know if an operation is ongoing to decide, if another trade is initiatetd.

Best regards,


@xabbu

PanagiotisCharalampous
24 Feb 2021, 11:11

Hi xabbu,

I am not sure what are you trying to address here. If the callback has not been called, then the operation is not executed yet. If you need to make decisions after the trade has been executed, then don't make then unless the callback has been called.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

xabbu
25 Feb 2021, 15:01

Thank you very much, Panagiotis - I think I have found a way to prevent multiple execution due to the speed of the ...async functionality. 


@xabbu

Prospect
11 Apr 2022, 23:11

RE:

xabbu said:

Thank you very much, Panagiotis - I think I have found a way to prevent multiple execution due to the speed of the ...async functionality. 

Do you mind sharing your solution to this?


@Prospect

amusleh
12 Apr 2022, 10:20

RE: RE:

Prospect said:

xabbu said:

Thank you very much, Panagiotis - I think I have found a way to prevent multiple execution due to the speed of the ...async functionality. 

Do you mind sharing your solution to this?

Hi,

This might help you:

using cAlgo.API;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private TradeOperation _operation;

        protected override void OnStart()
        {
            // Execute a new market order only if there is no ongoing operation
            if (_operation == null)
            {
                _operation = ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, ExecuteMarketOrderCallback);
            }
        }

        private void ExecuteMarketOrderCallback(TradeResult result)
        {
            _operation = null;

            // use result for getting the opened position
            // or error if there was any
        }
    }
}

 


@amusleh

Prospect
12 Apr 2022, 10:51

RE: RE: RE:

amusleh said:

Prospect said:

xabbu said:

Thank you very much, Panagiotis - I think I have found a way to prevent multiple execution due to the speed of the ...async functionality. 

Do you mind sharing your solution to this?

Hi,

This might help you:

using cAlgo.API;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private TradeOperation _operation;

        protected override void OnStart()
        {
            // Execute a new market order only if there is no ongoing operation
            if (_operation == null)
            {
                _operation = ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, ExecuteMarketOrderCallback);
            }
        }

        private void ExecuteMarketOrderCallback(TradeResult result)
        {
            _operation = null;

            // use result for getting the opened position
            // or error if there was any
        }
    }
}

 

Thank you


@Prospect