Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Unmanaged approach: managing Connection Status changes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Unmanaged approach: managing Connection Status changes

    Under unmanaged approach methods many things must be addressed manually. One critical aspect of automated trading is handling disconnections. NT has in OPTIONS a complete set to manage these situations, generally it takes care of them pretty well, but sometimes under certain circumstances it could go just wrong. A very typical example, for many users like me of Interactivebrokers, is experiment about mid-night, that IB reset its servers, which it doesn't take more of 2 or 3 minutes, BUT after this interruption of the data feed, very often synced accounts is not longer correct, like I've posted in this thread:


    This is why I'm interested in manually handle the change on connection status and syncing accounts when the connection status is back to connected. So, some questions:

    1. the only way to detect a change in status connection is with this snippet?
    Code:
    private ConnectionStatus dataFeed = ConnectionStatus.Connected;
    
    protected override void OnOrderUpdate(IOrder order) 
    { 
        if (dataFeed != ConnectionStatus.Connected)
        {
             // Do something
        }
    }
    
    protected override void OnConnectionStatus(ConnectionStatus orderStatus, ConnectionStatus priceStatus)
    {
       dataFeed = priceStatus; 
    }
    2.If there's a disconnection, how can instruct NT to proceed ? just setting in the Initialize method these variables?

    Code:
    protected override void Initialize()
    {
    DisconnectDelaySeconds = 10; // Disconnect has to be at least 10 seconds
        MaxRestartMinutes = 390; // Allow for restarting the strategy only if there were less restart attempts than MaxRestartAttempts within the last 6.5 hours
            MaxRestartAttempts = 4680; // Allow the strategy to restart every 5 seconds for 6.5 hours.
        ConnectionLossHandling = ConnectionLossHandling.Recalculate; // Strategy will attempt to recalculate its strategy position when a connection is reestablished.
        RestartDelaySeconds = 5; // Recalculate strategy only after connection is reestablished for at least 5 seconds
    }
    3. In the help guide there's a clear warning on connection loss under unamanaged approach:
    Connection Loss
    Even though NinjaTrader provides connection loss handling features it is still important to ensure your recovered strategy's internal state is not in limbo. Should you have internal variables tracking various information it may be necessary for you to program your own additional connection loss handling into OnConnectionStatus() to properly recover all aspects of your strategy in the manner you desired.
    So it's very important to know if there was a disconnection, that was recovered later. So: How can instruct to NT that it has changed its status from different than connected to connected ?

    I can do the typical comparison to know if NT is connected, using the snippet above commented, but what I don't know is how to tell to NT that its Status has changed from anything to connected once again. Any ideas would be highly appreciated.

    Thanks
    Last edited by pstrusi; 12-18-2013, 04:24 AM.

    #2
    Hi pstrusi,

    1) Yes, this is the only way I am aware of detecting the connection status is by using OnConnectionStatus().

    2) When you ask How to instruct NinjaTrader to proceed, are you asking how to have the strategy not be disabled when the connection is lost, or are you asking about how the strategy will behave once it has resumed.

    If you would like to prevent the strategy from being disabled, yes your suggestion is correct, turn the sensitivity down for the MaxRestartMinutes and MaxRestartAttempts. By default these are set to 5 and 4 respectively. By turning these up to 4680 and 390 this is very likely to prevent your strategy from becoming disabled.

    For the ConnectionLossHandling, you may want want the KeepRunning option. Using the re-calculate would cause the strategy to be disabled and then re-enabled to re-calculate the strategy using historical data.

    3) You can use two variables to detect the direct of state changes. For example:

    Code:
    private ConnectionStatus feedStatus = ConnectionStatus.Connected;
    private ConnectionStatus priorFeedStatus = ConnectionStatus.Connected;
    protected override void OnBarUpdate()
    {
    if (feedStatus == ConnectionStatus.Connected && priorFeedStatus != feedStatus )
    {
    Print("reconnected");
    priorFeedStatus = feedStatus;
    }
    
    }
    
    protected override void OnConnectionStatus(ConnectionStatus orderStatus, ConnectionStatus priceStatus)
    {
    feedStatus = priceStatus;
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks a lot Chelsea for your patience and help.

      Yes, my goal is to read correctly when the system is connected again in order to recover the strategy and the syncing of accounts. I'm pretty sure that your snippet just nails it as I wanted.

      Best regards,
      PS

      Comment


        #4
        Hi Chelsea,

        I did a little change to your snippet cause I think priorFeedStatus = feedStatus; should be updated in protected override void OnConnectionStatus().

        Code:
        private ConnectionStatus feedStatus = ConnectionStatus.Connected;
        private ConnectionStatus priorFeedStatus = ConnectionStatus.Connected;
        protected override void OnBarUpdate()
        {
           if (feedStatus == ConnectionStatus.Connected && priorFeedStatus != feedStatus )
           {
              Print("reconnected");
              priorFeedStatus = feedStatus;
           }
        }
        
        protected override void OnConnectionStatus(ConnectionStatus orderStatus, ConnectionStatus priceStatus)
        {
           feedStatus = priceStatus;
        }
        This is what I did

        Code:
        private ConnectionStatus dataFeed = ConnectionStatus.Connected;
        private ConnectionStatus priordataFeed = ConnectionStatus.Connected;
        
        if ( dataFeed == ConnectionStatus.Connected && priordataFeed != dataFeed )					
        {					
        	// Do your sync routine			
        }
        
        protected override void OnConnectionStatus(ConnectionStatus orderStatus, ConnectionStatus priceStatus)						
        {						
        	priordataFeed = dataFeed;					
        	dataFeed = priceStatus;					
        }
        A question:

        Where is the best place to set protected override void OnConnectionStatus() in the script, doesn't it matter, right?

        Comment


          #5
          Hi pstrusi,

          That's correct. It does not matter which order class methods are listed in.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by judysamnt7, 03-13-2023, 09:11 AM
          4 responses
          59 views
          0 likes
          Last Post DynamicTest  
          Started by ScottWalsh, Today, 06:52 PM
          4 responses
          36 views
          0 likes
          Last Post ScottWalsh  
          Started by olisav57, Today, 07:39 PM
          0 responses
          7 views
          0 likes
          Last Post olisav57  
          Started by trilliantrader, Today, 03:01 PM
          2 responses
          21 views
          0 likes
          Last Post helpwanted  
          Started by cre8able, Today, 07:24 PM
          0 responses
          10 views
          0 likes
          Last Post cre8able  
          Working...
          X