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.
//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
​
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];​

Comment