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

Instrument Manager

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

    Instrument Manager

    Hi. I'm developing an indicator that uses the ES to determine overall market sentiment. I want to "ADD" the most current ES contract in my instrument manager list. Is there a way to access the instrument manager list? If so,how would I go about accessing the most recent ES contract? thanks

    #2
    Hello FCatan,

    Thank you for your inquiry.

    The easiest way to add the ES contract to your indicator would be the syntax below:
    Code:
    Add("ES (contract month)", PeriodType periodType, int period);
    Please take a look at the NinjaTrader help guide at this link for more information on the Add() method call: http://ninjatrader.com/support/helpGuides/nt7/?add3.htm

    I do not know if a supported way of accessing an instrument list from NinjaScript. However, there is this method that is unsupported:

    Code:
    NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("instrument list name");
    Below is a sample that will pull only the ES instruments from the instrument list you have specified and sort them in descending order.
    Code:
    List<string> esInstruments = new List<string>();
    
    foreach (Instrument instrument in list.Instruments)
    {
         if (instrument.FullName.Contains("ES"))
              {
                   esInstruments.Add(instrument.FullName);
              }
    }
    
    esInstruments.Sort();
    esInstruments.Reverse();
    You would be able to pull the first index value of that esInstruments list and use it in your Add() method for the most current contract month:
    Code:
    Add(esInstruments[0], PeriodType periodType, int period);
    Please note that the method I have provided will only work correctly if your ES instruments in the instrument list have the same contract year.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      thanks zachary, regarding the method : NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("instrume nt list name");

      what do I use for "instrument list name"?

      Comment


        #4
        below is the code I added to the Initialize section

        // this section gets the most recent ES contract from the Instrument List
        NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("Instrume nts");
        List<string> esInstruments = new List<string>();
        foreach (Instrument instrument in list.Instruments)
        {
        if (instrument.FullName.Contains("ES ")) esInstruments.Add(instrument.FullName);
        }
        if (esInstruments != null)
        {
        esInstruments.Sort();
        esInstruments.Reverse();
        ES_contract = esInstruments[0];
        }
        else ES_contract = "";
        // ************************************************** ********************

        I get an error message "Failed to call method 'Initialize' for indicator 'TestInd': Object reference not set to an instance of an object."

        I'm relatively new to C# so I'm sure I missed something.

        thanks Vince

        Comment


          #5
          Hello FCatan,

          That would be the name of your instrument list.

          To see the name of the instrument list you want to pull from, you'll want to click on Tools -> Instrument Manager.

          At the top left of the window, you'll see a section titled Instrument lists. Below will be the name of your instrument list.

          So, if you'd like to use the Default instrument list, you would change "instrument list name" to "Default".
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by FCatan View Post

            I get an error message "Failed to call method 'Initialize' for indicator 'TestInd': Object reference not set to an instance of an object."

            I'm relatively new to C# so I'm sure I missed something.

            thanks Vince
            Hello Vince,

            This would be due to an improper instrument list name.

            Has your issue been rectified by my previous post?
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              yes! Thanks Zachary

              Comment


                #8
                Hi ... Zacahry assisted me awhile back on a section of code that selects the most current instrument form the Dfault instrument list. The code is as follows:

                NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("Default" );
                bool contractFound = false;
                List<string> selectedInstruments = new List<string>();
                foreach (Instrument myInstrument in list.Instruments)
                {
                if (myInstrument.FullName.Contains(defaultInst)) {selectedInstruments.Add(myInstrument.FullName); contractFound = true;}
                }

                if (contractFound)
                {
                selectedInstruments.Sort();
                selectedInstruments.Reverse();
                selected_contract = selectedInstruments[0];
                }
                else
                {
                selected_contract = "";
                runProgram = false;
                }


                This works find except when I have a contract that is for a prior year but a greater month. For example if I have the current ES contract "ES 09-15" as well as an older contract, "ES 12-14", the selectedInstruments.Sort(); selectedInstruments.Reverse(); lines will sort the "ES 12-14" first. But I want "ES 09-15". The issue being how the order of the text puts the month before the year. How do I sort first but year, then by month while keeping the text format in tact?

                thanks!

                Comment


                  #9
                  Hello FCatan,

                  I have mentioned in my initial post that "the method I have provided will only work correctly if your ES instruments in the instrument list have the same contract year." This is due to the fact that "12" is larger than "09" that it is sorting ES 12-14 before ES 09-15.

                  There is nothing built into NinjaScript that would provide this functionality for you. I would suggest looking into C# custom list sort methods to do this for you.
                  Zachary G.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by helpwanted, Today, 03:06 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post helpwanted  
                  Started by Brevo, Today, 01:45 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post Brevo
                  by Brevo
                   
                  Started by aussugardefender, Today, 01:07 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post aussugardefender  
                  Started by pvincent, 06-23-2022, 12:53 PM
                  14 responses
                  242 views
                  0 likes
                  Last Post Nyman
                  by Nyman
                   
                  Started by TraderG23, 12-08-2023, 07:56 AM
                  9 responses
                  384 views
                  1 like
                  Last Post Gavini
                  by Gavini
                   
                  Working...
                  X