Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Failure to plot values except when I solve for only the current bar " Close[0]

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

    Failure to plot values except when I solve for only the current bar " Close[0]

    Hello Ninjascript forum.
    I am interested in looking at one of many possible Kalman Filters.
    The value returns to the Series<double> and from there the Plot correctly only when I do not use loops to solve the Kalman equations over some period of time.


    Code:
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class DynamicKalmanfilterPrice : Indicator
        {    
            
            private Series<double> KalmanFilterPrice;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"We are testing a less choppy price using the Kalman Filter. ";
                    Name                                        = "DynamicKalmanfilterPrice";
                    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.BlueViolet, "KalmanPrice");
                    
                    
                    
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {    
                
                    KalmanFilterPrice = new Series<double>(this);
                }
            }
    
        
    
            protected override void OnBarUpdate()
        
    {
      
    /// UNSOLVED Kalman Filter which returns a value very similar to price
    //                /// Kalman filter variables
                    double x = 0; /// State variable
                    double p = 1; /// State covariance
                    double k = 0; /// Kalman gain
                    double q = 0.0001; /// Process noise
                    double r = 0.01; /// Measurement noise
                    /// Update the Kalman filter variables
                    
                    ///Prediction Step
                    x=x;
                    p=p+q;
                    
                    ///Update setup
                    k = p / ( p + r );
                    x = x + ( k * ( Close[0] - x ) );
                    p = ( 1 - k ) * p;
                
          KalmanFilterPrice[0] =x;
            KalmanPrice[0] = KalmanFilterPrice[0];
    
    
     }
    
            #region Properties
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> KalmanPrice
            {
                get { return Values[0]; }
            }
            #endregion
    
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private DynamicKalmanfilterPrice[] cacheDynamicKalmanfilterPrice;
            public DynamicKalmanfilterPrice DynamicKalmanfilterPrice()
            {
                return DynamicKalmanfilterPrice(Input);
            }
    
            public DynamicKalmanfilterPrice DynamicKalmanfilterPrice(ISeries<double> input)
            {
                if (cacheDynamicKalmanfilterPrice != null)
                    for (int idx = 0; idx < cacheDynamicKalmanfilterPrice.Length; idx++)
                        if (cacheDynamicKalmanfilterPrice[idx] != null &&  cacheDynamicKalmanfilterPrice[idx].EqualsInput(input))
                            return cacheDynamicKalmanfilterPrice[idx];
                return CacheIndicator<DynamicKalmanfilterPrice>(new DynamicKalmanfilterPrice(), input, ref cacheDynamicKalmanfilterPrice);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.DynamicKalmanfilterPrice DynamicKalmanfilterPrice()
            {
                return indicator.DynamicKalmanfilterPrice(Input);
            }
    
            public Indicators.DynamicKalmanfilterPrice DynamicKalmanfilterPrice(ISeries<double> input )
            {
                return indicator.DynamicKalmanfilterPrice(input);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.DynamicKalmanfilterPrice DynamicKalmanfilterPrice()
            {
                return indicator.DynamicKalmanfilterPrice(Input);
            }
    
            public Indicators.DynamicKalmanfilterPrice DynamicKalmanfilterPrice(ISeries<double> input )
            {
                return indicator.DynamicKalmanfilterPrice(input);
            }
        }
    }
    
    #endregion
    ​
    This is a good test for me to verify that I placed the plot properly however the Kalman equation is only calculated for the current bar Close[0] and is not usable to characterize the price action,

    When I apply the sort of techniques one might expect for a solution I do not know how to return the results to outside of the loop {} to plot

    How should I plot a value such as this which is always being solved for Close[i] values?

    I apologize if this topic was already covered somewhere else however I have not been able to find it in any of the manuals and the C# techniques do not valid.
    Please see the attachment as the full calculation is a bit longer. I dont expect anyone to care about the calculation only to see how I should be plotting the
    KalmanFilterPrice[0] =x;
    KalmanPrice[0] = KalmanFilterPrice[0];​

    Attached Files

    #2
    Hello DynamicTest,

    When working with NinjaTrader scripts you don't need to do loops because the script is always processing the bars in a loop. When you apply a script its OnBarUpdate method will be called for every bar from 0 to the max count.

    If you needed to include past values you would need to use a series or a class level variable to store data so that when OnBarUpdate is called again you can reference the previous bars value to add to your current calculations.

    In the script you provided you have 3 loops, instead of using 3 loops you would do the first calculation for just this bar and store the result. Then do the second calculation and the third and finally plot the result. This is then repeated for each bar using OnBarUpdate, your calculation is already in a loop so you just need to calculate for each single bar.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    557 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 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
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X