Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help - Indicator to calculate basis between spot index value and future price

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

    Help - Indicator to calculate basis between spot index value and future price

    NT Forum,

    I am attempting to construct an Indicator (or AddOn) which will return the basis between a spot index value and its associated current future contract (where basis = futures price - spot index value). It is my hope to use the basis in recent history to approximate the fair value of the future price (i.e. Spot index value + basis) for use in orders.

    This requires :
    • the series for spot index value and future price to be synced. It is my understanding this was cumbersome in NT7. I believe this has been addressed in NT8, Initializing a Series<T> with an Indicator Method.
    • the same periodicity between spot index series and future series


    Please find below (and attached) a failed attempt at synchronizing the two series and populating basisSeries.

    Any advice / assistance would be appreciated.

    Code:
    public class Basis : Indicator
    {
     private Series<double> primarySeries;
     private Series<double> secondarySeries;
     private Series<double> basisSeries;
    
     protected override void OnStateChange()
     {
      if (State == State.SetDefaults)
      {
       Description              = @"Basis = Futures price - Spot index value";
       Name                = "Basis";
       Calculate              = Calculate.OnBarClose;
       DisplayInDataBox          = true;
       IsSuspendedWhileInactive      = true;
      }
      else if (State == State.Configure)
      {
       // TODO align BarsPeriodType and period to primarySeries
       AddDataSeries("ES 03-16", Data.BarsPeriodType.Minute, 60, Data.MarketDataType.Last);
      } 
      else if (State == State.Historical)
      {
       // Syncs a Series object to the primary bar object
       primarySeries = new Series<double>(this);
    
       /* Syncs another Series object to the secondary bar object.
       We use an arbitrary indicator overloaded with an ISeries<double> input to achieve the sync.
       The indicator can be any indicator. The Series<double> will be synced to whatever the
       BarsArray[] is provided.*/
       secondarySeries = new Series<double>(SMA(BarsArray[1], 50));
       basisSeries = new Series<double>(this); 
    
       basisSeries[0] = (Closes[0][0] - Closes[1][0]);
      }
     }
    
     protected override void OnBarUpdate()
     {
      Print(string.Format("{0} - Index ({1}) Future ({2}) = basis ({3})",
             Time[0].ToString("g"),Closes[0][0],Closes[1][0],Closes[2][0]));
               
      Value[0] = basisSeries[0];
     }
    }
    Thanks
    Shannon
    Attached Files

    #2
    Hello Shansen,

    Attached is a an example of how to sync the secondary series to the added bar series. However, your code does not look like it needs it if it only needs to call Closes - which is already available through the primary and added bar series.
    Attached Files

    Comment


      #3
      Patrick,

      Thanks for your response. If my understanding is correct, the two series do not appear to be aligned.

      By calculating the basis (futures price - spot index value), I hope to use the basis in recent history to approximate the fair value of the future price (i.e. Spot index value + basis). Thus allowing me to execute a limit order futures trade (e.g. ES 03-16) from the index (e.g. ^SP500). For this application, the basis calculation must be correct (accepting that it is approximate)

      Please find attached a spread chart of ES 12-15 vs ^SP500 (i.e. ESZ5 vs SPX).
      • The x-axis covers the period when ESZ5 was the front month futures contract (18 September - 17 December 2015).
      • The top frame shows the close prices of ESZ5 and SPX during NYSE trading hours (9:30am - 4:00pm ET) (1:30am - 8:00am Australia time) with a period of 30mins.
      • The bottom frame shows the basis (i.e. ESZ5 - SPX).

      As you can see, basis converges to zero at contract expiry. While not required for this discussion, the reasons can be found in CME - Understanding stock index futures.

      Please find attached
      • The spread chart for the current contract ES 03-16 (ESH6) over the period when it is the front month contract (18 December - current). It shows basis has ranged between -11 and -6 points.
      • The output from the TestSyncSeries Indicator, which aims to replicate this analysis (noting the only data of interest is on/after 18 December). It shows basis has ranged from -150 to 0 points.

      Thus, I believe the two series ES 03-16 and ^SP500 are not aligned.

      Please advise how to correctly extract the basis between ES 03-16 and ^SP500.
      If it is of help, the NT data provider is Interactive Brokers (IB).

      As always, thanks
      Shannon
      Attached Files
      Last edited by Shansen; 01-16-2016, 05:59 PM.

      Comment


        #4
        NT Forum,

        Happy to report modifying the NT8 SpreadOscilator as built by NinjaTrader_Paul works as expected.

        I do not know why SpreadOscilator works and TestSyncSeries does not.

        Thanks again
        Shannon

        Comment


          #5
          Shansen,

          You will notice that the Spread Oscillator does not use nor need any additional <T>Series to calculate as the AddDataSeries() method has already provided the needed values.

          Another example is available under New > NinjaScript Editor > Strategies > SampleMultiInstrument.

          Comment


            #6
            Patrick,

            Apologies for my confusion. I have had difficulty with dataseries being out of sync in NT7, hence my starting position of ensuring dataseries were in sync in NT8. On this point, are there changes in NT8 which ensure dataseries are in sync?

            While I am still understanding the behaviour of the modified NT8 SpreadOscilator, I confirm that in State.Historical points calculate as expected.
            However, in State.Realtime as the CurrentBar develops the two series appear to get out of whack. I will look to understand this further as time goes on.

            Shannon

            Comment


              #7
              Shannon,

              We can try to analyze what is occurring first and then figure out what we need to do here. However, the series should stay in sync.

              Can you provide a specific case that I could replicate to see the series out of sync?

              Comment


                #8
                I do not mean to hijack this thread.... so please forgive me.

                I had a similar problem where 2 multiple time-frame dataseries were "out-of-sync".

                Please see the attached image. ----> set to Calculate.OnPriceChange.

                This was driving me insane to the point I dumped the 2nd DS and used a loop and a List to create an artificial DS from the 1st DS.

                I would be very interested in the progress of this thread.
                Attached Files

                Comment


                  #9
                  Hello Sim22,

                  Thank you for your post.

                  Historically the weekly bar would look back to the last close of the weekly bar. In real-time it would look to the last daily close for the current weekly bars close.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Perr0Grande, Today, 08:16 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post Perr0Grande  
                  Started by elderan, Today, 08:03 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post elderan
                  by elderan
                   
                  Started by algospoke, Today, 06:40 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post algospoke  
                  Started by maybeimnotrader, Today, 05:46 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post maybeimnotrader  
                  Started by quantismo, Today, 05:13 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post quantismo  
                  Working...
                  X