Find the first 3 highest volume bars in a specific period

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #148350 quote
    Robb
    Participant
    Average

    Dear All

    I tried to find the answer in old topics but unsuccessfully.

    I’d like to know how to find and store the first 3 (or more, 4, 5 does not matter) bars with highest volume in the last 50 bars.

    I mean taking into consideration a period back to 50 bars I want to store in 3 different variables the value of the index bar related to the first 3 high volume bars.

    thank you

    #148380 quote
    robertogozzi
    Moderator
    Master

    There you go (not tested):

    HiVol = (Volume = highest[50](Volume))
    i     = 0
    Count = 0
    While i < 50
       If HiVol[i] Then
          Count = Count + 1
          If Count = 1 then
            Bar1 = Barindex[i]
          Elsif Count = 2 Then
             Bar2 = Barindex[i]
          Elsif Count = 3 Then
             Bar3 = Barindex[i]
             Break
          Endif
       Endif
       i = i + 1
    Wend

    af the end of the iterations COUNT will retain the number of  variables with a high volume, it can be any number from 0 to 3. Bar1 will retain the first bar index, if any, Bar2 the second one and Bar3 the third one.

    This works with v10.3+, it’s a bit awkward to add other variables, if needed.

    This one, instead, will work with v11 only but it’s easier to increase the number of variables needed:

    HiVol = (Volume = highest[50](Volume))
    i     = 0
    Count = 0
    While i < 50     //scan the last 50 bars
       If Volume[i] = HiVol[i] Then
          Count = Count + 1
          $MyBar[Count - 1] = BarIndex[i]
          If Count = 3 Then  //retrieve the last 3
             Break
          Endif
       Endif
       i = I + 1
    Wend

    $MyBar[..], from 0 to 2, will retain values if COUNT > 0.

    #148384 quote
    Robb
    Participant
    Average

    Ciao Roberto

    thank you for your answer.

    I was not able to develop the code because I didn’t know the powerful  sintax

    HiVol = (Volume = highest[50](Volume))

    Instead of   HiVol =  highest[50](Volume), is yours able to define the highest volume bar in such a dynamic way? Can you explain me it better?

    Also I understood, I did not wrote very clear, my aim is actually to find the highest 3 bars in a period of 50 bars. Does your code do that, or simply it finds the first 3 bars going back to the past (and after it stops)? It seems so.

    After getting these 3 bars, I also need to put them in ascending or descending order.

    I know we can do that in the way below, but if I need more than 3 bars it becomes complicated:

       if (a <= b) {
            if (a <= c) {
                if (b <= c) printf("%d %d %d\n", a, b, c);
                else printf("%d %d %d\n", a, c, b);
            } else printf("%d %d %d\n", c, a, b);
        } else {
            if (b <= c) {
                if (a <= c) printf("%d %d %d\n", b, a, c);
                else printf("%d %d %d\n", b, c, a);
            } else printf("%d %d %d\n", c, b, a);
        }
    }

    Sorry for the programming language but it is just to explain.

    Grazie per l’aiuto

    Roberto

    #148398 quote
    robertogozzi
    Moderator
    Master

    HiVol = (Volume = highest[50](Volume))
    Instead of   HiVol =  highest[50](Volume), is yours able to define the highest volume bar in such a dynamic way? Can you explain me it better?

    The first determines a logical value (true or false), while the second assigns a volume value to the variable,  which is not what you need because any bar would have a highest value.

    The code scans the last 50 bars, which you can replace with any number or BarIndex to scan all previous bars.

    If you need to increase 3 to a higher number, then it’s easier using the second code snippet, but only if you are using v11,

    #148410 quote
    Robb
    Participant
    Average

    Hello

    1)  If Volume[i] = HiVol[i] Then

    Compares the i -volume bar with a  logical value (?) I am not sure it is going to work.

    2) since the formula is

    HiVol = (Volume = highest[50](Volume))

    when you do   If HiVol[i] Then
    you scan always and only the highest of 50 bars.
    So let’s imagine i=10 the scan is from bar 10 to bar 59 which is not good, I need to scan only the last 50 bars.
    HiVol(10) tells me if 10 bars ago the actual bar at that time was the highest of the last 50 bars so, based on the present time, if it was the highesr from bar 10 to 59 (50 bars).
    I think it is not so easy to get on a period of ie 50 bars the highest 3, 4 or whatever bars and their bar index.
    What do you think?
    #148411 quote
    robertogozzi
    Moderator
    Master

    You are right, that line ahould read:

    If HiVol[i] Then
    #148412 quote
    robertogozzi
    Moderator
    Master

    Counter i always starts from 0 up to 49. In that range there should be at least one highest volume, sometimes 2 or 3.

    #148416 quote
    Robb
    Participant
    Average

    Sorry if I chat again but I think it is important.

    HiVol = (Volume = highest[50](Volume))

    Example i=10

    HiVol[10] =  (Volume[10] = highest[50](Volume))

    How does 50 work?

    In my opinion the research of the highest volume bar goes from bar 10 back to 59.

    And the formula is true if the bar 10 is the highest of the last 50 bars back, otherwise it is false.

    #148422 quote
    robertogozzi
    Moderator
    Master

    Yes, it scans the last 50 bars for the highest volume in 50 bars.

    In bar 0 it will find the highest from bar 0 to bar 49, in bar 1 it will find the highest from bar 1 to bar 50 and so on… till bar 49 where it will find the highest from bar 49 to bar 98.

    #149095 quote
    Robb
    Participant
    Average
    HiVol = (Volume = highest[50](Volume))
    i     = 0
    Count = 0
    While i < 50     //scan the last 50 bars
       If Volume[i] = HiVol[i] Then
          Count = Count + 1
          $MyBar[Count - 1] = BarIndex[i]
          If Count = 3 Then  //retrieve the last 3
             Break
          Endif
       Endif
       i = I + 1
    Wend

    with this code there’s an error since it does not like [count-1], because an array wants the content>0 (a warning message comes out). Actually I saw this issue in other cases, so apart this one I think it is interesting to know the reason.

    I solved the issue by putting before the array an “if”

    if Count>0 then

    $MyBar[Count – 1] = BarIndex[i]

    etc

    but I found it a little bit odd, because it doesn’t change the value of count itself, it is just a sort of check.

    Your comment about that would be very appreciated

    thanks

    #149096 quote
    robertogozzi
    Moderator
    Master

    There’s no issue with COUNT.

    The issues are in my code, since I coded it incorrectly and I could not test it.

    The two correct versions (tested), you can remove RETURN…., I used it just to test the code:

    i     = 0
    Count = 0
    While i < 50     //scan the last 50 bars
       If Volume[i] = highest[50](Volume[i]) THEN
          Count = Count + 1
          $MyBar[Count - 1] = BarIndex[i]
          If Count = 3 Then  //retrieve the last 3
             Break
          Endif
       Endif
       i = I + 1
    Wend
    return Count AS "Count",0 AS "0",1 as "1",2 as "2",3 AS "3"
    i     = 0
    Count = 0
    While i < 50
       If Volume[i] = highest[50](Volume[i]) Then
          Count = Count + 1
          If Count = 1 then
             Bar1 = Barindex[i]
          Elsif Count = 2 Then
             Bar2 = Barindex[i]
          Elsif Count = 3 Then
             Bar3 = Barindex[i]
             Break
          Endif
       Endif
       i = i + 1
    Wend
    return Count AS "Count",0 AS "0",1 as "1",2 as "2",3 AS "3"
    #149098 quote
    Robb
    Participant
    Average
    numbar = 60
    i = 0
    count = 0
    AvgVolume = Average[10](volume)
    while i < numbar   
    if volume[i] > AvgVolume then
    count = count + 1
    $hvol[count-1] = volume[i]
    else
    $hvol[count-1] = 0
    endif
    i =i + 1
    wend
    
    return

    yes you are right your code is correct and it works. Still I don’t understand why my version above does not work and the message “a positive parameter is expected with $hvol” comes out.

    Because in my opinion both codes are equal for the technical point of view.

    #149105 quote
    robertogozzi
    Moderator
    Master

    That’s because the count is not increased when ELSE is executed.

    Remove line 10 and add this one between lines 5 and 6:

    $hvol[count] = 0
    #149117 quote
    bertrandpinoy
    Participant
    Veteran

    I’m lost … can you post ITF definitive version 10.3? thank you so much

    #149123 quote
    robertogozzi
    Moderator
    Master

    The last correct version for v10.3 is the one I posted at https://www.prorealcode.com/topic/find-the-first-3-highest-volume-bars-in-a-specific-period/#post-149096.

    bertrandpinoy thanked this post
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.

Find the first 3 highest volume bars in a specific period


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Robb @robb Participant
Summary

This topic contains 14 replies,
has 3 voices, and was last updated by robertogozzi
5 years, 3 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 10/24/2020
Status: Active
Attachments: No files
Logo Logo
Loading...