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

Indicator Drawing Problem

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

    Indicator Drawing Problem

    Hello. Can someone please help me with this. I have Stochastis_Color indicator that plots different colors for D up and down slope.
    It's ok if it's set to draw on bar close. However there's a problem when I set it to draw in realtime ( Calculate on bar close- set to False). Another problem is when the colored line splits into two colors, it also displays 2 different price markers. Don't remember where I got this indicator from.
    But here are the pics how it draws in real time. Problem areas marked with circles. Can someone please tell me what needs to be changed.

    Here's the code:

    Code:
    // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// This is a sample indicator plotting a SMA in three colors.
        /// </summary>
        [Description("Stochastics plotted with three colors.")]
        [Gui.Design.DisplayName("Stochastics_Color")]
        public class Stochastics_Color : Indicator
        {
            #region Variables
            private int                    periodD    = 3;
            private int                    periodK    = 14;
            private int                    smooth    = 7;
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                // Add one plot for every color you wish to use.
                Add(new Plot(new Pen(Color.Black, 1), "K"));
                Add(new Plot(new Pen(Color.Green, 2), PlotStyle.Line, "Rising"));
                Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Line, "Falling"));
                Add(new Plot(new Pen(Color.Blue, 2), PlotStyle.Line, "Neutral"));
    
                Add(new Line(Color.Black, 20, "Lower"));
                Add(new Line(Color.Black, 80, "Upper"));
                Add(new Line(Color.Black, 45, "NLower"));
                Add(new Line(Color.Black, 55, "NUpper"));
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                K.Set(Stochastics(PeriodD, PeriodK, Smooth).K[0]);
                // Checks to make sure we have at least 1 bar before continuing
                if (CurrentBar < 1)
                    return;
    
                // Plot green if the %D is rising
                // Rising() returns true when the current value is greater than the value of the previous bar.
                if (Rising(Stochastics(PeriodD, PeriodK, Smooth).D))
                {
                    // Connects the rising plot segment with the other plots
                    RisingPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).D[1]);                
    
                    // Adds the new rising plot line segment to the line
                    RisingPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).D[0]);                
                }
    
                // Plot red if the %D is falling
                // Falling() returns true when the current value is less than the value of the previous bar.
                else if (Falling(Stochastics(PeriodD, PeriodK, Smooth).D))
                {
                    // Connects the new falling plot segment with the rest of the line
                    FallingPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).D[1]);
    
                    // Adds the new falling plot line segment to the line
                    FallingPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).D[0]);
                }
    
                // Plot yellow if the %D is neutral
                else
                {
                    // Connects the neutral plot segment with the rest of the line
                    NeutralPlot.Set(1, Stochastics(PeriodD, PeriodK, Smooth).D[1]);
    
                    // Adds the new neutral plot line segment to the line
                    NeutralPlot.Set(Stochastics(PeriodD, PeriodK, Smooth).D[0]);
                }
            }
    
            #region Properties
    
            /// <summary>
            /// Gets the fast K value.
            /// </summary>
            [Browsable(false)]
            [XmlIgnore()]
            public DataSeries K
            {
                get { return Values[0]; }
            }
    
            // Adds three DataSeries to store the values of the three plots added in Initialize()
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries RisingPlot
            {
                get { return Values[1]; }
            }
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries FallingPlot
            {
                get { return Values[2]; }
            }
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries NeutralPlot
            {
                get { return Values[3]; }
            }
    
            // Adds an input for the period of the SMA
            /// <summary>
            /// </summary>
            [Description("Numbers of bars used for calculations")]
            [Category("Parameters")]
            public int PeriodK
            {
                get { return periodK; }
                set { periodK = Math.Max(1, value); }
            }
    
            // Adds an input for the period of the SMA
            /// <summary>
            /// </summary>
            [Description("Numbers of bars used for calculations")]
            [Category("Parameters")]
            public int PeriodD
            {
                get { return periodD; }
                set { periodD = Math.Max(1, value); }
            }
    
            [Description("Smoothing period for SMA")]
            [Category("Parameters")]
            public int Smooth
            {
                get { return smooth; }
                set { smooth = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Had to remove part of code because I exceeded max number of characters.
    Also zip file included.

    Thank you.
    Attached Files

    #2
    Hello

    Thank you for the post.

    In this situation, I believe this is just due to how the indicator is programmed which does not include logic to account for CalculateOnBarClose = false.

    The double price markers and also the line that bleeds over would be caused by both the rising and falling conditions becoming true within the same bar. You can do a quick test to see this fairly easily by printing the condition and CurrentBar value, we would see something like the following:

    Code:
    if (Rising(Stochastics(PeriodD, PeriodK, Smooth).D))
    {
         Print("Rising " + CurrentBar);
    } else if (Falling(Stochastics(PeriodD, PeriodK, Smooth).D))
    {
         Print("Falling " + CurrentBar);
    }
    Rising 30733
    Rising 30733
    Falling 30733
    Rising 30733
    Falling 30733
    Falling 30733

    The Rising and Falling plots are only set when the condition becomes true which is how the single price marker works, if one of the plots is set but not the other, that price marker is used. Being that both plots are set for this bar, we get two price markers. You can also confirm this by using the DataBox and hovering over various bars, the plots which are not set report as N/A.

    To make this work in realtime similar to how it does in historical while using OnEachTick, you would need to add some logic to account for that however you see best fit toward your goal. Using FirstTickOfBar is one way to simulate OnBarClose to do something one time per bar. Alternatively, you could use Reset() to Reset the value of the opposing plot each time the condition is true.

    https://ninjatrader.com/support/help...lightsub=reset

    Code:
    FallingPlot.Reset();
    RisingPlot.Set(...
    
    and
    
    RisingPlot.Reset();
    FallingPlot.Set(...
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Rapine Heihei, 04-23-2024, 07:51 PM
    2 responses
    30 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by Shansen, 08-30-2019, 10:18 PM
    24 responses
    943 views
    0 likes
    Last Post spwizard  
    Started by Max238, Today, 01:28 AM
    0 responses
    9 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by rocketman7, Today, 01:00 AM
    0 responses
    7 views
    0 likes
    Last Post rocketman7  
    Started by wzgy0920, 04-20-2024, 06:09 PM
    2 responses
    28 views
    0 likes
    Last Post wzgy0920  
    Working...
    X