I have tried programming an indicator that changes the color of an EMA depending on whether another MA crosses above or below it.
Cross above = green
cross below = red
At the moment I have managed to change the color of the exact area where the crossing occurs, however I also need it to maintain the color until a crossing occurs again and change to red or green depending on the case. Please see the code below.
I attach a photo so that it is better understood. I also have an NT7 indicator that performed this function but I don't know how to convert it to Ninjatrader 8...
Any help will be very appreciated!
namespace NinjaTrader.NinjaScript.Indicators
{
public class MovingAverageColorChange : Indicator
{
private EMA ema;
private HMA hma;
private SMA sma;
private TEMA tema;
private TMA tma;
private WMA wma;
private bool SlopeColor = true;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Change color of Moving Average.";
Name = "MovingAverageColorChange";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
MAType = 1;
Period = 20;
Up = Brushes.Green;
Down = Brushes.Red;
AddPlot(Brushes.Transparent, "MA");
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
ema = EMA(Inputs[0], Period);
hma = HMA(Inputs[0], Period);
sma = SMA(Inputs[0], Period);
tma = TMA(Inputs[0], Period);
tema = TEMA(Inputs[0], Period);
wma = WMA(Inputs[0], Period);
}
}
protected override void OnBarUpdate()
{
switch (MAType)
{
case 1:
{
MA[0] = ema[0];
break;
}
case 2:
{
MA[0] = hma[0];
break;
}
case 3:
{
MA[0] = sma[0];
break;
}
case 4:
{
MA[0] = tma[0];
break;
}
case 5:
{
MA[0] = tema[0];
break;
}
case 6:
{
MA[0] = wma[0];
break;
}
}
if(SlopeColor == true)
if(CrossAbove(EMA(8), MA, 1)) {PlotBrushes[0][0] = Up;}
else if(CrossBelow(EMA(8), MA, 1)) {PlotBrushes[0][0] = Down;}
}
[HASHTAG="t3322"]region[/HASHTAG] Properties
[Range(1, 6), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "MAType", GroupName = "Parameters", Order = 0)]
public int MAType
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Period", Order=1, GroupName="Parameters")]
public int Period
{ get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name="Up", Order=2, GroupName="Parameters")]
public Brush Up
{ get; set; }
[Browsable(false)]
public string UpSerializable
{
get { return Serialize.BrushToString(Up); }
set { Up = Serialize.StringToBrush(value); }
}
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name="Down", Order=3, GroupName="Parameters")]
public Brush Down
{ get; set; }
[Browsable(false)]
public string DownSerializable
{
get { return Serialize.BrushToString(Down); }
set { Down = Serialize.StringToBrush(value); }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> MA
{
get { return Values[0]; }
}
#endregion
}
}

Comment