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

AddOnFramework: Problems with Connect() method

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

    AddOnFramework: Problems with Connect() method

    Hi,

    i have some problems with the Connect() method of the AddOnFramework-Sample.
    I have used the code of the sample at my AddOn to establish a Simulated Data Feed connection.

    This is the Button:
    PHP Code:
    // When user presses the Connect Button:
        
    if (button != null && ReferenceEquals(buttonconnectButton))
        {
            if (
    connection == null)
            {
                
    // Establish connection:
                
    connection Connect("Simulated Data Feed");
                
    //connection = Connect("Interactive Brokers");
            
    }
            else
                
    connection.Disconnect();
        } 
    This is the OnConnectionStatusUpdate():
    PHP Code:
    // This method is fired on connection status events.
    private void OnConnectionStatusUpdate(object senderConnectionStatusEventArgs e)
    {
        try
        {
            
    // For multi-threading reasons, work with a copy of the EventArgs to prevent situations where the EventArgs may already be ahead of us
            // while in the middle processing it.
            
    ConnectionStatusEventArgs eCopy e;

            if (
    eCopy.Connection.Options.Name == "Simulated Data Feed")
            {
                
    // Switch button to be a connect button
                
    if (eCopy.Status == ConnectionStatus.Disconnected)
                {
                    
    connection null;
                    
    Dispatcher.InvokeAsync(() =>
                    {
                        
    connectButton.Content "Connect";
                    });
                }
                
    // Switch button to be a disconnect button
                
    else if (eCopy.Status == ConnectionStatus.Connected)
                {
                    if (
    connection == null)
                        
    connection eCopy.Connection;
                    
    Dispatcher.InvokeAsync(() =>
                    {
                        
    connectButton.Content "Disconnect";
                    });
                }
            }

            
    // If connection hits an error, report it.
            
    if (eCopy.Error != ErrorCode.NoError)
            {
                
    Dispatcher.InvokeAsync(() =>
                {
                    
    outputBox.AppendText(string.Format("{0}{1}. Error: {2} NativeError: {3}",
                        
    Environment.NewLine,
                        
    eCopy.Connection.Options.Name,
                        
    eCopy.Error,
                        
    eCopy.NativeError));
                });
            }

            
    Dispatcher.InvokeAsync(() =>
            {
                
    outputBox.AppendText(string.Format("{0}{1}. Status: {2}",
                        
    Environment.NewLine,
                        
    eCopy.Connection.Options.Name,
                        
    eCopy.Status));
            });
        }
        catch (
    Exception error)
        {
            
    Dispatcher.InvokeAsync(() =>
            {
                
    // It is important to protect NinjaTrader from any unhandled exceptions that may arise from your code
                
    LoggingTabInstance.LoggingBox "TradingSystem - OnConnectionStatusUpdate Exception: " error.ToString();
            });
        }

    This is Connect():
    PHP Code:
    private Connection Connect(string connectionName)
    {
        try
        {
            
    ConnectOptions connectOptions null;
            List<
    ConnectOptionsconnectionOptionList null;

            
    // Copy list of connection options:
            
    lock (Globals.ConnectOptions)
                
    connectionOptionList Globals.ConnectOptions.ToList();

            
    // Get the configured account connection:
            
    connectOptions connectionOptionList.FirstOrDefault(=> o.Name == connectionName);

            if (
    connectOptions == null)
            {
                
    outputBox.Text "Could not connect. No connection found.";
                return 
    null;
            }

            
    // If connection is not already connected, connect.
            
    lock (Connection.Connections)
                if (
    Connection.Connections.FirstOrDefault(=> c.Options.Name == connectionName) == null)
                {
                    
    Connection connect Connection.Connect(connectOptions);

                    
    // Only return connection if successfully connected:
                    
    if (connect.Status == ConnectionStatus.Connected)
                        return 
    connect;
                    else
                        return 
    null;
                }

            return 
    null;
        }
        catch (
    Exception error)
        {
            
    // It is important to protect NinjaTrader from any unhandled exceptions that may arise from your code.
            
    LoggingTabInstance.LoggingBox "TradingSystem - Connect Exception: " error.ToString();
            return 
    null;
        }

    If I click on "Connect" at my AddOn NinjaTrader hang up (the AddOn-Window and the ControlCenter-Window).
    Even if I click on the "Connect Kinetick EOD"-Button at the AddOnFramework-sample I've got the same behaviour, the AddOn-Window and the ControlCenter-Window hang up.

    I had to end task by windows taskmanager to close NinjaTrader.

    At the attached png you can see: There is no error logging.
    Attached Files

    #2
    Hello GoSPvC,

    Thanks for the report!

    I have been able to reproduce the issue with the AddOn Framework sample and we are looking into the matter further on our end.

    I'll update this post as more information becomes available.

    Thanks in advance for your patience.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      after some try and error, i have commented out the line:
      PHP Code:
      lock (Connection.Connections
      Now the Connect/Disconnect is doing without hang up.
      I am not that educated in C# to know what's the meaning of that but this should be a hint for you.

      Comment


        #4
        Hello GoSPvC,

        Thanks for the note.

        This issue seems to have come up recently. It has been accepted as a bug and is being tracked with the ticket ID NTEIGHT-11807.

        You can reference this ticket number in the Release Notes page for the version of NinjaTrader 8 that includes the bug fix.

        Release Notes can be found here: https://ninjatrader.com/support/help...ease_notes.htm

        Thanks for bringing this matter to light!
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi,

          maybe i have some additional information.
          If clicking on connections at ControlCenter the menu with available connections opens and i get several exceptions, all the same text:

          Code:
          22:52:15 NinjaTrader.exe - OnFirstChanceException(): hwnd darf nicht IntPtr.Zero (0) oder NULL sein.
          System.ArgumentException: hwnd darf nicht IntPtr.Zero (0) oder NULL sein.
             bei System.Windows.Automation.Provider.AutomationInteropProvider.HostProviderFromHandle(IntPtr hwnd)
          Maybe it helps you,

          GoS

          Comment


            #6
            Hello GoSPvC,

            Thanks for the reply.

            That sounds like a very strange issue.

            I am not running into any exceptions when I click on the Connections menu of the Control Center or when I open Connections window.

            Could you reproduce this issue after restarting NinjaTrader? If possible, please include a short video demonstrating how you are encountering this issue. We use Jing: https://www.techsmith.com/jing.html

            I have attached a revised version of the AddOnFramework sample. This will make its way to the help guide shortly.

            I look forward to being of further assistance.
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              Hi Jim,

              thanks for the updated Connect() method. NinjaTrader won't hang up anymore.
              Now there is another problem:

              If I use the new Connect() to connect to the Simulated Data Feed, I get this connection. But Connect() returns null. I have noticed this because on a try to disconnect I got a
              Code:
              System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
                 bei NinjaTrader.Gui.NinjaScript.V1TradingSystemTab.OnButtonClick(Object sender, RoutedEventArgs eArgs)
              If i force Connect() to return the connection, I got NO exception but a disconnect.

              It seems that after
              PHP Code:
              Connection connect Connection.Connect(connectOptions); 
              the connection isn't established but still connecting when asking for the return. The following returns a connect:

              PHP Code:
              // Only return connection if successfully connected
              if (connect.Status == ConnectionStatus.Connected
                  
              || connect.Status == ConnectionStatus.Connecting)
                  return 
              connect;
              else
                  return 
              null
              Thanks, GoS

              Comment


                #8
                Hi GoSPvC,

                Thanks for the reply.

                As per my reply in our email, we will not concern ourselves with any internal exceptions that we may find when we attach an exception handler to AppDomain. Exceptions within NinjaTrader should normally be a cause for a concern when the internal logic spits out one of these errors to a window or to the log and trace files.

                If you come across an issue and you notice some internal exceptions, we could gather that information when investigating the issue.

                As for Connect() returning null, yes this is the case in the sample code. This is because private Connection Connect(string connectionName) will only return the Connection class when connect.Status equals ConnectionStatus.Connected. The status reaches ConnectionStatus.Connecting when Connection.Connect() is called, so you would have to change the return condition in this private method as follows to have it return when connecting.

                Code:
                if (connect.Status == ConnectionStatus.Connected || connect.Status == ConnectionStatus.Connecting)
                	return connect;
                Please let me know if I can be of further help.
                JimNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CortexZenUSA, Today, 12:53 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by CortexZenUSA, Today, 12:46 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by usazencortex, Today, 12:43 AM
                0 responses
                5 views
                0 likes
                Last Post usazencortex  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,266 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                13 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X