Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Fetching an instrument from code

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

    Fetching an instrument from code

    Hello,

    Is there a way to bypass the instrumentSelector control and instantiate an instrument purely from code in an addon? I looked at the AddOn framework and I see the instrument object is passed unto it from the control which I would like to avoid. I need to create multiple instruments in parallel with no UI interaction.

    Thank you,

    Libor

    P.S: I apologize if this is a duplicate, but I did not manage to find an answer in the forum, though I did see some similiar requests from other users.

    #2
    Hello,

    You could use the GetInstrument method to get an Insturment by name as one example.
    Code:
    Instrument myInstrument = NinjaTrader.Cbi.Instrument.GetInstrument("ES 06-17");
    This would allow you to access an instrument in code and then you could utilize the Instrument object for other uses.

    Otherwise for anything that does not specifically require an instrument you could just use a string for the instrument name "ES 06-17".

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you very much for the fast response, Jesse!

      How resource-intensive is creating these? Is it realistic to have about 100 or 200 of these loaded in memory?

      Comment


        #4
        Hello liborc,

        Creating an object would not be very intense, it would more or less be how you later use them and what data is being subscribed to. Because this question would be very computer specific, I would suggest to try your idea and then use the computers Task Manager to monitor resource usage. This would really be the only accurate way to test resource specific questions without guessing.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you very much. One more question - how do I use this object to get the current market data snapshot?

          Comment


            #6
            Hello,

            Thank you for the reply.

            This would be the same process as the addon example shows already, you would just bypass selecting the instrument manually and supply the Instrument instead. The process should otherwise be identical. This would be required if you will be using the Addon specific items like the following examples:

            http://ninjatrader.com/support/helpG...marketdata.htm
            http://ninjatrader.com/support/helpG...arketdepth.htm
            http://ninjatrader.com/support/helpG...mentaldata.htm

            The sample Addon specifically uses a public Instrument property and the Instrument selector which sets the Instrument property when the instrument changes. To modify the example, you would simply need to set the Instrument property. This would work for a single instrument, for more than 1 instrument you would need to duplicate the logic surrounding the Instrument property.

            To see just the logic for subscribing, see line 1832 in the sample or the line that starts with:
            public Cbi.Instrument Instrument
            The set of this property displays what would be required for subscribing and unsubscribing from instruments.


            For other items, you can also use a BarsRequest which is part of the addons as well: http://ninjatrader.com/support/helpG...r_an_addon.htm
            http://ninjatrader.com/support/helpG...arsrequest.htm

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Hello and thank you for the quick and helpful responses, I really appreciate!

              I managed to get the code to work, but am only able to get market data for instruments that have already been loaded into a chart in NT. When my addin code tries to load one that had never been loaded into a chart in NT, my addin throws the following error:
              "Unhandled exception: Value cannot be null.
              Parameter name: instrument"

              The line that throws the error is this one:
              marketData = new MarketData(instrument);


              The few lines of code that I do the loading with are:

              instrument = NinjaTrader.Cbi.Instrument.GetInstrument("ICBK"); // ICBK is an instrument that had never been loaded into a chart inside NT
              marketData = new MarketData(instrument); //this line throws the error
              marketData.Update += OnMarketData;
              PropagateInstrumentChange(instrument);


              When I verify the value of the instrument object obtained by the first line above, it is null.
              I would be greatful for any pointers as to how to resolve this problem.

              Thank you very much,

              Libor
              Last edited by liborc; 04-07-2017, 08:13 AM.

              Comment


                #8
                Hello liborc,

                I wanted to check, are you adding these instruments into the Instrument manager prior to testing this, or do you mean that you are testing on instruments that currently do not exist in the database?

                By viewing a chart of an instrument that currently does not exist, the platform would create a generic instrument. Is this what you are seeing or that you have to go to a chart and create the instrument and then can load data? If so, I would expect this to be null as no instrument was found. The error would happen if there was no check for null to prevent the logic if no instrument was found. Generally, you would want to make sure that the instrument exists before doing anything with the symbol.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  I did add all the instruments into a custom instrument list, but the code still returns null as I described before. If, however, I load a chart with that instrument, the code works correctly. Similarly, if I use the AddonFramework sample addon and select that instrument in the UI instrument selector, that instrument will work in my own code even if it had never been loaded into a chart.
                  Last edited by liborc; 04-07-2017, 10:38 AM.

                  Comment


                    #10
                    Hello,

                    Without seeing the exact code you are using it would be difficult to say what may be happening.

                    If the sample using the selector is working, where yours is not, potentially there is some difference between the two syntaxes being used.

                    Could you provide an export of the script you are testing with and the specific steps of how you added the instrument before calling it? The instrument used in the prior example ICBK was not in the database by default, but I can see data for it so this would be a good testing instrument. If you could provide the steps you had used to add this instrument and display it originally that would be helpful.

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hello and thank you for your quick answers,

                      I attached the export of my script. There is a lot of copy/paste from the Addon Framework example in it,

                      I use an underlying MySQL database to retrieve a symbol into a label (button mySQL Test). That is unneccessary and we can just set the lablel lblTicker.ContentStringFormat to the instrument symbol.

                      Then the LoadSnapshot button loads the instrument, initializes the marketdata object, parses the snapshot and then sets the outputBox Text property to the snapshot values.

                      The event OncmdTestMySQL_Click on line 633 is where most of it happens. I hope this makes sense.

                      Many thanks for your continued help!

                      Best,

                      Libor
                      Attached Files

                      Comment


                        #12
                        Hello,

                        Thank you for the reply.

                        I do see the error you are getting, that is specifically because you are not checking for null before trying to use a null object. You can add the following after trying to get the instrument to prevent the error :

                        Code:
                        if(instrument == null) return;
                        If there was no instrument found, you should not try to execute logic with the null object as it would result in an exception.

                        Regarding adding the instrument, you could use the above check to know if the instrument is or is not being found. As I noted earlier, opening a chart or using the instrument picker to open that instrument would create a generic instrument if it was not already entered into the database.

                        This would not always work though as not all instruments are stocks and may require different symbol mapping or different type such as futures or currency. You could use the null check to identify that a new instrument needs to be configured before using it with your tool, or you would need to use the built-in instrument picker to do this for you. Keep in mind using a chart or instrument picker will always configure a stock type instrument so if you need to use any other type, please create the instrument before trying to use it in code.

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          OK, makes sense. Many thanks, Jesse!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by KirkHammett, Yesterday, 11:15 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post KirkHammett  
                          Started by soon123, Yesterday, 11:12 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post soon123
                          by soon123
                           
                          Started by JustinCross, Yesterday, 09:48 PM
                          0 responses
                          10 views
                          0 likes
                          Last Post JustinCross  
                          Started by Unsuitable, Yesterday, 06:30 PM
                          0 responses
                          5 views
                          0 likes
                          Last Post Unsuitable  
                          Started by michaelsh, Yesterday, 04:28 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post michaelsh  
                          Working...
                          X