Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Find second , third etc lowest low and highest highs

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

    Find second , third etc lowest low and highest highs

    Hello guys , in my strategy i 'd like to find the 4 Highest highs (prices) and lowest lows of a range of 12 5minutes bars so 1 hour , to compare them with a tolerance of 3 ticks .
    My idea is for defining a zone of multiple touch of price with horizontal levels without breaking it , in other word a range

    My idea to code this is , declare double variables for Hi1 , Hi2 , Hi3 , Hi4 , all assigned to 0
    and the same for Lo1 to Lo4 , all = 100000

    in OnBarUpdate() , i have made loops , one for the highs , one for the lows , ti looks like this :

    // FIND HIGHEST AND LOWEST PRICES OF THE RANGE

    for (int i=1;i<12;i++)
    {
    if ( Low[i] < Lo1 ) Lo1=Low[i];
    if (Low[i] < Lo2 && Low[i]>Lo1) Lo2=Low[i];
    if (Low[i] < Lo3 && Low[i]>Lo2) Lo3=Low[i];
    if (Low[i] < Lo4 && Low[i]>Lo3) Lo4=Low[i];
    }

    for (int j=1;j<12;j++)
    {
    if ( High[j] > Hi1 ) Hi1=High[j];
    if (High[j] > Hi2 && High[j]<Hi1) Hi2=High[j];
    if (High[j] > Hi3 && High[j]<Hi2) Hi3=High[j];
    if (High[j] > Hi4 && High[j]<Hi3) Hi4=High[j];
    }

    i have made print (Hi1 , Hi2 , Lo1 etc ) but all the values have no sens , prices are really far away from what it shows me , except that Hi1>Hi2>Hi3 , any idea ?

    thx

    #2
    Hello mrxrom,

    Thank you for your inquiry.

    NinjaScript comes with two methods, MAX() and MIN() that will calculate this for you.

    Please read the NinjaTrader help guide at this link for more information about using MAX(): http://ninjatrader.com/support/helpG...aximum_max.htm

    Please read the NinjaTrader help guide at this link for more information about using MIN();


    Here's the syntax you would use:
    Code:
    MAX(High, 12)[0];
    MIN(Low, 12)[0];
    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hello Zachary , and thx for the answer .

      i don't want to know the highest high , (easy to have it with MAX) , but i want to know the second highest high , the third one and the forth one , all that in the 12 bar period .

      i don't know how to have these values without a loop , am i wrong ?

      or do i have to use MAX and MIN , inside the loop ??
      Last edited by mrxrom; 07-16-2015, 03:06 PM.

      Comment


        #4
        Hello mrxrom,

        You could change that barsAgo value of both your MAX and MIN methods to obtain the 2nd, 3rd, and 4th highest high / lowest low.

        Example:
        Code:
        MAX(High, 12)[0]; // highest high
        MAX(High, 12)[1]; // second highest high
        MAX(High, 12)[2]; // third highest high
        MAX(High, 12)[3]; // fourth highest high
        
        MIN(Low, 12)[0]; // lowest low
        MIN(Low, 12)[1]; // second lowest low
        MIN(Low, 12)[2]; // third lowest low
        MIN(Low, 12)[3]; // fourth lowest low
        Please, let us know if we may be of further assistance.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          wow i'm impressed , thx a lot Zachary

          Comment


            #6
            hi again ,my head is burning : it doesn't work , it returns all the same values ... what a joke !

            i repeat my question ....

            in a range of 12 candles of 5 mn each , that is one HOUR
            Find the highest , second highest , third highest prices in this range

            i was doing it with MT4 with the loop i write on the first message .... how to make it on NT , im getting crazy with this very easy thing for a real programmer

            Could someone help here please ?
            Last edited by mrxrom; 07-17-2015, 01:07 AM.

            Comment


              #7
              To clarify the idea , here is chart

              When we were around 8h , just before the big black candle breaking the rectangle , that is what i want the script to tell me .
              Attached Files
              Last edited by mrxrom; 07-17-2015, 01:48 AM.

              Comment


                #8
                Another chart and the values it should returns , i would like to have these highs and lows prices in variables Hi1 Hi2 etc
                i had do that perfectly in MT4 with the code i write in first message
                thx
                Attached Files
                Last edited by mrxrom; 07-17-2015, 03:17 AM.

                Comment


                  #9
                  Originally posted by mrxrom View Post
                  hi again ,my head is burning : it doesn't work , it returns all the same values ... what a joke !

                  i repeat my question ....

                  in a range of 12 candles of 5 mn each , that is one HOUR
                  Find the highest , second highest , third highest prices in this range

                  i was doing it with MT4 with the loop i write on the first message .... how to make it on NT , im getting crazy with this very easy thing for a real programmer

                  Could someone help here please ?
                  A quick and dirty way.
                  1. Use a for loop to create a List<double> of the Highs.
                  2. Sort the List.
                  3. Grab the 4 highest values.

                  Repeat for the Lows.

                  Comment


                    #10
                    Hello mrxrom,

                    Just to clarify, the methods I have provided do not account for duplicate variables.

                    koganam provides a good solution for this. However, you may still get duplicate variables.

                    With two new using statements,
                    Code:
                    using System.Linq;
                    using.System.Collections.Generic
                    You can use this sample code below to grab the highs of the 12 bars and place them into a list, and create a new list with only unique variables:
                    Code:
                    List<double> highs = new List<double>();
                    for (int i = 0; i < 12; i++)
                    {
                         highs.Add(High[i]);
                    }
                    
                    highs.Sort((a, b) => -1* a.CompareTo(b));
                    List<double> highestHighs = highs.Distinct().ToList();
                    Now you can retrieve the first, second, third, and fourth highest values by using highestHighs[0]. highestHighs[1]. highestHighs[2], and highestHighs[3].
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Koganam , thanks a lot for your answer . i have search , but i don't understand how to : Use a for loop to create a List<double> of the Highs. As i am a beginner in programming , i have never use Lists , i have read the page from dotnetperls and csharp.net-informations but it's really not clear for me.

                      i have add this : using System.Collections.Generic;
                      in top of the code , to be able to create List

                      Have create a List for the highs:
                      List<double>TheHighs = new List<double>(); // New list for the highs

                      but in the for loop , don't know how to say , store each values of last 12 highs in the List .

                      Any idea please ?

                      Comment


                        #12
                        Thanks a lot Zachary for your great help , haven't read your post when typing my last one and you just give the answer to my question .

                        Great Help thx again

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        558 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        324 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
                        545 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        547 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X