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

Futures cadendar spread backtest strategy

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

    Futures cadendar spread backtest strategy

    Hi,
    I need to test a futures calendar spread not as main strategy but as a hedge instead of stop loss, the idea is to enter long on the current month contract and create sell stop lower with known risk, if it goes wrong and again me, I will have two contract, long on the current contract and short for the next month contract, the loss is freezed until it goes higher on my short leg and i can close my short re-arm the loss protection.
    I created a strategy (for backtesting first) that have a core entry/exit (main strategy) and I have few question about implementing in the script (not builder) the strategy logic:

    - for backtesting purpose, is it more simple to test using next month contract or using exactly the same instrument, I understand the FIFO requirement forbid to have opposit direction in the same instrument but it seems to be possible using two simulation accounts?
    - in simulation, Can I use unmanaged mode to enter in both directions ?
    - if I use a real world option (calendar spread as it is permitted) could you give some examples to:

    In a continuous data stream displayed by Ninjatrader, get data from current/next months in the history data to programmatically be able to "jump" over expiration
    A simple way to get/display next moth data in the same chart and manipulate it (get info, enter long/short)
    know in advance expiration date for both contracts and how to roll them (close 1/2 moths and reopen them on the months 2/3)

    Thanks in advance​

    #2
    Hello bourasrafik,

    Thank you for your post.

    A single NinjaScript strategy instance may only be enabled for one account. Whether you are using the managed or unmanaged approach, there is no way to "enter in both directions" on the same instrument and account. My colleague goes into further detail on this concept here:


    You already mentioned FIFO, which is explained in the help guide here as well:


    As for getting data from different contract months and programmatically rolling over, you may be able to consider something like the approach from the rollover indications indicator. If you have an open position that you would like to rollover, you will have to add logic that closes the position on the old month and opens the position on a new month.

    The rollover indications indicator is publicly available on our NinjaTrader Ecosystem website:
    Here is a basic guideline of how to import NinjaScript add-ons in NinjaTrader 8:

    Note — To import NinjaScripts you will need the original .zip file.

    To Import:
    1. Download the NinjaScripts to your desktop, keep them in the compressed .zip file.
    2. From the Control Center window select the menu Tools > Import > NinjaScript Add-on...
    3. Select the downloaded .zip file
    4. NinjaTrader will then confirm if the import has been successful.

    Critical - Specifically for some NinjaScripts, it will prompt that you are running newer versions of @SMA, @EMA, etc. and ask if you want to replace, press 'No'

    Once installed, you may add the indicator to a chart by:
    • Right-click your chart > Indicators... > Select the Indicator from the 'Available' list on the left > Add > OK

    Here is a short video demonstration of the import process:
    Please let me know if I can be of further assistance.

    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,
      Thanks for your quick answer, Could you help me to find sample script of getting access to next month contract , plot bars in the chart (current and next contract) and be able to place trades on both of them ?
      Thanks for helpa

      Comment


        #4
        Hello bourasrafik,

        Thank you for your reply.

        I believe what you are asking could get complicated to do programmatically. Typically, rollovers are handled manually. You could use AddDataSeries() to add the next month contract, though AddDataSeries() should only be called in OnStateChange() when the state is State.Configure. It is not supported to call AddDataSeries() dynamically, so you would have to update the contract months used in your script over time in between each rollover (which also would require you to reload the script) so you could not just leave it running indefinitely in between rollovers.

        To plot the bars on a chart, they would either need to be added manually or added with AddPlot() inside of an indicator script.

        As for placing trades on multiple series, please see the Multi-Time Frame & Instruments page here:


        Thank you for your time and patience.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi Emily,
          Thanks for your answer, very appreciated,
          I tried to add multiple contracts to my strategy but I get stuck in displaying them, the strategy seems to display only the current contract, i explain:
          - The main chart is displaying current contract: MES 09-23, I configured the data to not merge data in the global merge policy to get multiple contracts and not one continuous chart
          - Add a data serie using : AddDataSeries("MES 03-23", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last);

          What I noticed, the current contract only display strictly the current contract (3 month), the added contract does not show up as it happens before the current contract show up, I tried also the next contract without beeing able to display it.
          In the chart I'm able to display the past, current and next contracts,

          How can I configure the strategy to display the complete data ?
          Thanks

          Comment


            #6
            Hello bourasrafik,

            Thank you for your reply.

            AddDataSeries() adds a Bars object to your script that is held in the BarsArray. It can then be used for calculations, though AddDataSeries() does not add the series visually. The chart will visually display whichever data series (whether a singular series or multiple series) that you have set up in the Data Series window. The data would be added for your strategy's logic to be able to make comparisons between past, current, and future contracts in order to determine which ones you would like to place orders on, though if you would like to see the additional contracts visually you will have to add/remove those series manually from the Data Series window of the chart.

            Ultimately, there is no supported way to change the instrument on a chart programmatically. That said, there is a potential workaround of using the Instrument Selector that accepts inputs from your keyboard that was demonstrated in the Rollover Indications indicator that I linked in the second post of this thread. You could consider using SendKeys in your strategy to enter the new instrument for the Overlay Instrument selector. An example of this workaround is shown in the linked indicator starting from line 348 in the RolloverBtn_Click() event.

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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Waxavi, Today, 02:10 AM
            1 response
            17 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by Kaledus, Today, 01:29 PM
            5 responses
            13 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by Waxavi, Today, 02:00 AM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by alifarahani, Today, 09:40 AM
            5 responses
            23 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by gentlebenthebear, Today, 01:30 AM
            3 responses
            17 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X