Prevent the rectangle from being drawn continually, only at each new bar.
Forums › ProRealTime English forum › ProBuilder support › Prevent the rectangle from being drawn continually, only at each new bar.
- This topic has 17 replies, 3 voices, and was last updated 1 year ago by Fabian.
-
-
05/08/2023 at 10:19 AM #214284
Hi together,
I want to draw a rectangle after each new bar (timeframe 30 minutes) and not every time the loop is run and the conditions are true.
I’ve already tried a few things (bar index, etc.) and have not come to a satisfying result.
Does anyone have an idea?
I can also provide the complete code. (Calculations of the arrays – positions of the rectangle based on the Meander band).
Thanks
Drawing retangles12for i = 0 to LastSet($RechteckStartBar) doDRAWRECTANGLE($RechteckStartBar[i], $RechteckObereSeite[i], $RechteckEndBar[i], $RechteckUntereSeite[i]) coloured(0, 0, 255, 10) bordercolor(0, 0, 150)05/10/2023 at 9:22 AM #214448You should add a 2-element array named $myTime (or whatever name you like) to prevent arrays from being continuously updated, as they are not historicized, unlike common variables:
123456789101112131415161718ONCE $myTime[0] = 0ONCE $myTime[1] = -1$myTime[0] = TimeIF Time <> Time[1] AND $myTime[0] <> $myTime[1] THEN$myTime[0] = TimeENDIFIF $myTime[1] <> $myTime[0] THEN..--- your main code must be here ---..endif$myTime[1] = $myTime[0].--- the last part of your code (such as plotting on the chart) must be here ---.RETURN05/10/2023 at 9:35 AM #214449I think that to get a perfect and neat answer to your query, you should provide the complete code.
I understand that you plot the rectangles in the past from the current bar, right?
Did you use:
DEFPARAM DRAWONLASTBARONLY=TRUE
for that purpose ?
05/10/2023 at 1:26 PM #214463Your solution with the $myTime array is a good approach to get around this problem. I basically store the current time value in an array and then compare it to the previous stored value to determine if the current candle is a new one. If the current candle is new, the main code is executed, if I understand it correctly.
Thanks, I will try that. 🙂
05/10/2023 at 1:30 PM #214464Hi Nicolas,
Thanks for your answer!
Yes I use DEFPARAM DRAWONLASTBARONLY=TRUE
It’s also a Multitimeframe approach which takes into account the Waddah-Attar-Explosion in the H1 Chart, which i use in a modified version (better bollinger band).
I’ll try the solution of robertogozzi and then I’ll post the complete code.
05/10/2023 at 2:54 PM #214472Here is the code without the extension.
Code without optimization123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201defparam drawonlastbaronly = True//draw only from 08 o'clockif tageszaehlerAn = 1 thenif (hour[1] < 8 and hour = 8) thenCounter = 0// Setzt die Array-Variablen zurückfor i = 0 to LastSet($RechteckStartBar) do$RechteckStartBar[i] = -1$RechteckEndBar[i] = -1$RechteckObereSeite[i] = -1$RechteckUntereSeite[i] = -1nextendifendiftimeframe (1 hour)// -- parameterssensitivityh1 = 150deadZoneh1=10//Berechnung Better Bollinger BandsIbH1=20deH1=2alpH1=3/(IbH1+1)srcH1=(high+low+close)/3if barindex>IbH1 thenmtH1=alpH1*srcH1+(1-alpH1)*mtH1[1]utH1=alpH1*mtH1+(1-alpH1)*utH1[1]dtH1=((2-alpH1)*mtH1-utH1)/(1-alpH1)mt2H1=alpH1*abs(srcH1-dtH1)+(1-alpH1)*mt2H1[1]ut2H1=alpH1*mt2H1+(1-alpH1)*ut2H1[1]dt2H1=((2-alpH1)*mt2H1-ut2H1)/(1-alpH1)butH1=dtH1+deH1*dt2H1bltH1=dtH1-deH1*dt2H1endife1H1 = butH1 - bltH1// indisif barindex>40 thencalcMACDH1 = MACD[18,40,9](customclose)t1H1 = (calcMACDH1-calcMACDH1[1])*sensitivityh1e1H1 = e1H1if t1H1>=0 thentrendUPH1 = t1H1trendDOWNH1 = 0elsetrendUPH1 = 0trendDOWNH1 = -1*t1H1endifif trendUPH1<trendUPH1[1] thencolorH1 = -1elsecolorH1 = 1endifif trendDOWNH1<trendDOWNH1[1] thencolorH1 = -1elsecolorH1 = 1endifendifH1Short = TrenddownH1 < e1H1H1Long = TrendupH1 > e1H1timeframe (30 minutes)//Berechnung MeanderONCE Periods = 5ONCE Deviations = 2ONCE Middle1 = 0ONCE MeanderHI = 0ONCE MeanderLO = 0MyAvg = 0MyVar = 0StDev = 0FOR i = 0 TO (Periods - 1)$barO[i] = (Open[i] - close[i+1]) / close[i+1]$barH[i] = (High[i] - close[i+1]) / close[i+1]$barL[i] = (Low[i] - close[i+1]) / close[i+1]$barC[i] = (Close[i]- close[i+1]) / close[i+1]//-------------- average price --------------------------------------Avg = ($barO[i] + $barH[i] + $barL[i] + $barC[i]) / 4MyAvg = MyAvg + AvgNEXTMyAvg = MyAvg / Periods//-------------- calculation of standard deviation ------------------FOR i = 0 TO (Periods - 1)MyVar = MyVar + ((square($barO[i] - MyAvg) + square($barH[i] - MyAvg) + square($barL[i] - MyAvg) + square($barC[i] - MyAvg)) / 4)NEXT//StDev = SQRT(MyVar / Periods)//Middle1 = close * (1 + MyAvg)MeanderHI = close * (1 + MyAvg + (Deviations * StDev))MeanderLO = close * (1 + MyAvg - (Deviations * StDev))//Berechnung Einstiegeif MeanderLO > MeanderLO[1] and not (MeanderLO[1]>MeanderLO[2]) thenb=1elseif MeanderLO < MeanderLO[1] and not (MeanderLO[1]<MeanderLO[2]) then//and not (meanderlow[1]<meanderlow[2]) thenb=-1elseb=0endifendif//Bedingung für Long Viereckif (b[1]=1) and (H1Short) then// For Testing: if ((barindex > 1) and (b[1]=1) and (H1Short) and (barindex > LastSet($RechteckStartBar)) and (barindex <> barindex[1]) and (minute mod 5 = 0) thenBeginnRechteckLong = 1EndeRechteckLong = 4Counter = LastSet($RechteckStartBar) + 1ObereSeite = MeanderHIUntereSeite = Middle1// Speichert die Koordinaten des Rechtecks in Array-Variablen$RechteckStartBar[Counter] = barindex - 1 + BeginnRechteckLong$RechteckEndBar[Counter] = barindex - 1 + EndeRechteckLong$RechteckObereSeite[Counter] = ObereSeite$RechteckUntereSeite[Counter] = UntereSeite// Aktualisieren des ZählersCounter = Counter + 1endif// Zeichnet alle gespeicherten Rechtecke im Chartfor i = 0 to LastSet($RechteckStartBar) doDRAWRECTANGLE($RechteckStartBar[i], $RechteckObereSeite[i], $RechteckEndBar[i], $RechteckUntereSeite[i]) coloured(0, 0, 255, 10) bordercolor(0, 0, 150)// Zeichnen der horizontalen Linien im Rechteckif horizontaleLinien = 1 thenx1 = $RechteckStartBar[i]x2 = $RechteckEndBar[i]yMid1 = ($RechteckObereSeite[i] + 2 * $RechteckUntereSeite[i]) / 3yMid2 = (2 * $RechteckObereSeite[i] + $RechteckUntereSeite[i]) / 3DRAWSEGMENT(x1, yMid1, x2, yMid1) COLOURED(255, 0, 0, 255)DRAWSEGMENT(x1, yMid2, x2, yMid2) COLOURED(255, 0, 0, 255)endif// Berechnen der X- und Y-Koordinaten für die SternexCoord = ($RechteckStartBar[i] + $RechteckEndBar[i]) / 2yCoord = $RechteckObereSeite[i]//// Berechnung von StarCount basierend auf dem Vierecksif drawSterne = 1 thenRechteckMitte1 = yMid1RechteckMitte2 = yMid2maxHigh = highest[EndeRechteckLong - BeginnRechteckLong + 1](high)[barindex - $RechteckStartBar[i]]//if maxHigh > $RechteckObereSeite[i] thenStarCount = 3elsif maxHigh > RechteckMitte2 thenStarCount = 2elseStarCount = 1endifendif////// Zeichnen der Sterne über dem Rechteckif drawSterne = 1 thenfor j = 1 to StarCount doyPos = yCoord + (20 * j * pipsize)DRAWTEXT("●", xCoord, yPos, Monospaced, Bold, 15) COLOURED(136, 136, 0)nextendifnextreturn05/10/2023 at 5:44 PM #214477And here is the updated version. Did I miss something? I’m going to test it.
Updated Version123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200defparam drawonlastbaronly = True// Initialisieren des Arrays nur einmal zu BeginnONCE $myTime[0] = 0ONCE $myTime[1] = -1$myTime[0] = TimeIF Time <> Time[1] AND $myTime[0] <> $myTime[1] THEN$myTime[0] = TimeENDIFIF $myTime[1] <> $myTime[0] THENif tageszaehlerAn = 1 thenif (hour[1] < 8 and hour = 8) then //or (barindex = 1)Counter = 0// Setzt die Array-Variablen zurückfor i = 0 to LastSet($RechteckStartBar) do$RechteckStartBar[i] = -1$RechteckEndBar[i] = -1$RechteckObereSeite[i] = -1$RechteckUntereSeite[i] = -1nextendifendifendiftimeframe (1 hour)// -- parameterssensitivityh1 = 150deadZoneh1=10//Berechnung Better Bollinger BandsIbH1=20deH1=2alpH1=3/(IbH1+1)srcH1=(high+low+close)/3if barindex>IbH1 thenmtH1=alpH1*srcH1+(1-alpH1)*mtH1[1]utH1=alpH1*mtH1+(1-alpH1)*utH1[1]dtH1=((2-alpH1)*mtH1-utH1)/(1-alpH1)mt2H1=alpH1*abs(srcH1-dtH1)+(1-alpH1)*mt2H1[1]ut2H1=alpH1*mt2H1+(1-alpH1)*ut2H1[1]dt2H1=((2-alpH1)*mt2H1-ut2H1)/(1-alpH1)butH1=dtH1+deH1*dt2H1bltH1=dtH1-deH1*dt2H1endife1H1 = butH1 - bltH1// indisif barindex>40 thencalcMACDH1 = MACD[18,40,9](customclose)t1H1 = (calcMACDH1-calcMACDH1[1])*sensitivityh1e1H1 = e1H1if t1H1>=0 thentrendUPH1 = t1H1trendDOWNH1 = 0elsetrendUPH1 = 0trendDOWNH1 = -1*t1H1endifif trendUPH1<trendUPH1[1] thencolorH1 = -1elsecolorH1 = 1endifif trendDOWNH1<trendDOWNH1[1] thencolorH1 = -1elsecolorH1 = 1endifendifH1Short = TrenddownH1 < e1H1H1Long = TrendupH1 > e1H1timeframe (30 minutes)//Berechnung MeanderONCE Periods = 5ONCE Deviations = 2ONCE Middle1 = 0ONCE MeanderHI = 0ONCE MeanderLO = 0MyAvg = 0MyVar = 0StDev = 0FOR i = 0 TO (Periods - 1)$barO[i] = (Open[i] - close[i+1]) / close[i+1]$barH[i] = (High[i] - close[i+1]) / close[i+1]$barL[i] = (Low[i] - close[i+1]) / close[i+1]$barC[i] = (Close[i]- close[i+1]) / close[i+1]//-------------- average price --------------------------------------Avg = ($barO[i] + $barH[i] + $barL[i] + $barC[i]) / 4MyAvg = MyAvg + AvgNEXTMyAvg = MyAvg / Periods//-------------- calculation of standard deviation ------------------FOR i = 0 TO (Periods - 1)MyVar = MyVar + ((square($barO[i] - MyAvg) + square($barH[i] - MyAvg) + square($barL[i] - MyAvg) + square($barC[i] - MyAvg)) / 4)NEXT//StDev = SQRT(MyVar / Periods)//Middle1 = close * (1 + MyAvg)MeanderHI = close * (1 + MyAvg + (Deviations * StDev))MeanderLO = close * (1 + MyAvg - (Deviations * StDev))//Berechnung Einstiegeif MeanderLO > MeanderLO[1] and not (MeanderLO[1]>MeanderLO[2]) thenb=1elseif MeanderLO < MeanderLO[1] and not (MeanderLO[1]<MeanderLO[2]) then//and not (meanderlow[1]<meanderlow[2]) thenb=-1elseb=0endifendif// Bedingung für Long Viereckif (b[1]=1) and (H1Short) thenBeginnRechteckLong = 1EndeRechteckLong = 4Counter = LastSet($RechteckStartBar) + 1ObereSeite = MeanderHIUntereSeite = Middle1// Speichert die Koordinaten des Rechtecks in Array-Variablen$RechteckStartBar[Counter] = barindex - 1 + BeginnRechteckLong$RechteckEndBar[Counter] = barindex - 1 + EndeRechteckLong$RechteckObereSeite[Counter] = ObereSeite$RechteckUntereSeite[Counter] = UntereSeite// Aktualisieren des ZählersCounter = Counter + 1endif$myTime[1] = $myTime[0]// Zeichnet alle gespeicherten Rechtecke im Chartfor i = 0 to LastSet($RechteckStartBar) doDRAWRECTANGLE($RechteckStartBar[i], $RechteckObereSeite[i], $RechteckEndBar[i], $RechteckUntereSeite[i]) coloured(0, 0, 255, 10) bordercolor(0, 0, 150)// Zeichnen der horizontalen Linien im Rechteckif horizontaleLinien = 1 thenx1 = $RechteckStartBar[i]x2 = $RechteckEndBar[i]yMid1 = ($RechteckObereSeite[i] + 2 * $RechteckUntereSeite[i]) / 3yMid2 = (2 * $RechteckObereSeite[i] + $RechteckUntereSeite[i]) / 3DRAWSEGMENT(x1, yMid1, x2, yMid1) COLOURED(255, 0, 0, 255)DRAWSEGMENT(x1, yMid2, x2, yMid2) COLOURED(255, 0, 0, 255)endif// Berechnen der X- und Y-Koordinaten für die SternexCoord = ($RechteckStartBar[i] + $RechteckEndBar[i]) / 2yCoord = $RechteckObereSeite[i]//// Berechnung von StarCount basierend auf dem Vierecksif drawSterne = 1 thenRechteckMitte1 = yMid1RechteckMitte2 = yMid2maxHigh = highest[EndeRechteckLong - BeginnRechteckLong + 1](high)[barindex - $RechteckStartBar[i]]//if maxHigh > $RechteckObereSeite[i] thenStarCount = 3elsif maxHigh > RechteckMitte2 thenStarCount = 2elseStarCount = 1endifendif////// Zeichnen der Sterne über dem Rechteckif drawSterne = 1 thenfor j = 1 to StarCount doyPos = yCoord + (20 * j * pipsize)DRAWTEXT("●", xCoord, yPos, Monospaced, Bold, 15) COLOURED(136, 136, 0)nextendifnextRETURN05/11/2023 at 7:56 AM #214492Well, I did not get anything plotted on the chart. There is an error of multiple assignment of the “counter” variable through different timeframes (which is not possible), so you have to change it in one of the timeframe (because I don’t think they are related to each other, you are using it in your loop).
My question is: why do you want to plot only one time on the current bar? Is it related to performance of calculation?
Some tips:
1/ in order to get an huge performance increase, make your calculation only when the last bar of the chart update by encapsulate the parts of the code that plot in the past with a condition: IF ISLASTBARUPDATE THEN … ENDIF
2/ to plot only one time per bar, check if the OPENTIMESTAMP is different from the last saved one:
1234567IF OPENTIMESTAMP>$lastsaved[0] then$lastsaved[0] = OPENTIMESTAMP// YOUR CODEENDIF1 user thanked author for this post.
05/11/2023 at 11:33 AM #21451405/11/2023 at 11:48 AM #21452005/11/2023 at 2:18 PM #21454805/15/2023 at 12:40 PM #214732In fact, there is no problem, you are plotting 3 different rectangles on the same bar.
The X-axis coordinates are the same, but no the Y-axis (price). If you add this line right below the DRAWRECTANGLE, it will add the number of the plotted rectangle:
1drawtext("#i#",$RechteckStartBar[i], $RechteckObereSeite[i],dialog,bold,20)So if you consider that it can’t be more than 1 rectangle on the same bar, then do not increase you array that save the coordinates of the rectangle, just continuously save the coordinates in the same column.
05/15/2023 at 3:46 PM #214755Hi Nicolas,
Thanks for your answer.
When I use the extension under the draw function, no more retangle is drawn.
I’ve shortened the code and limited it to the core function for testing. (meander calculation + drawing the rectangle).
Unfortunately, I’ve not found a solution so far
Shortened12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273//Calculation MeanderONCE Periods = 5ONCE Deviations = 2ONCE Middle1 = 0ONCE MeanderHI = 0ONCE MeanderLO = 0MyAvg = 0MyVar = 0StDev = 0FOR i = 0 TO (Periods - 1)$barO[i] = (Open[i] - close[i+1]) / close[i+1]$barH[i] = (High[i] - close[i+1]) / close[i+1]$barL[i] = (Low[i] - close[i+1]) / close[i+1]$barC[i] = (Close[i]- close[i+1]) / close[i+1]//-------------- average price --------------------------------------Avg = ($barO[i] + $barH[i] + $barL[i] + $barC[i]) / 4MyAvg = MyAvg + AvgNEXTMyAvg = MyAvg / Periods//-------------- calculation of standard deviation ------------------FOR i = 0 TO (Periods - 1)MyVar = MyVar + ((square($barO[i] - MyAvg) + square($barH[i] - MyAvg) + square($barL[i] - MyAvg) + square($barC[i] - MyAvg)) / 4)NEXTStDev = SQRT(MyVar / Periods)Middle1 = close * (1 + MyAvg)MeanderHI = close * (1 + MyAvg + (Deviations * StDev))MeanderLO = close * (1 + MyAvg - (Deviations * StDev))//Calculation Entriesif MeanderLO > MeanderLO[1] and not (MeanderLO[1]>MeanderLO[2]) thenb=1elseif MeanderLO < MeanderLO[1] and not (MeanderLO[1]<MeanderLO[2]) then//and not (meanderlow[1]<meanderlow[2]) thenb=-1elseb=0endifendif// Condition for rectangleif (b[1]=1) thenBeginnRechteckLong = 1EndeRechteckLong = 4Counter = LastSet($RechteckStartBar) + 1ObereSeite = MeanderHIUntereSeite = Middle1// Saves the coordinates of the rectangle in array variables$RechteckStartBar[Counter] = barindex - 1 + BeginnRechteckLong$RechteckEndBar[Counter] = barindex - 1 + EndeRechteckLong$RechteckObereSeite[Counter] = ObereSeite$RechteckUntereSeite[Counter] = UntereSeite// Updating the counterCounter = Counter + 1endif// Draws all saved rectangles in the chartfor i = 0 to 0 doDRAWRECTANGLE($RechteckStartBar[i], $RechteckObereSeite[i], $RechteckEndBar[i], $RechteckUntereSeite[i]) coloured(0, 0, 255, 10) bordercolor(0, 0, 150)drawtext("#i#",$RechteckStartBar[i], $RechteckObereSeite[i],dialog,bold,20)nextRETURN05/15/2023 at 3:55 PM #21475705/15/2023 at 4:04 PM #214758 -
AuthorPosts