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

Strategy - Get futures current contract

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

    Strategy - Get futures current contract

    NT Team,

    Picking up on an older thread NT8 Strategy - Get Futures current contract, for a given instrument (e.g. ES) is it possible to get the current futures contract?

    For example, where the instrument is "ES", how could you get the current contract to add the series to a Strategy:
    Code:
    AddDataSeries("ES 03-17", ...)
    Regards
    Shannon

    #2
    Hello Shannon,

    I do have a snippet of code I've used when building a rollover indicator that I have not been able to finish as of yet.

    I'm happy to share the code that selects the next rollover with you.

    Code:
    int nextRollCount = -1;
    
    for (int i = 0; i < Instrument.MasterInstrument.RolloverCollection.Count; i++)
    	if (Instrument.Expiry == Instrument.MasterInstrument.RolloverCollection[i].ContractMonth && nextRollCount < 0)
    		nextRollCount = i;
    
    nextExpiryString = string.Format("CL {0}", Instrument.MasterInstrument.RolloverCollection[nextRollCount].ToString() );
    
    Print(string.Format( "Next Rollover: {0} | {1}", Instrument.MasterInstrument.RolloverCollection[nextRollCount].ToString(), Instrument.MasterInstrument.RolloverCollection[nextRollCount].Date.ToString("MM/dd/yyyy") ));
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea,

      Thanks for your reply. It would appear my approach is inadvisable.

      As stated on NT8 - AddDataSeries:
      - This method should ONLY be called from the OnStateChange() method during State.Configure
      - Arguments supplied to AddDataSeries() should be HARDCODED and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g. Instrument or Bars)

      Instrument and BarsPeriod should NOT be accessed within the OnStateChange() method before the State has reached State.DataLoaded.

      Any ideas on how to get this information for implementation in State.Configure (i.e. AddDataSeries) when is should not be accessed until State.DataLoade would be appreciated.

      Thanks again
      Shannon

      Comment


        #4
        Hello Shannon,

        You are correct, it is not supported to dynamically add instruments. However, if you have chosen to do this anyway, it should work unless you are attempting to optimize.

        The Instrument object will not always be ready by State.Configure. Regardless, even if it was, it is still not supported to dynamically add instruments in this manner.

        But if you choose to do this anyway, the code I have suggested should work.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Instrument and Instruments objects are accessible only for instruments, previously added in script with AddDataSeries().

          For example, there is the task: always add only actual ES contract.
          Can I instantiate Instrument type object for ES and run great code snippet from the post #2?
          fx.practic
          NinjaTrader Ecosystem Vendor - fx.practic

          Comment


            #6
            Hello fx.practic,

            You could instantiate an Instrument with Instrument.GetInstrument() to get a known contract month and then to look for the next contract rollover date following an approach similar to what NinjaTrader_Chelsea has provided. You could then check if that contract period has another rollover date in relation to the current time so you can add a more recent expiry. Using NinjaTrader.Core.Globals.Now can get the current time in association to the Time Zone configured in the platform.

            Instrument.GetInstrument() - https://ninjatrader.com/support/help...instrument.htm

            If you are adding an additional data series that uses the same instrument as the primary, I may suggest to pass null as the Instrument Name for AddDataSeries() instead of attempting to find the most recent contract month by this approach.

            Please let us know if we can be of further assistance.
            JimNinjaTrader Customer Service

            Comment


              #7
              PHP Code:
              Instrument.GetInstrument ("CL"
              returns "Colgate-Palmolive" equity.
              How to force it to return exactly Crude Oil futures?
              fx.practic
              NinjaTrader Ecosystem Vendor - fx.practic

              Comment


                #8
                Hello fx.practic,

                Thank you for your note.

                The instrumentName used for GetInstrument() should be the instrument's full name, including the expiration date for futures. For example, if you were using the 02-23 expiry you would use Instrument.GetInstrument("CL 02-23")

                For more information:


                Please let us know if we may be of further assistance.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Clear. Thank you, Emily.

                  May I create a feature request: the method that returns the actual futures contract instrument by master instrument name:
                  Instrument CL_Instruement = Instrument.GetActualFuturesContract ("CL");
                  ?
                  fx.practic
                  NinjaTrader Ecosystem Vendor - fx.practic

                  Comment


                    #10
                    Hello fx.practic,

                    Thank you for the suggestion.

                    Some users have used Instrument.GetInstrumentFuzzy() to get the current expiry of an instrument so that the contract can be pulled and added for the full name of the instrument. It is important to note that this is not supported nor documented, so you should use it at your own risk and results may vary. This is also mentioned in the following post:
                    I am working on an Indicator that references more than one symbol to measure overall market direction. I'd like to write this so that whenever the the contract rolls over to the latest contract that I will not need to modify the indicator. So is that something that I can query to get the latest name of the contract say the


                    That being said, I would be glad to submit a feature request to the development team on your behalf requesting the creation or documentation of a method that will get the futures contract expiry for an instrument. I will follow up with an internal tracking number for your reference as soon as it is created.

                    Thank you in advance for your patience.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks, Emily.

                      I've tested Instrument.GetInstrumentFuzzy().
                      It works funny, but it works very well.
                      I would like to ask for the same officially supported, documented method that will be available in all further NT versions.​
                      Last edited by fx.practic; 01-06-2023, 11:02 AM.
                      fx.practic
                      NinjaTrader Ecosystem Vendor - fx.practic

                      Comment


                        #12
                        Hello fx.practic,

                        Thanks for your patience.

                        The internal tracking number for your feature request to officially document a method that gets the contract expiry for a futures instrument is SFT-5719. Please reference this internal tracking number when contacting Platform Support if you ever have questions regarding this feature request.

                        When a feature request is implemented, you'll find a description of the new feature in the release notes:​Thank you for using NinjaTrader.
                        Emily C.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you, Emily.
                          fx.practic
                          NinjaTrader Ecosystem Vendor - fx.practic

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by lorem, Today, 09:18 AM
                          1 response
                          4 views
                          0 likes
                          Last Post lorem
                          by lorem
                           
                          Started by bmartz, Today, 09:30 AM
                          0 responses
                          2 views
                          0 likes
                          Last Post bmartz
                          by bmartz
                           
                          Started by GussJ, 03-04-2020, 03:11 PM
                          14 responses
                          3,244 views
                          0 likes
                          Last Post GussJ
                          by GussJ
                           
                          Started by ArkansasClint, Today, 09:28 AM
                          0 responses
                          0 views
                          0 likes
                          Last Post ArkansasClint  
                          Started by hazylizard, Today, 08:38 AM
                          4 responses
                          12 views
                          0 likes
                          Last Post hazylizard  
                          Working...
                          X