Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing Instrument and BarsPeriod within an AddOn

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

    Accessing Instrument and BarsPeriod within an AddOn

    When trying to access the Instrument or BarsPeriod objects (for example, Instrument.MasterInstrument.Name) within an AddOn, I'm met with:

    "An object reference is required for the non-static field, method or property..."

    It makes sense that it's not directly available due to the context of what an AddOn is. However, when in context of a chart, I'm hoping there is an object available that I'm missing. For example, I have access to the Window object, so perhaps I can gain context/access via that via a cast, etc?

    Is there a secret trick that I am missing? (such as the function GetAttachedToChartBars() when in the context of drawing tools, etc.)

    Thanks in advance!

    #2
    Hi neoikon,

    An addon does not automatically have an Instrument object created the way that an indicator or strategy that is added to a chart will be supplied the Instrument of the chart. Addons are not added to a chart and are stand alone.

    This means you will need to use get instrument to find an instrument by name and save this to a new Instrument object.
    http://ninjatrader.com/support/helpG...instrument.htm

    (edit)
    You also inquired about the BarsPeriod which would need to be made as a new BarsPeriod object.
    http://ninjatrader.com/support/helpG...barsperiod.htm

    For example:
    new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1440 }
    Last edited by NinjaTrader_ChelseaB; 02-09-2016, 01:55 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the info!

      I can see how you can manually create your own Instrument object or BarsPeriod object (populated by information you give it), but that doesn't tell me what the current chart's Instrument/BarsPeriod is.

      I can look at the window's "title", which contains the instrument name, so that's a start, but it doesn't tell me if the current chart is 5 min or daily, etc.

      Is there another object on the toolbar that I can peek at that may have this info? Perhaps the two dropdown menus have a setting I can read?

      Comment


        #4
        Hello neoikon,

        From an indicator or strategy, the Instrument.FullName and BarsPeriod objects are available once the script has entered the State.DataLoaded state (and retains this when the script enters State.Historical).

        If you would like to add some text to your chart using Draw.Text(), you can do this in either of these states and put the instrument name and interval in this text box.

        Looking at the chart itself, the currently selected instrument for the charts primary series is shown in the instrument selector on the title bar of the chart. The currently selected interval is shown in the interval selector also on the title bar.

        Attached is a screenshot of an ES 03-16 60 Second chart, highlighting where the interval selected is shown.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello neoikon,

          Are you trying to use an addon to find a chart and then find an indicator running on that chart and find that indicators primary instrument and interval?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the reply, but my context is within an AddOn (and not an Indicator or Strategy).

            I'm not necessarily interested in any indicators that are running on the chart (because there could be zero added to the chart), but purely what instrument and interval is displayed within the chart... the same items that appear in the two drop downs that are on the NinjaTrader toolbar at the top of every chart. Maybe I can read the innertext values of these actual drop down objects directly?

            For the sake of simplicity, my AddOn puts a button on the toolbar of a Chart, and once clicked, it simply prints the Instrument and Interval to the output window.

            Everything is working great (added the button to the Chart window, click events are triggering, and I can output to the Output window, etc), but I'm just looking to determine what the actual Instrument and Interval are displayed within the chart.

            For the time being, I have the "Chart.Title", which includes Instrument name, which may be the best I can do.

            Comment


              #7
              Hello neoikon,

              Thank you for confirming that you are trying to use an AddOn to access a chart and find the instrument information and barsperiod information from the AddOn.

              This is possible to do.

              Once you have the window, you can access the windows chart controls.

              NTWindow w = (NTWindow) window;
              TabControl tc = w.FindFirst("ChartWindowTabControl") as TabControl;
              if (tc != null)
              {
              Output.Process("Number of tabs: " + tc.Items.Count.ToString(), PrintTo.OutputTab1);
              foreach (TabItem ti in tc.Items)
              {
              ChartControl tabChartControl = (ti.Content as ChartTab).ChartControl;
              Output.Process(tabChartControl.Instrument.FullName + " " + tabChartControl.BarsPeriod.BarsPeriodType.ToString (), PrintTo.OutputTab1);
              }
              }
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thanks so much for this! It got a little tricky dealing with threading and the ASync calls I was making, but I got it all to work. I also keyed off ti.IsSelected to determine the active tab.

                Thanks again!

                Comment


                  #9
                  For any users stuck with threading, this needs to be done in the chart window's thread.

                  foreach (Window window in Globals.AllWindows)
                  {
                  window.Dispatcher.BeginInvoke(new Action(() =>
                  {
                  })));
                  }
                  Chelsea B.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by md4866, 05-01-2024, 08:15 PM
                  2 responses
                  18 views
                  0 likes
                  Last Post md4866
                  by md4866
                   
                  Started by samish18, Today, 12:20 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post samish18  
                  Started by thread, 04-15-2024, 11:58 PM
                  3 responses
                  48 views
                  0 likes
                  Last Post Georg1o
                  by Georg1o
                   
                  Started by leojimenezp, 04-20-2024, 05:49 PM
                  4 responses
                  49 views
                  0 likes
                  Last Post leojimenezp  
                  Started by nicthe, Today, 09:24 AM
                  1 response
                  7 views
                  0 likes
                  Last Post nicthe
                  by nicthe
                   
                  Working...
                  X