NexusFi: Find Your Edge


Home Menu

 





Measuring the slope of Kalman Filter Line (NinjaTrader)


Discussion in NinjaTrader

Updated
    1. trending_up 1,087 views
    2. thumb_up 1 thanks given
    3. group 3 followers
    1. forum 1 posts
    2. attach_file 0 attachments




 
Search this Thread

Measuring the slope of Kalman Filter Line (NinjaTrader)

  #1 (permalink)
 DogEars 
Arizona
 
Experience: Advanced
Platform: NinjaTrader
Posts: 1 since Dec 2020
Thanks Given: 1
Thanks Received: 0

Hi,

I've implemented a Kalman Filter into an indicator from one of the posts here and I'm having a heck of a time trying to measure the slope of the line in degrees like I've seen done in other indicators.

Was wondering if anyone could take a look at this and let me know where I'm going wrong. Thanks!

 
Code
public class KalmanFilter : Indicator
    {
        private double DeltaK;
        private double Smooth;
        private Series<double> Velocity;
        public double xSlope = 0;
        private const double radToDegrees = 180 / Math.PI;
        private int shallowAngle = 30;
        private int steepAngle = 60;
        private FilterDirection currentDirection;
        public FilterDirection CurrentDirection { get { Update(); return currentDirection; } }
        private FilterDirection directionStatus;
        public enum FilterDirection
        {
            Rising,
            Falling
        }

        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 = 190;
                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 = (radToDegrees * (Math.Atan((GainLevel[0] + (GainLevel[1]) / 2) / 1.56 / TickSize)));

            //Draw.TextFixed(this, "Gain", GainLevel[0] + " - Gain Level", TextPosition.BottomRight);
            //Draw.TextFixed(this, "Slope", xSlope + " - Current Slope", TextPosition.BottomLeft);

            Print("\tSlope @ : " + xSlope);

            if (IsRising(GainLevel))
            {
                Draw.TextFixed(this, "Rising", "Is Rising", TextPosition.TopRight);
                directionStatus = FilterDirection.Rising;
            }
            else
            {
                Draw.TextFixed(this, "Rising", "Is Falling", TextPosition.TopRight);
                directionStatus = FilterDirection.Falling;
            }

            currentDirection = directionStatus;

        }
I've tried a bunch of variations without success so far, this is the line in question


xSlope = (radToDegrees * (Math.Atan((GainLevel[0] + (GainLevel[1]) / 2) / 1.56 / TickSize)));

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Exit Strategy
NinjaTrader
How to apply profiles
Traders Hideout
MC PL editor upgrade
MultiCharts
Trade idea based off three indicators.
Traders Hideout
 
  #2 (permalink)
 
trendisyourfriend's Avatar
 trendisyourfriend 
Quebec Canada
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: AMP/CQG
Trading: ES, NQ, YM
Frequency: Daily
Duration: Minutes
Posts: 4,527 since Oct 2009
Thanks Given: 4,176
Thanks Received: 6,020

If you draw an oblique line from left to right, to find its slope you would procede like that:

I don't know if it will help but this is a piece of code i use in one of my project.

 
Code
float slope;
Point leftPoint, rightPoint;

leftPoint = new Point(100, 100); // Anchor point 1
rightPoint = new Point(200, 200); // Anchor point 2

// slope = rise over run (rise/run) or (Vertical Y / Time X)
slope = (float)(leftPoint.Y - rightPoint.Y) / (float)(leftPoint.X - rightPoint.X);

angleOfSlope = ( Math.Atan( slope ) * 180 / Math.PI ) *-1;
This may help too:
https://www.usgs.gov/science-support/osqi/yes/resources-teachers/determine-percent-slope-and-angle-slope

Reply With Quote
Thanked by:




Last Updated on July 1, 2021


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts