196 lines
7.6 KiB
C#
196 lines
7.6 KiB
C#
|
#region Using declarations
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.ComponentModel;
|
||
|
using System.ComponentModel.DataAnnotations;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
using System.Windows;
|
||
|
using System.Windows.Input;
|
||
|
using System.Windows.Media;
|
||
|
using System.Xml.Serialization;
|
||
|
using NinjaTrader.Cbi;
|
||
|
using NinjaTrader.Gui;
|
||
|
using NinjaTrader.Gui.Chart;
|
||
|
using NinjaTrader.Gui.SuperDom;
|
||
|
using NinjaTrader.Gui.Tools;
|
||
|
using NinjaTrader.Data;
|
||
|
using NinjaTrader.NinjaScript;
|
||
|
using NinjaTrader.Core.FloatingPoint;
|
||
|
using NinjaTrader.NinjaScript.DrawingTools;
|
||
|
using NinjaTrader.Gui.PropertiesTest;
|
||
|
#endregion
|
||
|
|
||
|
namespace NinjaTrader.NinjaScript.Indicators
|
||
|
{
|
||
|
public class MovingAverageTrend : Indicator
|
||
|
{
|
||
|
private EMA fastMA;
|
||
|
private EMA slowMA;
|
||
|
|
||
|
protected override void OnStateChange()
|
||
|
{
|
||
|
if (State == State.SetDefaults)
|
||
|
{
|
||
|
Description = @"Colors bars on chart according to price in relation to fast and slow moving averages";
|
||
|
Name = "Moving Average Trend";
|
||
|
Calculate = Calculate.OnPriceChange;
|
||
|
IsOverlay = true;
|
||
|
DisplayInDataBox = true;
|
||
|
DrawOnPricePanel = true;
|
||
|
ScaleJustification = ScaleJustification.Right;
|
||
|
IsSuspendedWhileInactive = true;
|
||
|
FastMAPeriod = 20;
|
||
|
SlowMAPeriod = 100;
|
||
|
UptrendColor = Brushes.LimeGreen;
|
||
|
DowntrendColor = Brushes.Red;
|
||
|
NeutralColor = Brushes.Gray;
|
||
|
}
|
||
|
else if (State == State.DataLoaded)
|
||
|
{
|
||
|
fastMA = EMA(FastMAPeriod);
|
||
|
slowMA = EMA(SlowMAPeriod);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnBarUpdate()
|
||
|
{
|
||
|
if (CurrentBar < SlowMAPeriod)
|
||
|
{
|
||
|
BarBrushes[0] = NeutralColor;
|
||
|
CandleOutlineBrushes[0] = NeutralColor;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (IsFirstTickOfBar)
|
||
|
{
|
||
|
double currentClose = Close[1];
|
||
|
if (currentClose > fastMA[1] && currentClose > slowMA[1])
|
||
|
{
|
||
|
BarBrushes[0] = UptrendColor;
|
||
|
CandleOutlineBrushes[0] = UptrendColor;
|
||
|
}
|
||
|
else if (currentClose < fastMA[1] && currentClose < slowMA[1])
|
||
|
{
|
||
|
BarBrushes[0] = DowntrendColor;
|
||
|
CandleOutlineBrushes[0] = DowntrendColor;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
BarBrushes[0] = NeutralColor;
|
||
|
CandleOutlineBrushes[0] = NeutralColor;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public override string DisplayName
|
||
|
{
|
||
|
get { return Name; }
|
||
|
}
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Range(1, int.MaxValue)]
|
||
|
[Display(Name = "Fast MA Period", GroupName = "Moving Average Trend", Order = 1)]
|
||
|
public int FastMAPeriod { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Range(1, int.MaxValue)]
|
||
|
[Display(Name = "Slow MA Period", GroupName = "Moving Average Trend", Order = 2)]
|
||
|
public int SlowMAPeriod { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[XmlIgnore]
|
||
|
[Display(Name = "Uptrend Color", GroupName = "Moving Average Trend", Order = 3)]
|
||
|
public Brush UptrendColor { get; set; }
|
||
|
|
||
|
[Browsable(false)]
|
||
|
public string UptrendColorSerialization
|
||
|
{
|
||
|
get { return Serialize.BrushToString(UptrendColor); }
|
||
|
set { UptrendColor = Serialize.StringToBrush(value); }
|
||
|
}
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[XmlIgnore]
|
||
|
[Display(Name = "Downtrend Color", GroupName = "Moving Average Trend", Order = 4)]
|
||
|
public Brush DowntrendColor { get; set; }
|
||
|
|
||
|
[Browsable(false)]
|
||
|
public string DowntrendColorSerialization
|
||
|
{
|
||
|
get { return Serialize.BrushToString(DowntrendColor); }
|
||
|
set { DowntrendColor = Serialize.StringToBrush(value); }
|
||
|
}
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[XmlIgnore]
|
||
|
[Display(Name = "Neutral Color", GroupName = "Moving Average Trend", Order = 5)]
|
||
|
public Brush NeutralColor { get; set; }
|
||
|
|
||
|
[Browsable(false)]
|
||
|
public string NeutralColorSerialization
|
||
|
{
|
||
|
get { return Serialize.BrushToString(NeutralColor); }
|
||
|
set { NeutralColor = Serialize.StringToBrush(value); }
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#region NinjaScript generated code. Neither change nor remove.
|
||
|
|
||
|
namespace NinjaTrader.NinjaScript.Indicators
|
||
|
{
|
||
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
||
|
{
|
||
|
private MovingAverageTrend[] cacheMovingAverageTrend;
|
||
|
public MovingAverageTrend MovingAverageTrend(int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor)
|
||
|
{
|
||
|
return MovingAverageTrend(Input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor);
|
||
|
}
|
||
|
|
||
|
public MovingAverageTrend MovingAverageTrend(ISeries<double> input, int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor)
|
||
|
{
|
||
|
if (cacheMovingAverageTrend != null)
|
||
|
for (int idx = 0; idx < cacheMovingAverageTrend.Length; idx++)
|
||
|
if (cacheMovingAverageTrend[idx] != null && cacheMovingAverageTrend[idx].FastMAPeriod == fastMAPeriod && cacheMovingAverageTrend[idx].SlowMAPeriod == slowMAPeriod && cacheMovingAverageTrend[idx].UptrendColor == uptrendColor && cacheMovingAverageTrend[idx].DowntrendColor == downtrendColor && cacheMovingAverageTrend[idx].NeutralColor == neutralColor && cacheMovingAverageTrend[idx].EqualsInput(input))
|
||
|
return cacheMovingAverageTrend[idx];
|
||
|
return CacheIndicator<MovingAverageTrend>(new MovingAverageTrend(){ FastMAPeriod = fastMAPeriod, SlowMAPeriod = slowMAPeriod, UptrendColor = uptrendColor, DowntrendColor = downtrendColor, NeutralColor = neutralColor }, input, ref cacheMovingAverageTrend);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
||
|
{
|
||
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
||
|
{
|
||
|
public Indicators.MovingAverageTrend MovingAverageTrend(int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor)
|
||
|
{
|
||
|
return indicator.MovingAverageTrend(Input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor);
|
||
|
}
|
||
|
|
||
|
public Indicators.MovingAverageTrend MovingAverageTrend(ISeries<double> input , int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor)
|
||
|
{
|
||
|
return indicator.MovingAverageTrend(input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
namespace NinjaTrader.NinjaScript.Strategies
|
||
|
{
|
||
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
||
|
{
|
||
|
public Indicators.MovingAverageTrend MovingAverageTrend(int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor)
|
||
|
{
|
||
|
return indicator.MovingAverageTrend(Input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor);
|
||
|
}
|
||
|
|
||
|
public Indicators.MovingAverageTrend MovingAverageTrend(ISeries<double> input , int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor)
|
||
|
{
|
||
|
return indicator.MovingAverageTrend(input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|