array variables availability in ProRealTime – examples and discussions
Forums › ProRealTime English forum › ProBuilder support › array variables availability in ProRealTime – examples and discussions
- This topic has 201 replies, 44 voices, and was last updated 1 month ago by Aragorna.
12/10/2020 at 4:42 PM #153246
For the Volume indicator example you gave in the first page , I try to adapt it for my need without success.
I try to reset the array after every new bar in order to separate the volume of each bar.
I tried with “Barindex”, “intradaybarindex” without success.
Here is my last code123456789101112131415161718192021once Barref=IntradayBarIndexif IntradayBarIndex > Barref thenBarref=IntradayBarIndex$upvol= 0$dnvol= 0endifif islastbarupdate thenif close>$lastclosethen$upvol=$upvol+volumeelsif close<$lastclose then$dnvol=$dnvol+volumeendif$lastclose=closedelta=($upvol-$dnvol)endifavg=average(delta)return $upvol coloured(0,255,255,100) style(histogram) as "up volumes", $dnvol coloured(255,0,0,100) style(histogram) as "down volumes", delta style(line,3), avg
Do you have any advice ?
Regards12/10/2020 at 4:53 PM #153248
I don’t know why but Intrabarindex is not working for the barindex comparison, I replaced it with BARINDEX:123456789101112131415161718if BarIndex > $Barref then$Barref=BarIndex$upvol= 0$dnvol= 0endifif islastbarupdate thenif close>$lastclosethen$upvol=$upvol+volumeelsif close<$lastclose then$dnvol=$dnvol+volumeendif$lastclose=closedelta=($upvol-$dnvol)endifavg=average(delta)return $upvol coloured(0,255,255,100) style(histogram) as "up volumes", $dnvol coloured(255,0,0,100) style(histogram) as "down volumes", delta style(line,3), avg
It works correctly now, the values are reset on each new bar.
1 user thanked author for this post.12/10/2020 at 5:12 PM #15325112/13/2020 at 9:40 PM #15360512/14/2020 at 9:45 AM #15363812/17/2020 at 2:42 AM #154066
Many thanks dear Nicolas for your never ending zest and passion in what you do and thereby helping others.01/02/2021 at 7:35 PM #15601101/02/2021 at 10:17 PM #156023
Multi dimensional arrays are not currently available in PRT but they are hopefully in the pipeline.
At the moment the only work around is to use for example location 123 to be location (1,2,3) and then use multiplication,division and rounding as required to get the info stored at that location.Filling array with CLOSE on Monday at 02031234567891011121314location = 0if opendayofweek = 1 thenlocation = location + 100endifif openhour = 2 thenlocation = location + 20endifif openminute = 3 thenlocation = location + 3endif$myarray(location) = closeRetrieving info from array123456789mylocation = 0mylocation = mylocation + (opendayofweek * 100)mylocation = mylocation + (openhour * 10)mylocation = mylocation + openminute * 10a = $myarray(mylocation)return a as "value this time last week"01/02/2021 at 10:30 PM #156027
Thanks a lot for the quick response. I am not sure I understand this workaround, at least the retrieving part doesn’t make much sense as you are rewriting the mylocation value. However, in my query I was referring to one-dimension arrays, i.e. I want to be able to define them at the beginning of the script, with the first positions filled in, kind of like a known table of values that I can use later on and loop through it.
A second, related question is, I noticed you cannot pass arrays as arguments to functions, correct? I am trying to do a simple moving average of an array that I have defined, but it throws a syntax error. For example:12345$myArray = 51$myArray = 0Average($myArray)01/02/2021 at 10:44 PM #156028
pablolo – Please be more careful when posting to follow the forum rules so that we keep a nice tidy forum. I tidied up your post.
- Be careful when quoting others in your posts. Only use the quote option when you need to highlight a particular bit of text that you are referring to or to highlight that you are replying to a particular member if there are several involved in a conversation. Do not include large amounts of code in your quotes. Just highlight the text you want to quote and then click on ‘Quote’.
An array is just a variable with multiple storage locations for values. You can get an average of the values at any location over the last p bars however what you are doing is not valid.return an average of the values stored at location 12 for the last 52 bars.1a = average($myarray(12))01/03/2021 at 12:29 AM #156033
Sorry if these are all basic questions, but I can’t understand your last example.
I get an error when using parenthesis in your last example:1a = average($myarray(12))
Shouldn’t it be like this?1a = average($myarray)
And, if I am right, what’s the point of calculating an average of $myarray, when it is going to be the same value for all bars?
I am still not clear of how I can calculate an average of the last 52 values of my array. I’m guessing Average function only averages across bars, not array values, is that the case?01/03/2021 at 12:58 AM #156034
Moreover, if I use “[” in this example , I get a syntax error, that it is expecting a positive whole number for “SRC”, which I am not sure what it refers to, as I do not have any variable called “SRC”.1a = average($myarray)01/03/2021 at 8:49 AM #156044
Yes my typo mistake – the correct syntax is with 
Yes AVERAGE just takes the average value over the last 52 bars of whatever you put after the instruction.
One thing you have to consider if you are using arrays in indicators is that the latest bar is formed on the fly and will continuously fill your array every time a condition to add a value is met until the bar finally closes.
If you simply want to get an average of the last 52 values when a condition was met then forget arrays and use loops.123456789101112131415161718192021defparam calculateonlastbars = 500total = 0count = 0for a = 1 to barindexif close[a] > open[a] thentotal=total + closecount = count + 1endifif count = 53 thenbreakendifnextif count = 53 thenavg = total/12elseavg = undefinedendifreturn avg as "average closing price of last 52 green candles"01/03/2021 at 2:45 PM #156088
I am actually trying to replicate the programming of the RSCMansfield indicator, as there is a limitation in probuilder and I cannot use the inbuilt function, because I want to compare an equity to an index, such as S&P 500, and that apparently is not allowed.
Given that you also cannot import external data in PRT, I am defining an array with the last 52 weekly close prices for S&P500 and wanted to calculate the RSCMansfield formula, as per https://www.stageanalysis.net/blog/4266/how-to-create-the-mansfield-relative-performance-indicator#:~:text=The%20formula%20for%20measuring%20relative,price%20of%20a%20market%20average.%22
RP = ( stock_close / index_close ) * 100
MRP = (( RP(today) / sma(RP(today), n)) - 1 ) * 100
So, what I am doing first is populate the table with S&P500 close prices:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253$sp500CierresSemanales = 3234.85$sp500CierresSemanales = 3265.35$sp500CierresSemanales = 3329.62$sp500CierresSemanales = 3295.47$sp500CierresSemanales = 3225.52$sp500CierresSemanales = 3327.71$sp500CierresSemanales = 3380.16$sp500CierresSemanales = 3337.75$sp500CierresSemanales = 2954.22$sp500CierresSemanales = 2972.37$sp500CierresSemanales = 2711.02$sp500CierresSemanales = 2304.92$sp500CierresSemanales = 2541.47$sp500CierresSemanales = 2488.65$sp500CierresSemanales = 2789.82$sp500CierresSemanales = 2874.56$sp500CierresSemanales = 2836.74$sp500CierresSemanales = 2830.71$sp500CierresSemanales = 2929.8$sp500CierresSemanales = 2863.7$sp500CierresSemanales = 2955.45$sp500CierresSemanales = 3044.31$sp500CierresSemanales = 3193.93$sp500CierresSemanales = 3041.31$sp500CierresSemanales = 3097.74$sp500CierresSemanales = 3009.05$sp500CierresSemanales = 3130.01$sp500CierresSemanales = 3185.04$sp500CierresSemanales = 3224.73$sp500CierresSemanales = 3215.63$sp500CierresSemanales = 3271.12$sp500CierresSemanales = 3351.28$sp500CierresSemanales = 3372.85$sp500CierresSemanales = 3397.16$sp500CierresSemanales = 3508.01$sp500CierresSemanales = 3426.96$sp500CierresSemanales = 3340.97$sp500CierresSemanales = 3319.47$sp500CierresSemanales = 3298.46$sp500CierresSemanales = 3348.44$sp500CierresSemanales = 3477.13$sp500CierresSemanales = 3483.81$sp500CierresSemanales = 3465.39$sp500CierresSemanales = 3269.96$sp500CierresSemanales = 3509.44$sp500CierresSemanales = 3585.15$sp500CierresSemanales = 3557.54$sp500CierresSemanales = 3638.35$sp500CierresSemanales = 3699.12$sp500CierresSemanales = 3663.46$sp500CierresSemanales = 3709.41$sp500CierresSemanales = 3703.06$sp500CierresSemanales = 3756.07
I am struggling trying to replicate the simple moving average part (SMA) using arrays or loops.
Any pseudo-code or idea on how I could achieve this? Or even better, the actual code of how PRT calculates this internally 🙂
Thank you01/03/2021 at 3:51 PM #156099
Having the last 52 closes will only allow you to calculate your RSCMansfield indicator for the last bar on the chart. Trying to do more than this will require you to fill in every weekly closing price far enough back in history (+52 more bars) to draw a line as long as you want it. You could then loop back from the LASTSET array and add 52 values and divide by 52 to get your weekly average. Then step one more bar back and repeat. You would need to update the array by adding a new array location and close value each week.
Find exclusive trading pro-tools on