Entity Not Found

Created at 08 May 2019, 00:41
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!
lec0456's avatar

lec0456

Joined 14.11.2012

Entity Not Found
08 May 2019, 00:41


I have code to close a trade if the price goes below its stoploss as a backup.  I have seen situations where the broker did not trigger the SL because it jumped over the value.

 

But the main issue here is that it will return a "entity not found" error if the server closes the trade first. How can I check for the existence of the position before modifying the trade or closing it?

Here is the code I use in the OnTick event to check

            foreach (var position in Positions)
            {
                if (position!=null && position.TradeType==TradeType.Buy && position.StopLoss!=null && Symbol.Bid<position.StopLoss)
                {
                    Log(paramIntLogEnable, paramExtLogEnable,"Sending Manuel SL Close of {0}", position.Label);
                    if (ClosePosition(position).IsSuccessful && Positions.Count == 0) return;
                }
                
                if (position!=null && position.TradeType==TradeType.Sell && position.StopLoss!=null && Symbol.Ask>position.StopLoss)
                {
                    Log(paramIntLogEnable, paramExtLogEnable,"Sending Manuel SL Close of {0}", position.Label);
                    if (ClosePosition(position).IsSuccessful && Positions.Count == 0) return;
                }
            }

 


@lec0456
Replies

lec0456
08 May 2019, 00:45

The log looks like this:

07/05/2019 15:45:25.808 | Master cBot V2.11, EURUSD, m15 | Sending Manuel SL Close of NYSess:-290
07/05/2019 15:45:25.808 | Master cBot V2.11, EURUSD, m15 | Closing position PID2928564
07/05/2019 15:45:26.120 | Master cBot V2.11, EURUSD, m15 | → Closing position PID2928564 FAILED with error "EntityNotFound", Position PID2928564
07/05/2019 15:45:26.152 | Master cBot V2.11, EURUSD, m15 | ErrorCode: Entity Not Found.


@lec0456

PanagiotisCharalampous
08 May 2019, 09:43

Hi lec0456,

You already do it. There isn't a safe way to achieve this since the server can close the postion at any time and the information might need some time to propagate to you. During this gap, your code might have already passed the check and trying to close the position.

Best Regards,

Panagiotis


@PanagiotisCharalampous