Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Indicator of indicators

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

    Indicator of indicators

    Hello Support,

    I would like to create a single indicator that uses two SMA plots. How do I go about this? I tried using the Indicator Wizard, but that doesn't have an option for other indicators. I know how to create a Strategy with multiple indicators but I want this new custom indicator to be available to different strategies. So, rather than duplicate code, I would like to centralize the logic into a single indicator.

    Sample code would be greatly appreciated!

    Thanks!
    Matt

    PS: I did a bit of searching in the forums but I wasn't able to find anything. Sorry if this has been asked before; I'm sure it has.

    #2
    Hello StealthM93,

    Thank you for your reply.

    Just add two plots to your indicator, one for each SMA Plot. Once you have that, simply call the SMA twice, and each time you call it with different parameters, assign the latest value of the called SMA to one of the two plots.

    Here's a very simple example script that will plot 2 SMAs, with user inputs for the Period for each of the two.

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class DualSMAExample : Indicator
        {
            private SMA SMA1;
            private SMA SMA2;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "DualSMAExample";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    SMA1Period                    = 14;
                    SMA2Period                    = 25;
                    AddPlot(Brushes.Orange, "SMA1Plot");
                    AddPlot(Brushes.RoyalBlue, "SMA2Plot");
                }
                else if (State == State.DataLoaded)
                {
                    SMA1 = SMA(SMA1Period);
                    SMA2 = SMA(SMA2Period);
                }
            }
    
            protected override void OnBarUpdate()
            {
                SMA1Plot[0] = SMA1[0];
                SMA2Plot[0] = SMA2[0];
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="SMA1Period", Order=1, GroupName="Parameters")]
            public int SMA1Period
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="SMA2Period", Order=2, GroupName="Parameters")]
            public int SMA2Period
            { get; set; }
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> SMA1Plot
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> SMA2Plot
            {
                get { return Values[1]; }
            }
            #endregion
    
        }
    }​
    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Awesome, thank you, Kate!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      76 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      63 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      53 views
      0 likes
      Last Post CarlTrading  
      Working...
      X