Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can variables from OR prevent variables from OBU from working?

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

    Can variables from OR prevent variables from OBU from working?

    Hello,

    I have this problem. I added those New Calculation as you can see in the code sample. They are all private doubles. AddPlot(Brushes.Yellow, "candle"); only display one yellow dot on the chart and candle wont return right answer.



    Code:
    protected override void OnBarUpdate()
    {
    if(visible)
    {
    Draw.HorizontalLine(this, "tag3", azure, azure141);
    }
    else{
    }
    
    
    if(visibleY) {
    Draw.HorizontalLine(this, "tag4", jaune, Yellow161);
    }
    else {}
    
    
    
    [U][I][B]// New calculation[/B][/I][/U]
    
    if(CurrentBar < 195) return;
    
    
    
    double closePrice16 = Bars.GetClose(194);
    double closePrice9 = Bars.GetClose(195);
    
    double difCP = closePrice16 - closePrice9;
    
    
    
    double candle = Close[0] + difCP + difCP00;
    
    if(CurrentBar < 389) return;
    
    double closePrice159 = Bars.GetClose(389);
    double closePrice89 = Bars.GetClose(390);
    
    difCP00 = closePrice159 - closePrice89;
    
    
    Value[0] = candle;
    
    }
    Why it wont display the yellow AddPlot and return the right answer?

    TY

    #2
    Hello frankduc,

    Thank you for your post.

    I'm not able to compile this as is - difCP00 is not defined anywhere within your code sample. However, I'm confused as to what you're trying to do here. You're only assigning a value to Value[0] if CurrentBar is greater than 389, so I wouldn't expect to see this print the value of candle until after that point. After that it I would expect it to print this value for each bar:


    candle = Close[0] + difCP + difCP00;

    Is this not what you're seeing? Can you elaborate on the intent of the script?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Kate,

      I want it to make it short (the code) because it is larger. Anyway its ok, i check the returns and result is as expected. I cant display addplot in the chart but its ok i dont need it.

      TY

      Comment


        #4
        Kate,

        I can understand why this prevent this morning my code from working. It was working on friday, i counld'nt make addplot display but at least the code calculation was working.


        Code:
        protected override void OnBarUpdate()
        {
        if(visible)
        {
        Draw.HorizontalLine(this, "tag3", azure, azure141);
        }
        else{
        }
        
        
        if(CurrentBar < 1) return;
        
        double closePrice16 = Bars.GetClose(0);
        double closePrice9 = Bars.GetClose(0);
        
        double difCP = closePrice16 - closePrice9;
        
        
        double candle = Close[0] + difCP + difCPc;
        Value[0] = candle;
        
        if(CurrentBar < 194) return;
        
        double closePricec = Bars.GetClose(194);
        double closePricec1 = Bars.GetClose(195);
        
        difCPc = closePricec - closePricec1;
        
        
        
        
        }

        Once i am adding that part of the code the indicator stop working:


        Code:
        if(CurrentBar < 1) return;
        
        double closePrice16 = Bars.GetClose(0);
        double closePrice9 = Bars.GetClose(0);
        
        double difCP = closePrice16 - closePrice9;
        
        
        double candle = Close[0] + difCP + difCPc;
        Value[0] = candle;
        
        if(CurrentBar < 194) return;
        
        double closePricec = Bars.GetClose(194);
        double closePricec1 = Bars.GetClose(195);
        
        difCPc = closePricec - closePricec1;   //private double difCPc in public class FiFOajust1Min : Indicator{
        
        
        
        }
        OBU is a very fragile class to work with.

        I get an error saying

        Indicator 'FiFOajust1Min': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.


        Comment


          #5
          Hello frankduc,

          Thank you for your reply.

          I'm not seeing an error when I copy the second code snippet into an indicator. Can you provide the entire current code you have?

          Thanks in advance; I look forward to assisting you further.

          Comment


            #6
            Kate,

            Here's a reproduction of the error with a smaller code:
            In this case azure line is displayed but the error message keep coming back and this is preventing the rest of my indicator from working.
            I did'nt provide a full indicator code, i dont want to go public and its more than a 1000 lines.

            Error: Indicator 'MyCustomIndicator6': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.

            But i think i have found a part of the problem. If you add/ remove AddPlot(Brushes.Yellow, "candle"); the yellow line wont be displayed, but neither are the data if you print candle.
            Do you know why? Is it not possible to print candle and access the data without AddPlot(Brushes.Yellow, "candle");?

            In my indicator here's an image attach what it looks like. Only a yellow dot appear and not that part of the indicator. Is there another place we can move AddPlot than if (State == State.SetDefaults)?

            Code:
            #region Using declarations
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.ComponentModel.DataAnnotations;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows;
            using System.Windows.Input;
            using System.Windows.Media;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Gui;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Gui.SuperDom;
            using NinjaTrader.Gui.Tools;
            using NinjaTrader.Data;
            using NinjaTrader.NinjaScript;
            using NinjaTrader.Core.FloatingPoint;
            using NinjaTrader.NinjaScript.DrawingTools;
            #endregion
            
            //This namespace holds Indicators in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Indicators
            {
            public class MyCustomIndicator6 : Indicator
            {
            private double difCPc = 0;
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter the description for your new custom Indicator here.";
            Name = "MyCustomIndicator6";
            Calculate = Calculate.OnBarClose;
            IsOverlay = true;
            DisplayInDataBox = true;
            DrawOnPricePanel = true;
            DrawHorizontalGridLines = true;
            DrawVerticalGridLines = true;
            PaintPriceMarkers = true;
            ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
            //Disable this property if your indicator requires custom values that cumulate with each new market data event.
            //See Help Guide for additional information.
            IsSuspendedWhileInactive = true;
            }
            else if (State == State.Configure)
            {
            }
            }
            
            protected override void OnBarUpdate()
            {
            
            Draw.HorizontalLine(this, "tag3", 3830, Brushes.Azure);
            
            
            if(CurrentBar < 1) return;
            
            double closePrice16 = Bars.GetClose(0);
            double closePrice9 = Bars.GetClose(0);
            
            double difCP = closePrice16 - closePrice9;
            
            
            double candle = Close[0] + difCP + difCPc;
            Value[0] = candle;
            
            if(CurrentBar < 194) return;
            
            double closePricec = Bars.GetClose(194);
            double closePricec1 = Bars.GetClose(195);
            
            difCPc = closePricec - closePricec1;
            
            
            
            
            }
            }
            }
            
            #region NinjaScript generated code. Neither change nor remove.
            
            namespace NinjaTrader.NinjaScript.Indicators
            {
            public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
            {
            private MyCustomIndicator6[] cacheMyCustomIndicator6;
            public MyCustomIndicator6 MyCustomIndicator6()
            {
            return MyCustomIndicator6(Input);
            }
            
            public MyCustomIndicator6 MyCustomIndicator6(ISeries<double> input)
            {
            if (cacheMyCustomIndicator6 != null)
            for (int idx = 0; idx < cacheMyCustomIndicator6.Length; idx++)
            if (cacheMyCustomIndicator6[idx] != null && cacheMyCustomIndicator6[idx].EqualsInput(input))
            return cacheMyCustomIndicator6[idx];
            return CacheIndicator<MyCustomIndicator6>(new MyCustomIndicator6(), input, ref cacheMyCustomIndicator6);
            }
            }
            }
            
            namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
            {
            public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
            {
            public Indicators.MyCustomIndicator6 MyCustomIndicator6()
            {
            return indicator.MyCustomIndicator6(Input);
            }
            
            public Indicators.MyCustomIndicator6 MyCustomIndicator6(ISeries<double> input )
            {
            return indicator.MyCustomIndicator6(input);
            }
            }
            }
            
            namespace NinjaTrader.NinjaScript.Strategies
            {
            public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
            {
            public Indicators.MyCustomIndicator6 MyCustomIndicator6()
            {
            return indicator.MyCustomIndicator6(Input);
            }
            
            public Indicators.MyCustomIndicator6 MyCustomIndicator6(ISeries<double> input )
            {
            return indicator.MyCustomIndicator6(input);
            }
            }
            }
            
            #endregion
            TY
            Attached Files

            Comment


              #7
              Hello frankduc,

              Thank you for your reply.

              I do not receive any errors when running the above on an ES chart. I note your screenshot is running what appears to be your full indicator and not just this code you've provided "MyCustomIndicator6". Can you confirm if you get the error running just the above code?

              Thanks in advance; I look forward to assisting you further.

              Comment


                #8
                Kate,

                The above code produce this error:

                Indicator 'MyCustomIndicator6': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.

                To remove the error AddPlot(Brushes.Yellow, "candle"); must be added in
                if (State == State.SetDefaults).

                The question is, can you make the code work without AddPlot(Brushes.Yellow, "candle");?

                Also i cant export the values of my varialbe candle in OnRender. Why? Even if all my variables are private in class variable in public class FiFOajust1Min : Indicator
                {, i cant use my variable candle in OnRender.

                Is there a technical reason why?

                TY

                Comment


                  #9
                  Hello frankduc,

                  Thank you for your reply.

                  I do not receive any error with the above code in OnBarUpdate only. However, you do have candle defined as both a variable in OnBarUpdate and a plot, so that is likely what is causing the error. I'm using a slightly different version that uses "MyPlot" as the plot name instead of "candle" and that works fine. I'm attaching that version below.

                  You cannot access the value of candle in OnRender because you have instantiated the variable within OnBarUpdate, so it is local to that method. You would need to declare the variable outside of OnBarUpdate but inside the indicator class as in the example I'm attaching below.

                  Please let us know if we may be of further assistance to you.
                  Attached Files

                  Comment


                    #10
                    Kate,

                    Forget post #8 i have found why i could'nt get the result. It was due to
                    Value[0].

                    Now i just want to know if its possible to export the values of candle variable in OnRender?

                    Because the following code only send the last result of candle and not all the values.

                    Code:
                    #region Using declarations
                    using System;
                    using System.Collections.Generic;
                    using System.ComponentModel;
                    using System.ComponentModel.DataAnnotations;
                    using System.Linq;
                    using System.Text;
                    using System.Threading.Tasks;
                    using System.Windows;
                    using System.Windows.Input;
                    using System.Windows.Media;
                    using System.Xml.Serialization;
                    using NinjaTrader.Cbi;
                    using NinjaTrader.Gui;
                    using NinjaTrader.Gui.Chart;
                    using NinjaTrader.Gui.SuperDom;
                    using NinjaTrader.Gui.Tools;
                    using NinjaTrader.Data;
                    using NinjaTrader.NinjaScript;
                    using NinjaTrader.Core.FloatingPoint;
                    using NinjaTrader.NinjaScript.DrawingTools;
                    #endregion
                    
                    //This namespace holds Indicators in this folder and is required. Do not change it.
                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                    public class MyCustomIndicator6 : Indicator
                    {
                    private double candle = 0;
                    
                    private double closePrice16 = 0;
                    private double closePrice9 = 0;
                    private double closePricec = 0;
                    private double closePricec1 = 0;
                    private double difCP = 0;
                    private double difCPc = 0;
                    
                    protected override void OnStateChange()
                    {
                    if (State == State.SetDefaults)
                    {
                    Description = @"Enter the description for your new custom Indicator here.";
                    Name = "MyCustomIndicator6";
                    Calculate = Calculate.OnBarClose;
                    IsOverlay = true;
                    DisplayInDataBox = true;
                    DrawOnPricePanel = true;
                    DrawHorizontalGridLines = true;
                    DrawVerticalGridLines = true;
                    PaintPriceMarkers = true;
                    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive = true;
                    AddPlot(Brushes.Yellow, "candle");
                    }
                    else if (State == State.Configure)
                    {
                    }
                    }
                    
                    protected override void OnBarUpdate()
                    {
                    
                    
                    if(CurrentBar < 1) return;
                    
                    
                    
                    closePrice16 = Bars.GetClose(0);
                    closePrice9 = Bars.GetClose(0);
                    
                    difCP = closePrice16 - closePrice9;
                    
                    
                    
                    
                    
                    
                    candle = Close[0] + difCP + difCPc ;
                    
                    
                    
                    if(CurrentBar < 194) return;
                    
                    closePricec = Bars.GetClose(194);
                    closePricec1 = Bars.GetClose(195);
                    
                    difCPc = closePricec - closePricec1;
                    
                    
                    
                    
                    
                    
                    
                    
                    }
                    
                    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                    {
                    
                    
                    
                    Print("Can"+candle);
                    
                    }
                    }
                    }
                    
                    #region NinjaScript generated code. Neither change nor remove.
                    
                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                    {
                    private MyCustomIndicator6[] cacheMyCustomIndicator6;
                    public MyCustomIndicator6 MyCustomIndicator6()
                    {
                    return MyCustomIndicator6(Input);
                    }
                    
                    public MyCustomIndicator6 MyCustomIndicator6(ISeries<double> input)
                    {
                    if (cacheMyCustomIndicator6 != null)
                    for (int idx = 0; idx < cacheMyCustomIndicator6.Length; idx++)
                    if (cacheMyCustomIndicator6[idx] != null && cacheMyCustomIndicator6[idx].EqualsInput(input))
                    return cacheMyCustomIndicator6[idx];
                    return CacheIndicator<MyCustomIndicator6>(new MyCustomIndicator6(), input, ref cacheMyCustomIndicator6);
                    }
                    }
                    }
                    
                    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                    {
                    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                    {
                    public Indicators.MyCustomIndicator6 MyCustomIndicator6()
                    {
                    return indicator.MyCustomIndicator6(Input);
                    }
                    
                    public Indicators.MyCustomIndicator6 MyCustomIndicator6(ISeries<double> input )
                    {
                    return indicator.MyCustomIndicator6(input);
                    }
                    }
                    }
                    
                    namespace NinjaTrader.NinjaScript.Strategies
                    {
                    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                    {
                    public Indicators.MyCustomIndicator6 MyCustomIndicator6()
                    {
                    return indicator.MyCustomIndicator6(Input);
                    }
                    
                    public Indicators.MyCustomIndicator6 MyCustomIndicator6(ISeries<double> input )
                    {
                    return indicator.MyCustomIndicator6(input);
                    }
                    }
                    }
                    
                    #endregion

                    Comment


                      #11
                      Kate,

                      Even with your example of Frankductest it only display in Output window the last price traded and not all the prices calculated.

                      Is there a difference between

                      private double candle = 0;
                      and

                      private double candle;

                      Print candle inside OBU will return all prices from calculation when printed inside OR return only last price.

                      TY

                      Comment


                        #12
                        Hello frankduc,

                        Thank you for your reply.

                        The first is simply instantiating the variable without assigning it a value yet. Since we're going to assign it a value in on bar update, it's not really necessary to set it to 0 immediately. Generally you'd want to instantiate at the class level and then set a default value to the variable in State.SetDefaults, though in this case it's not totally necessary.

                        Why are you trying to print the value of candle in OnRender?

                        Printing the value of candle inside OBU would be expected to return the value of candle on each bar. Printing it inside OnRender would only print the value of candle for when OnRender is called, when it's actually rendering something.

                        Thanks in advance; I look forward to assisting you further.

                        Comment


                          #13
                          Kate,

                          I am printing in OnRender to make sure i can access all the value of candle on each bar. My algorithm use some rendering so most of the calculation are in OnRender.
                          I need to be able to use the variable candle in OnRender.

                          Are you saying the code i provided in post 10 will never return all values in OnRender? If its the case that's bad

                          I started yesterday to reproduce the calculation of that code to make it work in OnRender. But again i have a bug.

                          TY

                          Comment


                            #14
                            Kate,

                            I found a solution.

                            Thank you

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            577 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
                            553 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