2023-11-14 20:16:45 +00:00
|
|
|
#region Using declarations
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
using NinjaTrader.Gui;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
// Do not change the namespace.
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
|
|
|
public class MovingAverage : Indicator
|
|
|
|
{
|
|
|
|
private EMA ema;
|
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
2024-06-07 13:11:26 +00:00
|
|
|
Description = "Basic moving average indicator";
|
2023-11-14 20:16:45 +00:00
|
|
|
Name = "Moving Average";
|
2024-06-07 13:11:26 +00:00
|
|
|
Calculate = Calculate.OnPriceChange;
|
2023-11-14 20:16:45 +00:00
|
|
|
IsOverlay = true;
|
|
|
|
IsSuspendedWhileInactive = true;
|
|
|
|
Period = 20;
|
|
|
|
AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "MA");
|
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
ema = EMA(Period);
|
|
|
|
}
|
|
|
|
else if (State == State.Historical)
|
|
|
|
{
|
|
|
|
SetZOrder(-1); // Display behind bars on chart.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
|
|
|
if (CurrentBar < Period)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MA[0] = ema[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Period + " " + "EMA"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Plots
|
|
|
|
[Browsable(false)]
|
|
|
|
[XmlIgnore]
|
|
|
|
public Series<double> MA
|
|
|
|
{
|
|
|
|
get { return Values[0]; }
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
[Range(1, int.MaxValue), NinjaScriptProperty]
|
|
|
|
[Display(Name = "Period", GroupName = "Parameters", Order = 0)]
|
|
|
|
public int Period { get; set; }
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region NinjaScript generated code. Neither change nor remove.
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
|
|
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
|
|
|
{
|
|
|
|
private MovingAverage[] cacheMovingAverage;
|
|
|
|
public MovingAverage MovingAverage(int period)
|
|
|
|
{
|
|
|
|
return MovingAverage(Input, period);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MovingAverage MovingAverage(ISeries<double> input, int period)
|
|
|
|
{
|
|
|
|
if (cacheMovingAverage != null)
|
|
|
|
for (int idx = 0; idx < cacheMovingAverage.Length; idx++)
|
|
|
|
if (cacheMovingAverage[idx] != null && cacheMovingAverage[idx].Period == period && cacheMovingAverage[idx].EqualsInput(input))
|
|
|
|
return cacheMovingAverage[idx];
|
|
|
|
return CacheIndicator<MovingAverage>(new MovingAverage(){ Period = period }, input, ref cacheMovingAverage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
|
|
{
|
|
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
|
|
{
|
|
|
|
public Indicators.MovingAverage MovingAverage(int period)
|
|
|
|
{
|
|
|
|
return indicator.MovingAverage(Input, period);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Indicators.MovingAverage MovingAverage(ISeries<double> input , int period)
|
|
|
|
{
|
|
|
|
return indicator.MovingAverage(input, period);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
|
|
{
|
|
|
|
public Indicators.MovingAverage MovingAverage(int period)
|
|
|
|
{
|
|
|
|
return indicator.MovingAverage(Input, period);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Indicators.MovingAverage MovingAverage(ISeries<double> input , int period)
|
|
|
|
{
|
|
|
|
return indicator.MovingAverage(input, period);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|