#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
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>
/// Cumulative Delta of Buys/Sells
/// </summary>
[Description("Cumulative Delta of Buys/Sells")]
public class DTCDelta : Indicator
{
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
private int CDelta = 0;
private int LastBar = 0;
private double lastPrice = 0;
private double askPrice = 0;
private double bidPrice = 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.FromKnownColor(KnownColor.Green), PlotStyle.Bar, "DeltaPositive"));
Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Bar, "DeltaNegative"));
Add(new Line(Color.FromKnownColor(KnownColor.FloralWhite), 0, "ZeroLine"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if (CDelta > 0)
{
DeltaPositive.Set(CDelta);
}
else
{
DeltaNegative.Set(CDelta);
}
}
// Called on each tick
protected override void OnMarketData(MarketDataEventArgs e)
{
// Print some data to the Output window
if (e.MarketDataType == MarketDataType.Last)
{
lastPrice = e.Price;
if ((lastPrice >= askPrice) && (lastPrice > bidPrice))
{
CDelta += (int) e.Volume;
}
else if ((lastPrice <= bidPrice) && (lastPrice < askPrice))
{
CDelta -= (int) e.Volume;
}
else // neutrals are too insignificant to waste ram space and processing power, just return
return;
}
else if (e.MarketDataType == MarketDataType.Ask)
askPrice = e.Price;
else if (e.MarketDataType == MarketDataType.Bid)
bidPrice = e.Price;
}
#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 DeltaPositive
{
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 DeltaNegative
{
get { return Values[1]; }
}
#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 DTCDelta[] cacheDTCDelta = null;
private static DTCDelta checkDTCDelta = new DTCDelta();
/// <summary>
/// Cumulative Delta of Buys/Sells
/// </summary>
/// <returns></returns>
public DTCDelta DTCDelta()
{
return DTCDelta(Input);
}
/// <summary>
/// Cumulative Delta of Buys/Sells
/// </summary>
/// <returns></returns>
public DTCDelta DTCDelta(Data.IDataSeries input)
{
if (cacheDTCDelta != null)
for (int idx = 0; idx < cacheDTCDelta.Length; idx++)
if (cacheDTCDelta[idx].EqualsInput(input))
return cacheDTCDelta[idx];
lock (checkDTCDelta)
{
if (cacheDTCDelta != null)
for (int idx = 0; idx < cacheDTCDelta.Length; idx++)
if (cacheDTCDelta[idx].EqualsInput(input))
return cacheDTCDelta[idx];
DTCDelta indicator = new DTCDelta();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
Indicators.Add(indicator);
indicator.SetUp();
DTCDelta[] tmp = new DTCDelta[cacheDTCDelta == null ? 1 : cacheDTCDelta.Length + 1];
if (cacheDTCDelta != null)
cacheDTCDelta.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheDTCDelta = 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>
/// Cumulative Delta of Buys/Sells
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.DTCDelta DTCDelta()
{
return _indicator.DTCDelta(Input);
}
/// <summary>
/// Cumulative Delta of Buys/Sells
/// </summary>
/// <returns></returns>
public Indicator.DTCDelta DTCDelta(Data.IDataSeries input)
{
return _indicator.DTCDelta(input);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Cumulative Delta of Buys/Sells
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.DTCDelta DTCDelta()
{
return _indicator.DTCDelta(Input);
}
/// <summary>
/// Cumulative Delta of Buys/Sells
/// </summary>
/// <returns></returns>
public Indicator.DTCDelta DTCDelta(Data.IDataSeries input)
{
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.DTCDelta(input);
}
}
}
#endregion