Hello – I need some help.
I am writing an add-on that can detect when an account has been disconnected within NinjaTrader and automatically reconnect after some time. If it cannot automatically reconnect, it will sent an email notification to the user.
I have tried to handle disconnects in 2 different ways.
-
Utilizing the connection status update event – this posses a problem that I get notified about disconnects on different threads for the same connection if there’s more than one account within that connection. If used, I cannot reliably track if I have already started the reconnect process. I have moved on from this method to a polling method.
-
Polling. This is my current preferred method of understanding the connection status. The idea is to check (a) If the connection itself is “Disconnected” or “ConnectionLost”, (b) Checking the price status if it’s “Disconnected” or “ConnectionLost”, and (c) Checking the Connection Status of each account within the given connection if it’s “Disconnected” or “ConnectionLost”.
My current issue is that NinjaTrader will show a Green “Connected” status within NinjaScript for a given subaccount and its associated connection, green status icon in the bottom left of NinjaTrader as well. BUT the individual account will show a Grey status icon on it’s associated row on the GUI, my code cannot detect this and the account is unable to take trades.
Is there any way to detect this? Here’s the code that current checks if the connection is disconnected:
bool IsDisconnected(Connection conn)
{
if (conn == null)
return true;
if (conn.Status == ConnectionStatus.Disconnected || conn.Status == ConnectionStatus.ConnectionLost || conn.PriceStatus == ConnectionStatus.Disconnected || conn.PriceStatus == ConnectionStatus.ConnectionLost)
return true;
bool accountDisconnected = conn.Accounts.Any(account => account.ConnectionStatus == ConnectionStatus.ConnectionLost || account.ConnectionStatus == ConnectionStatus.Disconnected);
if(accountDisconnected) return true;
return false;
}