trying to code a formula into
Forums › ProRealTime English forum › ProBuilder support › trying to code a formula into
- This topic has 8 replies, 3 voices, and was last updated 5 hours ago by
Patrick K Templar.
-
-
05/29/2025 at 10:52 PM #247736
Hello i’ve been trying to code a formula into the one that I’ve already got in the one I’ve used for this range indicator is based off the London opening closed ranges it looked back at 10 of them and then gives me an average I want to change the former a bit so I so I get a different reading slightly I don’t know how to do that it seems a bit more complicated for me
I’ve also added a picture in that picture I’ve used rulers and I’ve colored them and I’ve worked out the formula by hand and then draw on the boxes to their measurements then measurements ain’t the same as the formula that’s working in the program you can see the lines behind the boxes they’re slightly there are off the boxes lines are the results I am looking for
1. Overall Average Formula
[ \text{Overall Average} = \frac{\sum \text{All Values}}{\text{Total Number of Values}} ]
For example, if you have 10 numbers:
[ \text{Average} = \frac{x_1 + x_2 + … + x_{10}}{10} ]
2. Sorting the Data
Before calculating high and low averages, I sort the data in ascending order.
3. Splitting into Two Halves
– Low Half: The first half of the sorted values.
– High Half: The second half of the sorted values.
4. Low Average Formula
[ \text{Low Average} = \frac{\sum \text{Lower Half Values}}{\text{Number of Values in Lower Half}} ]
5. High Average Formula
[ \text{High Average} = \frac{\sum \text{Upper Half Values}}{\text{Number of Values in Upper Half}} ]LND range123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109defparam drawonlastbaronly = trueonce liveSessionOpen = 0once sessionDrawn = 0once startBarNYC = 0openLO = 080000closeLO = 160000limitday = 180000rangelookback = 10atr = averagetruerange[2](close)if gettimeframe <= 3600 then//--- Capture open price and bar atif opentime = openLO thenliveSessionOpen = openstartBarNYC = barindexsessionDrawn = 0endif//----------------------------------------------////----- London Zone Tracking -------------------////----------------------------------------------//once maxHighLO = highonce minLowLO = lowif opentime >= openLO and opentime <= closeLO thenbarLO = barLO + 1if high >= maxHighLO thenmaxHighLO = highendifif low <= minLowLO thenminLowLO = lowendifendifif opentime = closeLO then$minLowLO[n+1] = minLowLO$maxhighLo[n+1] = maxHighLO$dailyrange[n+1] = maxHighLO - minLowLO$barStart[n+1] = barindex[barLO]$barEnd[n+1] = barindexn = n + 1prevLowLO = minLowLOprevHighLO = maxHighLOprevidxLO = barindexbarLO = 0minLowLO = high * 100maxHighLO = 0endif//----------------------------------------------////----- Drawing and Statistics -----------------////----------------------------------------------//if islastbarupdate thencumrange = 0numrange = 0maxrange = $dailyrange[n - rangelookback + 1]minrange = $dailyrange[n - rangelookback + 1]for i = n downto n - rangelookback + 1 dodailyRange = $dailyrange[i]cumrange = cumrange + dailyRangenumrange = numrange + 1if dailyRange > maxrange thenmaxrange = dailyRangeendifif dailyRange < minrange thenminrange = dailyRangeendifnextif numrange > 0 thenavgrange = round(cumrange / numrange, 1)elseavgrange = 0endifsessionOpenPrice = liveSessionOpenlevelAvg = sessionOpenPrice + avgrangelevelAvg2 = sessionOpenPrice - avgrangelevelHigh = sessionOpenPrice + maxrangelevelHigh2 = sessionOpenPrice - maxrangelevelLow = sessionOpenPrice - minrangelevelLow2 = sessionOpenPrice + minrangedrawsegment(startBarNYC, sessionOpenPrice, startBarNYC + 10, sessionOpenPrice)drawsegment(startBarNYC, levelAvg, startBarNYC + 10, levelAvg) coloured("green")drawsegment(startBarNYC, levelAvg2, startBarNYC + 10, levelAvg2) coloured("green")drawsegment(startBarNYC, levelHigh, startBarNYC + 10, levelHigh) coloured("red")drawsegment(startBarNYC, levelHigh2, startBarNYC + 10, levelHigh2) coloured("red")drawsegment(startBarNYC, levelLow, startBarNYC + 10, levelLow) coloured("blue")drawsegment(startBarNYC, levelLow2, startBarNYC + 10, levelLow2) coloured("blue")drawtext("LNDRangeA=#avgrange#", -220, -40) anchor(topright, xshift, yshift)drawtext("LNDRangeH=#maxrange#", -220, -20) anchor(topright, xshift, yshift)drawtext("LNDRangeL=#minrange#", -220, -60) anchor(topright, xshift, yshift)endifelsedrawtext("Change timeframe to 1hr or less", 0, 0, SansSerif, bold, 34) anchor(middle, xshift, yshift)endifreturn05/29/2025 at 11:06 PM #247739Step 1: Overall Average
[ \frac{253.4 + 181.2 + 360.8 + 123.6 + 304.3 + 290.7 + 503.7 + 91.8 + 211.6 + 157.2}{10} ]
Summing the values:
[ 253.4 + 181.2 + 360.8 + 123.6 + 304.3 + 290.7 + 503.7 + 91.8 + 211.6 + 157.2 = 2478.3 ]
Dividing by 10:
[ 2478.3 \div 10 = 247.83 ]
So, the overall average is 247.83.
Step 2: Sorting the Numbers
Sorted list: 91.8, 123.6, 157.2, 181.2, 211.6, 253.4, 290.7, 304.3, 360.8, 503.7
Step 3: Low Average (Bottom Half)
Numbers: 91.8, 123.6, 157.2, 181.2, 211.6
[ \frac{91.8 + 123.6 + 157.2 + 181.2 + 211.6}{5} ]
Summing:
[ 91.8 + 123.6 + 157.2 + 181.2 + 211.6 = 765.4 ]
Dividing:
[ 765.4 \div 5 = 153.08 ]
So, the low average is 153.08.
Step 4: High Average (Top Half)
Numbers: 253.4, 290.7, 304.3, 360.8, 503.7
[ \frac{253.4 + 290.7 + 304.3 + 360.8 + 503.7}{5} ]
Summing:
[ 253.4 + 290.7 + 304.3 + 360.8 + 503.7 = 1713.9 ]
Dividing:
[ 1713.9 \div 5 = 342.78 ]
So, the high average is 342.78.Final Results:
– Overall Average: 247.83
– Low Average: 153.08
– High Average: 342.7805/30/2025 at 10:51 AM #247745Try this one.
I removed empty lines and comments just to make more lines visible in the editor.
I addes some ONCEs at the beginning, used MAX and MIN in place of some IF..ENDIFs, then made sure indexes within brackets are never lower than 1.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384defparam drawonlastbaronly = trueonce liveSessionOpen = 0once sessionDrawn = 0once startBarNYC = 0ONCE barLO = 0once maxHighLO = highonce minLowLO = lowONCE n = 0openLO = 080000closeLO = 160000limitday = 180000rangelookback = 10atr = averagetruerange[2](close)if gettimeframe <= 3600 thenif opentime = openLO thenliveSessionOpen = openstartBarNYC = barindexsessionDrawn = 0maxHighLO = highminLowLO = lown = 0endifif opentime >= openLO and opentime <= closeLO thenbarLO = barLO + 1maxHighLO = max(maxHighLO,high)minLowLO = min(minLowLO,low)endifendifif opentime = closeLO then$minLowLO[n+1] = minLowLO$maxhighLo[n+1] = maxHighLO$dailyrange[n+1] = maxHighLO - minLowLO$barStart[n+1] = barindex[barLO]$barEnd[n+1] = barindexn = n + 1prevLowLO = minLowLOprevHighLO = maxHighLOprevidxLO = barindexbarLO = 0minLowLO = high * 100maxHighLO = 0endifif islastbarupdate thencumrange = 0numrange = 0maxrange = $dailyrange[max(1,n - rangelookback + 1)]minrange = $dailyrange[max(1,n - rangelookback + 1)]for i = n downto n - rangelookback + 1 dodailyRange = $dailyrange[max(1,i)]cumrange = cumrange + dailyRangenumrange = numrange + 1if dailyRange > maxrange thenmaxrange = dailyRangeendifif dailyRange < minrange thenminrange = dailyRangeendifnextif numrange > 0 thenavgrange = round(cumrange / numrange, 1)elseavgrange = 0endifsessionOpenPrice = liveSessionOpenlevelAvg = sessionOpenPrice + avgrangelevelAvg2 = sessionOpenPrice - avgrangelevelHigh = sessionOpenPrice + maxrangelevelHigh2 = sessionOpenPrice - maxrangelevelLow = sessionOpenPrice - minrangelevelLow2 = sessionOpenPrice + minrangedrawsegment(startBarNYC, sessionOpenPrice, startBarNYC + 10, sessionOpenPrice)drawsegment(startBarNYC, levelAvg, startBarNYC + 10, levelAvg) coloured("green")drawsegment(startBarNYC, levelAvg2, startBarNYC + 10, levelAvg2) coloured("green")drawsegment(startBarNYC, levelHigh, startBarNYC + 10, levelHigh) coloured("red")drawsegment(startBarNYC, levelHigh2, startBarNYC + 10, levelHigh2) coloured("red")drawsegment(startBarNYC, levelLow, startBarNYC + 10, levelLow) coloured("blue")drawsegment(startBarNYC, levelLow2, startBarNYC + 10, levelLow2) coloured("blue")drawtext("LNDRangeA=#avgrange#", -220, -40) anchor(topright, xshift, yshift)drawtext("LNDRangeH=#maxrange#", -220, -20) anchor(topright, xshift, yshift)drawtext("LNDRangeL=#minrange#", -220, -60) anchor(topright, xshift, yshift)elsedrawtext("Change timeframe to 1hr or less", 0, 0, SansSerif, bold, 34) anchor(middle, xshift, yshift)endifreturn05/30/2025 at 11:07 PM #247761Hello i’m sorry to tell you that code didn’t work it did print the three ranges but they all have the same number and the number is 195.7 I also looked at the Look Back of 10 ranges and manually did the equation and I didn’t get anywhere near that number and I also shifted the 10 ranges back one more just a double check has the way that I did the equations and the output of those equations
They also is a screenshot if you look at the bottom right that’s this code that you’ve helped me with top right is my previous code we’ve standard deviations not this more complex but better equation
360.8 123.6 304.3 290.7 503.7 91.8 211.6 157.2 528.9 195.7
Overall Average
\frac{360.8 + 123.6 + 304.3 + 290.7 + 503.7 + 91.8 + 211.6 + 157.2 + 528.9 + 195.7}{10} ]
Summing the values: [ 360.8 + 123.6 + 304.3 + 290.7 + 503.7 + 91.8 + 211.6 + 157.2 + 528.9 + 195.7 = 2768.3 ]
Dividing by 10: [ 2768.3 \div 10 = 276.83 ]
So, overall average = 276.83.– Overall Average: 276.83
– Low Average: 155.98
– High Average: 397.68Low Average
Numbers: 91.8, 123.6, 157.2, 195.7, 211.6 [ \frac{91.8 + 123.6 + 157.2 + 195.7 + 211.6}{5} ]
Summing: [ 91.8 + 123.6 + 157.2 + 195.7 + 211.6 = 779.9 ]
Dividing by 5: [ 779.9 \div 5 = 155.98 ]High Average
Numbers: 290.7, 304.3, 360.8, 503.7, 528.9 [ \frac{290.7 + 304.3 + 360.8 + 503.7 + 528.9}{5} ]
Summing: [ 290.7 + 304.3 + 360.8 + 503.7 + 528.9 = 1988.4 ]
Dividing by 5: [ 1988.4 \div 5 = 397.68 ]Sorting the Numbers
Sorted list: 91.8, 123.6, 157.2, 181.2, 211.6, 290.7, 304.3, 360.8, 503.7, 528.9Low Average
Numbers: 91.8, 123.6, 157.2, 181.2, 211.6 [ \frac{91.8 + 123.6 + 157.2 + 181.2 + 211.6}{5} ]
Summing: [ 91.8 + 123.6 + 157.2 + 181.2 + 211.6 = 765.4 ]
Dividing by 5: [ 765.4 \div 5 = 153.08 ]
So, low average = 153.08.
High Average
Numbers: 290.7, 304.3, 360.8, 503.7, 528.9 [ \frac{290.7 + 304.3 + 360.8 + 503.7 + 528.9}{5} ]
Summing: [ 290.7 + 304.3 + 360.8 + 503.7 + 528.9 = 1988.4 ]
Dividing by 5: [ 1988.4 \div 5 = 397.68 ]
So, high average = 397.68.
05/30/2025 at 11:19 PM #247764calculate the overall average of the last 10 London session ranges, which you’ve already set up. Now, we take it a step further with a refined statistical approach:
1️⃣ Identify the 5 highest values in the dataset of 10.
2️⃣ Calculate the average of these 5 highest values.
3️⃣ Identify the 5 lowest values in the dataset.
4️⃣ Calculate the average of these 5 lowest values.This gives you a high average and low average, which is a more nuanced measure than standard deviation. It’s brilliant because it filters extreme variations without being overly sensitive to outliers.
✅ Collects the last 10 London ranges
✅ Sorts the dataset to easily extract the highest and lowest values
✅ Selects the 5 highest and 5 lowest values
✅ Computes their respective averages05/31/2025 at 11:18 AM #247772Please check this code for the correct ranges:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758defparam drawonlastbaronly = trueonce liveSessionOpen = 0once startBarNYC = 0once maxHighLO = highonce minLowLO = lowONCE n = 0ONCE openLO = 080000ONCE closeLO = 160000ONCE limitday = 180000ONCE rangelookback = 10atr = averagetruerange[2](close)if gettimeframe <= 3600 thenif opentime = openLO thenliveSessionOpen = openstartBarNYC = barindexmaxHighLO = highminLowLO = lowendifif opentime >= openLO and opentime <= closeLO thenmaxHighLO = max(maxHighLO,high)minLowLO = min(minLowLO,low)endifif opentime = closeLO thenn = n + 1$dailyrange[n] = maxHighLO - minLowLOendifIF n >= rangelookback THENcumrange = 0maxrange = 0minrange = 9999999for i = n downto n - rangelookback + 1 documrange = cumrange + $dailyrange[i]maxrange = max(maxrange,$dailyrange[i])minrange = min(minrange,$dailyrange[i])nextavgrange = round(cumrange / rangelookback, 1)sessionOpenPrice = liveSessionOpenlevelAvg = sessionOpenPrice + avgrangelevelAvg2 = sessionOpenPrice - avgrangelevelHigh = sessionOpenPrice + maxrangelevelHigh2 = sessionOpenPrice - maxrangelevelLow = sessionOpenPrice - minrangelevelLow2 = sessionOpenPrice + minrangedrawsegment(startBarNYC, sessionOpenPrice, BarIndex + 1, sessionOpenPrice)drawsegment(startBarNYC, levelAvg, startBarNYC + 10, levelAvg) coloured("green")drawsegment(startBarNYC, levelAvg2, startBarNYC + 10, levelAvg2) coloured("green")drawsegment(startBarNYC, levelHigh, startBarNYC + 10, levelHigh) coloured("red")drawsegment(startBarNYC, levelHigh2, startBarNYC + 10, levelHigh2) coloured("red")drawsegment(startBarNYC, levelLow, startBarNYC + 10, levelLow) coloured("blue")drawsegment(startBarNYC, levelLow2, startBarNYC + 10, levelLow2) coloured("blue")drawtext("LNDRangeA=#avgrange#", -220, -40) anchor(topright, xshift, yshift)drawtext("LNDRangeH=#maxrange#", -220, -20) anchor(topright, xshift, yshift)drawtext("LNDRangeL=#minrange#", -220, -60) anchor(topright, xshift, yshift)ENDIFelsedrawtext("Change timeframe to 1hr or less", 0, 0, SansSerif, bold, 34) anchor(middle, xshift, yshift)endifreturnI also removed all the unused variables and arrays.
1 user thanked author for this post.
05/31/2025 at 1:01 PM #247773When using an average, it does not necessarily mean that the average is also the middle value (the median)…
The number of values above or below the average does not have to be equal…
The code below first calculates the average, then determines how many values are above or below that average…
Based on this, the “HighAvg” and “LowAvg” are calculated…
1234567891011121314151617181920212223N=10HigherSum=0HNumber=0LowerSum=0LNumber=0xAverage=Average[N](Close)For i=0 to N-1If Close[i]>xAverage thenHigherSum=HigherSum+Close[i]HNumber=HNumber+1ElsIf Close[i]<xAverage thenLowerSum=LowerSum+Close[i]LNumber=LNumber+1EndIfNextHighAvg=HigherSum/HNumberLowAvg=LowerSum/LNumberReturn xAverage as "Average"Coloured("Yellow"),HighAvg as "HighAvg" Coloured("Green"),LowAvg as "LowAvg" Coloured("Red")1 user thanked author for this post.
06/02/2025 at 1:25 PM #247805By the looks of it on the studies it’s taking the lowest Range and marking that up as the low and then it’s taking the highest range of marking that up as the high not taking the 10 ranges and split in the top five and then average in them and then obviously taking the bottom five and then finding the average out then out of them, don’t know if PRT is capable of doing that a gentleman has also responded with a slightly different way of doing it not too sure how to implement that into the code logic that’s where I struggle that might show some nice results ,,check file,,
06/03/2025 at 6:08 PM #247844HELLO would you have a look again pleas
-
AuthorPosts
Find exclusive trading pro-tools on