Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Taking Highs and Lows depending on an indicator

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

    Taking Highs and Lows depending on an indicator

    Hi

    I developed a custom indicator, and I'd like to store the security High and Low for each period when the indicator is below 0.
    I'm not sure how I should code this.
    Any help appreciated

    Thank you

    #2
    Hello,

    Thank you for your post.

    Try something like this:

    if(myIndicator[0] < 0)
    {
    myLow = MIN(Low, periodValue)[0];
    }

    Here are links on MIN and MAX:



    If this is not what you want, please let me know.
    DenNinjaTrader Customer Service

    Comment


      #3
      Hi Ben,

      Thank you for your reply, but it's not what I need.
      The problem in what you wrote
      if(myIndicator[0] < 0)
      {
      myLow = MIN(Low, periodValue)[0];
      }

      is that periodValue depends on the indicator itself.

      Let me explain: let's say that my custom indicator turns negative [7] bars ago and then turns positive [3] bars ago. I need to calculate (and plot) the MIN and MAX of that range (between 7 and 3 bars ago).

      Your help is very much appreciated

      Comment


        #4
        double myMax = MAX(myIndicator, 4)[3];

        where the value of 4 is the absolute value between 3 bars ago and 7 bars ago.
        RayNinjaTrader Customer Service

        Comment


          #5
          Thank you, Ray, but the example above was just an example.

          I have an indicator "MyIndicator" which can swing above and below zero.

          I want to store (and plot) the security's MAX and MIN price in the interval when MyIndicator is negative.

          Something like:

          if (MyIndicator[1] >0
          && MyIndicator[0] <0)


          {
          startdate
          }

          if (MyIndicator[1] <0
          && MyIndicator[0] >0)

          {
          enddate
          }

          double MIN = MIN(Low, enddate - startdate)[0]
          double MAX = MAX(High, enddate - startdate)[0]

          Thank you for help

          Comment


            #6
            Not sure what your question is? Based on your last example it looks like you know what to do? Just subsitute startDate with startBar. You can assign it CurrentBar and thus you can then subtraced endBar from startBar to get the period length.
            RayNinjaTrader Customer Service

            Comment


              #7
              Ray,

              I did this but it doesn't work (it compiles, but it plots just once a day, and not correctly):

              if (MyIndicator[1] >0
              && MyIndicator[0] <0)
              {
              startDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute, 0);
              }

              if (MyIndicator[1] <0
              && MyIndicator[0] >0)
              {
              endDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, startHour, startMinute, 0);
              }

              int startBarsAgo = GetBar(startDateTime);
              int endBarsAgo = GetBar(endDateTime);

              double highestHigh = MAX(High, startBarsAgo - endBarsAgo)[endBarsAgo];
              double lowestLow = MIN(Low, startBarsAgo - endBarsAgo)[endBarsAgo];

              HighestHigh.Set(highestHigh);
              LowestLow.Set(lowestLow);


              I understand you were suggesting a simpler way to go, but I didn't understand what you meant with "Just subsitute startDate with startBar. You can assign it CurrentBar and thus you can then subtraced endBar from startBar to get the period length." How can I do it?

              Thank you

              Comment


                #8
                You will have to debug your code to see where the logic fails.

                What I suggested is that since you need to now the start bar and end bar, the property "CurrentBar" is an integer value for every bar on the chart. Once the conditino for the starting bar is met, record the value of CurrentBar since that represents a unique value for when this condition was met. Same for the end bar. Now that you have marked the values, you now the bar range. This is the period value that you wanted...
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Thank you, Ray. I did as you suggested and it works (and it was much easier to do!).

                  Unfortunately, I don't get two horizontal lines each time the conditions are met, but something resembling two bands, changing almost at each bar.

                  How can I edit the code to require that once the local high and low are plotted, they will stay the same in the following bars until the conditions for a new high and low are met?

                  Thank you

                  Comment


                    #10
                    On each call to OnBarUpdate() always set the same plot value.
                    RayNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    633 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    364 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    105 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    567 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    568 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X