#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>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class M2 : Indicator
{
#region Variables
// Wizard generated variables
private string second_instr = @"6E 09-15";
private int periodSMA = 20; // Default setting for PeriodSMA
// 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.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Delta"));
Add(Second_instr,BarsPeriod.Id, BarsPeriod.Value);
Overlay = false;
CalculateOnBarClose = false;
BarsRequired = periodSMA;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (BarsInProgress != 0) return;
if(Closes[0][0]<=0 || Closes[1][0]<=0) return;
if(CurrentBar < 1000 ) return;
Spread.Set(Closes[0][0]/Closes[1][0]);
Moving_Average.Set(SMA(Spread,periodSMA)[0]);
Delta.Set(Closes[0][0]-(Closes[1][0]*SMA(Spread,periodSMA)[0]));
}
#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 Spread
{
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 Moving_Average
{
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 Delta
{
get { return Values[2]; }
}
[Description("")]
[GridCategory("Parameters")]
public int PeriodSMA
{
get { return periodSMA; }
set { periodSMA = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public string Second_instr
{
get { return second_instr; }
set { second_instr = 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 M2[] cacheM2 = null;
private static M2 checkM2 = new M2();
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public M2 M2(int periodSMA, string second_instr)
{
return M2(Input, periodSMA, second_instr);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public M2 M2(Data.IDataSeries input, int periodSMA, string second_instr)
{
if (cacheM2 != null)
for (int idx = 0; idx < cacheM2.Length; idx++)
if (cacheM2[idx].PeriodSMA == periodSMA && cacheM2[idx].Second_instr == second_instr && cacheM2[idx].EqualsInput(input))
return cacheM2[idx];
lock (checkM2)
{
checkM2.PeriodSMA = periodSMA;
periodSMA = checkM2.PeriodSMA;
checkM2.Second_instr = second_instr;
second_instr = checkM2.Second_instr;
if (cacheM2 != null)
for (int idx = 0; idx < cacheM2.Length; idx++)
if (cacheM2[idx].PeriodSMA == periodSMA && cacheM2[idx].Second_instr == second_instr && cacheM2[idx].EqualsInput(input))
return cacheM2[idx];
M2 indicator = new M2();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.PeriodSMA = periodSMA;
indicator.Second_instr = second_instr;
Indicators.Add(indicator);
indicator.SetUp();
M2[] tmp = new M2[cacheM2 == null ? 1 : cacheM2.Length + 1];
if (cacheM2 != null)
cacheM2.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheM2 = 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>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.M2 M2(int periodSMA, string second_instr)
{
return _indicator.M2(Input, periodSMA, second_instr);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.M2 M2(Data.IDataSeries input, int periodSMA, string second_instr)
{
return _indicator.M2(input, periodSMA, second_instr);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.M2 M2(int periodSMA, string second_instr)
{
return _indicator.M2(Input, periodSMA, second_instr);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.M2 M2(Data.IDataSeries input, int periodSMA, string second_instr)
{
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.M2(input, periodSMA, second_instr);
}
}
}
#endregion