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

How to get the current CME Future instrument from the primary name

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

    How to get the current CME Future instrument from the primary name

    Hi
    I tried this but it does not work.
    Code:
    Output.Process(
    	string.Format("The current ES contract should be '06-18' but the GetNextExpiry method returns: '{0}'", 
    	Instrument.GetInstrument("ES")
    	.MasterInstrument.GetNextExpiry(DateTime.Now).ToString("MM-yy")), 
    	PrintTo.OutputTab1
    );
    Output:
    The current ES contract should be '06-18' but the GetNextExpiry method returns: '04-18'
    There are correctly only 4 contracts in the platform db:
    Code:
    Instrument.All.Each<Instrument>(item => {
    	if (item.MasterInstrument.Name.Equals("ES") && item.FullName.EndsWith("18")) {
    		Output.Process(item.FullName, PrintTo.OutputTab1);
    	}
    });
    Output:
    ES 03-18
    ES 06-18
    ES 09-18
    ES 12-18
    It looks like this method should accomplish just that but I cannot figure out all the parameters. Unfortunately the assembly has very few summaries, and this method has none. So looking up the method definition is no help.
    Code:
    MasterInstrument.GetInstrumentByDate(Instrument instrument, DateTime date, bool getActualExiry, bool suppressCalculateRollOvers, IProgress progress);
    I really prefer not to cook up my own solution for what seems should be a simple canned method. All it needs is to expose a param for rollover offset, usually 7-10 days before actual rollover.

    Please help.

    Thanks

    #2
    Hello xcondor,

    The Instrument.GetInstrument() method needs an expiry supplied to recognize this as a future.

    Try
    Code:
    Instrument esInstrument = Instrument.GetInstrument("ES 06-18");
    NinjaTrader.Code.Output.Process(string.Format("{0}", esInstrument.MasterInstrument.GetNextExpiry(DateTime.Now).ToString("MM-yy")), PrintTo.OutputTab1);

    The RolloverIndications indicator is a good example of finding the next rollover expiry.
    Below is a public link.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      [QUOTE=NinjaTrader_ChelseaB;539343]Hello xcondor,

      The Instrument.GetInstrument() method needs an expiry supplied to recognize this as a future.

      Try
      Code:
      Instrument esInstrument = Instrument.GetInstrument("ES 06-18");
      NinjaTrader.Code.Output.Process(string.Format("{0}", esInstrument.MasterInstrument.GetNextExpiry(DateTime.Now).ToString("MM-yy")), PrintTo.OutputTab1);
      Thanks Chelsea. This works even though somewhat buggy. I assume the Instrument.GetInstrument method is a SQL query wrapper.

      The whole point is to get dynamically the right current contract when I don't know that it is "06-18". So I tried several old values, some work some throw an exception.

      "ES 03-18" ——> ES 06-18
      "ES 03-08" ——> ES 06-18
      "ES 09-11" ——> ES 06-18 //ES 09-11 was a valid contract
      "ES 01-12" ——> bust //ES 01-12 was not a valid contract
      "ES 03-05" ——> ES 06-18
      "ES 03-01" ——> bust //ES 03-01 was a valid contract but I guess the db not populated that far back?
      "ES 12-18" ——> ES 06-18
      "ES 06-19" ——> bust //ES 06-19 is a valid contract but I guess the db not populated that far forward?
      "ES 06-20" ——> bust

      I cannot understand why such a buggy implementation, when there is a nice InstrumentType enum that can be used as a method param for data integrity to specify exactly only InstrumentType.Future.

      What about the use of this method? Can you help me how to use it. I cannot figure out what IProgress is.
      Code:
      MasterInstrument.GetInstrumentByDate(Instrument instrument, DateTime date, bool getActualExiry, bool suppressCalculateRollOvers, IProgress progress);
      Last edited by xcondor; 04-24-2018, 05:54 AM.

      Comment


        #4
        Hello xcondor,

        GetInstrument just needs to recognize the instrument as the future and not as an equity. This is why a valid contract month needs to be supplied.

        Try opening 'ES' on a chart without using an expiry or using an expiry that is not listed in the 'Contract months' in the Instruments -> settings editor window.
        (It will attempt to open the equity or give you an error message that the instrument is invalid)

        You can use any valid contract month listed in the Contract months window, but you have to use something to allow NinjaTrader to recognize the instrument you are looking for is a future.

        Once you have the proper instrument object, you are finding the MasterInstrument which is not specific to the expiry you selected when making the Instrument object.

        The MasterInstrument's GetNextExpiry gets whatever the next expiry is based on the date supplied.

        You are correct, contract months in the future (2019 forward) have not been added. These are subject to change and NinjaTrader gets this information from the exchange website. (In this case the cmegroup.com website)

        You can add these manually if you would like.

        Specifically what is the bug you feel you are seeing?


        Regarding MasterInstrument.GetInstrumentByDate(), this method is not documented in the help guide and not intended for clients to use. This is a NinjaScriptBase method necessary for the backend.
        Last edited by NinjaTrader_ChelseaB; 04-24-2018, 08:09 AM.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          extension method TryGetCurrentInstrument(InstrumentType.Future)

          Here is an extension method which does not require the user to provide an old expiry.

          Code:
          using System;
          using NinjaTrader.Cbi;
          
          namespace NinjaTrader.NinjaScript.AddOns
          {
          	public static class InstrumentExtension
          	{
          		/// <summary>
          		/// This extension method gets the current future contract when only the contract's primary name is known.
          		/// <Example> //Use in code like this:
          		/// <para />Instrument currentInstrument = Instrument.GetInstrument("ES").TryGetCurrentInstrument(InstrumentType.Future);
          		/// </Example>
          		/// </summary>
          		/// <param name="instrument"> the instance of the instrumt object being extended in this method.</param>
          		/// <param name="instrumentType">specifies the InstrumentType.Future. All other instrumentType values throw.</param>
          		/// <returns>returns null if the instrument instance is null. 
          		/// <para/>throws ArgumentOutOfRangeException if the param is not InstrumentType.Future 
          		/// </returns>
          		public static Instrument TryGetCurrentInstrument(this Instrument instrument, InstrumentType instrumentType)
          		{
          			if (instrument == null) {
          				return null;
          			} else if (instrumentType != InstrumentType.Future) {
          				throw new ArgumentOutOfRangeException("instrumentType", instrumentType,
          					"The InstrumentExtension.TryGetCurrentInstrument method only works for InstrumentType.Future");
          			} else {
          				return Instrument.GetInstrument(string.Format("{0} {1:MM-yy}",
          					instrument.MasterInstrument.Name,
          					Instrument.GetInstrument(string.Format("{0} 03-{1}",
          						instrument.MasterInstrument.Name,
          						DateTime.Now.Year.ToString("00")
          					)).MasterInstrument.GetNextExpiry(DateTime.Now)
          				));
          			}
          		}
          	}
          }
          For even cleaner code it would be better if this was static method the Instrument class.
          The the code would look like this:
          Code:
          Instrument currentInstrument = Instrument.GetCurrentInstrument("masterInstrumentName", InstrumentType.Future);
          A static class cannot be extended. So, would you make this a feature request.

          Comment


            #6
            Hello xcondor,

            I have submitted your request to add the method you have supplied to our development. Once I have a tracking ID for this request I will forward this to you for future reference.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello xcondor,

              I've received a tracking ID for your request.

              Your request to add this custom method to the Instrument class is being tracked with ID #SFT-3174.

              Please note it is up to the NinjaTrader Development to decide if and when any request will be implemented.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by adeelshahzad, Today, 03:54 AM
              5 responses
              32 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by stafe, 04-15-2024, 08:34 PM
              7 responses
              32 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by merzo, 06-25-2023, 02:19 AM
              10 responses
              823 views
              1 like
              Last Post NinjaTrader_ChristopherJ  
              Started by frankthearm, Today, 09:08 AM
              5 responses
              21 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by jeronymite, 04-12-2024, 04:26 PM
              3 responses
              43 views
              0 likes
              Last Post jeronymite  
              Working...
              X