How to count events or values over a defined number of the newest bars?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #239636 quote
    repu
    Participant
    New

    Dear Comunity,

    I created an indicator that takes “1” if a certain level is reached and “0” if not. Over a number of available  bars (e.g. 100) it will show a number of “1” (e.g. 43). What I like to do do is to count this number of “1”.

    Therfore I tried this and some modifications of it, where “indicator” is my indicator that takes 1 or 0.

    count = 0
    FOR i = 0 TO 99
        IF (BarIndex - 1 - i) >= 0 THEN
            IF indicator[BarIndex - 1 - i] = 1 THEN
                count = count + 1
            ENDIF
        ENDIF
    NEXT

    Problem is, that it counts all the 1 over 100 bars but starting from the first available bar, that is the oldest (2015 or so) but I need the youngest.

    Can someone solve the problem?

    Thanks a lot!

    #239642 quote
    druby
    Participant
    New

     

    defparam drawonlastbaronly = true
    
    level = 19500
    LB    = 100 // lookback
    
    
    if close >= level then
    count = 1
    else
    count=0
    endif
    
    countLB = summation[LB](count)
    
    
    if islastbarupdate then
    drawVline(barindex[LB-1])coloured("violet",100)style(dottedline,1)
    endif
    return count coloured("red") as"count", countLB coloured("lime")style(dottedline,1) as"countLB"
    robertogozzi and Iván González thanked this post
    Screenshot-2024-10-28-192610.png Screenshot-2024-10-28-192610.png
    #239657 quote
    repu
    Participant
    New

    Thanks a lot druby!

    Almost perfect. But the summation function seems to count up and down. But I need the accumulated sum: 1+0+0+1+1+1 = 4 an only increasing line?

    #239667 quote
    druby
    Participant
    New

    Die vertikale Linie stellt den Anfang der letzten 100 Balken dar.

    Wenn ein Balken endet, wird eine Variable historisiert und der nächste Balken beginnt neu.

    Auf vorherige Werte einer Variablen wird mit [n] zugegriffen.

    ‘SUMMATION’ verwendet die letzten [100] Balken von (COUNT) und summiert alle vorherigen Werte aus den 100 Balken.

    ZÄHLB = [100]+[99]+[98]+…. [2]+[1].
    Da die gespeicherten Werte in COUNT[n] entweder 1 oder 0 sind, werden diese summiert.

    ‘ZÄHLB’ = 1+0+1+…+0+1.

    Im Bild ist die grüne 58 der summierte Wert von “1” aus den letzten 100 Balken.

    Das 100-Balken-Fenster bewegt sich mit jedem neuen Balken.

    Wenn eine neue Stange hinzugefügt wird, fällt eine vom anderen Ende ab.

    Wenn sowohl neu als auch alt identisch sind, ändert sich die Anzahl nicht.
    Wenn beide unterschiedlich sind, wird die Zählung nach oben oder unten gehen, je nachdem, ob eine “1” gewonnen oder verloren wird.

    Die rote Linie stellt die 1 oder 0 jedes Balkens dar, die durch die Bedingung IF/ELSE? ENDIF-Anweisung.

    Wenn Sie etwas anderes in meinem hatten, folge ich nicht.

    #239839 quote
    repu
    Participant
    New

    Ja du hast recht, es ist genau richtig. Habe von Hand nachgezählt. Ich war nur total verwirrt vom auf-und-ab.

    Vielen Dank für deine sehr schnelle Hilfe! 🙂

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

How to count events or values over a defined number of the newest bars?


ProBuilder: Indikatoren & Custom Tools

New Reply
Author
author-avatar
repu @repu Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by repu
1 year, 4 months ago.

Topic Details
Forum: ProBuilder: Indikatoren & Custom Tools
Language: German
Started: 10/28/2024
Status: Active
Attachments: 1 files
Logo Logo
Loading...