Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Data Not Updating OnBarUpdate()

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

    Data Not Updating OnBarUpdate()

    Hi,

    I've got a weird thing going and wanted to bounce this off you.

    Code:
    protected override void OnBarUpdate()
    public class KalmanFilter : Indicator
    {
    private double DeltaK;
    private double Smooth;
    private Series<double> Velocity;
    public int xSlope = 0;
    private const double radToDegrees = 180 / Math.PI;
    private int shallowAngle = 30;
    private int steepAngle = 60;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Kalman Filter.";
    Name = "KalmanFilter";
    Calculate = Calculate.OnPriceChange;
    IsOverlay = true;
    DisplayInDataBox = false;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = false;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    IsSuspendedWhileInactive = true;
    
    Gain = 200;
    UseAngle = true;
    fontSize = 12;
    
    
    AddPlot(Brushes.Red, "GainLevel");
    Plots[0].Width = 2;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    Velocity = new Series<double>(this, MaximumBarsLookBack.Infinite);
    }
    }
    
    protected override void OnBarUpdate()
    {
    var simpleFont = new SimpleFont("Arial", fontSize) { Size = fontSize, Bold = false };
    
    if (CurrentBar == 0)
    {
    GainLevel[0] = Input[0];
    Velocity[0] = 0;
    return;
    }
    
    DeltaK = Input[0] - GainLevel[1];
    Smooth = GainLevel[1] + DeltaK * Math.Sqrt((Gain / 10000) * 2);
    Velocity[0] = Velocity[1] + ((Gain / 10000) * DeltaK);
    GainLevel[0] = Smooth + Velocity[0];
    
    xSlope = (int)(radToDegrees * (Math.Atan((GainLevel[0] - (GainLevel[1]) / 2) / 1.56 / TickSize)));
    
    [B]THIS WORKS [/B] Draw.TextFixed(this, "Gain", GainLevel[0] + " - Gain Level", TextPosition.BottomRight);
    
    [B]THIS DOES NOT WORK[/B] Draw.TextFixed(this, "Slope", xSlope + " - Current Slope", TextPosition.BottomLeft);
    
    Print("\tSlope @ : " + xSlope);
    
    if (IsRising(GainLevel))
    {
    
    }
    }
    }
    I've got the method set to OnPriceChange and in the code above my xSlope gets an initial reading and never updates again on the screen or printing out to a status window. The other does work and updates in both places accordingly.

    I must be missing something really obvious here...

    #2
    Hello DogEars,

    Thank you for your reply.

    Looks like something might be amiss in your slope calculation. If you print the value of xSlope as a double, you'll see that it is changing, but minutely:

    Print("xSlope as double: " + (radToDegrees * (Math.Atan((GainLevel[0] - ((GainLevel[1]) / 2) / 1.56) / TickSize))));

    What are you attempting to display the slope in terms of, exactly? Have you taken a look at the Slope() function?



    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by rdtdale, Today, 01:02 PM
    0 responses
    3 views
    0 likes
    Last Post NinjaTrader_LuisH  
    Started by alifarahani, Today, 09:40 AM
    3 responses
    15 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by RookieTrader, Today, 09:37 AM
    4 responses
    18 views
    0 likes
    Last Post RookieTrader  
    Started by PaulMohn, Today, 12:36 PM
    0 responses
    9 views
    0 likes
    Last Post PaulMohn  
    Started by love2code2trade, 04-17-2024, 01:45 PM
    4 responses
    41 views
    0 likes
    Last Post love2code2trade  
    Working...
    X