Kalman For Loop [BackQuant]Kalman For Loop
Introducing BackQuant's Kalman For Loop (Kalman FL) — a highly adaptive trading indicator that uses a Kalman filter to smooth price data and generate actionable long and short signals. This advanced indicator is designed to help traders identify trends, filter out market noise, and optimize their entry and exit points with precision. Let’s explore how this indicator works, its key features, and how it can enhance your trading strategies.
Core Concept: Kalman Filter
The Kalman Filter is a mathematical algorithm used to estimate the state of a system by filtering noisy data. It is widely used in areas such as control systems, signal processing, and time-series analysis. In the context of trading, a Kalman filter can be applied to price data to smooth out short-term fluctuations, providing a clearer view of the underlying trend.
Unlike moving averages, which use fixed weights to smooth data, the Kalman Filter adjusts its estimate dynamically based on the relationship between the process noise and the measurement noise. This makes the filter more adaptive to changing market conditions, providing more accurate trend detection without the lag associated with traditional smoothing techniques.
Please see the original Kalman Price Filter
In this script, the Kalman For Loop applies the Kalman filter to the price source (default set to the closing price) to generate a smoothed price series, which is then used to calculate signals.
Adaptive Smoothing with Process and Measurement Noise
Two key parameters govern the behavior of the Kalman filter:
Process Noise: This controls the extent to which the model allows for uncertainty in price changes. A lower process noise value will make the filter smoother but slower to react to price changes, while a higher value makes it more sensitive to recent price fluctuations.
Measurement Noise: This represents the uncertainty or "noise" in the observed price data. A higher measurement noise value gives the filter more leeway to ignore short-term fluctuations, focusing on the broader trend. Lowering the measurement noise makes the filter more responsive to minor changes in price.
These settings allow traders to fine-tune the Kalman filter’s sensitivity, adjusting it to match their preferred trading style or market conditions.
For-Loop Scoring Mechanism
The Kalman FL further enhances the effectiveness of the Kalman filter by using a for-loop scoring system. This mechanism evaluates the smoothed price over a range of periods (defined by the Calculation Start and Calculation End inputs), assigning a score based on whether the current filtered price is higher or lower than previous values.
Long Signals: A long signal is generated when the for-loop score surpasses the Long Threshold (default set at 20), indicating a strong upward trend. This helps traders identify potential buying opportunities.
Short Signals: A short signal is triggered when the score crosses below the Short Threshold (default set at -10), signaling a potential downtrend or selling opportunity.
These signals are plotted on the chart, giving traders a clear visual indication of when to enter long or short positions.
Customization and Visualization Options
The Kalman For Loop comes with a range of customization options to give traders full control over how the indicator operates and is displayed on the chart:
Kalman Price Source: Choose the price data used for the Kalman filter (default is the closing price), allowing you to apply the filter to other price points like open, high, or low.
Filter Order: Set the order of the Kalman filter (default is 5), controlling how far back the filter looks in its calculations.
Process and Measurement Noise: Fine-tune the sensitivity of the Kalman filter by adjusting these noise parameters.
Signal Line Width and Colors: Customize the appearance of the signal line and the colors used to indicate long and short conditions.
Threshold Lines: Toggle the display of the long and short threshold lines on the chart for better visual clarity.
The indicator also includes the option to color the candlesticks based on the current trend direction, allowing traders to quickly identify changes in market sentiment. In addition, a background color feature further highlights the overall trend by shading the background in green for long signals and red for short signals.
Trading Applications
The Kalman For Loop is a versatile tool that can be adapted to a variety of trading strategies and markets. Some of the primary use cases include:
Trend Following: The adaptive nature of the Kalman filter helps traders identify the start of new trends with greater precision. The for-loop scoring system quantifies the strength of the trend, making it easier to stay in trades for longer when the trend remains strong.
Mean Reversion: For traders looking to capitalize on short-term reversals, the Kalman filter's ability to smooth price data makes it easier to spot when price has deviated too far from its expected path, potentially signaling a reversal.
Noise Reduction: The Kalman filter excels at filtering out short-term price noise, allowing traders to focus on the broader market movements without being distracted by minor fluctuations.
Risk Management: By providing clear long and short signals based on filtered price data, the Kalman FL helps traders manage risk by entering positions only when the trend is well-defined, reducing the chances of false signals.
Alerts and Automation
To further assist traders, the Kalman For Loop includes built-in alert conditions that notify you when a long or short signal is generated. These alerts can be configured to trigger notifications, helping you stay on top of market movements without constantly monitoring the chart.
Final Thoughts
The Kalman For Loop is a powerful and adaptive trading indicator that combines the precision of the Kalman filter with a for-loop scoring mechanism to generate reliable long and short signals. Whether you’re a trend follower or a reversal trader, this indicator offers the flexibility and accuracy needed to navigate complex markets with confidence.
As always, it’s important to backtest the indicator and adjust the settings to fit your trading style and market conditions. No indicator is perfect, and the Kalman FL should be used alongside other tools and sound risk management practices for the best results.
Kalman
Kalman Hull RSI [BackQuant]Kalman Hull RSI
At its core, this indicator uses a Kalman filter of price, put inside of a hull moving average function (replacing the weighted moving averages) and then using that as a price source for the the RSI, very similar to the Kalman Hull Supertrend just processing price for a different indicator.
This also allows it to make it more adaptive to price and also sensitive to recent price action. This indicator is also mainly built for trend-following systems
PLEASE Read the following, knowing what an indicator does at its core before adding it into a system is pivotal. The core concepts can allow you to include it in a logical and sound manner.
1. What is a Kalman Filter
The Kalman Filter is an algorithm renowned for its efficiency in estimating the states of a linear dynamic system amidst noisy data. It excels in real-time data processing, making it indispensable in fields requiring precise and adaptive filtering, such as aerospace, robotics, and financial market analysis. By leveraging its predictive capabilities, traders can significantly enhance their market analysis, particularly in estimating price movements more accurately.
If you would like this on its own, with a more in-depth description please see our Kalman Price Filter.
OR our Kalman Hull Supertrend
2. Hull Moving Average (HMA) and Its Core Calculation
The Hull Moving Average (HMA) improves on traditional moving averages by combining the Weighted Moving Average's (WMA) smoothness and reduced lag. Its core calculation involves taking the WMA of the data set and doubling it, then subtracting the WMA of the full period, followed by applying another WMA on the result over the square root of the period's length. This methodology yields a smoother and more responsive moving average, particularly useful for identifying market trends more rapidly.
3. Combining Kalman Filter with HMA
The innovative combination of the Kalman Filter with the Hull Moving Average (KHMA) offers a unique approach to smoothing price data. By applying the Kalman Filter to the price source before its incorporation into the HMA formula, we enhance the adaptiveness and responsiveness of the moving average. This adaptive smoothing method reduces noise more effectively and adjusts more swiftly to price changes, providing traders with clearer signals for market entries or exits.
The calculation is like so:
KHMA(_src, _length) =>
f_kalman(2 * f_kalman(_src, _length / 2) - f_kalman(_src, _length), math.round(math.sqrt(_length)))
Use Case
The Kalman Hull RSI is particularly suited for traders who require a highly adaptive indicator that can respond to rapid market changes without the excessive noise associated with typical RSI calculations. It can be effectively used in markets with high volatility where traditional indicators might lag or produce misleading signals.
Application in a Trading System
The Kalman Hull RSI is versatile in application, suitable for:
Trend Identification: Quickly identify potential reversals or confirmations of existing trends.
Overbought/Oversold Conditions: Utilize the dynamic RSI thresholds to pinpoint potential entry and exit points, adapting to current market conditions.
Risk Management: Enhance trading strategies by integrating a more reliable measure of momentum, which can lead to improved stop-loss placements and exit strategies.
Core Calculations and Benefits
Dynamic State Estimation: By applying the Kalman Filter, the indicator continually adjusts its calculations based on incoming price data, providing a real-time, smoothed response to price movements.
Reduced Lag: The integration with HMA significantly reduces lag, offering quicker responses to price changes than traditional moving averages or RSI alone.
Increased Accuracy: The dual filtering effect minimizes the impact of price spikes and noise, leading to more accurate signaling for trades.
Thus following all of the key points here are some sample backtests on the 1D Chart
Disclaimer: Backtests are based off past results, and are not indicative of the future.
INDEX:BTCUSD
INDEX:ETHUSD
BINANCE:SOLUSD
Kalman Volume Filter [ChartPrime]The "Kalman Volume Filter" , aims to provide insights into market volume dynamics by filtering out noise and identifying potential overbought or oversold conditions. Let's break down its components and functionality:
Settings:
Users can adjust various parameters to customize the indicator according to their preferences:
Volume Length: Defines the length of the volume period used in calculations.
Stabilization Coefficient (k): Determines the level of noise reduction in the signals.
Signal Line Length: Sets the length of the signal line used for identifying trends.
Overbought & Oversold Zone Level: Specifies the threshold levels for identifying overbought and oversold conditions.
Source: Allows users to select the price source for volume calculations.
Volume Zone Oscillator (VZO):
Calculates a volume-based oscillator indicating the direction and intensity of volume movements.
Utilizes a volume direction measurement over a specified period to compute the oscillator value.
Normalizes the oscillator value to improve comparability across different securities or timeframes.
// VOLUME ZONE OSCILLATOR
VZO(get_src, length) =>
Volume_Direction = get_src > get_src ? volume : -volume
VZO_volume = ta.hma(Volume_Direction, length)
Total_volume = ta.hma(volume, length)
VZO = VZO_volume / (Total_volume)
VZO := (VZO - 0) / ta.stdev(VZO, 200)
VZO
Kalman Filter:
Applies a Kalman filter to smooth out the VZO values and reduce noise.
Utilizes a stabilization coefficient (k) to control the degree of smoothing.
Generates a filtered output representing the underlying volume trend.
// KALMAN FILTER
series float M_n = 0.0 // - the resulting value of the current calculation
series float A_n = VZO // - the initial value of the current measurement
series float M_n_1 = nz(M_n ) // - the resulting value of the previous calculation
float k = input.float(0.06) // - stabilization coefficient
// Kalman Filter Formula
kalm(k)=>
k * A_n + (1 - k) * M_n_1
Volume Visualization:
Displays the volume histogram, with color intensity indicating the strength of volume movements.
Adjusts bar colors based on volume bursts to highlight significant changes in volume.
Overbought and Oversold Zones:
Marks overbought and oversold levels on the chart to assist in identifying potential reversal points.
Plotting:
Plots the Kalman Volume Filter line and a signal line for visual analysis.
Utilizes different colors and fills to distinguish between rising and falling trends.
Highlights specific events such as local buy or sell signals, as well as overbought or oversold conditions.
This indicator provides traders with a comprehensive view of volume dynamics, trend direction, and potential market turning points, aiding in informed decision-making during trading activities.
Kalman Hull Supertrend [BackQuant]Kalman Hull Supertrend
At its core, this indicator uses a Kalman filter of price, put inside of a hull moving average function (replacing the weighted moving averages) and then using that as a price source for the supertrend instead of the normal hl2 (high+low/2).
Therefore, making it more adaptive to price and also sensitive to recent price action.
PLEASE Read the following, knowing what an indicator does at its core before adding it into a system is pivotal. The core concepts can allow you to include it in a logical and sound manner.
1. What is a Kalman Filter
The Kalman Filter is an algorithm renowned for its efficiency in estimating the states of a linear dynamic system amidst noisy data. It excels in real-time data processing, making it indispensable in fields requiring precise and adaptive filtering, such as aerospace, robotics, and financial market analysis. By leveraging its predictive capabilities, traders can significantly enhance their market analysis, particularly in estimating price movements more accurately.
If you would like this on its own, with a more in-depth description please see our Kalman Price Filter.
2. Hull Moving Average (HMA) and Its Core Calculation
The Hull Moving Average (HMA) improves on traditional moving averages by combining the Weighted Moving Average's (WMA) smoothness and reduced lag. Its core calculation involves taking the WMA of the data set and doubling it, then subtracting the WMA of the full period, followed by applying another WMA on the result over the square root of the period's length. This methodology yields a smoother and more responsive moving average, particularly useful for identifying market trends more rapidly.
3. Combining Kalman Filter with HMA
The innovative combination of the Kalman Filter with the Hull Moving Average (KHMA) offers a unique approach to smoothing price data. By applying the Kalman Filter to the price source before its incorporation into the HMA formula, we enhance the adaptiveness and responsiveness of the moving average. This adaptive smoothing method reduces noise more effectively and adjusts more swiftly to price changes, providing traders with clearer signals for market entries or exits.
The calculation is like so:
KHMA(_src, _length) =>
f_kalman(2 * f_kalman(_src, _length / 2) - f_kalman(_src, _length), math.round(math.sqrt(_length)))
4. Integration with Supertrend
Incorporating this adaptive price smoothing technique into the Supertrend indicator further enhances its efficiency. The Supertrend, known for its proficiency in identifying the prevailing market trend and providing clear buy or sell signals, becomes even more powerful with an adaptive price source. This integration allows the Supertrend to adjust more dynamically to market changes, offering traders more accurate and timely trading signals.
5. Application in a Trading System
In a trading system, the Kalman Hull Supertrend indicator can serve as a critical component for identifying market trends and generating signals for potential entry and exit points. Its adaptiveness and sensitivity to price changes make it particularly useful for traders looking to minimize lag in signal generation and improve the accuracy of their market trend analysis. Whether used as a standalone tool or in conjunction with other indicators, its dynamic nature can significantly enhance trading strategies.
6. Core Calculations and Benefits
The core of this indicator lies in its sophisticated filtering and averaging techniques, starting with the Kalman Filter's predictive adjustments, followed by the adaptive smoothing of the Hull Moving Average, and culminating in the trend-detecting capabilities of the Supertrend. This multi-layered approach not only reduces market noise but also adapts to market volatility more effectively. Benefits include improved signal accuracy, reduced lag, and the ability to discern trend changes more promptly, offering traders a competitive edge.
Thus following all of the key points here are some sample backtests on the 1D Chart
Disclaimer: Backtests are based off past results, and are not indicative of the future.
INDEX:BTCUSD
INDEX:ETHUSD
BINANCE:SOLUSD
Kalman Filtered RSI Oscillator [BackQuant]Kalman Filtered RSI Oscillator
The Kalman Filtered RSI Oscillator is BackQuants new free indicator designed for traders seeking an advanced, empirical approach to trend detection and momentum analysis. By integrating the robustness of a Kalman filter with the adaptability of the Relative Strength Index (RSI), this tool offers a sophisticated method to capture market dynamics. This indicator is crafted to provide a clearer, more responsive insight into price trends and momentum shifts, enabling traders to make informed decisions in fast-moving markets.
Core Principles
Kalman Filter Dynamics:
At its core, the Kalman Filtered RSI Oscillator leverages the Kalman filter, renowned for its efficiency in predicting the state of linear dynamic systems amidst uncertainties. By applying it to the RSI calculation, the tool adeptly filters out market noise, offering a smoothed price source that forms the basis for more accurate momentum analysis. The inclusion of customizable parameters like process noise, measurement noise, and filter order allows traders to fine-tune the filter’s sensitivity to market changes, making it a versatile tool for various trading environments.
RSI Adaptation:
The RSI is a widely used momentum oscillator that measures the speed and change of price movements. By integrating the RSI with the Kalman filter, the oscillator not only identifies the prevailing trend but also provides a smoothed representation of momentum. This synergy enhances the indicator's ability to signal potential reversals and trend continuations with a higher degree of reliability.
Advanced Smoothing Techniques:
The indicator further offers an optional smoothing feature for the RSI, employing a selection of moving averages (HMA, THMA, EHMA, SMA, EMA, WMA, TEMA, VWMA) for traders seeking to reduce volatility and refine signal clarity. This advanced smoothing mechanism is pivotal for traders looking to mitigate the effects of short-term price fluctuations on the RSI's accuracy.
Empirical Significance:
Empirically, the Kalman Filtered RSI Oscillator stands out for its dynamic adjustment to market conditions. Unlike static indicators, the Kalman filter continuously updates its estimates based on incoming price data, making it inherently more responsive to new market information. This dynamic adaptation, combined with the RSI's momentum analysis, offers a powerful approach to understanding market trends and momentum with a depth not available in traditional indicators.
Trend Identification and Momentum Analysis:
Traders can use the Kalman Filtered RSI Oscillator to identify strong trends and momentum shifts. The color-coded RSI columns provide immediate visual cues on the market's direction and strength, aiding in quick decision-making.
Optimal for Various Market Conditions:
The flexibility in tuning the Kalman filter parameters makes this indicator suitable for a wide range of assets and market conditions, from volatile to stable markets. Traders can adjust the settings based on empirical testing to find the optimal configuration for their trading strategy.
Complementary to Other Analytical Tools:
While powerful on its own, the Kalman Filtered RSI Oscillator is best used in conjunction with other analytical tools and indicators. Combining it with volume analysis, price action patterns, or other trend-following indicators can provide a comprehensive view of the market, allowing for more nuanced and informed trading decisions.
The Kalman Filtered RSI Oscillator is a groundbreaking tool that marries empirical precision with advanced trend analysis techniques. Its innovative use of the Kalman filter to enhance the RSI's performance offers traders an unparalleled ability to navigate the complexities of modern financial markets. Whether you're a novice looking to refine your trading approach or a seasoned professional seeking advanced analytical tools, the Kalman Filtered RSI Oscillator represents a significant step forward in technical analysis capabilities.
Thus following all of the key points here are some sample backtests on the 1D Chart
Disclaimer: Backtests are based off past results, and are not indicative of the future.
INDEX:BTCUSD
INDEX:ETHUSD
BINANCE:SOLUSD
Kalman Price Filter [BackQuant]Kalman Price Filter
The Kalman Filter, named after Rudolf E. Kálmán, is a algorithm used for estimating the state of a linear dynamic system from a series of noisy measurements. Originally developed for aerospace applications in the early 1960s, such as guiding Apollo spacecraft to the moon, it has since been applied across numerous fields including robotics, economics, and, notably, financial markets. Its ability to efficiently process noisy data in real-time and adapt to new measurements has made it a valuable tool in these areas.
Use Cases in Financial Markets
1. Trend Identification:
The Kalman Filter can smooth out market price data, helping to identify the underlying trend amidst the noise. This is particularly useful in algorithmic trading, where identifying the direction and strength of a trend can inform trade entry and exit decisions.
2. Market Prediction:
While no filter can predict the future with certainty, the Kalman Filter can be used to forecast short-term market movements based on current and historical data. It does this by estimating the current state of the market (e.g., the "true" price) and projecting it forward under certain model assumptions.
3. Risk Management:
The Kalman Filter's ability to estimate the volatility (or noise) of the market can be used for risk management. By dynamically adjusting to changes in market conditions, it can help traders adjust their position sizes and stop-loss orders to better manage risk.
4. Pair Trading and Arbitrage:
In pair trading, where the goal is to capitalize on the price difference between two correlated securities, the Kalman Filter can be used to estimate the spread between the pair and identify when the spread deviates significantly from its historical average, indicating a trading opportunity.
5. Optimal Asset Allocation:
The filter can also be applied in portfolio management to dynamically adjust the weights of different assets in a portfolio based on their estimated risks and returns, optimizing the portfolio's performance over time.
Advantages in Financial Applications
Adaptability: The Kalman Filter continuously updates its estimates with each new data point, making it well-suited to markets that are constantly changing.
Efficiency: It processes data and updates estimates in real-time, which is crucial for high-frequency trading strategies.
Handling Noise: Its ability to distinguish between the signal (e.g., the true price trend) and noise (e.g., random fluctuations) is particularly valuable in financial markets, where price data can be highly volatile.
Challenges and Considerations
Model Assumptions: The effectiveness of the Kalman Filter in financial applications depends on the accuracy of the model used to describe market dynamics. Financial markets are complex and influenced by numerous factors, making model selection critical.
Parameter Sensitivity: The filter's performance can be sensitive to the choice of parameters, such as the process and measurement noise values. These need to be carefully selected and potentially adjusted over time.
Despite these challenges, the Kalman Filter remains a potent tool in the quantitative trader's arsenal, offering a sophisticated method to extract useful information from noisy financial data. Its use in trading strategies should, however, be complemented with sound risk management practices and an awareness of the limitations inherent in any model-based approach to trading.
Kalman Filtered ROC & Stochastic with MA SmoothingThe "Smooth ROC & Stochastic with Kalman Filter" indicator is a trend following tool designed to identify trends in the price movement. It combines the Rate of Change (ROC) and Stochastic indicators into a single oscillator, the combination of ROC and Stochastic indicators aims to offer complementary information: ROC measures the speed of price change, while Stochastic identifies overbought and oversold conditions, allowing for a more robust assessment of market trends and potential reversals. The indicator plots green "B" labels to indicate buy signals and blue "S" labels to represent sell signals. Additionally, it displays a white line that reflects the overall trend for buy signals and a blue line for sell signals. The aim of the indicator is to incorporate Kalman and Moving Average (MA) smoothing techniques to reduce noise and enhance the clarity of the signals.
Rationale for using Kalman Filter:
The Kalman Filter is chosen as a smoothing tool in the indicator because it effectively reduces noise and fluctuations. The Kalman Filter is a mathematical algorithm used for estimating and predicting the state of a system based on noisy and incomplete measurements. It combines information from previous states and current measurements to generate an optimal estimate of the true state, while simultaneously minimizing the effects of noise and uncertainty. In the context of the indicator, the Kalman Filter is applied to smooth the input data, which is the source for the Rate of Change (ROC) calculation. By considering the previous smoothed state and the difference between the current measurement and the predicted value, the Kalman Filter dynamically adjusts its estimation to reduce the impact of outliers.
Calculation:
The indicator utilizes a combination of the ROC and the Stochastic indicator. The ROC is smoothed using a Kalman Filter (credit to © Loxx: ), which helps eliminate unwanted fluctuations and improve the signal quality. The Stochastic indicator is calculated with customizable parameters for %K length, %K smoothing, and %D smoothing. The smoothed ROC and Stochastic values are then averaged using the formula ((roc + d) / 2) to create the blended oscillator. MA smoothing is applied to the combined oscillator aiming to further reduce fluctuations and enhance trend visibility. Traders are free to choose their own preferred MA type from 'EMA', 'DEMA', 'TEMA', 'WMA', 'VWMA', 'SMA', 'SMMA', 'HMA', 'LSMA', and 'PEMA' (credit to: © traderharikrishna for this code: ).
Application:
The indicator's buy signals (represented by green "B" labels) indicate potential entry points for buying assets, suggesting a bullish trend. The white line visually represents the trend, helping traders identify and follow the upward momentum. Conversely, the sell signals (blue "S" labels) highlight possible exit points or opportunities for short selling, indicating a bearish trend. The blue line illustrates the bearish movement, aiding in the identification of downward momentum.
The "Smoothed ROC & Stochastic" indicator offers traders a comprehensive view of market trends by combining two powerful oscillators. By incorporating the ROC and Stochastic indicators into a single oscillator, it provides a more holistic perspective on the market's momentum. The use of a Kalman Filter for smoothing helps reduce noise and enhance the accuracy of the signals. Additionally, the indicator allows customization of the smoothing technique through various moving average types. Traders can also utilize the overbought and oversold zones for additional analysis, providing insights into potential market reversals or extreme price conditions. Please note that future performance of any trading strategy is fundamentally unknowable, and past results do not guarantee future performance.
Kalman RSIThis is a simplified version of Kalman RSI by onegreencandle.
Simplifications:
It shows the indicator for a single configurable length with a default of 14.
It does not color by region.
It allows selecting the source, with a default of close . The version by onegreencandle uses ohlc4 instead. Note that both versions also use high and low .
It uses the newer version (5) of Pine Script.
It sets bands at 85 and 15.
Local Model Kalman Market ModeIntroduction
Heyo guys, I made a new (repainting) indicator called Local Model Kalman Market Mode.
I created it, because I wanted a reliable market mode filter for a potential mean-reversion strategy (e. g. BB Scalping).
On the screenshot you can see an example of how to use it in a BB strategy.
E.g. you would enter long when you have bullish divergence, price is under lower BB, price is under PoC and this indicator here shows range-bound market phase.
You would exit long on cross of the middle band.
Description
The indicator attempts to model the underlying market using different local models (i.e., trending, range-bound, and choppy) and combines them using the T3 Six Pole Kalman Filter to generate an overall estimate of the market.
The Fisher Transform is applied on the price to reach a Gaussian distribution, which increases the accuracy of the indicator itself.
The script first defines state variables for each local model, which include trend direction, trend strength, upper and lower bounds of the range, volatility of the range, level of choppiness, and strength of noise.
Then, likelihood functions are defined for each local model based on the state variables.
Next, the script calculates weights for each local model based on their likelihoods and uses them to calculate state variables for the overall estimate.
Finally, the script combines the state variables using the T3 Six Pole Kalman Filter to generate the overall estimate of the market, which is plotted in blue.
Fundamental Knowledge
To understand the explanation of the indicator and the script, there are a few fundamental concepts that you need to know:
Market: A market is a place where buyers and sellers come together to exchange goods or services.
In the context of trading, the market refers to the exchange where financial instruments such as stocks, currencies, and commodities are bought and sold.
Local models: Local models are statistical models that attempt to capture the characteristics of a particular market regime.
For example, a trending market may have different characteristics than a range-bound market or a choppy market.
The indicator uses different local models to capture the different market regimes.
Trend direction and strength: The trend direction refers to the direction in which the market is moving, either up or down.
The trend strength refers to the magnitude of the trend and how likely it is to continue.
Range-bound market: A range-bound market is a market where prices are trading within a specific range, with a clear upper and lower bound.
Choppiness: Choppiness refers to the degree of irregularity in price movements, often seen in sideways or range-bound markets.
Volatility: Volatility refers to the degree of variation in the price of an asset over time. High volatility implies larger price swings, while low volatility implies smaller price swings.
Kalman filter: A Kalman filter is a mathematical algorithm used to estimate an unknown variable from a series of noisy measurements.
In the context of the indicator, the Kalman filter is used to generate an overall estimate of the market by combining the local models.
T3 Six Pole Kalman Filter: The T3 Six Pole Kalman Filter is a specific type of Kalman filter that is used to smooth and filter time-series data, such as the price data of a financial instrument.
Fisher Transform: The Fisher Transform is a mathematical formula used to transform any probability distribution into a Gaussian normal distribution. It is commonly used in technical analysis to transform non-Gaussian indicators into ones that are more suitable for statistical analysis.
By understanding these fundamental concepts, you should have a basic understanding of how the indicator works and how it generates an overall estimate of the market.
Usage
You can use this indicator on every timeframe.
Users can customize the parameters of the T3 Six Pole Kalman Filter (T3 length, alpha, beta, gamma, and delta) using input functions.
Try out different parameter combinations and use the one you like most.
Thank you for checking this out. Leave me a comment or boost the script, when you wanna support me! 👌
--
Credits to:
▪@HPotter - Fisher Transform
▪@loxx - T3
▪ChatGPT - Helped me to make the research for this indicator and helped to build the core algorithm.
Kalman Filter [by Hajixde]A simple form of recursive filtering using an adjustable gain and a memory length.
The filter predicts the next sample based on the previous values and the calculated error.
HMA-Kahlman Trend & Trendlines (v.2)This is an upgrade to the HMA-Kahlman Trend & Trendlines script ().
This version gives more flexibility because you can play around with 2 parameters to Kalman function (Sharpness and K (aka. step size)).
Kalman Gain Parameter MechanicsFrequently asked question is to explain how Gain parameter works in kalman funtion. This script serves as a visual representation of Gain parameter of Kalman function used in HMA-Kalman & Trendlines script. (The function creator's name was misspeled in that script as Kahlman)
To see better results set your Chart's timeframe to Daily.
STD/Clutter Filtered, One-Sided, N-Sinc-Kernel, EFIR Filt [Loxx]STD/Clutter Filtered, One-Sided, N-Sinc-Kernel, EFIR Filt is a normalized Cardinal Sine Filter Kernel Weighted Fir Filter that uses Ehler's FIR filter calculation instead of the general FIR filter calculation. This indicator has Kalman Velocity lag reduction, a standard deviation filter, a clutter filter, and a kernel noise filter. When calculating the Kernels, the both sides are calculated, then smoothed, then sliced to just the Right side of the Kernel weights. Lastly, blackman windowing is used for our purposes here. You can read about blackman windowing here:
Blackman window
Advantages of Blackman Window over Hamming Window Method for designing FIR Filter
The Kernel amplitudes are shown below with their corresponding values in yellow:
This indicator is intended to be used with Heikin-Ashi source inputs, specially HAB Median. You can read about this here:
Moving Average Filters Add-on w/ Expanded Source Types
What is a Finite Impulse Response Filter?
In signal processing, a finite impulse response (FIR) filter is a filter whose impulse response (or response to any finite length input) is of finite duration, because it settles to zero in finite time. This is in contrast to infinite impulse response (IIR) filters, which may have internal feedback and may continue to respond indefinitely (usually decaying).
The impulse response (that is, the output in response to a Kronecker delta input) of an Nth-order discrete-time FIR filter lasts exactly {\displaystyle N+1}N+1 samples (from first nonzero element through last nonzero element) before it then settles to zero.
FIR filters can be discrete-time or continuous-time, and digital or analog.
A FIR filter is (similar to, or) just a weighted moving average filter, where (unlike a typical equally weighted moving average filter) the weights of each delay tap are not constrained to be identical or even of the same sign. By changing various values in the array of weights (the impulse response, or time shifted and sampled version of the same), the frequency response of a FIR filter can be completely changed.
An FIR filter simply CONVOLVES the input time series (price data) with its IMPULSE RESPONSE. The impulse response is just a set of weights (or "coefficients") that multiply each data point. Then you just add up all the products and divide by the sum of the weights and that is it; e.g., for a 10-bar SMA you just add up 10 bars of price data (each multiplied by 1) and divide by 10. For a weighted-MA you add up the product of the price data with triangular-number weights and divide by the total weight.
Ultra Low Lag Moving Average's weights are designed to have MAXIMUM possible smoothing and MINIMUM possible lag compatible with as-flat-as-possible phase response.
Ehlers FIR Filter
Ehlers Filter (EF) was authored, not surprisingly, by John Ehlers. Read all about them here: Ehlers Filters
What is Normalized Cardinal Sine?
The sinc function sinc (x), also called the "sampling function," is a function that arises frequently in signal processing and the theory of Fourier transforms.
In mathematics, the historical unnormalized sinc function is defined for x ≠ 0 by
sinc x = sinx / x
In digital signal processing and information theory, the normalized sinc function is commonly defined for x ≠ 0 by
sinc x = sin(pi * x) / (pi * x)
What is a Clutter Filter?
For our purposes here, this is a filter that compares the slope of the trading filter output to a threshold to determine whether to shift trends. If the slope is up but the slope doesn't exceed the threshold, then the color is gray and this indicates a chop zone. If the slope is down but the slope doesn't exceed the threshold, then the color is gray and this indicates a chop zone. Alternatively if either up or down slope exceeds the threshold then the trend turns green for up and red for down. Fro demonstration purposes, an EMA is used as the moving average. This acts to reduce the noise in the signal.
What is a Dual Element Lag Reducer?
Modifies an array of coefficients to reduce lag by the Lag Reduction Factor uses a generic version of a Kalman velocity component to accomplish this lag reduction is achieved by applying the following to the array:
2 * coeff - coeff
The response time vs noise battle still holds true, high lag reduction means more noise is present in your data! Please note that the beginning coefficients which the modifying matrix cannot be applied to (coef whose indecies are < LagReductionFactor) are simply multiplied by two for additional smoothing .
Included
Bar coloring
Loxx's Expanded Source Types
Signals
Alerts
Pips-Stepped, R-squared Adaptive T3 [Loxx]Pips-Stepped, R-squared Adaptive T3 is a a T3 moving average with optional adaptivity, trend following, and pip-stepping. This indicator also uses optional flat coloring to determine chops zones. This indicator is R-squared adaptive. This is also an experimental indicator.
What is the T3 moving average?
Better Moving Averages Tim Tillson
November 1, 1998
Tim Tillson is a software project manager at Hewlett-Packard, with degrees in Mathematics and Computer Science. He has privately traded options and equities for 15 years.
Introduction
"Digital filtering includes the process of smoothing, predicting, differentiating, integrating, separation of signals, and removal of noise from a signal. Thus many people who do such things are actually using digital filters without realizing that they are; being unacquainted with the theory, they neither understand what they have done nor the possibilities of what they might have done."
This quote from R. W. Hamming applies to the vast majority of indicators in technical analysis . Moving averages, be they simple, weighted, or exponential, are lowpass filters; low frequency components in the signal pass through with little attenuation, while high frequencies are severely reduced.
"Oscillator" type indicators (such as MACD , Momentum, Relative Strength Index ) are another type of digital filter called a differentiator.
Tushar Chande has observed that many popular oscillators are highly correlated, which is sensible because they are trying to measure the rate of change of the underlying time series, i.e., are trying to be the first and second derivatives we all learned about in Calculus.
We use moving averages (lowpass filters) in technical analysis to remove the random noise from a time series, to discern the underlying trend or to determine prices at which we will take action. A perfect moving average would have two attributes:
It would be smooth, not sensitive to random noise in the underlying time series. Another way of saying this is that its derivative would not spuriously alternate between positive and negative values.
It would not lag behind the time series it is computed from. Lag, of course, produces late buy or sell signals that kill profits.
The only way one can compute a perfect moving average is to have knowledge of the future, and if we had that, we would buy one lottery ticket a week rather than trade!
Having said this, we can still improve on the conventional simple, weighted, or exponential moving averages. Here's how:
Two Interesting Moving Averages
We will examine two benchmark moving averages based on Linear Regression analysis.
In both cases, a Linear Regression line of length n is fitted to price data.
I call the first moving average ILRS, which stands for Integral of Linear Regression Slope. One simply integrates the slope of a linear regression line as it is successively fitted in a moving window of length n across the data, with the constant of integration being a simple moving average of the first n points. Put another way, the derivative of ILRS is the linear regression slope. Note that ILRS is not the same as a SMA ( simple moving average ) of length n, which is actually the midpoint of the linear regression line as it moves across the data.
We can measure the lag of moving averages with respect to a linear trend by computing how they behave when the input is a line with unit slope. Both SMA (n) and ILRS(n) have lag of n/2, but ILRS is much smoother than SMA .
Our second benchmark moving average is well known, called EPMA or End Point Moving Average. It is the endpoint of the linear regression line of length n as it is fitted across the data. EPMA hugs the data more closely than a simple or exponential moving average of the same length. The price we pay for this is that it is much noisier (less smooth) than ILRS, and it also has the annoying property that it overshoots the data when linear trends are present.
However, EPMA has a lag of 0 with respect to linear input! This makes sense because a linear regression line will fit linear input perfectly, and the endpoint of the LR line will be on the input line.
These two moving averages frame the tradeoffs that we are facing. On one extreme we have ILRS, which is very smooth and has considerable phase lag. EPMA has 0 phase lag, but is too noisy and overshoots. We would like to construct a better moving average which is as smooth as ILRS, but runs closer to where EPMA lies, without the overshoot.
A easy way to attempt this is to split the difference, i.e. use (ILRS(n)+EPMA(n))/2. This will give us a moving average (call it IE /2) which runs in between the two, has phase lag of n/4 but still inherits considerable noise from EPMA. IE /2 is inspirational, however. Can we build something that is comparable, but smoother? Figure 1 shows ILRS, EPMA, and IE /2.
Filter Techniques
Any thoughtful student of filter theory (or resolute experimenter) will have noticed that you can improve the smoothness of a filter by running it through itself multiple times, at the cost of increasing phase lag.
There is a complementary technique (called twicing by J.W. Tukey) which can be used to improve phase lag. If L stands for the operation of running data through a low pass filter, then twicing can be described by:
L' = L(time series) + L(time series - L(time series))
That is, we add a moving average of the difference between the input and the moving average to the moving average. This is algebraically equivalent to:
2L-L(L)
This is the Double Exponential Moving Average or DEMA , popularized by Patrick Mulloy in TASAC (January/February 1994).
In our taxonomy, DEMA has some phase lag (although it exponentially approaches 0) and is somewhat noisy, comparable to IE /2 indicator.
We will use these two techniques to construct our better moving average, after we explore the first one a little more closely.
Fixing Overshoot
An n-day EMA has smoothing constant alpha=2/(n+1) and a lag of (n-1)/2.
Thus EMA (3) has lag 1, and EMA (11) has lag 5. Figure 2 shows that, if I am willing to incur 5 days of lag, I get a smoother moving average if I run EMA (3) through itself 5 times than if I just take EMA (11) once.
This suggests that if EPMA and DEMA have 0 or low lag, why not run fast versions (eg DEMA (3)) through themselves many times to achieve a smooth result? The problem is that multiple runs though these filters increase their tendency to overshoot the data, giving an unusable result. This is because the amplitude response of DEMA and EPMA is greater than 1 at certain frequencies, giving a gain of much greater than 1 at these frequencies when run though themselves multiple times. Figure 3 shows DEMA (7) and EPMA(7) run through themselves 3 times. DEMA^3 has serious overshoot, and EPMA^3 is terrible.
The solution to the overshoot problem is to recall what we are doing with twicing:
DEMA (n) = EMA (n) + EMA (time series - EMA (n))
The second term is adding, in effect, a smooth version of the derivative to the EMA to achieve DEMA . The derivative term determines how hot the moving average's response to linear trends will be. We need to simply turn down the volume to achieve our basic building block:
EMA (n) + EMA (time series - EMA (n))*.7;
This is algebraically the same as:
EMA (n)*1.7-EMA( EMA (n))*.7;
I have chosen .7 as my volume factor, but the general formula (which I call "Generalized Dema") is:
GD (n,v) = EMA (n)*(1+v)-EMA( EMA (n))*v,
Where v ranges between 0 and 1. When v=0, GD is just an EMA , and when v=1, GD is DEMA . In between, GD is a cooler DEMA . By using a value for v less than 1 (I like .7), we cure the multiple DEMA overshoot problem, at the cost of accepting some additional phase delay. Now we can run GD through itself multiple times to define a new, smoother moving average T3 that does not overshoot the data:
T3(n) = GD ( GD ( GD (n)))
In filter theory parlance, T3 is a six-pole non-linear Kalman filter. Kalman filters are ones which use the error (in this case (time series - EMA (n)) to correct themselves. In Technical Analysis , these are called Adaptive Moving Averages; they track the time series more aggressively when it is making large moves.
What is R-squared Adaptive?
One tool available in forecasting the trendiness of the breakout is the coefficient of determination (R-squared), a statistical measurement.
The R-squared indicates linear strength between the security's price (the Y - axis) and time (the X - axis). The R-squared is the percentage of squared error that the linear regression can eliminate if it were used as the predictor instead of the mean value. If the R-squared were 0.99, then the linear regression would eliminate 99% of the error for prediction versus predicting closing prices using a simple moving average.
R-squared is used here to derive a T3 factor used to modify price before passing price through a six-pole non-linear Kalman filter.
Included:
Bar coloring
Signals
Alerts
Flat coloring
Kalman Filter [Loxx]Kalman filter is a recursive algorithm that has been invented in the 1960s to track a moving target, remove any noisy measurements of its position and predict its future position. In finance, KF has been used by the asset management industry for various purposes. KF is an optimal choice in many cases and do at least better than a moving average smoothing.
A port of Kalman filter - indicator for MetaTrader 4
Added color change based on whether velocity is over/under 0
Loft Strategy V4This strategy is an advanced version of the Loft Strategy V1, I shared earlier. (Loft Strategy V1 consists of a kalman filter (by alexgrover ) and a "stop and reverse" line which is following and the kalman filter. If the price goes in the same direction as the position side, the "stop and reverse" line approaches the kalman filter as set on the "Approach Decrease Step" parameter.)
In addition to the previous version, it includes a martingale like deviation and multiple take-profit.
Here it is some parameters definitions of the strategy:
Kalman Filter: The higher this parameter, the faster and more aggressive the filter. Otherwise the filter goes very smoothly
Beginning Approach: First approximation as a percentage of stop-n-reverse line
Final Approach: Minimum approximation of stop-n-reverse line
Approach Decrease Step: If the price moves in the same direction as the strategy, the approach percentage is reduced by this parameter. Otherwise nothing do
Base Order Quantity: Initial capital of position
Max Safe Order Attempt: This parameter determines the maximum number of times the strategy will raise the bet after losing in a row.
Safe Order Deviation: if the last trade is loss, multiply the bet by this parameter (aka. martingale factor)
Profit Deviation: if last trade in loss, multiply the take-profit points
Max Order Quantity: Maximum capital allowed for a position
TP1, TP2, TP3 : Take profit spots in percentage
QT1, QT2, QT3: Amount of take-profit spots
Stop Loss: Maximum stop loss allowed for a trade
Long Entry, Short Entry: Only long side, only short side or both side
Safe Stop After TP2: If the price reaches the TP2 point, move the stop-loss point to the entry price.
Safe Stop After TP1: If the price reaches TP1, move the stop-loss point to the stop-n-reverse line.
Loft Strategy V1This strategy consists of a kalman filter (by alexgrover ) and a "stop and reverse" line which is following the kalman filter.
If the price goes in the same direction as the position side, the "stop and reverse" line approaches the kalman filter as set on the "Approach Decrease Step" parameter.
ZLEMA Zero lag EMA with Kalman filter [Morty]This indicator plot 3 Kalman filter zero lag EMA lines. It has less lag and is also smoother than the original EMA.
It also has an option to show the crossover of two EMAs.
OneGreenCandle Kalman - RSIKalman filter on multiple RSI periods. Usefull on higher timeframes to confirm a change of trend.
powerful moving average crossoverThis script is a simplified version of John Ehlers's adaption of Dr. Kalman's optimum estimator as applied to price action (More can be found on this here: www.dimensionetrading.com). Here I have adapted two of these optimum estimators to work together to provide crossover signals. The user can choose the input of this filter in the 'input source'. The 'Ratio of Uncertainties' controls how adaptive the moving averages are, increasing this number will increase adaptivity and vice versa for decreasing. The 'Kalman Gain' allows the user to choose how much error to let into the calculation. The smaller this number is the quicker the moving average will approach price action.
In practice this indicator is much smoother than most other moving averages and has significantly less whiplash while still getting very early entries. If anyone wants to adapt this script for their own uses please feel free. Message me what you make with it, I am very curious what this can do when in the right hands!
Happy trading!
Dynamically Adjustable FilterIntroduction
Inspired from the Kalman filter this indicator aim to provide a good result in term of smoothness and reactivity while letting the user the option to increase/decrease smoothing.
Optimality And Dynamical Adjustment
This indicator is constructed in the same manner as many adaptive moving averages by using exponential averaging with a smoothing variable, this is described by :
x= x_1 + a(y - x_1)
where y is the input price (measurements) and a is the smoothing variable, with Kalman filters a is often replaced by K or Kalman Gain , this Gain is what adjust the estimate to the measurements. In the indicator K is calculated as follow :
K = Absolute Error of the estimate/(Absolute Error of the estimate + Measurements Dispersion * length)
The error of the estimate is just the absolute difference between the measurements and the estimate, the dispersion is the measurements standard deviation and length is a parameter controlling smoothness. K adjust to price volatility and try to provide a good estimate no matter the size of length . In order to increase reactivity the price input (measurements) has been summed with the estimate error.
Now this indicator use a fraction of what a Kalman filter use for its entire calculation, therefore the covariance update has been discarded as well as the extrapolation part.
About parameters length control the filter smoothness, the lag reduction option create more reactive results.
Conclusion
You can create smoothing variables for any adaptive indicator by using the : a/(a+b) form since this operation always return values between 0 and 1 as long as a and b are positive. Hope it help !
Thanks for reading !
Well Rounded Moving AverageIntroduction
There are tons of filters, way to many, and some of them are redundant in the sense they produce the same results as others. The task to find an optimal filter is still a big challenge among technical analysis and engineering, a good filter is the Kalman filter who is one of the more precise filters out there. The optimal filter theorem state that : The optimal estimator has the form of a linear observer , this in short mean that an optimal filter must use measurements of the inputs and outputs, and this is what does the Kalman filter. I have tried myself to Kalman filters with more or less success as well as understanding optimality by studying Linear–quadratic–Gaussian control, i failed to get a complete understanding of those subjects but today i present a moving average filter (WRMA) constructed with all the knowledge i have in control theory and who aim to provide a very well response to market price, this mean low lag for fast decision timing and low overshoots for better precision.
Construction
An good filter must use information about its output, this is what exponential smoothing is about, simple exponential smoothing (EMA) is close to a simple moving average and can be defined as :
output = output(1) + α(input - output(1))
where α (alpha) is a smoothing constant, typically equal to 2/(Period+1) for the EMA.
This approach can be further developed by introducing more smoothing constants and output control (See double/triple exponential smoothing - alpha-beta filter) .
The moving average i propose will use only one smoothing constant, and is described as follow :
a = nz(a ) + alpha*nz(A )
b = nz(b ) + alpha*nz(B )
y = ema(a + b,p1)
A = src - y
B = src - ema(y,p2)
The filter is divided into two components a and b (more terms can add more control/effects if chosen well) , a adjust itself to the output error and is responsive while b is independent of the output and is mainly smoother, adding those components together create an output y , A is the output error and B is the error of an exponential moving average.
Comparison
There are a lot of low-lag filters out there, but the overshoots they induce in order to reduce lag is not a great effect. The first comparison is with a least square moving average, a moving average who fit a line in a price window of period length .
Lsma in blue and WRMA in red with both length = 100 . The lsma is a bit smoother but induce terrible overshoots
ZLMA in blue and WRMA in red with both length = 100 . The lag difference between each moving average is really low while VWRMA is way more precise.
Hull MA in blue and WRMA in red with both length = 100 . The Hull MA have similar overshoots than the LSMA.
Reduced overshoots moving average (ROMA) in blue and WRMA in red with both length = 100 . ROMA is an indicator i have made to reduce the overshoots of a LSMA, but at the end WRMA still reduce way more the overshoots while being smoother and having similar lag.
I have added a smoother version, just activate the extra smooth option in the indicator settings window. Here the result with length = 200 :
This result is a little bit similar to a 2 order Butterworth filter. Our filter have more overshoots which in this case could be useful to reduce the error with edges since other low pass filters tend to smooth their amplitude thus reducing edge estimation precision.
Conclusions
I have presented a well rounded filter in term of smoothness/stability and reactivity. Try to add more terms to have different results, you could maybe end up with interesting results, if its the case share them with the community :)
As for control theory i have seen neural networks integrated to Kalman flters which leaded to great accuracy, AI is everywhere and promise to be a game a changer in real time data smoothing. So i asked myself if it was possible for a neural networks to develop pinescript indicators, if yes then i could be replaced by AI ? Brrr how frightening.
Thanks for reading :)