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:
// 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 } }
Also zip file included.
Thank you.
Comment