ProRealCode - Trading & Coding with ProRealTime™
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}} ]
defparam drawonlastbaronly = true
once liveSessionOpen = 0
once sessionDrawn = 0
once startBarNYC = 0
openLO = 080000
closeLO = 160000
limitday = 180000
rangelookback = 10
atr = averagetruerange[2](close)
if gettimeframe <= 3600 then
//--- Capture open price and bar at
if opentime = openLO then
liveSessionOpen = open
startBarNYC = barindex
sessionDrawn = 0
endif
//----------------------------------------------//
//----- London Zone Tracking -------------------//
//----------------------------------------------//
once maxHighLO = high
once minLowLO = low
if opentime >= openLO and opentime <= closeLO then
barLO = barLO + 1
if high >= maxHighLO then
maxHighLO = high
endif
if low <= minLowLO then
minLowLO = low
endif
endif
if opentime = closeLO then
$minLowLO[n+1] = minLowLO
$maxhighLo[n+1] = maxHighLO
$dailyrange[n+1] = maxHighLO - minLowLO
$barStart[n+1] = barindex[barLO]
$barEnd[n+1] = barindex
n = n + 1
prevLowLO = minLowLO
prevHighLO = maxHighLO
previdxLO = barindex
barLO = 0
minLowLO = high * 100
maxHighLO = 0
endif
//----------------------------------------------//
//----- Drawing and Statistics -----------------//
//----------------------------------------------//
if islastbarupdate then
cumrange = 0
numrange = 0
maxrange = $dailyrange[n - rangelookback + 1]
minrange = $dailyrange[n - rangelookback + 1]
for i = n downto n - rangelookback + 1 do
dailyRange = $dailyrange[i]
cumrange = cumrange + dailyRange
numrange = numrange + 1
if dailyRange > maxrange then
maxrange = dailyRange
endif
if dailyRange < minrange then
minrange = dailyRange
endif
next
if numrange > 0 then
avgrange = round(cumrange / numrange, 1)
else
avgrange = 0
endif
sessionOpenPrice = liveSessionOpen
levelAvg = sessionOpenPrice + avgrange
levelAvg2 = sessionOpenPrice - avgrange
levelHigh = sessionOpenPrice + maxrange
levelHigh2 = sessionOpenPrice - maxrange
levelLow = sessionOpenPrice - minrange
levelLow2 = sessionOpenPrice + minrange
drawsegment(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)
endif
else
drawtext("Change timeframe to 1hr or less", 0, 0, SansSerif, bold, 34) anchor(middle, xshift, yshift)
endif
return
Step 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.78
Try 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.
defparam drawonlastbaronly = true
once liveSessionOpen = 0
once sessionDrawn = 0
once startBarNYC = 0
ONCE barLO = 0
once maxHighLO = high
once minLowLO = low
ONCE n = 0
openLO = 080000
closeLO = 160000
limitday = 180000
rangelookback = 10
atr = averagetruerange[2](close)
if gettimeframe <= 3600 then
if opentime = openLO then
liveSessionOpen = open
startBarNYC = barindex
sessionDrawn = 0
maxHighLO = high
minLowLO = low
n = 0
endif
if opentime >= openLO and opentime <= closeLO then
barLO = barLO + 1
maxHighLO = max(maxHighLO,high)
minLowLO = min(minLowLO,low)
endif
endif
if opentime = closeLO then
$minLowLO[n+1] = minLowLO
$maxhighLo[n+1] = maxHighLO
$dailyrange[n+1] = maxHighLO - minLowLO
$barStart[n+1] = barindex[barLO]
$barEnd[n+1] = barindex
n = n + 1
prevLowLO = minLowLO
prevHighLO = maxHighLO
previdxLO = barindex
barLO = 0
minLowLO = high * 100
maxHighLO = 0
endif
if islastbarupdate then
cumrange = 0
numrange = 0
maxrange = $dailyrange[max(1,n - rangelookback + 1)]
minrange = $dailyrange[max(1,n - rangelookback + 1)]
for i = n downto n - rangelookback + 1 do
dailyRange = $dailyrange[max(1,i)]
cumrange = cumrange + dailyRange
numrange = numrange + 1
if dailyRange > maxrange then
maxrange = dailyRange
endif
if dailyRange < minrange then
minrange = dailyRange
endif
next
if numrange > 0 then
avgrange = round(cumrange / numrange, 1)
else
avgrange = 0
endif
sessionOpenPrice = liveSessionOpen
levelAvg = sessionOpenPrice + avgrange
levelAvg2 = sessionOpenPrice - avgrange
levelHigh = sessionOpenPrice + maxrange
levelHigh2 = sessionOpenPrice - maxrange
levelLow = sessionOpenPrice - minrange
levelLow2 = sessionOpenPrice + minrange
drawsegment(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)
else
drawtext("Change timeframe to 1hr or less", 0, 0, SansSerif, bold, 34) anchor(middle, xshift, yshift)
endif
return
Hello 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.68
Low 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.9
Low 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.
calculate 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 averages
Please check this code for the correct ranges:
defparam drawonlastbaronly = true
once liveSessionOpen = 0
once startBarNYC = 0
once maxHighLO = high
once minLowLO = low
ONCE n = 0
ONCE openLO = 080000
ONCE closeLO = 160000
ONCE limitday = 180000
ONCE rangelookback = 10
atr = averagetruerange[2](close)
if gettimeframe <= 3600 then
if opentime = openLO then
liveSessionOpen = open
startBarNYC = barindex
maxHighLO = high
minLowLO = low
endif
if opentime >= openLO and opentime <= closeLO then
maxHighLO = max(maxHighLO,high)
minLowLO = min(minLowLO,low)
endif
if opentime = closeLO then
n = n + 1
$dailyrange[n] = maxHighLO - minLowLO
endif
IF n >= rangelookback THEN
cumrange = 0
maxrange = 0
minrange = 9999999
for i = n downto n - rangelookback + 1 do
cumrange = cumrange + $dailyrange[i]
maxrange = max(maxrange,$dailyrange[i])
minrange = min(minrange,$dailyrange[i])
next
avgrange = round(cumrange / rangelookback, 1)
sessionOpenPrice = liveSessionOpen
levelAvg = sessionOpenPrice + avgrange
levelAvg2 = sessionOpenPrice - avgrange
levelHigh = sessionOpenPrice + maxrange
levelHigh2 = sessionOpenPrice - maxrange
levelLow = sessionOpenPrice - minrange
levelLow2 = sessionOpenPrice + minrange
drawsegment(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)
ENDIF
else
drawtext("Change timeframe to 1hr or less", 0, 0, SansSerif, bold, 34) anchor(middle, xshift, yshift)
endif
return
I also removed all the unused variables and arrays.
When 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…
N=10
HigherSum=0
HNumber=0
LowerSum=0
LNumber=0
xAverage=Average[N](Close)
For i=0 to N-1
If Close[i]>xAverage then
HigherSum=HigherSum+Close[i]
HNumber=HNumber+1
ElsIf Close[i]<xAverage then
LowerSum=LowerSum+Close[i]
LNumber=LNumber+1
EndIf
Next
HighAvg=HigherSum/HNumber
LowAvg=LowerSum/LNumber
Return xAverage as "Average"Coloured("Yellow"),HighAvg as "HighAvg" Coloured("Green"),LowAvg as "LowAvg" Coloured("Red")
By 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,,
HELLO would you have a look again pleas
i would like to use this
// Copy original values into sorted array
FOR i = 0 TO 9 DO
sorted[i] = rangeValues[i]
NEXT
// Sort descending using simple bubble sort
FOR i = 0 TO 8 DO
FOR j = i + 1 TO 9 DO
IF sorted[j] > sorted[i] THEN
temp = sorted[i]
sorted[i] = sorted[j]
sorted[j] = temp
ENDIF
NEXT
NEXT
sumHigh = 0
FOR i = 0 TO 4 DO // Top 5 values: sorted[0] to sorted[4]
sumHigh = sumHigh + sorted[i]
NEXT
highAvg = sumHigh / 5
In your first post you wrote “it looked back at 10 of them and then gives me an average“. I still can’t understand why you want to sort those data, as when calculating their average, it won’t change if data are sorted or not.
Example:
. unsorted list: 8 3 2 3 9 7 6 0 1 5 (total 44, avg = 4,4)
. sorted list: 0 1 2 3 3 5 6 7 8 9 (total 44, avg = 4.4)
unsorted list: 8 3 2 3 9 7 6 0 1 5 (total 44, avg = 4,4) 8 3 2 3 9 = 25 7 6 0 1 5 = 19 this is wrong no point
sorted list: 0 1 2 3 4 5 6 7 8 9 (total 44, avg = 4.4) LOW 0 1 2 3 4 = 10 HIGH 5 6 7 8 9 = 35 with 10 results split in too low group and high group it remove at outliers or more like faze it into the high or low
trying to code a formula into
This topic contains 11 replies,
has 3 voices, and was last updated by Patrick K Templar
7 months, 3 weeks ago.
| Forum: | ProBuilder: Indicators & Custom Tools |
| Language: | English |
| Started: | 05/29/2025 |
| Status: | Active |
| Attachments: | 5 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.