Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Awesome, thank you, Kate!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by WHICKED, Today, 12:45 PM
      2 responses
      16 views
      0 likes
      Last Post WHICKED
      by WHICKED
       
      Started by GussJ, 03-04-2020, 03:11 PM
      15 responses
      3,271 views
      0 likes
      Last Post xiinteractive  
      Started by Tim-c, Today, 02:10 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by Taddypole, Today, 02:47 PM
      0 responses
      2 views
      0 likes
      Last Post Taddypole  
      Started by chbruno, 04-24-2024, 04:10 PM
      4 responses
      51 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Working...
      X