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

Multi-Instrument Quandry

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

    Multi-Instrument Quandry

    I'm developing and indicator that monitors NYSE TICK and ES at the same time.

    I would like users to be able to put this indicator in any chart.

    So in my initialize function, I'm Add() ing both the TICK and the ES instruments. If adding to a chart of AXP, for example, this would give me my bar values in elements 1 and 2 in the BarsArray if I understand correctly.

    But what if I add this indicator to a TICK chart? Well then, since TICK is already the primary Bars array, my Add("^TICK...) is ignored (assuming timeframes match).

    So my question is... what's the best way to code around this issue so that my code is not dependent on which chart the indicator is placed it? Or do I have to recognize that the primary bars are the same as what I want and handle things differently in the code?

    Second question: Since there is no continuous ES contract, how do I set my code up to use the current ES contract without user interaction on every rollover? Am I going to have to calculate the rollover day and 'construct' the ES instrument name (e.g. ES 09-12) on my own?

    Thanks for any help.

    #2
    Hello BigWaveDave,

    Welcome to the NinjaTrader Support Forums!

    The easiest way to way to have an indicator not dependent on what chart it is applied to would be to use the Add() method for the instruments that you would like to have the calculations done on regardless of what instrument it is applied to. This will ensure that you will always have access to the data that you need for you calculations in one spot.

    You may have to create a condition before using the Add() method for the Contract but who is your data provider?

    Happy to be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      Hi:

      Thanks for the response. Yes, I understand what you're saying.

      But my understanding is that if the indicator is added to a chart of the same instrument and timeframe as one of my "Add()"ed instruments, that because the instrument is already the primary instrument of the chart that the Add() is not done.

      For example on a 1 minute "^TICK" chart...

      ...

      Add("^TICK", 1)... (or whatever the exact syntax is...)

      ....

      Bar[0] will be the active bar of the ^TICK (primary instrument)
      and Bar[1][0] will *not* exist.

      If, however, I "Add("^TICK") when and the indicator happens to be added to an AXP chart for example... The Bar[0] will be the active bar of AXP and Bar[1][0] will be the active bar of "TICK".

      So depending on the primary chart instrument, "TICK" will necessarily have to be referenced differently based on the primary instrument.

      I'm simply wondering what the best way to deal with this programatically is.

      Am I going to have to identify what the primary instrument is and then create a reference to the appropriate Bars array so that the rest of my code can be written without special case handling each time I want to reference the "TICK" bars array.

      Oh, and question #2 from above?

      I'm running in simulated mode right now... so I'm using downloaded NT Market replay data.

      Thanks.

      Comment


        #4
        Hello BigWaveDave,

        The secondary instrument will exist even if it is the same instrument, period type, and value.

        You will want to ues BarsArray[] or CurrentBars[] for you references to Multi-Instrument Indicator. Here are a few link to our Help Guide going over these.

        BarsArray: http://www.ninjatrader.com/support/h...?barsarray.htm

        CurrentBars: http://www.ninjatrader.com/support/h...urrentbars.htm

        For a reference sample you can view the SampleMultiInstrument by going to the Tools -> Edit NinjaScript -> Strategy -> and double click on the SampleMultiInstrument NinjaScript file.


        Let us know if we can be of further assistance.
        JCNinjaTrader Customer Service

        Comment


          #5
          Ok, but your documentation states this:


          If a multi-series script adds an additional Bars object that already exists on the chart, the script will use the preexisting series instead of creating a new one to conserve memory.

          This states that if I attempt to add a secondary instrument which is an exact match for the Primary instrument, that another BarsArray will, in fact, not be created, and not be added to BarsArray since it already exists elsewhere in the BarsArray array.

          Without additional code to track this condition I have no way of knowing whether BarsArray[1] even exists. Sure, I can subsequently check BarsArray.Length to see if it was added, but that doesn't help me inside of Initialize().

          It's a little hard to believe that Add() doesn't at least have a return value that indicates whether or not the instrument Add()ed was a duplicate or not... Or, even better, return the correct BarsArray index for the instrument just Add()ed whether it be a duplicate somewhere in the array of already allocated BarsArrays or whether it is, in fact, a brand new BarsArray.

          If Add() would simply return '0' in this case where it matches the Primary, or '1' in the case where a new BarsArray is actually created, then I could more easily track the array of interest.

          Without that basic information... My code must know what the Primary instrument is and test for a match with the secondary instruments inside of Initialize().

          I'm assuming this is what I'll have to do, but it seems pretty awkward.

          Thanks.
          Last edited by BigWaveDave; 09-17-2012, 06:12 PM. Reason: reformatted

          Comment


            #6
            Here's my kludge...

            It *assumes* that the user hasn't entered more than one DataSeries in the chart...
            I guess I'll have to figure that out and modify the 'BarsIndex' variables that I'm setting
            based on how many DataSeries are in the chart...

            If Add() returned the Index instead of being void... it would save a bunch of code...

            And, of course, if the underlying implementation of BarsArray changes, it could cause me to have to refactor the indicator. I'm making a bunch of behavioral assumptions here.

            Code:
               
            protected override void Initialize()
                    {
                        ChartOnly = true;
                        Overlay = true;
                        CalculateOnBarClose = false;
                
                        // If our chart instrument is the same as the TICK we want to use... don't add it
                        if ((Instrument.FullName == "^TICK") && (BarsPeriod.Id == PeriodType.Minute) && (BarsPeriod.Value == 1))
                            nTICKBarsIndex = 0;
                        else
                        {
                            Add("^TICK", PeriodType.Minute, 1);
                            nTICKBarsIndex = 1;
                        }
            
                        // If our chart instrument is the same as the ES contract we want to use... don't add it
                        string ESFullName = "ES 12-12"; //GetCurrentESContractName();
                        if ((Instrument.FullName == ESFullName) && (BarsPeriod.Id == PeriodType.Minute) && (BarsPeriod.Value == 1))
                            nESBarsIndex = 0;
                        else
                        {
                            Add(ESFullName, PeriodType.Minute, 1);
                            nESBarsIndex = nTICKBarsIndex + 1;
                        }
                    }
            
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
                        if (Bars == null)
                            return;
                        
                        // Only process when we get a tick in for either the TICK indicator or ES...
                        if ((BarsInProgress != nTICKBarsIndex) && (BarsInProgress != nESBarsIndex))
                            return;
                        
                        // If our Window isn't already open... check to see if we should open it...
                        if (!bWindowOpen)
                        {
                            if ((ToTime(Time[0]) >= (nWindowOpenTime*100) && ToTime(Time[0]) < 160000))
                                bWindowOpen = true;
                            else
                            {
                                bWindowOpen = false;
                                return;
                            }
                        }
                            
                        // Update our hi/lo information from the current bar
                        // Grab current high and low from each instrument on every tick...
                        tickHigh = Highs[nTICKBarsIndex][0];
                        tickLow = Lows[nTICKBarsIndex][0];
                        priceHigh = Highs[nESBarsIndex][0];
                        priceLow = Lows[nESBarsIndex][0];
            
            ....

            Comment


              #7
              Hello BigWaveDave,

              It is recommended to code in the name of the Instrument rather than having it be dynamically setup.

              With that said, you would not have to check to see the name of the Instrument as Add() will create the second BarsArray regardless of if the instrument and period are the same. That way you do not have to have multiple conditions if the instrument is the same as it will be calculated the same each time and you will always know what BarsArray/BarsInProgress is for each instrument. For Example:

              Code:
              protected override void Initialize()
              {
                  // Add a ES 12-12 5 minute Bars object - BarsInProgress index = 1 
                  Add("ES 12-12, PeriodType.Minute, 5);
               
                  // Add a NQ 12-12 100 tick Bars object  - BarsInProgress index = 2 
                  Add("NQ 12-12", PeriodType.Tick, 100); 
              }
              The second note in the Help Guide is more referring to the session template handling.

              Let us know if we can be of further assistance.
              JCNinjaTrader Customer Service

              Comment


                #8
                Ok, I see that, thanks. Though the Docs are confusing.

                I have another question.

                If you are adding instruments inside Initialize() to a multiple data series chart... (one in which the user has already added more than one instrument to...) How can you determine how many dataseries that user has already added to the chart from the Initialize() function?

                Without this information, one cannot determine the correct BarsInProgress index to use for each of the indicator added instruments. In other words, one cannot assume that the 1st Add()'ed instrument will be BarInProgress == 1.

                Is this handled under the hood somehow so that the indicator code need not concern itself? Or is there a way to programmatically determine the count of user added Dataseries from within Initialize()... or even from within OnBarUpdate should work I imagine.

                thx

                Comment


                  #9
                  Hello BigWaveDave,

                  NinjaScript will only look at the Input Data Series that is passed to it regardless of how many Data Series is applied to a chart.

                  Using the Example below.

                  Originally posted by NinjaTrader_JC View Post

                  Code:
                  protected override void Initialize()
                  {
                      // Add a ES 12-12 5 minute Bars object - BarsInProgress index = 1 
                      Add("ES 12-12, PeriodType.Minute, 5);
                   
                      // Add a NQ 12-12 100 tick Bars object  - BarsInProgress index = 2 
                      Add("NQ 12-12", PeriodType.Tick, 100); 
                  }
                  .
                  If you have a chart with a ES 12-12 1 Min, NQ 12-12 1 Min, and ES 12-12 60 Min Data Series on it and you use the Input Data Series as the ES 12-12 1 Min the BarsInProgress would be:

                  * BarsInProgress index 0 would be the Input Data Series or in this Case the ES 12-12 1 Min
                  * BarsInProgress index 1 would be the first Add() method which is the ES 12-12 5 Min
                  * BarsInProgress index 2 would be the next Add() method which is the NQ 12-12 100Tick

                  Let us know if we can be of further assistance.
                  JCNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by mjairg, 07-20-2023, 11:57 PM
                  3 responses
                  213 views
                  1 like
                  Last Post PaulMohn  
                  Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                  4 responses
                  544 views
                  0 likes
                  Last Post PaulMohn  
                  Started by GLFX005, Today, 03:23 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post GLFX005
                  by GLFX005
                   
                  Started by XXtrader, Yesterday, 11:30 PM
                  2 responses
                  12 views
                  0 likes
                  Last Post XXtrader  
                  Started by Waxavi, Today, 02:10 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post Waxavi
                  by Waxavi
                   
                  Working...
                  X