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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    57 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    78 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    41 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    101 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    61 views
    0 likes
    Last Post PaulMohn  
    Working...
    X