• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Question on Instrument.GetInstrument

  • Filter
  • Time
  • Show
Clear All
new posts

    Question on Instrument.GetInstrument


    When used in an AddOn, Instrument.GetInstrument(symbol) will return null if the instrument is not in the Ninja Trader local database (i.e. has never been used before). Some graph, Market Analyzer or other Ninja Trader window needs to be opened for that instrument to be downloaded to the local database.

    Is there any safe undocumented way that you can provide me that would allow my AddOn to initiate the instrument download to the database programmatically?

    Instrument instrument = Instrument.GetInstrument(symbol);

    If (instrument == null)
    "download the instrument information from DataProvider (in my case Kinetick) servers to the database"

    Thank you!

    Hello jmneto,

    Thanks for your post.

    I know we have a feature request to add instruments to the DB programmatically, but I am not aware of any undocumented way to do so. I'm inquiring further and will update this post as I learn more.

    Thanks in advance for your patience.
    JimNinjaTrader Customer Service


      Thanks, I do not want to manually add them to the database, I just want to request them to download similar to what happens when you open a Chart for example.


        Ok, thanks for clarifying.

        We aren't prepared to share how instruments are added to the DB programmatically, but I'll do some experimenting and testing to model how a chart would request the instrument. I'll be looking into barsRequesting the instrument that is null. If you are able to test this and find something before I get a chance, please feel free to bump the thread with your findings.
        JimNinjaTrader Customer Service


          Hi Jim,

          You cannot do a DoBarsRequest without an instrument. So, it begs the question of how would your sample below work for an instrument that is not in the database yet?

          From https://ninjatrader.com/support/helpGuides/nt8/en-us/
          NinjaScript > Language Reference > Add On > BarsRequest
          public MyAddOnTab()
          // create a new bars request. This will determine the insturment and range for the bars to be requested
          barsRequest = new BarsRequest(Cbi.Instrument.GetInstrument("AAPL"), DateTime.Now.AddDays(-daysBack), DateTime.Now);

          The line above will not work if instead of apple you add some instrument that is not in the database yet. You would have to load such instrument on a Chart/Quote/SuperDOM anything, before the line above is able to work.

          So my question is how to have this functionality in an AddOn.

          Cbi.Instrument.GetInstrument is a fantastic tool (As is NT is general), don't get me wrong, but I think we should have a way to be able to load new instruments from there.


            Hello jmneto,

            Modelling the behavior a chart takes to add an instrument does not download instrument information, rather it will configure a new instrument, copying symbol mapping for what was entered in the Instrument Selector and using a default Tick Size and Exchange.

            I have created a demonstration showing a segment of code where I create a MasterInstrument (based on AAPL, which I removed from my database) and use that to create an Instrument that gets sent to GetInstrument(), passing true for create. This models the same behavior then when adding a new symbol to a chart.

            Demo - https://drive.google.com/file/d/187N...w?usp=drivesdk

            MasterInstrument master = new MasterInstrument
                Name = "AAPL", // Grab name from Instrument Selector
            Instrument instrument = new Instrument {Exchange = Exchange.Default, MasterInstrument = master};
            Instrument instrument2 = Instrument.GetInstrument(instrument.FullName, true);
            if (instrument2 == null)
                NinjaTrader.Code.Output.Process("Instrument null", PrintTo.OutputTab1);
            Please let us know if it helps resolve your inquiry. Since this falls into the realm of undocumented/unsupported, our next steps would be to submit a feature request if this does not help.
            Last edited by NinjaTrader_Jim; 10-11-2018, 01:41 PM.
            JimNinjaTrader Customer Service


              Hi Jim, thanks for the reply this is the direction I am looking for, but the code above is not inserting the new instrument "master" we just configured into the database. For example. I am receiving errors like trading hours is null when I try to use the new inserted instruments. I checked the database via tools > instruments and in fact the details are not being inserted. I modified the code to add the required details but noted that the instrument is not being added to the database. I would expect that since GetInstrument is receiving only a string as the instrument fullname and has no knowledge of object "master", so it just inserts a plain instrument and does not care about object "master" we just created. Please advise.


                Hello jmneto,

                The demonstration would model adding an instrument like when a new instrument gets typed into a chart. Testing further, the Name appears to be the only piece used in this process. (I've updated the snippet to reflect) Information isn't downloaded on the instrument and symbol mappings are made off of the instrument name given. Other items like tick size appear to be created with static values. (I.E. TickSize 0.01.) Adding other properties comes back to adding an instrument to the database programmatically which I could not provide further direction for accomplishing.

                The best way I could help from here would be to note your interest and goals in the feature request. I've noted your interest and what we tried here in the ticket. The ID is SFT-1849. We cannot offer an ETA or promise of fulfillment, but your interest is being tracked. Upon implementation, the number for the ticket ID can be publicly found in the Release Notes page of the help guide.

                Release Notes - https://ninjatrader.com/support/help...ease_notes.htm

                If there is anything else I can do to help, please let me know.
                JimNinjaTrader Customer Service


                  I found what I need: Let me know your thoughts, please check with your devs if they see anything bad doing the below, or if there are more properties that I should set.:

                  string[] providernames = new string[45];
                  for (int i = 0; i < 45; i++)
                  providernames[i] = symbol;

                  MasterInstrument master = new MasterInstrument
                  Name = symbol,
                  InstrumentType = InstrumentType.Stock,
                  Currency = Currency.UsDollar,
                  Exchanges = { Exchange.Default },
                  PointValue = 1,
                  MergePolicy = MergePolicy.UseGlobalSettings,
                  TradingHours = TradingHours.Get("US Equities ETH"),
                  TickSize = contractDetails.MinTick,
                  Description = "_" + contractDetails.LongName,
                  Url = new System.Uri(String.Format(@"https://finance.yahoo.com/quote/{0}/", symbol)),
                  ProviderNames = providernames



                    I'm glad you got something worked out.

                    Development did not want to comment on any way to add instruments programmatically, so this isn't something I would go back to ask. Our typical response for unsupported items is "If it is unsupported and it works for you, use it! Something may change and break this later down the line, so beware!" Since behaviors may change, we do not provide any direction on these items.

                    I hope others on the forum will find this useful.
                    JimNinjaTrader Customer Service


                    Latest Posts


                    Topics Statistics Last Post
                    Started by Eric M., Today, 05:25 PM
                    0 responses
                    Last Post Eric M.
                    by Eric M.
                    Started by ttodua, Today, 01:52 PM
                    1 response
                    Last Post NinjaTrader_Jim  
                    Started by muisca, 05-19-2017, 11:49 AM
                    8 responses
                    Last Post NinjaTrader_Tyler  
                    Started by NinjaTrader_Brandon, Today, 12:08 PM
                    0 responses
                    Last Post NinjaTrader_Brandon  
                    Started by iq200, Today, 12:00 PM
                    2 responses
                    Last Post iq200
                    by iq200