• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 StevenNelson, Today, 05:49 AM
          1 response
          4 views
          0 likes
          Last Post NinjaTrader_PatrickG  
          Started by fx.practic, Today, 01:41 AM
          0 responses
          11 views
          0 likes
          Last Post fx.practic  
          Started by wolfcuring, Today, 01:29 AM
          0 responses
          3 views
          0 likes
          Last Post wolfcuring  
          Started by BrandonT1292, 06-17-2019, 03:03 PM
          2 responses
          30 views
          0 likes
          Last Post thinarthrill  
          Started by cbentrikin, Yesterday, 07:46 PM
          1 response
          11 views
          0 likes
          Last Post NinjaTrader_Marco  
          Working...
          X