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

Windows Form displaying data

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

    Windows Form displaying data

    Hey,

    I'm currently working on a project and I am wondering if someone can help because I am not sure how NT works with respect to this.

    Basically, I have a form that is created asynchronously in my strategy (so the form pops up and if I close it, NT doesn't close, and NT can also keep on working in the background). Now, I want to display data from the strategy on the form. I am using an event listener that should be listening to an event in the strategy and then updating display info on the form. I have some sample code that works outside of NT (just a simple version I made to understand how everything works). This sample works and provides the data I need.

    However, when I try to do it in NT, I get an error that states "Object reference not set to an instance of an object". This happens when I try to register my listener. I have a property in my form that sets the "base strategy" (the one that opened the form) to the instance I want to listen on (using a "this" reference when the form is created).

    I have attatched an example to clarify this. It is modeled after an example I found online, but adapted to what I am trying to do. It's just a metronome that ticks, and a listener that listens, and prints a "Heard it at x" when the metronome ticks. Think of it as my form listening for new data on the strategy and displaying it when it happens.

    Here is the code:

    Code:
        using System;
    
        namespace Project2
        {
            // Event arg class 
            public class TimeOfTick : EventArgs
            {
                private DateTime TimeNow;
                public DateTime Time
                {
                    set
                    {
                        TimeNow = value;
                    }
                    get
                    {
                        return this.TimeNow;
                    }
                }
            }
    
            // Metronome class is like the "strategy"
            public class Metronome
            {
                // This is for the event listening wihthin the form (in this case the form is the listener)
                public event TickHandler Tick;
                public delegate void TickHandler(Metronome m, TimeOfTick e);
    
                // This is for the asynchronous call to the listener (the form)
                public delegate void ListenerDelegate();
                public static void myCallback(IAsyncResult result)
                {
                    ListenerDelegate del = (ListenerDelegate)result.AsyncState;
                    del.EndInvoke(result);
                }
                // This is a method for starting the listener asynchronously 
                public void MetronomeStarter()
                {
                    Listener listen = new Listener();
                    listen.MetronomeItem = this; // This is where I set the property to this. In my case, the metronome is my strategy and when I launch the form, I tell the form that this is the straetgy class I am listening on...
                    ListenerDelegate del = new ListenerDelegate(listen.Starter);
                    del.BeginInvoke(myCallback, del);
                }
    
                // This would be the section of the strategy that generates data - this is what I am listening to 
                public void Start()
                {
                    while (true)
                    {
                        System.Threading.Thread.Sleep(3000);
                        if (Tick != null) // This is where the event is triggered if someone is listening 
                        {
                            TimeOfTick TOT = new TimeOfTick();
                            TOT.Time = DateTime.Now;
                            Tick(this, TOT);
                        }
                    }
                }
            }
    
            // This is the listener, which is the form in my case. 
            public class Listener
            {
                private Metronome metronomeItem;   
                public Metronome MetronomeItem // This public property sets my private metronome variable
                {
                    get { return metronomeItem; }
                    set { metronomeItem = value; }
                }
    
                public void Starter() // This initializes the class (in my case the form)
                {
                    Console.WriteLine("Starting Listener class");                         
                    metronomeItem.Tick += new Metronome.TickHandler(HeardIt);  // ... and this is what creates the error - "object reference not set to an instance of an object"
                }
    
                void HeardIt(Metronome m, TimeOfTick e)
                {
                    System.Console.WriteLine("HEARD IT AT {0}", e.Time); // This is the result of the l
                }
    
            }
    
            class Test
            {
                static void Main() // This is what I am not sure about. How does NT do this. I think this is why the error occurs. 
                { 
                    Metronome m = new Metronome();            
                    m.MetronomeStarter();
                    m.Start();
                }
            }
        }
    I spent the better part of yesterday trying to figure this out. If anyone can help, I would greatly appreciate it. If you need further clarifications, please feel free to ask. Furthermore, if there is a simpler way to do what I want to do, please don't hesitate to mention it!

    Cheers,
    pmorissette

    #2
    pmorissette, unfortunately this is outside of the scope we can support here - hopefully fellow C# / NinjaScript programmers can add their thoughts here.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      On another related note, is there a way we can get data within custom code? Is there some kind of interface or function that we can call and subscribe/unsubscribe to data?

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by andrewtrades, Today, 04:57 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      5 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      7 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      19 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X