Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bug Report: Multiple Security Indicator bug

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

    Bug Report: Multiple Security Indicator bug

    Hey,

    I seem to have found a bug. This bug seems to occur when you have an indicator that uses more than one security to calculate its value.

    I have an indicator called Spread and it basically calculates the spread between two contracts...pretty straight forward, Closes[0][0] - Closes[1][0] is pretty much the essence of it.

    When I use this indicator in a strategy, it works - i.e. I Add() it in initialize, and displays correctly. However, as soon as I do any calculation involving it, the spread value becomes completely out of whack. For example, in OnBarUpdate, let's say I check for this:
    Code:
    if (SMA(mySpread,2)[0] > 10)
    {
         // do something
    }
    then the values get all messed. It doesn't matter what other indicator I use (it can be EMA, MACD, whatever) as long as I "touch" it in some way, it gets screwy. I placed some print statements in the code, and inside the indicator, the values are still calculating ok. In the strategy however, the values are no longer correct. Basically, it starts returning the base instrument's value instead of the spread.

    Here is my code for the strategy:
    Code:
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Simple moving average cross over strategy.
        /// </summary>
        [Description("Simple moving average cross over strategy.")]
        public class BasicTester : Strategy
        {
            #region Variables
            private Spread bobby;
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
                bobby = Spread("6C 09-10", 100, 100);
                Add(bobby);
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick).
            /// </summary>
            protected override void OnBarUpdate()
            {
                if (SMA(Spread("6C 09-10",100,100),2)[0] > 100)
                {
                    Print("BOb is big!");
                }
                Print("From strategy: " + bobby[0]);
                Print("From strategy recalc: " + Spread("6C 09-10", 100, 100)[0]);
                Print("Public var from strat: " + bobby.MySpread[0]);
            }
    
            #region Properties
            /// <summary>
            /// </summary>
            /// [Description("Period for fast MA")]
            /// [GridCategory("Parameters")]
            /// public int X
            /// {
            ///    get { return x; }
            ///    set { x = Math.Max(1, value); }
            ///}
            #endregion
        }
    }
    Doesn't matter if I use the variable of type Spread bobby, or if I recalculate it (Spread(xx)). Internal properties of the object are fine, but not what is sent back to the strategy...

    Any ideas?

    Here is the spread code if needed:
    Code:
        /// <summary>
        /// Spread between two instruments given a specific spread ratio
        /// </summary>
        [Description("Spread between two instruments given a specific spread ratio")]
        public class Spread : Indicator
        {
            #region Variables
            // Variables                           
                private int     quotedQuantity        = 1;     // Default setting for QuotedQuantity
                   private int     hedgeQuantity         = 1;     // Default setting for HedgeQuantity
                private string     hedgeInstrument     = @"";    // Default setting for HedgeInstrument
                
                private double            spreadRough;
                private DataSeries         mySpread;
                private int             barValue; 
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                   Add(new Plot(Color.Gray, PlotStyle.Line, "Spread"));
                                   
                barValue        =  Math.Max(1, base.BarsPeriod.Value);
                Add(hedgeInstrument, BarsPeriod.Id, barValue);
                
                mySpread     = new DataSeries(this);
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // NT Crashes if these conditions aren't in place...
                if (hedgeInstrument == "" || hedgeInstrument == Instrument.FullName)
                {
                    Print("instrument error");
                    return;
                }
                if (CurrentBars[0] < 1)
                {
                    Print("CurrentBars[0]: " + CurrentBars[0] + " " + Time[0] + " " + CurrentBar);
                    return;
                }
    
                if (CurrentBars[1] < 1)
                {
                    Print("CurrentBars[1]: " + CurrentBars[1] + " " + Time[0] + " " + CurrentBar);
                    return;
                }
                            
                //Calculate spread
                try
                {    
                    spreadRough = (Closes[0][0]*quotedQuantity)-(Closes[1][0]*hedgeQuantity);
                    mySpread.Set(Math.Round(spreadRough, 4));
                    Print("CloseBase: " + Closes[0][0] + " CloseHedge: " + Closes[1][0] + "Spread: " + spreadRough);
                    Value.Set(Math.Round(spreadRough, 4));
                }
                catch (Exception e)
                {
                    Print(Time[0] + " Indicator SpreadZScore on " + Instrument.FullName+ " Error: mySpread.Set" + " " + e.ToString());
                }                        
            }
    
            #region Properties
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries MySpread
            {
                get { return mySpread; }
            }
    
            [Description("Number of Contracts for Base Series")]
            [GridCategory("Spread Parameters")]
            public int QuotedQuantity
            {
                get { return quotedQuantity; }
                set { quotedQuantity = Math.Max(1, value); }
            }
    
            [Description("Numbers of Contracts for Hedge Instrument")]
            [GridCategory("Spread Parameters")]
            public int HedgeQuantity
            {
                get { return hedgeQuantity; }
                set { hedgeQuantity = Math.Max(1, value); }
            }
            
            [Description("Hedge Instrument Symbol - ex. ES 09-10")]
            [GridCategory("Spread Parameters")]
            public string HedgeInstrument
            {
                get { return hedgeInstrument; }
                set { hedgeInstrument = value.ToUpper(); }
            }    
            #endregion
        }

    #2
    pmorissette, can you please remove the Spread indicator from the strategy calculations (just do all the spread calculations inside the strategy itself) and see if the issue persists? As far as I know, this is the first report of this kind.

    There is a great Pairs (spread) indicator in the NT7 indicator sharing section that runs through these types of calculations fine.

    Can you please provide a solid numerical example (with instrument, timeframe, dates, etc) of this so we can take a deeper look?
    AustinNinjaTrader Customer Service

    Comment


      #3
      Hey,

      If I caclculate the spread within my strategy it's fine. I was displaying the values for both of them on the chart and when first initialized, the values are ok. However, after the first call to OnBarUpdate, the values get messed up. The whole indicator seems to recalculate or something and then looks different with the wrong values. I wrote a print statement in both my indicator, and the strategy. The spread I am calculating is 6A 09-10 - 6C 09-10, 1:1.

      Within the indicator, the print values are fine, both before & after assigning the value. In my strategy, if I calculate it manually, the result is fine. If I only add the indicator, the values are fine. If I access the indicator's public property that is a DataSeries object, the value is fine. However, if I add any "modifier" to my indicator, be it SMA(myIndicator) or anything of the sort, then the value gets messed up. If I use print statements, the value of the indicator just becomes the value of the base instrument (6A 09-10) in this case.

      I'll check out that other indicator but I do want to resolve this bug...

      Comment


        #4
        pmorissette, thank you for the additional information. I'll test your code now and let you know what I find.
        AustinNinjaTrader Customer Service

        Comment


          #5
          I edited your code a bit, and everything looks fine now, including the Print()s from the strategy. I think the multipliers were throwing all the calculations off, because they were set to 100 for a 1:1 spread.
          Attached Files
          AustinNinjaTrader Customer Service

          Comment


            #6
            What multipliers are you talking about exactly?

            Comment


              #7
              ...and thanks for fixing the problem :-)

              Comment


                #8
                I was referring to the Bobby = Spread(instrument, 100, 100) multipliers.
                Attached Files
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  Oh ok my bad. Forgot I had done that. I tried going with big numbers because I thought there might be some kind of rounding error.

                  Thanks again for the help!

                  Comment


                    #10
                    Hi Austin,

                    This code still does not work. Current code does not do anything on the "bobby" the spread. The if condition has the following line:

                    if (SMA(Spread("6C 09-10",100,100),2)[0] > 100)

                    However the spread here is not bobby so you actually do not touch bobby inside the onbarupdate. When you replace this with if (SMA(bobby,2)[0] > 100) the results are again weird.

                    Could you please check this?

                    Thanks

                    Comment


                      #11
                      aseke,

                      Your two lines are exactly the same. I am not sure why you think the SMA(Spread...) is not the same as SMA(bobby...).

                      In the code Austin posted he has this in Initialize(): bobby = Spread("6C 09-10", 1, 1);

                      This line means "bobby" is the same thing as typing out Spread("6C 09-10", 1, 1).

                      If you are looking for Spread() indicator parameters with 100 instead of 1 you will need to change in the code for the references to be 100 and not 1. Austin has it set to 1 right now.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Ok here is the problem, nice and clear.

                        Attached are two files - one is the indicator (MySpreadFromNT.cs), the other is the strategy (TestAccount.cs).

                        The indicator works fine on its own.

                        When you add it to a strategy, it works fine.

                        When you add it to a strategy, but then use it to calculate something else....well everything seems to break down (see the IF statement in the strategy - comment it out to check that it works fine without any operations).

                        These two files are as clear as they get.

                        Please let me know why this is happening and how to fix it. This seems to be a pretty serious bug.
                        Attached Files

                        Comment


                          #13
                          This is because you are trying to host an indicator with this line: CalculateOnBarClose = false;

                          Indicators that are going to be hosted cannot have any CalculateOnBarClose lines at all. Please remove this line from the indicator and then try it again. The hosted indicator will use the CalculateOnBarClose setting of the host.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Which code are you talking about? The only place I see this in the two files I sent is in the "NinjaScript generated code. Neither change nor remove" region of the indicator.

                            I also made the changes you said and it still doesn't work well in the strategy. Even with BarsRequired = 0. The indicator populates in a fraction of a second, and the strategy takes over 30 seconds...

                            Comment


                              #15
                              You should not touch code in that section ever. If you have already made changes to that section unfortunately I would not know what undesirable effects would have occurred cause of it.

                              The line I was referring to was in your MySpreadFromNT indicator. This is the Initialize() method you have:
                              Code:
                              protected override void Initialize()
                                      {
                                          Add(new Plot(Color.FromKnownColor(KnownColor.SlateGray), PlotStyle.Line, "Plot0"));    
                                          barsPeriod = Math.Max(1, BarsPeriods[0].Value);
                                          Add(leg2,BarsPeriod.Id, barsPeriod);
                                          Overlay                = false;
                                          [COLOR=Red][B]CalculateOnBarClose = false;[/B][/COLOR]
                                      }
                              The line in red needs to be removed.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by trilliantrader, Today, 08:16 AM
                              2 responses
                              6 views
                              0 likes
                              Last Post trilliantrader  
                              Started by samish18, Today, 08:31 AM
                              1 response
                              1 view
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by Creamers, 09-08-2023, 10:26 AM
                              6 responses
                              157 views
                              0 likes
                              Last Post JonyGurt  
                              Started by funk10101, Today, 08:14 AM
                              1 response
                              2 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by bill2023, Yesterday, 08:51 AM
                              3 responses
                              22 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X