Thanks
Jim
Also, I tried to change the 12 and 26 EMA to and SMA but I received and error when compiling. Do you not simply change all instances of fastEma to fastSma?
{
/// <summary>
/// The MACDBarColor (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.
/// </summary>
[Description("The MACDBarColor (Moving Average Convergence/Divergence) is a trend following momentum indicator that shows the relationship between two moving averages of prices.")]
public class MACDBarColor : Indicator
{
#region Variables
private int fast = 12;
private int slow = 26;
private int smooth = 9;
private DataSeries fastEma;
private DataSeries slowEma;
private bool above = true;
private Color aboveColor = Color.Green;
private Color belowColor = Color.Red;
#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.Green, "Macd"));
Add(new Plot(Color.DarkViolet, "Avg"));
Add(new Plot(new Pen(Color.Navy, 5), PlotStyle.Bar, "Diff"));
Add(new Line(Color.DarkGray, 0, "Zero line"));
fastEma = new DataSeries(this);
slowEma = new DataSeries(this);
}
/// <summary>
/// Calculates the indicator value(s) at the current index.
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
{
fastEma.Set(Input[0]);
slowEma.Set(Input[0]);
Value.Set(0);
Avg.Set(0);
Diff.Set(0);
}
else
{
fastEma.Set((2.0 / (1 + Fast)) * Input[0] + (1 - (2.0 / (1 + Fast))) * fastEma[1]);
slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);
double macd = fastSma[0] - slowDma[0];
double macdAvg = (2.0 / (1 + Smooth)) * macd + (1 - (2.0 / (1 + Smooth))) * Avg[1];
Value.Set(macd);
Avg.Set(macdAvg);
if(CrossAbove(Value, Avg, 1))
above = true;
else if (CrossBelow(Value, Avg, 1))
above = false;
if(above)
BarColor = AboveColor;
else
BarColor = BelowColor;
Diff.Set(macd - macdAvg);
}
}
#region Properties
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Avg
{
get { return Values[1]; }
}
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Default
{
get { return Values[0]; }
}
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Diff
{
get { return Values[2]; }
}
// Create our user definable color input
[XmlIgnore()]
[Description("Color for painted region")]
[GridCategory("Parameters")]
public Color AboveColor
{
get { return aboveColor; }
set { aboveColor = value; }
}
// Serialize our Color object
[Browsable(false)]
public string AboveColorSerialize
{
get { return NinjaTrader.Gui.Design.SerializableColor.ToString( aboveColor); }
set { aboveColor = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
}
[XmlIgnore()]
[Description("Color for painted region")]
[GridCategory("Parameters")]
public Color BelowColor
{
get { return belowColor; }
set { belowColor = value; }
}
[Browsable(false)]
public string BelowColorSerialize
{
get { return NinjaTrader.Gui.Design.SerializableColor.ToString( belowColor); }
set { belowColor = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
}
/// <summary>
/// </summary>
[Description("Number of bars for fast EMA")]
[GridCategory("Parameters")]
public int Fast
{
get { return fast; }
set { fast = Math.Max(1, value); }
}
/// <summary>
/// </summary>
[Description("Number of bars for slow EMA")]
[GridCategory("Parameters")]
public int Slow
{
get { return slow; }
set { slow = Math.Max(1, value); }
}
/// <summary>
/// </summary>
[Description("Number of bars for smoothing")]
[GridCategory("Parameters")]
public int Smooth
{
get { return smooth; }
set { smooth = Math.Max(1, value); }
}
#endregion
}
}
Comment