Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating 3 Moving Average Indicator

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

    Creating 3 Moving Average Indicator

    Hi,

    I am just getting started with NinjaScript and I was trying to plot 3 moving averages but it doesn't seem to work it right. It is only showing 1 moving average with the below code. Kindly help me identify what is wrong with my coding and is there something missing to associate the value of plots from the moving averages variables. Thanks.


    region Using declarations
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Windows.Media;
    #endregion

    // This namespace holds indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators.KezyIndicators

    {
    /// <summary>
    /// The SMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
    /// </summary>
    public class MyMA3 : Indicator
    {
    private EMA emaFast;
    private SMA smaMedium;
    private SMA smaSlow;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "My 3 moving Average, Fast EMA, Medium SMA, Slow SMA";
    Name = "MyMA3";
    IsOverlay = true;
    FastEMA = 9;
    MediumSMA = 50;
    SlowSMA = 200;

    AddPlot(Brushes.White, "Fast");
    AddPlot(Brushes.Cyan, "Medium");
    AddPlot(Brushes.Magenta, "Slow");
    }
    else if (State == State.DataLoaded)
    {
    emaFast = EMA(FastEMA);
    smaMedium = SMA(MediumSMA);
    smaSlow = SMA(SlowSMA);
    }
    }

    protected override void OnBarUpdate()
    {
    Fast[0] = emaFast[0];
    Medium[0] = smaMedium[0];
    Slow[0] = smaSlow[0];

    Plots[0].Brush = Brushes.White;
    Plots[1].Brush = Brushes.Cyan;
    Plots[2].Brush = Brushes.Magenta;
    }

    public Series<double> Fast
    {
    get { return Values[0]; }
    }

    public Series<double> Medium
    {
    get { return Values[1]; }
    }

    public Series<double> Slow
    {
    get { return Values[2]; }
    }

    region Properties
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "FastEMA", GroupName = "MA Parameters", Order = 0)]
    public int FastEMA
    { get; set; }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "MediumSMA", GroupName = "MA Parameters", Order = 0)]
    public int MediumSMA
    { get; set; }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "SlowSMA", GroupName = "MA Parameters", Order = 0)]
    public int SlowSMA
    { get; set; }
    #endregion

    #2
    Hello kezyclaire,

    Thanks for your post.

    Please see the attached example script demonstrating how to add three plots to a script using AddPlot() and how to assign values to those plots.

    You could compare the attached example script to your script to see where differences might be.

    See the help guide documentation below for more information about AddPlot() and assigning values to plots using the Values collections.

    AddPlot: https://ninjatrader.com/support/help...t8/addplot.htm
    Values: https://ninjatrader.com/support/help.../nt8/value.htm

    Please let me know if I may assist further.
    Attached Files
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    647 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
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X