Calculating the Median of Recent Close Prices

13 Jul 2023
0 comment
0 attachment

This ProBuilder code snippet demonstrates how to calculate the median of the last ‘n’ close prices in a trading chart. The median is a valuable statistical measure that indicates the middle value of a dataset, providing insights into the central tendency of the data.


once Median=0 
if barindex>length then 
FOR X = 0 TO length-1 
M = close[X] //this example takes the median of the last 5 closes 
SmallPart = 0 
LargePart = 0 
FOR Y = 0 TO length-1 
IF close[Y] < M THEN 
SmallPart = SmallPart + 1 
ELSIF 
close[Y] > M THEN 
LargePart = LargePart + 1 
ENDIF 
IF LargePart = SmallPart AND Y = length-1 THEN 
Median = M 
BREAK 
ENDIF 
NEXT 
NEXT 
endif 
Return Median as "Median"

Explanation of the Code:

  • The code initializes the Median variable to 0 and checks if the current bar index is greater than the specified length to ensure there are enough data points.
  • A FOR loop iterates from 0 to length-1, setting M to each close price in this range.
  • For each M, two counters, SmallPart and LargePart, are initialized to zero. These counters track the number of close prices less than and greater than M, respectively.
  • Another FOR loop iterates through all close prices again to compare each with M and increments the appropriate counter based on whether the close price is less than or greater than M.
  • If at the end of the inner loop (Y = length-1), SmallPart equals LargePart, it indicates that M is the median, and the loop breaks.
  • The median value is then returned.

This code effectively finds the median of the last ‘n’ close prices, which can be useful for identifying the middle value in a series of trading data points, helping to understand market trends without the influence of outliers.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/median-function-coding/#post-62659

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Despair Master
Developer by day, aspiring writer by night. Still compiling my bio... Error 404: presentation not found.
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
134
strategy
171

Recent Snippets

How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Logo Logo
Loading...