count bars since event
Forums › ProRealTime English forum › ProBuilder support › count bars since event
- This topic has 7 replies, 3 voices, and was last updated 1 month ago by robertogozzi.
-
-
10/21/2024 at 12:05 PM #239293
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.
10/21/2024 at 4:45 PM #239299Use BarsSince (https://www.prorealcode.com/documentation/barssince/):
123myEvent = rsi[14] crosses over 50myBar = barssince(myEvent)return myBar as "last occurence"Please select the correct language. I moved this topic from the French forum.
10/21/2024 at 9:11 PM #239310Hi 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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// images - IG DAX German 40 DFBavg = average[34,0](close)c1 = close crosses over avg//------------------- XCOUNTxcount = xcount+1if c1 then// true condition Vlinedrawvline(barindex)coloured("red",100)style(dottedline,1)// Xcount Value Labeldrawtext(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")endifif c1 thenxcount=0xbar = barindexendif//------------------- BARSSINCEbs=barssince(c1,1)if c1 then// barsSince Value Labeldrawtext(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 Labelsif barindex = 0 thendrawtext("barsSince",0,-20)anchor(top,xshift,yshift)coloured("violet")drawtext("Xcount",0,20)anchor(bottom,xshift,yshift)coloured("aqua")endifreturn xcount coloured("aqua") as"Xcount",bs coloured("violet") as"barSince"//,xbar1 user thanked author for this post.
10/22/2024 at 10:27 AM #239320This code works like a charm:
12345678910111213avg = average[34,0](close)c1 = close crosses over avgIF c1 THENBar1 = BarsSince(c1)Bar2 = BarsSince(c1,1)IF Bar1 >= 0 THENDrawarrowUP(BarIndex[Bar1],low[Bar1] * 0.995) coloured("LightBlue")ENDIFIF Bar2 >= 0 THENDrawarrowUP(BarIndex[Bar2],low[Bar2] * 0.990) coloured("LightGreen")ENDIFENDIFRETURN10/24/2024 at 1:48 PM #239418Bonjour 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..cordialementHello, 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..regards123456789101112// order executionif timeopen thenif not(OnMarket) then// ---------- Buy ----------if Trend = 1 then // based on crossover conditionsif SMA1 crosses over SMA2 thenbuy nshare contracts at marketSET TARGET $PROFIT profitvalueendifendifendifendif10/24/2024 at 7:09 PM #239430You have chosen the English forum, please stick to it. Thanks 🙂
10/24/2024 at 8:04 PM #23943210/25/2024 at 3:34 PM #239466You 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:
12345678910111213141516171819202122// initialize the variable FLAGonce Flag = 0// when trading is OFF, detect any crossover and set FLAG accordinglyif not timeopen and (Trend = 1) and (SMA1 crosses over SMA2) thenFlag = 1endif// clear FLAG whenever SMA1 crossews UNDER SMA2if Flag thenif (SMA1 crosses under SMA2) thenFlag = 0endifendif// order execution (for both cases)if (timeopen and not(OnMarket) and (Trend = 1) and (SMA1 crosses over SMA2)) OR Flag thenbuy nshare contracts at marketSET TARGET $PROFIT profitvalue// clear FLAGflag = 0endif -
AuthorPosts
Find exclusive trading pro-tools on