I have an addon where I subscribe to account events to monitor the OnAccountItemUpdate event.
I create a position with an ATM strategy (with an SL and TP OCO order) and everything works OK, I see the OnAccountItemUpdate event being executed correctly.
The problem is when I cancel the SL or TP order. The system deletes the 2 orders and I see that the OnOrderUpdate event of the SL and TP orders is executed with the status "Cancelled".
From then on, the OnAccountItemUpdate event is never executed again.
Is this a platform error or am I doing something wrong?
NOTE: What I really need is to control that the SL orders are not deleted. So, is there any way to cancel deleting an order? Can I intercept a "beforedelete" type order event to not allow the user to delete it? What I'm trying to do is detect in the OnAccountItemUpdate event that there is an open item without SL and give an alert message.
This is the code I am using
//subscribe events
protected override void OnStateChange()
{
israUtils.outputLog("OnStateChange -> " + State.ToString());
if (State == State.SetDefaults)
{
Description = @"TEST";
Name = "MyAddon";
Account.AccountStatusUpdate += OnAccountStatusUpdate;
}
else if (State == State.Terminated)
{
// Unsubscribe to events
Account.AccountStatusUpdate -= OnAccountStatusUpdate;
// Iterar sobre todas las cuentas disponibles
foreach (var account in Account.All)
{
if (account != null)
{
// Suscribirse a los eventos específicos de cada cuenta
account.AccountItemUpdate -= OnAccountItemUpdate;
account.ExecutionUpdate -= OnExecutionUpdate;
account.OrderUpdate -= OnOrderUpdate;
account.PositionUpdate -= OnPositionUpdate;
}
}
//events
private void OnOrderUpdate(object sender, OrderEventArgs e)
{
Order order = e.Order;
israUtils.outputLog($"Order {order.ToString()}");
}
private void OnAccountStatusUpdate(object sender, AccountStatusEventArgs e)
{
israUtils.outputLog(string.Format("OnAccountStatusUpdate, Account: {0} Status: {1}", e.Account.Name, e.Status));
if (e.Status == ConnectionStatus.Connected)
{
e.Account.AccountItemUpdate += OnAccountItemUpdate;
e.Account.ExecutionUpdate += OnExecutionUpdate;
e.Account.OrderUpdate += OnOrderUpdate;
e.Account.PositionUpdate += OnPositionUpdate; }
}
private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
{
// Do something with the account item update
israUtils.outputLog(string.Format("OnAccountItemUpdate Account: {0} AccountItem: {1} Value: {2}",
e.Account.Name, e.AccountItem, e.Value));
if (openPositions())
{
Order stopOrder = e.Account.Orders
.FirstOrDefault(o =>
o.OrderType == OrderType.StopMarket &&
(o.OrderState == OrderState.Working ||
o.OrderState == OrderState.Accepted) &&
o.Oco != "");
if (stopOrder == null)
{
//alguien se ha cargado la orden SL
israUtils.outputLog("ALERT: order SL not found");
}
}
}

Comment