Your help is welcome..
//
// Copyright (C) 2006, 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.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 (bdtCCI) 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 (bdtCCI) 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.")]
public class bdtCCI : Indicator
{
#region Variables
private int period = 14;
private Color upColor = Color.Green;
private Color downColor = Color.Red;
private Color stallColor = Color.Black;
private double prevValue = 0;
#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, "CCI"));
Add(new Line(Color.DarkGray, 200, "Level 2"));
Add(new Line(Color.DarkGray, 100, "Level 1"));
Add(new Line(Color.DarkGray, 0, "Zero line"));
Add(new Line(Color.DarkGray, -100, "Level -1"));
Add(new Line(Color.DarkGray, -200, "Level -2"));
DrawOnPricePanel = false;
}
/// <summary>
/// Calculates the indicator value(s) at the current index.
/// </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 plotValue = (Typical[0] - SMA(Typical, Period)[0]) / (mean == 0 ? 1 : (0.015 * (mean / Math.Min(Period, CurrentBar + 1))));
if (plotValue >= 0)
{
PlotColors[0][0] = (plotValue >= prevValue ? (plotValue == prevValue ? stallColor: upColor) : downColor);
}
else
{
PlotColors[0][0] = plotValue <= prevValue ? (plotValue == prevValue ? stallColor: downColor) : upColor;
}
Value.Set(plotValue);
if (plotValue >= Lines[1].Value) //100
DrawRegion("Overbought1-" + CurrentBar, 1, 0, Values[0], Lines[1].Value, upColor, upColor, 2);
if (plotValue >= Lines[0].Value) //200
DrawRegion("Overbought2-" + CurrentBar, 1, 0, Values[0], Lines[0].Value, upColor, upColor, 5);
if (plotValue <= Lines[3].Value) //-100
DrawRegion("Oversold1-" + CurrentBar, 1, 0, Values[0], Lines[3].Value, downColor, downColor, 2);
if (plotValue <= Lines[4].Value) //-200
DrawRegion("Oversold2-" + CurrentBar, 1, 0, Values[0], Lines[4].Value, downColor, downColor, 5);
prevValue = plotValue;
}
}
#region Properties
/// <summary>
/// </summary>
[Description("Numbers of bars used for calculations")]
[GridCategory("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private bdtCCI[] cachebdtCCI = null;
private static bdtCCI checkbdtCCI = new bdtCCI();
/// <summary>
/// The Commodity Channel Index (bdtCCI) 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>
/// <returns></returns>
public bdtCCI bdtCCI(int period)
{
return bdtCCI(Input, period);
}
/// <summary>
/// The Commodity Channel Index (bdtCCI) 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>
/// <returns></returns>
public bdtCCI bdtCCI(Data.IDataSeries input, int period)
{
if (cachebdtCCI != null)
for (int idx = 0; idx < cachebdtCCI.Length; idx++)
if (cachebdtCCI[idx].Period == period && cachebdtCCI[idx].EqualsInput(input))
return cachebdtCCI[idx];
lock (checkbdtCCI)
{
checkbdtCCI.Period = period;
period = checkbdtCCI.Period;
if (cachebdtCCI != null)
for (int idx = 0; idx < cachebdtCCI.Length; idx++)
if (cachebdtCCI[idx].Period == period && cachebdtCCI[idx].EqualsInput(input))
return cachebdtCCI[idx];
bdtCCI indicator = new bdtCCI();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.Period = period;
Indicators.Add(indicator);
indicator.SetUp();
bdtCCI[] tmp = new bdtCCI[cachebdtCCI == null ? 1 : cachebdtCCI.Length + 1];
if (cachebdtCCI != null)
cachebdtCCI.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cachebdtCCI = tmp;
return indicator;
}
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// The Commodity Channel Index (bdtCCI) 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>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.bdtCCI bdtCCI(int period)
{
return _indicator.bdtCCI(Input, period);
}
/// <summary>
/// The Commodity Channel Index (bdtCCI) 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>
/// <returns></returns>
public Indicator.bdtCCI bdtCCI(Data.IDataSeries input, int period)
{
return _indicator.bdtCCI(input, period);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// The Commodity Channel Index (bdtCCI) 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>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.bdtCCI bdtCCI(int period)
{
return _indicator.bdtCCI(Input, period);
}
/// <summary>
/// The Commodity Channel Index (bdtCCI) 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>
/// <returns></returns>
public Indicator.bdtCCI bdtCCI(Data.IDataSeries input, int period)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.bdtCCI(input, period);
}
}
}
#endregion

Comment