Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi instrument indicator...

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

    Multi instrument indicator...

    Hello I cannot understand, why it does not work? Сan you help me?

    Code:
     protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Plot1"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Plot2"));
                Add(new Line(Color.FromKnownColor(KnownColor.Gold), 0, "ZeroLine"));
                Overlay				= false;
    			//BarsRequired =6;
    			
    			
    			string Instr1Name =Instruments[0].FullName;
    			
    			Add(Instr1Name,PeriodType.Minute,1,MarketDataType.Ask);//Ask1)
    			Add(nameOfSecondInstr,PeriodType.Minute,1, MarketDataType.Bid);//Bid2
    			
    			Add(nameOfSecondInstr,PeriodType.Minute,1, MarketDataType.Bid);//Ask2)
    			Add(Instr1Name,PeriodType.Minute,1,MarketDataType.Bid);//bid1)
    			
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
    			Print(Closes[0].ToString()+Closes[0].IsValidPlot(0).ToString());
    			Print(Closes[1].ToString()+Closes[1].IsValidPlot(0).ToString());
    			Print(Closes[2].ToString()+Closes[2].IsValidPlot(0).ToString()); //#Error on calling 'OnBarUpdate' method for indicator 'A1MySprend2' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    			Print(Closes[3].ToString()+Closes[3].IsValidPlot(0).ToString());
                Plot1.Set(1);
    			Plot2.Set(2);
            }
    PS:Error at the reference to Closes [2][0]...
    //#Error on calling 'OnBarUpdate' method for indicator 'A1MySprend2' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    Last edited by Teks71; 04-17-2010, 10:46 AM.

    #2
    Teks71, in order to reference the Closes array, you need to specify two parameters: the bar index (which instrument you want), and how many bars back. Please try this:
    Code:
    Print(Closes[0][0].ToString())
    Print(Closes[1][0].ToString())
    Print(Closes[2][0].ToString())
    Print(Closes[3][0].ToString())
    I do not know how the IsValidPlot() method works, so that could be the source of the error as well.
    AustinNinjaTrader Customer Service

    Comment


      #3
      I'm getting a similar error in a two-instrument indicator... sometimes when I change the period loaded (for example, 1 day, 5 mins, etc works, then I get an exception when I try 1 minute. On another set of instruements, 1 minute works but 1 day throws and exception).

      The exception appears to be on the Value.Set action.

      My OnBarUpdate()

      PHP Code:
      protected override void OnBarUpdate() /////////////////////////////////////////////
      {
      //Calculate spread
      try
      { 
      spreadRough = (Closes[0][0]*quotedQuantity)-(Closes[1][0]*hedgeQuantity);
      mySpread.Set(Math.Round(spreadRough, 2));
      }
      catch (Exception e)
      {
      Print(Time[0] + " " + e.ToString());
      }
       
       
      //Calculate ZScore of spread
      try
      {
      double i,j;
      i=SMA(mySpread, zPeriod)[0];
      j=StdDev(mySpread, zPeriod)[0];
      myzscore = ( (mySpread[0]-i) / j );
      zPlot.Set(myzscore);
      }
      catch (Exception e)
      {
      Print(Time[0] + " " + e.ToString());
      }
       
      try
      {
      Value.Set(zPlot[0]);
      }
      catch (Exception e)
      {
      Print(Time[0] + " " + e.ToString());
      }
      } 
      
      The exception is:

      2010-04-19 20:01:00 System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
      Parameter name: index
      at System.Collections.ArrayList.get_Item(Int32 index)
      at NinjaTrader.Data.DataSeries.Set(Double value)
      at NinjaTrader.Indicator.SpreadZScore.OnBarUpdate()

      2009-04-24 12:00:00 System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
      Parameter name: index
      at System.Collections.ArrayList.get_Item(Int32 index)
      at NinjaTrader.Data.MemBars.GetClose(Int32 index)
      at NinjaTrader.Data.Bars.GetClose(Int32 index)
      at NinjaTrader.Indicator.DataSeriesHelper.get_Item(In t32 barsAgo)
      at NinjaTrader.Indicator.SpreadZScore.OnBarUpdate()

      I note the time stamps of the errors are somewhere in the time series data. Those errors are from two different tests.

      Is there a better way to code this to make it more robust?

      Thanks.

      Comment


        #4
        Some more information. This was thrown on set of instruments on a Daily chart:

        2009-04-24 12:00:00 SpreadCalc error! System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
        Parameter name: index
        at System.Collections.ArrayList.get_Item(Int32 index)
        at NinjaTrader.Data.MemBars.GetClose(Int32 index)
        at NinjaTrader.Data.Bars.GetClose(Int32 index)
        at NinjaTrader.Indicator.DataSeriesHelper.get_Item(In t32 barsAgo)
        at NinjaTrader.Indicator.SpreadZScore.OnBarUpdate()


        While this was thrown on a different set of instruments on a Minute chart:

        2010-04-19 20:01:00 Value.Set error! System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
        Parameter name: index
        at System.Collections.ArrayList.get_Item(Int32 index)
        at NinjaTrader.Data.DataSeries.Set(Double value)
        at NinjaTrader.Indicator.SpreadZScore.OnBarUpdate()


        Any ideas? Thanks.

        Comment


          #5
          And FYI SpreadCalc, Value.Set, etc are strings I put in the try/catch blocks to isolate the errors as follows:

          PHP Code:
          protected override void OnBarUpdate() /////////////////////////////////////////////
          {
          //Calculate spread
          try
          { 
          spreadRough = (Closes[0][0]*quotedQuantity)-(Closes[1][0]*hedgeQuantity);
          mySpread.Set(Math.Round(spreadRough, 2));
          }
          catch (Exception e)
          {
          Print(Time[0] + " SpreadCalc error!" + " " + e.ToString());
          }
          
          
          //Calculate ZScore of spread
          try
          {
          double i,j;
          i=SMA(mySpread, zPeriod)[0];
          j=StdDev(mySpread, zPeriod)[0];
          myzscore = ( (mySpread[0]-i) / j );
          zPlot.Set(myzscore);
          }
          catch (Exception e)
          {
          Print(Time[0]+ " ZScoreCalc error!" + " " + e.ToString());
          }
          
          try
          {
          Value.Set(zPlot[0]);
          }
          catch (Exception e)
          {
          Print(Time[0] + " Value.Set error!" + " " + e.ToString());
          }
          } 
          

          Comment


            #6
            MXASJ, is this occuring on NT65 or NT7 for you? Could you send me your code for testing so support at ninjatrader dot com?

            Do you have a CurrentBars[x] check running for all series in question if you're on NT7?

            Thanks.

            Comment


              #7
              Thanks Bertrand,

              The indicator "SpreadZScore" is attached. Try it on a few instruments, and also try scrolling back and forth in history some times.

              NT7b14 compile. The only "trick" in there might be how I'm getting the second instrument to default to the same periodicity as the first, but the errors that pop up look more like a NaN handling issue.
              Attached Files

              Comment


                #8
                So far I only saw it, as you for example tried hedging with the same instrument producing i and j values of 0, this will cause indeed a plot overflow error as you divide by 0 then in your spread formula.

                Comment


                  #9
                  That is a good point. I have similar code working fine on a single instrument ZScore indicator as follows:

                  PHP Code:
                  protected override void OnBarUpdate()/////////////////////////////////////////////////////////
                  {
                  double i,j;
                  i=SMA(Close, zPeriod)[0];
                  j=StdDev(Close, zPeriod)[0];
                  myzscore = ( (Close[0]-i) / j );
                  zPlot.Set(myzscore);
                  
                  Value.Set(zPlot[0]);
                  
                  } 
                  
                  Close would, I presume, never be <=0 and as such that indicator works great.

                  With two instruments, I'm replacing 'Close' with a variable which might be 0 or < 0 as it represents a relationship between those instruments. I need to think about that... but am very open to input from anyone who already has thought about it. The full code for the indicator was posted earlier (SpreadZScore, NT7 only), so if anyone has a cointegrated pair they want to try it on to better this code... pitch in!

                  Thanks

                  Comment


                    #10
                    This will kill one of the errors in runtime:

                    PHP Code:
                    protected override void OnBarUpdate() /////////////////////////////////////////////
                    {
                     
                    if (hedgeInstrument == ""||hedgeInstrument == Instrument.FullName) return;
                     
                    etc etc 
                    
                    And this would give you a heads up when you try it:

                    PHP Code:
                    protected override void OnStartUp()///////////////////////////////////////////////////////////
                    {
                    if (hedgeInstrument == ""||hedgeInstrument == Instrument.FullName)
                    {
                    MessageBox.Show("Hedge Instrument field is blank or the same as the Quoted instrument", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );// Requires using System.Windows.Forms
                    return;
                    }
                    } 
                    
                    But the

                    if mySpread <= 0

                    still has me thinking.

                    Comment


                      #11
                      Adding this to OnBarUpdate() helped clear my original errors that were the reason for the post:

                      PHP Code:
                      if (CurrentBar < 1) return; 
                      
                      Updated version attached. NT7b14 compile.

                      Still open to ideas to make it better.
                      Attached Files

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      579 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      334 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      101 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      554 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      551 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X