Variance WindowsJust a quick trial at using statistical variance/standard deviation as an indicator. The general idea is that higher variance in the short term tends to indicate more volatility/movement. The other thing is that it can help set probabilistic boundaries for movements (e.g., if you set the bars to be 2 standard deviations, you are visualizing a range that denotes a 95% probability window).
I haven't really tried forming any sort of strategies around this indicator, but there are a few potential possibilities for its usability.
Generally speaking, the magnitude of the standard deviation (relative to the price) is small when the market is consolidating. It is larger when the market is trending up or own.
If the long term variance and the short-term variance are close to each other in scale, the trend is strong. Otherwise, the trend is weak. Note that I am only saying that the "trend" is strong , not that it is necessarily positive. this could be an up-trend, down-trend, or a sideways trend.
When the magnitudes of the variances are changing from very similar to very different (usually it's the long-term variance getting much larger than the short-term one), that's an indication that the previous trend is coming to an end.
Typically, it's the long-term variance that is bigger than the short-term. However, when you see them cross where the short-term is bigger or even much bigger than the long-term, it's indicative of a spike event (more often than not, one that is not favorable if you are holding any position on a given security).
Because you have probabilistic windows based on some n standard deviations from the midline (which in this version, I've used a ZLEMA as that midline), those boundaries could possibly be used to set stop-loss limits and the like.
There's nothing too complicated or deep about this particular indicator. All I'm really doing is assuming that we are dealing with a Gaussian random process. I am actually using EMA as my mean computation, even though for a proper Gaussian variance calculation, I should be using SMA. When I used SMA, though, it felt a lot more sensitive to noise, which made it feel less usable. In any case, it's just a simple first trial in many years after not having even looked at Pine Script to finally messing around with it again. Open to a litany of criticisms as I'm sure there will be many that are rightly deserved. Otherwise, happy scalping to thee.
Variance
Beta ScreenerThis script allows you to screen up to 38 symbols for their beta. It also allows you to compare the list to not only SPY but also CRYPTO10! Features include custom time frame and custom colors.
Here is a refresher on what beta is:
Beta (β) is a measure of the volatility—or systematic risk—of a security or portfolio compared to the market as a whole (usually the S&P 500 ). Stocks with betas higher than 1.0 can be interpreted as more volatile than the S&P 500 .
Beta is used in the capital asset pricing model (CAPM), which describes the relationship between systematic risk and expected return for assets (usually stocks). CAPM is widely used as a method for pricing risky securities and for generating estimates of the expected returns of assets, considering both the risk of those assets and the cost of capital.
How Beta Works
A beta coefficient can measure the volatility of an individual stock compared to the systematic risk of the entire market. In statistical terms, beta represents the slope of the line through a regression of data points. In finance, each of these data points represents an individual stock's returns against those of the market as a whole.
Beta effectively describes the activity of a security's returns as it responds to swings in the market. A security's beta is calculated by dividing the product of the covariance of the security's returns and the market's returns by the variance of the market's returns over a specified period.
cov (a,b)/var(b)
Generalized Black-Scholes-Merton on Variance Form [Loxx]Generalized Black-Scholes-Merton on Variance Form is an adaptation of the Black-Scholes-Merton Option Pricing Model including Numerical Greeks. The following information is an excerpt from Espen Gaarder Haug's book "Option Pricing Formulas". This version is to price Options using variance instead of volatility.
Black- Scholes- Merton on Variance Form
In some circumstances, it is useful to rewrite the BSM formula using variance as input instead of volatility, V = v^2:
c = S * e^((b - r) * T) * N(d1) - X * e^(-r * T) * N(d2)
p = X * e^(-r * T) * N(-d2) - S * e^((b - r) * T) * N(-d1)
where
d1 = (log(S / X) + (b + V^2 / 2) * T) / (V * T)^0.5
d2 = d1 - (V * T)^0.5
BSM on variance form clearly gives the same price as when written on volatility form. The variance form is used indirectly in terms of its partial derivatives in some stochastic variance models, as well as for hedging of variance swaps. The BSM on variance form moreover admits an interesting symmetry between put and call options as discussed by Adamchuk and Haug (2005) at www.wilmott.com .
c(S, X, T, r, b, V) = -c(-S, -X, -T, -r, -b, -V)
and
p(S, X, T, r, b, V) = -p(-S, -X, -T, -r, -b, -V)
It is possible to find several similar symmetries if we introduce imaginary numbers.
b = r ... gives the Black and Scholes (1973) stock option model.
b = r — q ... gives the Merton (1973) stock option model with continuous dividend yield q.
b = 0 ... gives the Black (1976) futures option model.
b = 0 and r = 0 ... gives the Asay (1982) margined futures option model.
b = r — rf ... gives the Garman and Kohlhagen (1983) currency option model.
Inputs
S = Stock price.
X = Strike price of option.
T = Time to expiration in years.
r = Risk-free rate
cc = Cost of Carry
V = Variance of the underlying asset price
cnd (x) = The cumulative normal distribution function
nd(x) = The standard normal density function
convertingToCCRate(r, cmp ) = Rate compounder
Numerical Greeks or Greeks by Finite Difference
Analytical Greeks are the standard approach to estimating Delta, Gamma etc... That is what we typically use when we can derive from closed form solutions. Normally, these are well-defined and available in text books. Previously, we relied on closed form solutions for the call or put formulae differentiated with respect to the Black Scholes parameters. When Greeks formulae are difficult to develop or tease out, we can alternatively employ numerical Greeks - sometimes referred to finite difference approximations. A key advantage of numerical Greeks relates to their estimation independent of deriving mathematical Greeks. This could be important when we examine American options where there may not technically exist an exact closed form solution that is straightforward to work with. (via VinegarHill FinanceLabs)
Things to know
Only works on the daily timeframe and for the current source price.
You can adjust the text size to fit the screen
Barndorff-Nielsen and Shephard Jump Statistic [Loxx]The following comments and descriptions are from from "Problems in the Application of Jump Detection Tests to Stock Price Data" by Michael William Schwert; Professor George Tauchen, Faculty Advisor.
This indicator applies several jump detection tests to intraday stock price data sampled at various frequencies. It finds that the choice of sampling frequency has an effect on both the amount of jumps detected by these tests, as well as the timing of those jumps. Furthermore, although these tests are designed to identify the same phenomenon, they find different amounts and timing of jumps when performed on the same data. These results suggest that these jump detection tests are probably identifying different types of jump behavior in stock price data, so they are not really substitutes for one another.
In recent years there has been a great deal of interest in studying jumps in asset price movements. Reasons why it is important to know when and how frequently jumps occur include risk management and the pricing and hedging of derivative contracts. Investors would benefit greatly from knowing the properties of jumps, since large instantaneous drops in asset prices result in large instantaneous losses. The effect of jumps on derivative pricing is equally significant, especially considering the important role derivatives play in modern financial markets. When asset price movements are continuous, investors can perfectly hedge derivative contracts such as options, but when jumps occur, they cause a change in the derivative price that is non-linear to the change in the price of the underlying asset. Thus, jumps introduce an unhedgeable risk to the holders of derivative contracts.
The ability to identify realized jumps in the financial markets could provide helpful information such as how frequently jumps occur, how large the jumps are, and whether they tend to occur in clusters. With this goal in mind, several authors have developed tests to determine whether or not an asset price movement is a statistically significant jump. These tests take advantage of the high-frequency intraday price data available today through electronic sources. Barndorff-Nielsen and Shephard (2004, 2006) use the difference between an estimate of variance and a jump-robust measure of variance to detect jumps over the course of a day. Approaching the problem differently, Jiang and Oomen (2007) exploit high order sample moments of returns to identify days that include jumps. Aїt-Sahalia and Jacod (2008) also exploit high order sample moments of returns to detect jumps by comparing price data sampled at two different frequencies. Lee and Mykland (2007) test for jumps at individual price observations by scaling returns by a local volatility measure. While these tests employ different strategies for detecting jumps, they are all designed to identify the same phenomenon.
For this indicator we are focused on the Barndorff-Nielsen and Shephard jump statistic.
Barndorff-Nielsen and Shephard (2004, 2006) developed a test that uses high-frequency price data to determine whether there is a jump over the course of a day. Their test compares two measures of variance: Realized Variance, which converges to the integrated variance plus a jump component as the time between observations approaches zero; and Bipower Variation, which converges to the integrated variance as the time between observations approaches zero, and is robust to jumps in the price path, an important fact for this application. The integrated variance of a price process is the integral of the square of the σ(t) term in (2.2.2), taken over the course of a day. Since prices cannot be observed continuously, one cannot calculate integrated variance exactly, and must estimate it instead.
For our purposes here, this is calculated as:
r = log(p /p )
This the geometric return from time ti-1 to time ti.
Then, Realized Variance and Bipower Variation are described by the following functions (see code for details)
realizedVariance(float src, int per)
and
bipowerVariance(float src, int per)
Huang and Tauchen (2005) also consider Relative Jump, a measure that approximates the percentage of total variance attributable to jumps:
RJ = (RV - BV) / RV
This statistic approximates the ratio of the sum of squared jumps to the total variance and is useful because it scales out long-term trends in volatility so one can compare the relative contribution of jumps to the variance of two price series with different volatilities.
To develop a statistical test to determine whether there is a significant difference between RV and BV, one needs an estimate of integrated quarticity. Andersen, Bollerslev, and Diebold (2004) recommend using a jump-robust realized Tri-Power Quarticity, I've included commentary in code to better explain how this indicator is collocated. See code for details.
How to use this indicator
When the bars turn gray, it's an indication that a jump has occurred in the market. It serves a warning that price jumped. I've included a percent point function (or inverse cumulative distribution function) to cutoff Z-score values depicted by histogram values. The top line at 3 is the empirical maximum Z-score value a serves merely as a point of reference. The Red line is the cutoff line calculated using PPF. When the histogram is green, no jumps have been detected. This indicator also includes alerts, signals, and bar coloring. I've also expanded the possible source types using my own Expanded Source Types library so you can test different log return methods as inputs. It is recommended to use window sizes of 7, 16, 78, 110, 156, and 270 returns for sampling intervals of 1 week, 1 day, 1 hour, 30 minutes, 15 minutes, and 5 minutes, respectively.
If you'ed like to better understand PPF, see here: Distributions in python
Included:
Bar coloring
Signals
Alerts
Loxx's Expanded Source Types
Variance (Welford) [Loxx]The standard deviation is a measure of how much a dataset differs from its mean; it tells us how dispersed the data are. A dataset that’s pretty much clumped around a single point would have a small standard deviation, while a dataset that’s all over the map would have a large standard deviation. You can. use this calculation for other indicators.
Given a sample the standard deviation is defined as the square root of the variance
Here you can find a Welford’s method for computing (single pass method) that avoids errors in some cases (if the variance is small compared to the square of the mean, and computing the difference leads catastrophic cancellation where significant leading digits are eliminated and the result has a large relative error)
Read more here: jonisalonen.com
Incliuded
Loxx's Expanded Source Types
Drift Study (Inspired by Monte Carlo Simulations with BM) [KL]Inspired by the Brownian Motion ("BM") model that could be applied to conducting Monte Carlo Simulations, this indicator plots out the Drift factor contributing to BM.
Interpretation : If the Drift value is positive, then prices are possibly moving in an uptrend. Vice versa for negative drifts.
GME REGIONAL PRICES OVERLAYGME Regional Prices Overlay (VWAP)
1. Select a chart 24-hour ticker like FX_IDC:USDEUR
2. Select a timescale (5 min, 15 min, ...)
3. Monitor the regional price variance
Exchanges included: NYSE, XETR, BMV, FWB, SWB, BITTREX, FTX, LSE, CAPITALCOM (CFD)
Currency conversion: FX_IDC
[BCT] Can BTC be predicted or is it purely random?Variance Ratio**This indicator can be applied to the ticker of your choice (not just BTC)**
Markets are said to be "efficient". An efficient market is by definition unpredictable - no matter the amount of ML, computation, or indicators thrown at it. In particular, in an efficient market, TA will not be of help.
An illustration of efficient markets is the WSJ's longstanding monkey vs. human contest:Blindfolded Monkey Beats Humans With Stock Picks, granted there are several flaws to it.
BTC is a relatively new market. New markets are typically highly inefficient (easier to make money) and become more and more efficient over time (harder to make money). How much more efficient is BTC becoming?
We apply the Variance Ratio method and apply it to BTC.
BACKGROUND ON THE VARIANCE RATIO METHOD
Based on 1988 MacKinlay's seminal paper "Stock Market Prices do not Follow a Random Walk", the idea is to exploit a phenomenon called "variance scaling".
For those keen on looking into the math, the short version of it is under the assumption of iid (random walk) we have the following:
H0: Var(Sum(returns over K bars))=Sum(Var(returns over 1 bar))=k*Var(return over 1 bar)
We look to reject or not H0 depending on the observations.
In this script, we compare the variance of the (log) returns for the chart selected between:
(1) The (average) variance over k bars (call this Vk)
(2) The (average) variance over 1 bar (call this V1)
H0 simply says that Vk=k*V1 if the stock follows a random walk.
We compute the Variance Ratio VR(k)=Variance(returns over k bar)/(Sum(Var(returns over 1 bar)))-1
We then compute the associated Z-score which we chart out for a configurable k number of bars.
HOW TO INTERPRET THE CHART
The line drawn is the Z-Score for VR(k). It represents the number of standard deviations of VR(k) from 0 - the further out, the less random.
- If the line is close / hovers around 0, the ticker appears to follow a random walk (i.e. may not be predictable)
- If the line is consistently > 2 or <-2, the ticker likely does not follow a random walk (i.e. may have predictable features)
- If the line is positive, it means that the Variance on the k bars is larger than the variance on 1 bar (more variance on longer timeframes)
- If the line is negative, it means that the Variance on the k bars is smaller than the variance on 1 bar (more variance on smaller timeframes)
USE CASES
- Identify timeframes where you won't be able to make money
- Identify whether a stock cannot be predicted (forget about TA, indicators etc. -- a random walk is not predictable)
- Identify whether a stock is becoming less and less predictable (Z-score amplitude will decrease over time)
FEATURES
- select the number of K bar to compare vs. 1 bar (default = 16) - ideally a power of 2 but any other number will work. The chart is based off this selection
- select the lookback period for the analysis (500 bars by default)
- select the source to analyze (default = close, but you may select other inputs to calculate the returns from)
- results form the statistical tests on different K's in the table on the right/bottom side of the chart (H0 rejected = not random walk; H0 not rejected = it essentially looks rather random and we can't conclude that it's not a random walk)
COMMENTARY ON BTC
- It appears BTC's absolute value of the ZScore on the Variance Ratio is declining year after year - corroborating an increasingly efficient market as new participants join.
- However, we can still detect a fair amount of potential inefficiency using this simple test.
As usual, this is not investment advice. DYOR.
With love,
🐵BCT🐵
CV_VWAP_GMECoefficient of variance GME ‰
Gray area: Regional price variance of GME in per milles
Light gray thick line: NYSE:GME deviation from global mean
1. Select a chart 24-hour ticker like FX_IDC:USDEUR
2. Select a timescale (5 min, 15 min, ...)
3. Monitor the regional price variance
Exchanges included: NYSE, XETR, BMV, FWB, SWB, BITTREX, FTX
Currency conversion: Forex
Adapted from Detecting the great short squeeze on Volkswagen, Godfrey, K. (2016, February 18).
Realized Variables for Options ComparisonThese variables can be used in comparison with the implied volatility of options.
Variables:
Realized Volatility
mathematical notation lowercase 'sigma'
Realized Variance
mathematical notation lowercase 'sigma' squared
Realized Beta
mathematical notation lowercase 'beta'
Timeframes:
Yearly = 250 or 365
Quarterly = 50 or 90
Monthly = 20 or 30
Important Note:
Options Contract Expiry = barmerge.lookahead_on
"Merge strategy for the requested data position. Requested barset is merged with current barset in the order of sorting bars by their opening time. This merge strategy can lead to undesirable effect of getting data from "future" on calculation on history. This is unacceptable in backtesting strategies, but can be useful in indicators."
[ All other timeframes barmerge.lookahead is disabled.
Risk Metrics: beta 'β', correl 'ρxy', stdev 'σ', variance 'σ²'Portfolio Risk Metrics (Part I):
beta 'β'
The beta coefficient can be interpreted as follows:
β =1 exactly as volatile as the market
β >1 more volatile than the market
β <1>0 less volatile than the market
β =0 uncorrelated to the market
β <0 negatively correlated to the market
excerpt from the Corporate Finance Institute
correlation coefficient 'ρxy'
The correlation coefficient is a value that indicates the strength of the relationship between variables.
The coefficient can take any values from -1 to 1. The interpretations of the values are:
-1: Perfect negative correlation. The variables tend to move in opposite directions
(i.e., when one variable increases, the other variable decreases).
0: No correlation. The variables do not have a relationship with each other.
1: Perfect positive correlation. The variables tend to move in the same direction
(i.e., when one variable increases, the other variable also increases).
excerpt from the Corporate Finance Institute
standard deviation 'σ'
68% of returns will fall within 1 standard deviation of the arithmetic mean
95% of returns will fall within 2 standard deviations of the arithmetic mean
99% of returns will fall within 3 standard deviations of the arithmetic mean
excerpt from Corporate Finance Institute
variance 'σ²'
In investing, variance is used to compare the relative performance of each asset in a portfolio.
Because the results can be difficult to analyze, standard deviation is often used instead of variance.
In either case, the goal for the investor is to improve asset allocation.
excerpt from Investopedia
Garch (1,1) ModelThe Garch (General Autoregressive Conditional Heteroskedasticity) model is a non-linear time series model that uses past data to forecast future variance.
The Garch (1,1) formula is:
Garch = (gamma * Long Run Variance) + (alpha * Squared Lagged Returns) + (beta * Lagged Variance)
The gamma, alpha, and beta values are all weights used in the Garch calculations. According to RiskMetrics by JP Morgan, the optimal beta weight is 0.94, but this figure is highly disputed in the academic realm. The biggest problem academics and economists have with the 0.94 figure is that JP Morgan used monthly data to come to this number, meaning it does not take other time frames into account. Because of the disputed nature of what beta should be, this script will automatically calculate the beta weight for you in real time, taking into account the time frame you're using and realized variance, by using the Minimum Sum of Squared Errors Method.
The gamma and alpha weights are also calculated for you.
Even though the Garch formula provides today's projected variance, today's projected deviation is also calculated. This is done by taking the square root of Garch.
Additionally, if you want to project the variance or deviation for as many days forward as you want, you can.
In order to project the variance and deviation beyond just today, these equations are used:
Projected Variance = Long Run Variance + (alpha + beta)^Days Forward * (Garch - Long Run Variance)
Projected Deviation = sqrt(Projected Variance)
How to use this model:
1st. Decide the type of data you want: Projected Variance in % or Projected Deviation in %.
2nd. Decide how many days you want projected forward. If you input 0, you will get projections for today. If you input 1, you will get projections for tomorrow, and etc.
That's it. If you have any further questions, I left detailed comments in the code explaining each step, as best as I could.
Minimum Variance SMAReturn the value of a simple moving average with a period within the range min to max such that the variance of the same period is the smallest available.
Since the smallest variance is often the one with the smallest period, a penalty setting is introduced, and allows the indicator to return moving averages values with higher periods more often, with higher penalty values returning moving averages values with higher periods.
Because variances with smaller periods are more reactive than ones with higher periods, it is common for the indicator to return the value of an SMA of a higher period during more volatile market, this can be seen on the image below:
here variances from period 10 to 15 are plotted, a blueish color represents a higher period, note how they are the smallest ones when fluctuations are more volatile.
Indicator with min = 50, max = 200 and penalty = 0.5
In blue the indicator with penalty = 0, in red with penalty = 1, with both min = 50 and max = 200.
On The Script
The script minimize Var(i)/p with i ∈ (min,max) and p = i^penalty , this is done by computing the variance for each period i and keeping the smallest one currently in the loop, if we get a variance value smaller than the previously one found we calculate the value of an SMA with period i , as such the script deal with brute force optimization.
For our use case it is not possible to use the built-in sma and variance functions within a loop, as such we use cumulative forms for both functions.
Functions Allowing Series As Length - PineCoders FAQ█ WARNING
Improvements to the following Pine built-ins have deprecated the vast majority of this publication's functions, as the built-ins now accept "series int" `length` arguments:
ta.wma()
ta.linreg()
ta.variance()
ta.stdev()
ta.correlation()
NOTE
For an EMA function that allows a "series int" argument for `length`, please see `ema2()` in the ta library by TradingView .
█ ORIGINAL DESCRIPTION
Pinescript requires many of its built-in functions to use a simple int as their period length, which entails the period length cannot vary during the script's execution. These functions allow using a series int or series float for their period length, which means it can vary on each bar.
The functions shared in this script include:
Rolling sum: Sum(src,p)
Simple moving average: Sma(src,p)
Rolling variance: Variance(src,p)
Rolling standard deviation: Stdev(src,p)
Rolling covariance: Covariance(x,y,p)
Rolling correlation: Correlation(x,y,p)
If p is a float then it is rounded to the nearest int .
How to Use the Script
Most of the functions in the script are dependent on the Sma function. The Correlation function uses the Covariance and Stdev functions. Be sure you include all the required functions in your script.
Make sure the series you use as the length argument is greater than 0, else the functions will return na . When using a series as length argument, the following error might appear:
Pine cannot determine the referencing length of a series. Try using max_bars_back in the study or strategy function.
This can be frequent if you use barssince(condition) where condition is a relatively rare event. You can fix it by including max_bars_back=5000 in your study declaration statement as follows:
study("Title",overlay=true,max_bars_back=5000)
Example
The chart shows the Sma , Stdev , Covariance and Correlation functions. The Sma uses the closing price as input and bars as period length where:
bars = barssince(change(security(syminfo.tickerid,"D",close ,lookahead=true)))
The Stdev uses the closing price as input and bars + 9 as period length. The Covariance and Correlation use the closing price as x and bar_index as y , with bars + 9 as period length.
Look first. Then leap.
TBCRI - Trend Bar Color Reversal IndicatorAn idea I had today morning so I had to write. It seems to detect trends well. It has three phases like a semaphor, painting the chart bars of green, yellow or red.
=== Bar Color Meaning ===
Green: uptrend
Yellow: don't care
Red: downtrend
I think it can be useful!
Thanks!
Close Enough FunctionProvides a user input to determine the amont of "slop" or variance between a target and given price point.
Variance of OBVIDEA is to easily spot the length of a calm periods based on OBV.
Some says that after a longer OBV-calm (but not supercalm) period up or down rallies are somewhat more likely)
METHOD: variance of OBV
ADVISE: cannot be used on its own, just with others (RSI, CCI, Coppock, MACD etc.)
Period shall be adjusted to the market.
PERSONAL: I also use it to evaluate how long an uptrend/downtrend is "normal" and when it is a "changer".
Also to see if a market is too flat. (No changes in flat periods is also not a good sign for me)
Also to evaluate magnitude of bursts.
Have fun, use stops, avoid FOMO and comments welcome!
Function CovarianceCovariance Function as described here:
www.investopedia.com
can be used for example to calculate Beta: