Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Rolling over contract automatically within a strategy. Is this possible in NT8?

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

    Rolling over contract automatically within a strategy. Is this possible in NT8?

    I was hopping that NT8 would allow to roll over contract automatically. It would be nice to have this as part of the the settings of a strategy.
    I couldn't find anything about roll over except a mention of the instrument list being automatically rolled over in the help guide.

    On NT7 I have been manually rolling over strategies (once a month for CL). Sometimes, depending on when I do the switch to the new contract I either miss a trade on the new contract or need close the open position early for the expiring contract.
    I would really love to be able to set a strategy and not have to worry about rolling over.

    I would like to be able to set something like this : X days before the current contract expiry, new trades to be placed on the new contract.

    Is this possible in NT8? if not is there a way to code a role over?


    Hope my question makes sens.


    Cheers,
    Christophe.

    #2
    Hello Christophe,

    While the contract will not rollover automatically within a running strategy, you can set up something like the following to get the rollover date of the current contract in NinjaTrader 8:
    Code:
    #region Variables
        private DateTime nextRollover;
        private int contractCounter = 0;
    #endregion
    //-----------------------------------------------------
    
    private void updateRollover(Instrument inst)
    {
      if(inst.Expiry != null)
      {
        for(int i = 0; i < inst.MasterInstrument.RolloverCollection.Count; i++)
        {
          if(inst.MasterInstrument.RolloverCollection[i].ContractMonth.Equals(inst.Expiry))
          {
            nextRollover = inst.MasterInstrument.RolloverCollection[i+1].Date;
          }
        }
      }
    }
    You can pre-add a bunch of contracts to the strategy:
    Code:
    protected override void OnStateChange()
    {
      if(State == State.Configure)
      {
        Add("ES 03-16", PeriodType.Minute, 5);
        Add("ES 06-16", PeriodType.Minute, 5);
        Add("ES 09-16", PeriodType.Minute, 5);
        Add("ES 12-16", PeriodType.Minute, 5);
        updateRollover(Instrument);
      }
    }
    Then you can check if it is rollover date and update the counter as necessary in OnBarUpdate():
    Code:
    protected override void OnBarUpdate()
    {
      if(Bars.FirstBarOfSession)
      {
        if(DateTime.Compare(DateTime.Now, nextRollover) > 0)
        {
          contractCounter++;
          updateRollover(BarsArray[contractCounter].Instrument);
        }
      }
      if(BarsInProgress == contractCounter)
      {
        //Your normal strategy logic
      }
    }
    You can modify the above code to subtract x number of days from the rollover date (although the rollover date already occurs in advance of the actual expiration). You would then use your entry methods without the barsInProgress parameter overload or with it set to contractCounter. You could also go further with this code and make it so it would adjust itself properly regardless of what expiry you had loaded on the chart originally (in this example, it would have to be the ES 12-15 to work correctly). Then inside the code where contractCounter++ happens, you could exit existing positions on the old contract and/or move them onto the new contract.

    Please let me know if you have any questions or if I may be of further assistance.
    Last edited by NinjaTrader_MichaelM; 09-22-2015, 11:32 AM.
    Michael M.NinjaTrader Quality Assurance

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    4 views
    0 likes
    Last Post Javierw.ok  
    Started by timmbbo, Today, 08:59 AM
    2 responses
    10 views
    0 likes
    Last Post bltdavid  
    Started by alifarahani, Today, 09:40 AM
    6 responses
    40 views
    0 likes
    Last Post alifarahani  
    Started by Waxavi, Today, 02:10 AM
    1 response
    18 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by Kaledus, Today, 01:29 PM
    5 responses
    15 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X