count bars since event

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #239293 quote
    xpe74
    Participant
    New

    Hello, does anyone knows how to count the number of candles/bars since an event took place before the actual one. i’m trying to code and order based on 2 Ma crossing ”X ” bars/candles before the actual one /during the day .  is barindex and additional conditions usefull for that kind of code . Thanks for your help.

    #239299 quote
    robertogozzi
    Moderator
    Master

    Use BarsSince (https://www.prorealcode.com/documentation/barssince/):

    myEvent = rsi[14] crosses over 50
    myBar   = barssince(myEvent)
    return myBar as "last occurence"

    Please select the correct language. I moved this topic from the French forum.

    druby, Iván González and xpe74 thanked this post
    #239310 quote
    druby
    Participant
    New

    Hi Roberto, I was comparing  barsSince with a basic count and got some differing results.

    The count values presented regarding the true conditions are the same.

    However, the lines plotted are different.

    It appears that barsSince counts from the last count value of the last true condition, then jumps to the correct new count value level on next true condition.

    This means the  prior value [1],[2] etc wouldn’t be representing the correct prior values of the count.

    Is this the intended operation or is there something more to it.

     

    If using a basic count , the prior values are relevant between counts for prior values.

     

     

     

    // images - IG DAX German 40 DFB
    
    avg = average[34,0](close)
    c1 = close crosses over avg
    
    
    //------------------- XCOUNT
    xcount = xcount+1
    
    
    if c1 then
    // true condition Vline
    drawvline(barindex)coloured("red",100)style(dottedline,1)
    // Xcount Value Label
    drawtext(xcount[0],barindex[1],50)anchor(bottom,index,yshift)coloured("aqua")
    m1xcount = xcount[1]
    drawtext("[ #m1xcount#]",barindex[2], 30)anchor(bottom,index,yshift)coloured("aqua")
    endif
    
    
    if c1 then
    xcount=0
    xbar = barindex
    endif
    
    
    
    
    //------------------- BARSSINCE
    
    bs=barssince(c1,1)
    
    if c1 then
    // barsSince Value Label
    drawtext(bs,barindex[1],-50)anchor(top,index,yshift)coloured("violet")
    m1bs = bs[1]
    drawtext("[ #m1bs#]",barindex[2],-30)anchor(top,index,yshift)coloured("violet")
    endif
    
    
    // header Labels
    if barindex = 0 then
    drawtext("barsSince",0,-20)anchor(top,xshift,yshift)coloured("violet")
    drawtext("Xcount",0,20)anchor(bottom,xshift,yshift)coloured("aqua")
    
    endif
    
    return xcount coloured("aqua") as"Xcount",bs coloured("violet") as"barSince"//,xbar
    xpe74 thanked this post
    #239320 quote
    robertogozzi
    Moderator
    Master

    This code works like a charm:

    avg = average[34,0](close)
    c1  = close crosses over avg
    IF c1 THEN
       Bar1 = BarsSince(c1)
       Bar2 = BarsSince(c1,1)
       IF Bar1 >= 0 THEN
          DrawarrowUP(BarIndex[Bar1],low[Bar1] * 0.995) coloured("LightBlue")
       ENDIF
       IF Bar2 >= 0 THEN
          DrawarrowUP(BarIndex[Bar2],low[Bar2] * 0.990) coloured("LightGreen")
       ENDIF
    ENDIF
    RETURN
    xpe74 and Iván González thanked this post
    #239418 quote
    xpe74
    Participant
    New

    Bonjour merci pour les réponses, déjà intégrées dans le code. j’ai codé l’algo de manière à démarrer à 9h et terminer à 19h50 avec une clôture de trades ouvert automatique à 19h48 pour éviter de passer la nuit avec une position ouverte (margin broker) et voir le marché décaler dès l’ouverture.

    Le problème est (je n’ai pas trouvé la solution) que si le trend initial qui à ouvert le trade j-1 (que je ferme à 19h48 automatiquement) ne se termine pas obligatoirement à la clôture de marché et peut continuer jusqu’a deux heures du matin. Mon code est basé sur le croisement de 2 SMA (par exemple) mais si le croisement des SMA n’intervient pas à la clôture du marché mais le lendemain dans la nuit, ou juste après la réouverture du marché, je rate une partie des trades possibles dans la mesure ou l’algo attend un nouveau croisement.

    J’ai un bout de code qui utilise les condition Timeopen + not(onmarket) et SMA1 crosses over SMA2 ou SMA1 crosses under SMA2 mais cela ne fonctionne pas à la réouverture du marché avant qu’un croisement (de sma) ne se produise . Si vous avez une solution, je suis preneur..cordialement

    Hello, thank you for the answers, already integrated into the code. I coded the algo to start at 9am and end at 7:50pm with an automatic closing of open trades at 7:48pm to avoid spending the night with an open position (margin broker) and seeing the market shift as soon as it opens.

    The problem is (I haven’t found the solution) that if the initial trend that opened the trade j-1 (which I close at 7:48pm automatically) does not necessarily end at the market close and can continue until two in the morning. My code is based on the crossing of 2 SMAs (for example) but if the crossing of the SMAs does not occur at the market close but the next day during the night, or just after the market reopens, I miss some of the possible trades since the algo is waiting for a new crossing.

    I have a piece of code that uses the conditions Timeopen + not(onmarket) and SMA1 crosses over SMA2 or SMA1 crosses under SMA2 but it doesn’t work when the market reopens before a (sma) crossover occurs.
    If you have a solution, I’m interested..regards

    // order execution
    if timeopen then
       if not(OnMarket) then
          // ---------- Buy ----------
          if Trend = 1 then // based on crossover conditions
             if SMA1 crosses over SMA2 then
                buy nshare contracts at market
                SET TARGET $PROFIT profitvalue
             endif
          endif
       endif
    endif
    #239430 quote
    robertogozzi
    Moderator
    Master

    You have chosen the English forum, please stick to it. Thanks 🙂

    #239432 quote
    xpe74
    Participant
    New

    no problemo. Thanks and best regards

    #239466 quote
    robertogozzi
    Moderator
    Master

    You need a variable to be used as a flag, so that when trading is off it still scans and sets its value to 1 whenever a crossover is detected. When trading resumes, you will check this flag and, if set, you can enter a trade without having to wait for the next crossover:

    // initialize the variable FLAG
    once Flag = 0
    
    // when trading is OFF, detect any crossover and set FLAG accordingly
    if not timeopen and (Trend = 1) and (SMA1 crosses over SMA2) then
       Flag = 1
    endif
    
    // clear FLAG whenever SMA1 crossews UNDER SMA2
    if Flag then
       if (SMA1 crosses under SMA2) then
          Flag = 0
       endif
    endif
    
    // order execution (for both cases)
    if (timeopen and not(OnMarket) and (Trend = 1) and (SMA1 crosses over SMA2)) OR Flag  then
       buy nshare contracts at market
       SET TARGET $PROFIT profitvalue
       // clear FLAG
       flag = 0
    endif
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

count bars since event


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
xpe74 @xpe74 Participant
Summary

This topic contains 7 replies,
has 3 voices, and was last updated by robertogozzi
1 year, 3 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 10/21/2024
Status: Active
Attachments: 2 files
Logo Logo
Loading...