My first ever ninjascript and I cant see where I am going wrong.
I want to set a flag whenever there is a change in the direction of the MACD.Diff indicator. I cant get this to plot on a chart and it is not giving the expected results when I put this in the Market analyser.... can someone give me some pointers please?
The logic in my if statements are meant to be as follows (all in relation to MACD.Diff). If positive and [2] < [1] > [0] then return value of 1. If negative and [2] > [1] < [0] then return 2 otherwise return 0.
// 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 MACDTrend : Indicator
{
#region Variables
// Wizard generated variables
private int fast = 12; // Default setting for Fast
private int slow = 26; // Default setting for Slow
private int smooth = 9; // Default setting for Smooth
private int result = 0; // Default result
// 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.DeepSkyBlue), PlotStyle.Bar, "Plot0"));
CalculateOnBarClose = true;
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if (MACD(fast, slow, smooth).Diff[0] > 0 && MACD(fast, slow, smooth).Diff[2] < MACD(fast, slow, smooth).Diff[1] && MACD(fast, slow, smooth).Diff[1] > MACD(fast, slow, smooth).Diff[0])
{
result = 1;
Plot0.Set(result);
}
if (MACD(fast, slow, smooth).Diff[0] < 0 && MACD(fast, slow, smooth).Diff[2] > MACD(fast, slow, smooth).Diff[1] && MACD(fast, slow, smooth).Diff[1] < MACD(fast, slow, smooth).Diff[0])
{
result = 2;
Plot0.Set(result);
}
}
#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 Plot0
{
get { return Values[0]; }
}
[Description("")]
[GridCategory("Parameters")]
public int Fast
{
get { return fast; }
set { fast = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Slow
{
get { return slow; }
set { slow = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Smooth
{
get { return smooth; }
set { smooth = Math.Max(1, value); }
}
#endregion
}
}
Comment