Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding Indicators

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

    Adding Indicators

    I would like to create a new indicator that has added two EMA indicators to the LinReg indicator.
    In the Variables Region in addition to the period for the LinReg indicator I declared a highPeriod and a lowPeriod for the EMAs.
    In the protected override void Initialize() I added
    Add(new Plot(new Pen(Color.Blue, 3), PlotStyle.Line, "HighPlot"));
    Add(new Plot(new Pen(Color.Red, 3), PlotStyle.Line, "LowPlot"));
    My problem is with the code for OnBarUpdate()section. Any suggestions would be greatly appreciated.
    Last edited by winja; 10-04-2008, 07:53 PM.

    #2
    What exactly is your problem?

    Comment


      #3
      Thank you Dierk for your reply. Please let me backtrack slightly on this indicator and start with a single LinReg and SMA combination. This will help me learn and I believe be educational for others as well.

      Using the LinReg indicator as a base I added the part of the code from the OnBarUpdate section of the SMA indicator to the LinReg indicator OnBarUpdate section as follows:

      ///<summary>
      /// Linear Regression and SMA
      ///</summary>
      [Description("Linear Regression and SMA")]
      public class LinRegSMA : Indicator
      {
      #region Variables
      private int period = 14;
      #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.Blue, "LinReg"));
      Add(new Plot(Color.Red, "SMA"));

      Overlay = true;
      PriceTypeSupported = true;
      }
      ///<summary>
      /// Called on each bar update event (incoming tick)
      ///</summary>
      protected override void OnBarUpdate()
      // LinReg code
      {
      double sumX = (double) Period * (Period - 1) * 0.5;
      double divisor = sumX * sumX - (double) Period * Period * (Period - 1) * (2 * Period - 1) / 6;
      double sumXY = 0;
      for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
      sumXY += count * Input[count];

      double slope = ((double)Period * sumXY - sumX * SUM(Input, Period)[0]) / divisor;
      double intercept = (SUM(Input, Period)[0] - slope * sumX) / Period;

      Value.Set(intercept + slope * (Period - 1));
      // Add SMA code here
      double last = Value[1] * Math.Min(CurrentBar, Period);
      if (CurrentBar >= Period)
      Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period));
      else
      Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
      }
      In the Variables region I left the single private int period = 14;
      as I got compile errors when I tried to declare period2.

      The code compiles but when I attach the indicator to a chart no lines are drawn. I really appreciate your help with this as I am learning how to do this.
      Attached Files
      Last edited by winja; 10-05-2008, 06:13 PM.

      Comment


        #4
        - make sure you always check your logs first in case something goes wrong
        - this may help: http://www.ninjatrader-support.com/v...ead.php?t=3170

        Comment


          #5
          Originally posted by winja View Post
          I would like to create a new indicator that has added two EMA indicators to the LinReg indicator.
          In the Variables Region in addition to the period for the LinReg indicator I declared a highPeriod and a lowPeriod for the EMAs.
          In the protected override void Initialize() I added

          My problem is with the code for OnBarUpdate()section. Any suggestions would be greatly appreciated.
          this is linreg + a MA, maybe you can add a second MA to the code

          Comment


            #6
            Thanks Nkhoi, I appreciate the response. Here is something that I had in mind but I programmed it in rather an easy way by adding DrawRegion() to the code of LinReg and not the actual moving averages. Therefore the parameters for the SMAs are not user definable but are hard coded into the script. This indicator has two SMA(20) lines based on High and Low with the region in between colored and added to the LinReg(14) indicator.

            Any suggestions on how I could get the same output and allow for user defined parameters would be appreciated.
            Attached Files

            Comment


              #7
              winja,

              I am not sure what exactly you need help on.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Thanks Josh,

                I would like to add user defined parameters to the indicator. For example, the image in my prior post displays two SMAs and LinReg. Only the LinReg allows for change in the period of the indicator. How could I make the indicator to have a drop down selection of the moving averages and allow the user to change the periods? Would I have to use case select in the programming? Do you know of some code I could look at that has something similar to what I am asking for, ie, multiple moving averages in the same indicator with drop down selection?

                Comment


                  #9
                  Hi winja,

                  You do not need to use a case switch for this. What you want to do is just create several more variables. It may be easiest just to title them Period2 and Period3.

                  Please expand the Properties region of your code. Copy paste this two more times:
                  Code:
                  [Description("Numbers of bars used for calculations")]
                  [Category("Parameters")]
                  public int Period
                  {
                      get { return period; }
                      set { period = Math.Max(2, value); }
                  }
                  Then just change it now to take on Period2 and Period3 instead.
                  So at the end it should look like this:
                  Code:
                  [Description("Numbers of bars used for calculations")]
                  [Category("Parameters")]
                  public int Period2
                  {
                      get { return period2; }
                      set { period2 = Math.Max(2, value); }
                  }
                  Now you want to go back up to the Variables region of your code and add these:
                  Code:
                  private int period2 = 10;
                  private int period3 = 20;
                  You can set whatever value you want here. These will be the defaults for your user defined inputs.

                  Now in your code you can just use Period2 and Period3 instead of hardcoding in anything.
                  Code:
                  SMA(High, Period2)[0];
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Error message when I tried this

                    Hi Josh,

                    I have been looking for a way to add more than four parameters to my indicator and I thought this might have been the answer. I added this at the end of the Properties section
                    [Description("Signal Lag")]
                    [Category("Parameters")]
                    public int SigLag
                    {
                    get { return SigLag; }
                    set { SigLag = Math.Max(30, value); }
                    }
                    #endregion

                    and this in the Variables sections
                    // Wizard generated variables
                    private double minV = 1.500; // Default setting for MinV
                    private int lookBack = 15; // Default setting for LookBack
                    private int confCandle = 5; // Default setting for ConfCandle
                    private double maxFill = 1.500; // Default setting for MaxFill
                    private int SigLag = 10;
                    // User defined variables (add any user defined variables below)

                    But when I try to compile I get a warning message saying "Your indicator likely holds one or more recursive properties which could cause NinjaTrader to crash? SigLag. Do you want to edit these properties?"

                    I'm not game to compile it without fixing this error. Any hints much appreciated. Better still, if there is an official way to add more than four parameters to an indicator I'd love to know about it.

                    Thanks,
                    Ian

                    Comment


                      #11
                      Hi Owlman,
                      For creating more custom inputs, please check out this tip - http://www.ninjatrader-support2.com/...ead.php?t=5782

                      For your code, please use this snippet below -

                      Code:
                      [SIZE=2][SIZE=2][Description([/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"Signal Lag"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2])]
                      [Category([/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"Parameters"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2])]
                      [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] [COLOR=red]s[/COLOR]igLag
                      {
                      [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] { [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] SigLag; }
                      [/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]set[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] { SigLag = Math.Max([/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]30[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2], value); }
                      }
                      [/SIZE][/SIZE]

                      Comment


                        #12
                        Still NBG :-(

                        Thanks Bertrand,

                        I actually found that link on another thread (called "input parameters on wizzard") and posted there too (apologies for the double post). Have to say I didn't follow the first character upper/lower case format exactly as I just didn't see it. However following exactly what you posted (parm defined with first char upper case, then exactly as you posted, and still got the same errors.

                        BUT... noticed just now that the errors are not in the indicator, but in strategies that use the indicator!!!

                        Looks like I have to delete the indicator from all my strategies, then update the indicator, then delete all the old backups and test strategies that I don't need any more (lots of them, and I can't delete them, tried deleting one and now it's causing problems in my indicator ) I'll work through it and get back to you...

                        Thanks,
                        Ian

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        579 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        334 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        101 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        554 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        551 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X