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

Using existing indicator value in custom indicator

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

    Using existing indicator value in custom indicator

    I am trying modify the existing Donchian Channel indicator so it uses the upper and lower Bollinger Band for the upper and lower channels. Right now I am using an indicator of an indicator for the upper and lower channels. I want one indicator to do what depicted in the attached chart. Thanks.

    I have no scripting knowledge. I cut and pasted the following from other coding examples.

    Code:
    public class DonchianBands : Indicator
        {
            #region Variables
            private int            period        = 20;
            double upperValue = Bollinger(2, 20).Upper[0];
            double lowerValue = Bollinger(2, 20).Lower[0];
            #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.Orange, "Mean"));
                Add(new Plot(Color.Blue, "Upper"));
                Add(new Plot(Color.Blue, "Lower"));
    
                Overlay            = true;
            }
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                Value.Set((MAX(upperValue, Period)[0] + MIN(lowerValue, Period)[0]) / 2);
                Upper.Set(MAX(upperValue, Period)[0]);
                Lower.Set(MIN(lowerValue, Period)[0]);
            }
    Click image for larger version

Name:	example.jpg
Views:	1
Size:	278.3 KB
ID:	904654

    #2
    Hello ActiveTrader09,

    The Bollinger() indicator cannot be called outside of a method.

    The line:
    double upperValue = Bollinger(2, 20).Upper[0];

    will not compile.

    Try the following instead:

    Value.Set((MAX(Bollinger(2, 20).Upper[0], Period)[0] + MIN(Bollinger(2, 20).Lower[0], Period)[0]) / 2);

    Or you can continue using the variables, but set these in OnBarUpdate on each bar before they are used.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I replaced the code with the following and still get errors on compile. Like I said I am a complete newbie when it comes to coding anything. I attached the .cs file for reference. Thanks.

      Code:
      public class DonchianBands : Indicator
          {
              #region Variables
              private int            period        = 20;
              #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.Orange, "Mean"));
                  Add(new Plot(Color.Blue, "Upper"));
                  Add(new Plot(Color.Blue, "Lower"));
      
                  Overlay            = true;
              }
              
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  Value.Set((MAX(Bollinger(2, 20).Upper[0], Period)[0] + MIN(Bollinger(2, 20).Lower[0], Period)[0]) / 2);
                  Upper.Set(MAX(Bollinger(2, 20).Upper[0], Period)[0]);
                  Lower.Set(MIN(Bollinger(2, 20).Lower[0], Period)[0]);
              }
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello ActiveTrader09,

      The Bollinger() indicator cannot be called outside of a method.

      The line:
      double upperValue = Bollinger(2, 20).Upper[0];

      will not compile.

      Try the following instead:

      Value.Set((MAX(Bollinger(2, 20).Upper[0], Period)[0] + MIN(Bollinger(2, 20).Lower[0], Period)[0]) / 2);

      Or you can continue using the variables, but set these in OnBarUpdate on each bar before they are used.
      Attached Files

      Comment


        #4
        Hello ActiveTrader09,

        Please take a look at the following overloads for MAX() and MIN():
        Code:
        MAX(IDataSeries input, int period);
        MIN(IDataSeries input, int period);
        You are attempting pass a double value into the parameter that accepts an IDataSeries object.

        You will need to replace your code within OnBarUpdate() with:
        Code:
        Value.Set((MAX(Bollinger(2, 20).Upper, Period)[0] + MIN(Bollinger(2, 20).Lower, Period)[0]) / 2);
        Upper.Set(MAX(Bollinger(2, 20).Upper, Period)[0]);
        Lower.Set(MIN(Bollinger(2, 20).Lower, Period)[0]);
        Please take a look at the following link on how methods function within C#: http://www.tutorialspoint.com/csharp/csharp_methods.htm

        Please, let us know if we may be of further assistance.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Zachary G.,

          Thanks for your help, it worked like a charm!!!!

          Surprisingly, I figured out how to use the standard deviation and period as a user defined variable.

          Thanks again, it turned out exactly as expected.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Mestor, 03-10-2023, 01:50 AM
          16 responses
          388 views
          0 likes
          Last Post z.franck  
          Started by rtwave, 04-12-2024, 09:30 AM
          4 responses
          31 views
          0 likes
          Last Post rtwave
          by rtwave
           
          Started by yertle, Yesterday, 08:38 AM
          7 responses
          29 views
          0 likes
          Last Post yertle
          by yertle
           
          Started by bmartz, 03-12-2024, 06:12 AM
          2 responses
          22 views
          0 likes
          Last Post bmartz
          by bmartz
           
          Started by funk10101, Today, 12:02 AM
          0 responses
          7 views
          0 likes
          Last Post funk10101  
          Working...
          X