It almost works, but there are gaps between the green and orange colored plots (see attachment.)
The modified indicator code looks like this:
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>
/// The Commodity Channel Index (CCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
/// </summary>
[Description("The Commodity Channel Index (CCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.")]
[Gui.Design.DisplayName("CCIcolored1 (Commodity Channel Index)")]
public class CCIcolored1 : Indicator
{
#region Variables
// Wizard generated variables
private int period = 14; // Default setting for Period
// User defined variables (add any user defined variables below)
#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(new Plot(Color.Orange, PlotStyle.Line, "CCIorange"));
Add(new Plot(Color.Green, PlotStyle.Line, "CCIgreen"));
Add(new Line(Color.DarkGray, 200, "Level2"));
Add(new Line(Color.DarkGray, 100, "Level1"));
Add(new Line(Color.DarkGray, 0, "ZeroLine"));
Add(new Line(Color.DarkGray, -100, "LevelM1"));
Add(new Line(Color.DarkGray, -200, "LevelM2"));
Period = 14;
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
Value.Set(0);
else
{
double mean = 0;
for (int idx = Math.Min(CurrentBar, Period - 1); idx >= 0; idx--)
mean += Math.Abs(Typical[idx] - SMA(Typical, Period)[0]);
double plotval = (Typical[0] - SMA(Typical, Period)[0])
/ (mean == 0 ? 1 : (0.015 * (mean / Math.Min(Period, CurrentBar + 1))));
int val_idx = (Math.Abs(plotval) >= 100) ? 1 : 0; // Select Green plot color for oversold (<-100)
// or overbought (>+100).
Values[ val_idx].Set( plotval ); // Plot a point on the chart, using Orange (Values[0]) or Green (Values[1]).
}
}
#region Properties
[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 CCIorange
{
get { return Values[0]; }
}
[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 CCIgreen
{
get { return Values[1]; }
}
[Description("Numbers of bars used for calculations")]
[Category("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}
#endregion
}
}
Any ideas?
KBJ
Comment