how do I loop back to save price and bar index between two points
Forums › ProRealTime English forum › ProBuilder support › how do I loop back to save price and bar index between two points
- This topic has 13 replies, 4 voices, and was last updated 1 year ago by PeterSt.
-
-
12/28/2022 at 3:00 PM #206400
Could somebody please advise. I am completely stuck. I am trying to store the last high over a specific period(but not including the current bar),once a condition is met. Then save both the bar index of the bar that has the high on it and the high price. So that both may be referenced later.
I am guessing it is a loop but I don’t know how to create a loop that starts looking for the high only from the previous bar. I think I need to store the value and the bar index in arrays but again I am not sure. I have attached a picture to show exactly what I mean.
Please excuse the repetition in the question and diagram I just want to be clear so as not to waste anybodies time.
many thanks in advance
12/28/2022 at 7:46 PM #206429Yes, you need to loop through the past bars:
1234567891011121314ONCE Periods = 20ONCE HHprice = 0ONCE HHbar = 0IF MyCondition THENHHprice = highest[Periods](high[1])FOR i = 1 TO BarIndex - 1IF high[i] = HHprice THENHHbar = BarIndex[i]breakENDIFNEXTENDIF// HHprice is the prior HIGH value// HHbar is the BarIndex where that HIGH is sitting1 user thanked author for this post.
12/29/2022 at 7:05 AM #206432Hi @blackwing
Maybe this will help you too…
I have chosen as an (example) condition for a “crosses over” …
When this condition is true, I have the following results stored in an array:
The point at which the crossing takes place, the price (high) of the crossing and the distance in bars compared to the previous crossing…
Regards Jaap
Check Crossings1234567891011121314151617181920212223242526DefParam CalculateOnLastBars=200DefParam DrawOnLastBarOnly=trueN=200 //Total number of barsxIndex=0 //Reset the index counterFor i=0 to N-1 //Index i runs from 0 to N-1$Avg[i]=Average[20](Close[i])//Store signal in array $AvgIf Close[i+1] < $Avg[i+1] and Close[i] > $Avg[i] then //Check "Crosses Over" condition$Result[xIndex]=1 //Result of condition (boolean)$ResultBarIndex[xIndex]=BarIndex-i //BarIndex of result$ResultPrice[xIndex]=High[i] //Price of resultxBarDiffResult=$ResultBarIndex[xIndex]-$ResultBarIndex[xIndex+1] //Difference in bars between first result and second result//PlotDrawSegment(BarIndex-i,$Avg[i],BarIndex-(i+1),$Avg[i+1])coloured(255,0,0) //Plot the average signalDrawPoint($ResultBarIndex[xIndex],$ResultPrice[xIndex]+1,2)Coloured(255,0,0) //Plot a point above the resultDrawText("Distance=#xBarDiffResult#",$ResultBarIndex[xIndex],$ResultPrice[xIndex]+2) //Plot the difference between two resultsEndIfxIndex=xIndex+1 //Update the index counterNextReturn1 user thanked author for this post.
01/02/2023 at 1:10 PM #206654Hi Roberto,
Thank you for the loop instruction that has helped me a lot!
It has taken me a while to figure this one out. I couldn’t get your elegant code to work correctly, when I run it, it is showing multiple high points and does not include the previous condition bar in the calculation.(image1).
However using your loop structure and adding my very not elegant code I got it working (image2). But this feels rather clunky. Do you have a better way? Or am I missing something completely?
Also how do you recall the HHPrice from the past. ie. 3rd last HHPrice not HHPrice[3] value from 3 bars ago.
Many thanks again.
12345678910111213141516171819202122ONCE Periods = 20ONCE HHprice = 0ONCE HHbar = 0C1 = low<low[1] and high<high[1] and high[1]>high[2] and low[1]>Low[2] //Down bar after an Up barIF C1 THENBarssinceC= BarsSince(C1[1])+1FOR i = 1 TO BarssinceCIF high[i] = highest[BarssinceC](high[1]) THENHHbar = BarIndex[i]HHPrice= high[i]breakENDIFNEXTDRAWCANDLE(open, high, low, close) coloured(200,0,0)Drawpoint(HHbar,HHPrice,2)ENDIFreturn// HHprice is the prior HIGH value// HHbar is the BarIndex where that HIGH is sitting01/02/2023 at 3:29 PM #206672Thank you @JS,
That is super helpful.
I have one more question I am having trouble recalling the the values stored in the array. How do I recall the last [n] value of the $ResultPrice.
In your example, how would I recall the $ResultPrice the last time the cross occurred or the $ResultPrice[n]th last time it occurred, I am sure it is simple but I can’t seem to grasp it.
Many thanks in advance
01/02/2023 at 3:59 PM #20667601/02/2023 at 7:38 PM #206692Thanks again @JS
I am still confused. My apologies if I am being slow.
Returning “$ResultPrice[0]” after the Return is all good. But when I try “$ResultPrice[1]” it does not give the value prior to the “$ResultPrice[0]” but just moves it along one bar, also when I run your programme I get the attached image. With the plot of the average only going back one bar this seems to be a similar issue.
I am trying to recall several past values and plot them. My understanding of an $Array was that each saved value of it is stored, but I can’t seem to recall them later. For example how do I plot the 3rd value ago of the crossover, rather than the value of the last crossover 3 bars ago?
In the attached image I am returning “$ResultPrice[0], $ResultPrice[3]”
Many thanks
01/02/2023 at 8:47 PM #206697Hi @blackwing
Probably most of the forum agrees with me when I say that understanding programming with arrays is one of the most difficult things, so it’s not your fault…
In the code, the Index is related to the crossings, Index=1 is crossing 1 and so on, and these crossings have several data such as the bar index and the price, but these only belong to these crossings…
What I added is the Close of each crossing and to meet your request there is an additional variable called “xPriceBefore” and when you give it a certain values like for example 3, then the code plots the value of the close three bars for the first crossing…
I can’t make it any more fun… 😉
Check Crossings v212345678910111213141516171819202122232425262728DefParam CalculateOnLastBars=200DefParam DrawOnLastBarOnly=trueN=200 //Total number of barsxIndex=0 //Reset the index counterxPriceBefore=0For i=0 to N-1 //Index i runs from 0 to N-1$Avg[i]=Average[20](Close[i])//Store signal in array $AvgIf Close[i+1] < $Avg[i+1] and Close[i] > $Avg[i] then //Check "Crosses Over" condition$Result[xIndex]=1 //Result of condition (boolean)$ResultBarIndex[xIndex]=BarIndex-i //BarIndex of result$ResultPrice[xIndex]=Close[i+xPriceBefore] //Price of resultxResultPrice=$ResultPrice[xIndex]xBarDiffResult=$ResultBarIndex[xIndex]-$ResultBarIndex[xIndex+1] //Difference in bars between first result and second result//PlotDrawSegment(BarIndex-i,$Avg[i],BarIndex-(i+1),$Avg[i+1])coloured(255,0,0) //Plot the average signalDrawPoint($ResultBarIndex[xIndex],$ResultPrice[xIndex]+1,3)Coloured(255,0,0)BorderColor(0,255,0)//Plot a point above the resultDrawText("Distance=#xBarDiffResult#",$ResultBarIndex[xIndex],$ResultPrice[xIndex]+15*pipsize,Serif,Italic,18) //Plot the difference between two resultsDrawText("Close=#xResultPrice#",$ResultBarIndex[xIndex],$ResultPrice[xIndex]+25*pipsize,Serif,Italic,18)EndIfxIndex=xIndex+1 //Update the index counterNextReturn $ResultPrice[0]01/04/2023 at 9:16 AM #206745Thank you @JS
Oh my goodness.You have just made my head explode!
You have probably answered this question, my apologies but I still don’t understand. I am trying to keep track of the last 3 or 4 values (and barindex) of the saved point (crossing, in your example), plot them and then use them in code later.
So if I simply wanted to see if C< C1 and C1<C2 where C are the last instances of Crossing. How do I do that? (diag1.)
Many thanks
01/04/2023 at 10:47 AM #206751Hi @blackwing
Here’s the simple version… 😉
You can retrieve the values of the Close and the BarIndex of the first intersection with:
$xClose[0] and $xBarIndex[0]
And of the previous crossing with:
$xClose[1] and $xBarIndex[1]
And so on…Hi @blackwing
Check Crossings v3123456789101112N=200xIndex=0For i=0 to N-1If Close[i] crosses over Average[20](Close[i]) then$xClose[xIndex]=Close[i]$xBarIndex[xIndex]=BarIndex[BarIndex-i]xIndex=xIndex+1EndIfNextReturn $xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]01/04/2023 at 12:26 PM #206763Thank you @JS
Looks simple but ?
Please could you plot this image. Because when I try with either of the following it doesn’t work
Many thanks in advance
1234567891011121314N=200xIndex=0For i=0 to N-1If Close[i] crosses over Average[20](Close[i]) then$xClose[xIndex]=Close[i]$xBarIndex[xIndex]=BarIndex[BarIndex-i]xIndex=xIndex+1EndIfNextDrawText("1",$xBarIndex[0],$Close[0]+10*pipsize,Serif,Italic,18)DrawText("2",$xBarIndex[1],$Close[1]+10*pipsize,Serif,Italic,18)DrawText("3",$xBarIndex[2],$Close[2]+10*pipsize,Serif,Italic,18)Return $xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]1234567891011121314N=200xIndex=0For i=0 to N-1If Close[i] crosses over Average[20](Close[i]) then$xClose[xIndex]=Close[i]$xBarIndex[xIndex]=BarIndex[BarIndex-i]xIndex=xIndex+1EndIfNextDrawText("1",barindex[$xBarIndex[0]],Close[$Close[0]]+10*pipsize,Serif,Italic,18)DrawText("2",barindex[$xBarIndex[1]],Close[$Close[1]]+10*pipsize,Serif,Italic,18)DrawText("3",barindex[$xBarIndex[2]],Close[$Close[2]]+10*pipsize,Serif,Italic,18)Return $xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]01/04/2023 at 3:30 PM #206782Hi @blackwing
Final touch…
Check Crossings v4123456789101112131415161718DefParam DrawOnLastBarOnly=trueDefParam CALCULATEONLASTBARS=200N=200xIndex=0For i=0 to N-1If Close[i] crosses over Average[20](Close[i]) then$xClose[xIndex]=Close[i]$xBarIndex[xIndex]=BarIndex-ixIndex=xIndex+1EndIfNextDrawText("1",$xBarIndex[0],$xClose[0]+15*pipsize,Serif,Italic,30)Coloured("Red")DrawText("2",$xBarIndex[1],$xClose[1]+15*pipsize,Serif,Italic,30)Coloured("Red")DrawText("3",$xBarIndex[2],$xClose[2]+15*pipsize,Serif,Italic,30)Coloured("Red")Return //$xClose[0], $xClose[1], $xClose[2], $xBarIndex[0], $xBarIndex[1], $xBarIndex[2]1 user thanked author for this post.
01/09/2023 at 12:08 PM #20710301/10/2023 at 5:59 AM #207143But when I try “$ResultPrice[1]” it does not give the value prior to the “$ResultPrice[0]”
I don’t think it will help you much, but for understandings : with arrays you can’t ask for the previous value of an element in it (or for the previous value of the whole array).
Probably most of the forum agrees with me when I say that understanding programming with arrays is one of the most difficult things, so it’s not your fault…
Agreed. But it is not the programming with arrays which is dificult – it is the way PRT implemented it (and this is not about not being able to ask for the previous bar value (as in [1])). If only the arrays would be multi dimensional most of the issues *I* have with arrays in PRT would be gone. This is not because I need more dimensions, but because it would allow for making clear what you are doing with them. This in itself is hard to explain, but you can simply know when you are used to a normal environment with arrays.
Hats off to those like you, Jaap, who can utilise arrays within PRT. I never came further than using the available means without arrays, accomplishing the same (knowing that everything and all already *are* arrays (the [1], [2], [3] thing) and that you can let loose all functions (e.g. Average) on that too – which is not even possible with arrays (last time I looked)).
-
AuthorPosts