Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

First ninjascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    First ninjascript

    Hi,

    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
    }
    }

    #2
    Hi lorgan,

    Thanks for the post and welcome to the NinjaTrader forums! If you check log tab of control center, there are probably error messages related to accessing index outside the bounds of the array.

    This item is described here:


    You can prevent the script from trying to access the first few bars by using a statement like this at the top of OnBarUpdate()

    if (CurrentBar < 2) return;
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Awesome - that was it. Thx

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      648 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      369 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      572 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      574 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X