Details of a Trading Operation when Trade Result is not successful

Created at 12 Jul 2023, 13:14
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

Details of a Trading Operation when Trade Result is not successful
12 Jul 2023, 13:14


 Hello,

Does anyone know how to get further details on a Trading Operation when its Trade Result is not successful?
For instance, an order label can be taken from a successful Trade Result but not from an unsuccessful. So, how to exactly know which Trade Result has failed when managing multiple orders?

This could be easily implemented if an argument could be passed to the callback function, which is not the case.

Thanks.

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MarketOrderTest : Robot
    {
        protected override void OnStart()
        {
            ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, "Position #1", OnTradeResult);
            ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, "Position #2", OnTradeResult);
        }

        private void OnTradeResult(TradeResult obj)
        {
            if (obj.IsSuccessful) // Here the obj label is known
                Print("Market order execued :", obj.Position.Label);
                
            //if (!obj.IsSuccessful)
                // How to know, exactly, which Trade Result has failed ?
        }
    }
}

 


@ncel01
Replies

firemyst
13 Jul 2023, 03:43

//Does this get what you want?

if (!obj.IsSuccessful)
{
    if (obj.Error.HasValue)
    {
        Print("Error! : {0}", obj.Error.Value.ToString();
    }
}

 


@firemyst

ncel01
13 Jul 2023, 20:13

firemyst,

Not really. That's simply the error description.

What I want is to access to the trade operation ExecuteMarketOrderAsync() parameters, in this case to the label.
The goal is to know, exactly, which operation has failed: "Position #1" vs "Position #2".


@ncel01

firemyst
14 Jul 2023, 03:29

RE:

ncel01 said:

firemyst,

Not really. That's simply the error description.

What I want is to access to the trade operation ExecuteMarketOrderAsync() parameters, in this case to the label.
The goal is to know, exactly, which operation has failed: "Position #1" vs "Position #2".

 

Then you might want to try a brute force way:

 

//written off the top of my head, so may not work exactly as is
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class MarketOrderTest : Robot
    {

    //we'll add the labels of the orders we place to this object:
	Dictionary<int, string> d = new Dictionary<int, string>();
    //keep track of each async trade
	bool trade1Finished = false;
	bool trade2Finished = false;

        protected override void OnStart()
        {
                                string label1 = "Position #1";
                                string label2 = "Position #2";
                                d.Add(1, label1);
                                d.Add(2, label2);
                                trade1Finished = false;
                                trade2Finished = false;
                                ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, d[1], (TradeResult r) =>
                                {
                                    if (r.IsSuccessful)
                                    {
                                        //remove from dictionary object as we know it completed
                                        if (r.Position.Label == label1)
                                            d.Remove(1);
                                    }
                                    trade1Finished = true; //done tracking this trade
                                });
                                ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, d[2], (TradeResult r) =>
                                {
                                    if (r.IsSuccessful)
                                    {
                                        //remove from dictionary object as we know it completed
                                        if (r.Position.Label == label2)
                                            d.Remove(2);
                                    }
                                    trade2Finished = true; //done. This trade finished
                                });
        }

        protected override void OnTick()
        {
        //check when both trades are done.
        //can also alter so that you check each trade individually
		if (trade1Finished && trade2Finished)
		{
                                if (d.ContainsKey(1))
                                {
                                    //error occurred with this order since key still exists
                                }
                                if (d.ContainsKey(2))
                                {
                                    //error occurred with this order
                                }
		}

        }
    }
}

 


@firemyst

ncel01
14 Jul 2023, 09:49

firemyst,

Thanks for your reply.

What I really need is to identify the respective trade operation when trade result is not successful.

I have an auxiliary variable aux[i] that will be set to true before ExecuteMarketOrderAsync(), and I want it to be false in case trade result is not successful. However, I need to exactly know what is the applicable index "i", which can only be taken from the operation label.

I am affraid that the only way to get this done is to create several callback functions from 0 to i_max, and define multiple ExecuteMarketOrderAsync() inside a switch case, each of these with its own callback function as argument.


@ncel01

PanagiotisChar
14 Jul 2023, 09:52 ( Updated at: 14 Jul 2023, 09:53 )

Hi ncel01,

I did not try it but shouldn't this work?

        private void OnTradeResult(TradeResult obj)
        {            
            if (!obj.IsSuccessful) 
                Print("Market order failed :", obj.PendingOrder.Label);
        }

Aieden Technologies

Need help? Join us on Telegram

 


@PanagiotisChar

ncel01
14 Jul 2023, 10:26

Hi Panagiotis,

No, I don't think so. This was the first option I've checked, I guess.

It returns NullReference since no order/position took place. This is the problem here. Apparently, there is no way to access to the operation parameters when trade result is not successful.


@ncel01