TradingView
alessm65617
Feb 10, 2024 8:57 AM

Mean and Standard Deviation Lines  

NVIDIA CorporationNASDAQ

Description

Description:

Calculates the mean and standard deviation of close-to-close price differences over a specified period, providing insights into price volatility and potential breakouts.
Manually calculates mean and standard deviation for a deeper understanding of statistical concepts.
Plots the mean line, upper bound (mean + standard deviation), and lower bound (mean - standard deviation) to visualize price behavior relative to these levels.
Highlights bars that cross the upper or lower bounds with green (above) or red (below) triangles for easy identification of potential breakouts or breakdowns.
Customizable period input allows for analysis of short-term or long-term volatility patterns.

Probability Interpretations based on Standard Deviation:

50% probability: mean or expected value
68% probability: Values within 1 standard deviation of the mean (mean ± stdev) represent roughly 68% of the data in a normal distribution. This implies that around 68% of closing prices in the past period fell within this range.
95% probability: Expanding to 2 standard deviations (mean ± 2*stdev) captures approximately 95% of the data. So, in theory, there's a 95% chance that future closing prices will fall within this wider range.
99.7% probability: Going further to 3 standard deviations (mean ± 3*stdev) encompasses nearly 99.7% of the data. However, these extreme values become less likely as you move further away from the mean.

Key Features:

Uses manual calculations for mean and standard deviation, providing a hands-on approach.
Excludes the current bar's close price from calculations for more accurate analysis of past data.
Ensures valid index usage for robust calculation logic.
Employs unbiased standard deviation calculation for better statistical validity.
Offers clear visual representation of mean and volatility bands.

Considerations:

Manual calculations might have a slight performance impact compared to built-in functions.
Not a perfect normal distribution: Financial markets often deviate from a perfect normal distribution. This means probability interpretations based on standard deviation shouldn't be taken as absolute truths.
Non-stationarity: Market conditions and price behavior can change over time, impacting the validity of past data as a future predictor.
Other factors: Many other factors influence price movements beyond just the mean and standard deviation.
Always consider other technical and fundamental factors when making trading decisions.

Potential Use Cases:

Identifying periods of high or low volatility.
Discovering potential breakout or breakdown opportunities.
Comparing volatility across different timeframes.
Complementing other technical indicators for confirmation.
Understanding statistical concepts for financial analysis.

Release Notes

made some changes on how the mean and standard deviation are calculated and I think this time it works.

Release Notes

changed some colors for clarity and make some correction to the descriptions.

Release Notes

updated the math. This indicator is a work in progress so be careful when you use it.

Release Notes

changed default period length to 100.
Comments
sinan751cf
Thanks for this, I can't see 2 and 3 deviations from the standard mean, what am I missing?
alessm65617
@sinan751cf, only one standard deviation available in this script, you can add my probability indicator to see the probability of price levels.
alessm65617
@sinan751cf, I have to say anything more than 1 standard deviation is too much as that accounts for approximately 68% of the price action.
sinan751cf
@alessm65617, 68.27% :)
it would be beneficial to scan weekly 99.7% (3 pt deviation) IMO
that's why I asked., thanks for your response.if I need to add 2 and 3.
would that be this line to adjust?
stdev_pct_change = math.sqrt(sum_squared_deviations / (period - 1)) // Unbiased calculation
to something like this
stdev_pct_change = math.sqrt(sum_squared_deviations / (period - 2)) ??
alessm65617
@sinan751cf, you can change upper_bound = close[1] * (1 + mean_pct_change + 3 * stdev_pct_change)
lower_bound = close[1] * (1 + mean_pct_change -3 * stdev_pct_change)
alessm65617
@sinan751cf, I have never used it to scan, how is that done?
sinan751cf
@alessm65617, I m not sure how, but I hope I can :)
sinan751cf
@sinan751cf,
// Calculate upper and lower bounds based on percent change upper_bound = close[1] * (1 + mean_pct_change + stdev_pct_change) lower_bound = close[1] * (1 + mean_pct_change - stdev_pct_change) upper_bound1 = close[1] * (1 + mean_pct_change + 2*stdev_pct_change) lower_bound1 = close[1] * (1 + mean_pct_change - 2*stdev_pct_change) upper_bound2 = close[1] * (1 + mean_pct_change + 3*stdev_pct_change) lower_bound2 = close[1] * (1 + mean_pct_change - 3*stdev_pct_change) upper_bound3 = close[1] * (1 + mean_pct_change + 4*stdev_pct_change) lower_bound3 = close[1] * (1 + mean_pct_change - 4*stdev_pct_change) // Plot mean line plot(close[1] * (1 + mean_pct_change) , color=color.gray, linewidth=2, title="Mean (% Change)") // Plot upper and lower bound lines plot(upper_bound, color=color.blue, linewidth=2, title="Upper Bound") plot(lower_bound, color=color.orange, linewidth=2, title="Lower Bound") plot(upper_bound1, color=color.rgb(107, 33, 243), linewidth=2, title="Upper Bound1") plot(lower_bound1, color=color.rgb(128, 117, 105), linewidth=2, title="Lower Bound1") plot(upper_bound2, color=color.rgb(107, 33, 243), linewidth=2, title="Upper Bound1") plot(lower_bound2, color=color.rgb(128, 117, 105), linewidth=2, title="Lower Bound1") plot(upper_bound3, color=color.rgb(107, 33, 243), linewidth=2, title="Upper Bound1") plot(lower_bound3, color=color.rgb(128, 117, 105), linewidth=2, title="Lower Bound1")


so it would look like this?
alessm65617
@sinan751cf, I think so
alessm65617
@sinan751cf, if you check the latest version of the probability indicator, you will find that it has the probability of closing lower than current price in red which will help you achieve what you are trying to do without needing to draw the other stdev levels.
More