OPEN-SOURCE SCRIPT

Any Oscillator Underlay [TTF]

We are proud to release a new indicator that has been a while in the making - the Any Oscillator Underlay (AOU)!

Note: There is a lot to discuss regarding this indicator, including its intent and some of how it operates, so please be sure to read this entire description before using this indicator to help ensure you understand both the intent and some limitations with this tool.

Our intent for building this indicator was to accomplish the following:
  • Combine all of the oscillators that we like to use into a single indicator
  • Take up a bit less screen space for the underlay indicators for strategies that utilize multiple oscillators
  • Provide a tool for newer traders to be able to leverage multiple oscillators in a single indicator


Features:
  • Includes 8 separate, fully-functional indicators combined into one
  • Ability to easily enable/disable and configure each included indicator independently
  • Clearly named plots to support user customization of color and styling, as well as manual creation of alerts
  • Ability to customize sub-indicator title position and color
  • Ability to customize sub-indicator divider lines style and color


Indicators that are included in this initial release:
  • TSI
  • 2x RSIs (dubbed the Twin RSI)
  • Stochastic RSI
  • Stochastic
  • Ultimate Oscillator
  • Awesome Oscillator
  • MACD
  • Outback RSI (Color-coding only)


Quick note on OB/OS:
Before we get into covering each included indicator, we first need to cover a core concept for how we're defining OB and OS levels. To help illustrate this, we will use the TSI as an example.

The TSI by default has a mid-point of 0 and a range of -100 to 100. As a result, a common practice is to place lines on the -30 and +30 levels to represent OS and OB zones, respectively. Most people tend to view these levels as distance from the edges/outer bounds or as absolute levels, but we feel a more way to frame the OB/OS concept is to instead define it as distance ("offset") from the mid-line. In keeping with the -30 and +30 levels in our example, the offset in this case would be "30".

Taking this a step further, let's say we decided we wanted an offset of 25. Since the mid-point is 0, we'd then calculate the OB level as 0 + 25 (+25), and the OS level as 0 - 25 (-25).

Now that we've covered the concept of how we approach defining OB and OS levels (based on offset/distance from the mid-line), and since we did apply some transformations, rescaling, and/or repositioning to all of the indicators noted above, we are going to discuss each component indicator to detail both how it was modified from the original to fit the stacked-indicator model, as well as the various major components that the indicator contains.

TSI:
スナップショット
This indicator contains the following major elements:
  • TSI and TSI Signal Line
  • Color-coded fill for the TSI/TSI Signal lines
  • Moving Average for the TSI
  • TSI Histogram
  • Mid-line and OB/OS lines


Default TSI fill color coding:
  • Green: TSI is above the signal line
  • Red: TSI is below the signal line


Note: The TSI traditionally has a range of -100 to +100 with a mid-point of 0 (range of 200). To fit into our stacking model, we first shrunk the range to 100 (-50 to +50 - cut it in half), then repositioned it to have a mid-point of 50. Since this is the "bottom" of our indicator-stack, no additional repositioning is necessary.

Twin RSI:
スナップショット
This indicator contains the following major elements:
  • Fast RSI (useful if you want to leverage 2x RSIs as it makes it easier to see the overlaps and crosses - can be disabled if desired)
  • Slow RSI (primary RSI)
  • Color-coded fill for the Fast/Slow RSI lines (if Fast RSI is enabled and configured)
  • Moving Average for the Slow RSI
  • Mid-line and OB/OS lines


Default Twin RSI fill color coding:
  • Dark Red: Fast RSI below Slow RSI and Slow RSI below Slow RSI MA
  • Light Red: Fast RSI below Slow RSI and Slow RSI above Slow RSI MA
  • Dark Green: Fast RSI above Slow RSI and Slow RSI below Slow RSI MA
  • Light Green: Fast RSI above Slow RSI and Slow RSI above Slow RSI MA


Note: The RSI naturally has a range of 0 to 100 with a mid-point of 50, so no rescaling or transformation is done on this indicator. The only manipulation done is to properly position it in the indicator-stack based on which other indicators are also enabled.

Stochastic and Stochastic RSI:
スナップショット
スナップショット
These indicators contain the following major elements:
  • Configurable lengths for the RSI (for the Stochastic RSI only), K, and D values
  • Configurable base price source
  • Mid-line and OB/OS lines


Note: The Stochastic and Stochastic RSI both have a normal range of 0 to 100 with a mid-point of 50, so no rescaling or transformations are done on either of these indicators. The only manipulation done is to properly position it in the indicator-stack based on which other indicators are also enabled.

Ultimate Oscillator (UO):
スナップショット
This indicator contains the following major elements:
  • Configurable lengths for the Fast, Middle, and Slow BP/TR components
  • Mid-line and OB/OS lines
  • Moving Average for the UO
  • Color-coded fill for the UO/UO MA lines (if UO MA is enabled and configured)


Default UO fill color coding:
  • Green: UO is above the moving average line
  • Red: UO is below the moving average line


Note: The UO naturally has a range of 0 to 100 with a mid-point of 50, so no rescaling or transformation is done on this indicator. The only manipulation done is to properly position it in the indicator-stack based on which other indicators are also enabled.

Awesome Oscillator (AO):
スナップショット
This indicator contains the following major elements:
  • Configurable lengths for the Fast and Slow moving averages used in the AO calculation
  • Configurable price source for the moving averages used in the AO calculation
  • Mid-line
  • Option to display the AO as a line or pseudo-histogram
  • Moving Average for the AO
  • Color-coded fill for the AO/AO MA lines (if AO MA is enabled and configured)


Default AO fill color coding (Note: Fill was disabled in the image above to improve clarity):
  • Green: AO is above the moving average line
  • Red: AO is below the moving average line


Note: The AO is technically has an infinite (unbound) range - -∞ to ∞ - and the effective range is bound to the underlying security price (e.g. BTC will have a wider range than SP500, and SP500 will have a wider range than EUR/USD). We employed some special techniques to rescale this indicator into our desired range of 100 (-50 to 50), and then repositioned it to have a midpoint of 50 (range of 0 to 100) to meet the constraints of our stacking model. We then do one final repositioning to place it in the correct position the indicator-stack based on which other indicators are also enabled. For more details on how we accomplished this, read our section "Binding Infinity" below.

MACD:
スナップショット
This indicator contains the following major elements:
  • Configurable lengths for the Fast and Slow moving averages used in the MACD calculation
  • Configurable price source for the moving averages used in the MACD calculation
  • Configurable length and calculation method for the MACD Signal Line calculation
  • Mid-line


Note: Like the AO, the MACD also technically has an infinite (unbound) range. We employed the same principles here as we did with the AO to rescale and reposition this indicator as well. For more details on how we accomplished this, read our section "Binding Infinity" below.

Outback RSI (ORSI):
This is a stripped-down version of the Outback RSI indicator (linked above) that only includes the color-coding background (suffice it to say that it was not technically feasible to attempt to rescale the other components in a way that could consistently be clearly seen on-chart). As this component is a bit of a niche/special-purpose sub-indicator, it is disabled by default, and we suggest it remain disabled unless you have some pre-defined strategy that leverages the color-coding element of the Outback RSI that you wish to use.

Binding Infinity - How We Incorporated the AO and MACD (Warning - Math Talk Ahead!)

Note: This applies only to the AO and MACD at time of original publication. If any other indicators are added in the future that also fall into the category of "binding an infinite-range oscillator", we will make that clear in the release notes when that new addition is published.

To help set the stage for this discussion, it's important to note that the broader challenge of "equalizing inputs" is nothing new. In fact, it's a key element in many of the most popular fields of data science, such as AI and Machine Learning. They need to take a diverse set of inputs with a wide variety of ranges and seemingly-random inputs (referred to as "features"), and build a mathematical or computational model in order to work. But, when the raw inputs can vary significantly from one another, there is an inherent need to do some pre-processing to those inputs so that one doesn't overwhelm another simply due to the difference in raw values between them. This is where feature scaling comes into play.

With this in mind, we implemented 2 of the most common methods of Feature Scaling - Min-Max Normalization (which we call "Normalization" in our settings), and Z-Score Normalization (which we call "Standardization" in our settings). Let's take a look at each of those methods as they have been implemented in this script.

Min-Max Normalization (Normalization)
This is one of the most common - and most basic - methods of feature scaling. The basic formula is: y = (x - min)/(max - min) - where x is the current data sample, min is the lowest value in the dataset, and max is the highest value in the dataset. In this transformation, the max would evaluate to 1, and the min would evaluate to 0, and any value in between the min and the max would evaluate somewhere between 0 and 1.

The key benefits of this method are:
  • It can be used to transform datasets of any range into a new dataset with a consistent and known range (0 to 1).
  • It has no dependency on the "shape" of the raw input dataset (i.e. does not assume the input dataset can be approximated to a normal distribution).


But there are a couple of "gotchas" with this technique...
  • First, it assumes the input dataset is complete, or an accurate representation of the population via random sampling. While in most situations this is a valid assumption, in trading indicators we don't really have that luxury as we're often limited in what sample data we can access (i.e. number of historical bars available).
  • Second, this method is highly sensitive to outliers. Since the crux of this transformation is based on the max-min to define the initial range, a single significant outlier can result in skewing the post-transformation dataset (i.e. major price movement as a reaction to a significant news event).


You can potentially mitigate those 2 "gotchas" by using a mechanism or technique to find and discard outliers (e.g. calculate the mean and standard deviation of the input dataset and discard any raw values more than 5 standard deviations from the mean), but if your most recent datapoint is an "outlier" as defined by that algorithm, processing it using the "scrubbed" dataset would result in that new datapoint being outside the intended range of 0 to 1 (e.g. if the new datapoint is greater than the "scrubbed" max, it's post-transformation value would be greater than 1). Even though this is a bit of an edge-case scenario, it is still sure to happen in live markets processing live data, so it's not an ideal solution in our opinion (which is why we chose not to attempt to discard outliers in this manner).

Z-Score Normalization (Standardization)
This method of rescaling is a bit more complex than the Min-Max Normalization method noted above, but it is also a widely used process. The basic formula is: y = (x – μ) / σ - where x is the current data sample, μ is the mean (average) of the input dataset, and σ is the standard deviation of the input dataset. While this transformation still results in a technically-infinite possible range, the output of this transformation has a 2 very significant properties - the mean (average) of the output dataset has a mean (μ) of 0 and a standard deviation (σ) of 1.

The key benefits of this method are:
  • As it's based on normalizing the mean and standard deviation of the input dataset instead of a linear range conversion, it is far less susceptible to outliers significantly affecting the result (and in fact has the effect of "squishing" outliers).
  • It can be used to accurately transform disparate sets of data into a similar range regardless of the original dataset's raw/actual range.


But there are a couple of "gotchas" with this technique as well...
  • First, it still technically does not do any form of range-binding, so it is still technically unbounded (range -∞ to ∞ with a mid-point of 0).
  • Second, it implicitly assumes that the raw input dataset to be transformed is normally distributed, which won't always be the case in financial markets.


The first "gotcha" is a bit of an annoyance, but isn't a huge issue as we can apply principles of normal distribution to conceptually limit the range by defining a fixed number of standard deviations from the mean. While this doesn't totally solve the "infinite range" problem (a strong enough sudden move can still break out of our "conceptual range" boundaries), the amount of movement needed to achieve that kind of impact will generally be pretty rare.

The bigger challenge is how to deal with the assumption of the input dataset being normally distributed. While most financial markets (and indicators) do tend towards a normal distribution, they are almost never going to match that distribution exactly. So let's dig a bit deeper into distributions are defined and how things like trending markets can affect them.

Skew (skewness): This is a measure of asymmetry of the bell curve, or put another way, how and in what way the bell curve is disfigured when comparing the 2 halves. The easiest way to visualize this is to draw an imaginary vertical line through the apex of the bell curve, then fold the curve in half along that line. If both halves are exactly the same, the skew is 0 (no skew/perfectly symmetrical) - which is what a normal distribution has (skew = 0). Most financial markets tend to have short, medium, and long-term trends, and these trends will cause the distribution curve to skew in one direction or another. Bullish markets tend to skew to the right (positive), and bearish markets to the left (negative).

Kurtosis: This is a measure of the "tail size" of the bell curve. Another way to state this could be how "flat" or "steep" the bell-shape is. If the bell is steep with a strong drop from the apex (like a steep cliff), it has low kurtosis. If the bell has a shallow, more sweeping drop from the apex (like a tall hill), is has high kurtosis. Translating this to financial markets, kurtosis is generally a metric of volatility as the bell shape is largely defined by the strength and frequency of outliers. This is effectively a measure of volatility - volatile markets tend to have a high level of kurtosis (>3), and stable/consolidating markets tend to have a low level of kurtosis (<3). A normal distribution (our reference), has a kurtosis value of 3.

So to try and bring all that back together, here's a quick recap of the Standardization rescaling method:
  • The Standardization method has an assumption of a normal distribution of input data by using the mean (average) and standard deviation to handle the transformation
  • Most financial markets do NOT have a normal distribution (as discussed above), and will have varying degrees of skew and kurtosis


Q: Why are we still favoring the Standardization method over the Normalization method, and how are we accounting for the innate skew and/or kurtosis inherent in most financial markets?
A: Well, since we're only trying to rescale oscillators that by-definition have a midpoint of 0, kurtosis isn't a major concern beyond the affect it has on the post-transformation scaling (specifically, the number of standard deviations from the mean we need to include in our "artificially-bound" range definition).

Q: So that answers the question about kurtosis, but what about skew?
A: So - for skew, the answer is in the formula - specifically the mean (average) element. The standard mean calculation assumes a complete dataset and therefore uses a standard (i.e. simple) average, but we're limited by the data history available to us. So we adapted the transformation formula to leverage a moving average that included a weighting element to it so that it favored recent datapoints more heavily than older ones. By making the average component more adaptive, we gained the effect of reducing the skew element by having the average itself be more responsive to recent movements, which significantly reduces the effect historical outliers have on the dataset as a whole. While this is certainly not a perfect solution, we've found that it serves the purpose of rescaling the MACD and AO to a far more well-defined range while still preserving the oscillator behavior and mid-line exceptionally well.

The most difficult parts to compensate for are periods where markets have low volatility for an extended period of time - to the point where the oscillators are hovering around the 0/midline (in the case of the AO), or when the oscillator and signal lines converge and remain close to each other (in the case of the MACD). It's during these periods where even our best attempt at ensuring accurate mirrored-behavior when compared to the original can still occasionally lead or lag by a candle.
Note: If this is a make-or-break situation for you or your strategy, then we recommend you do not use any of the included indicators that leverage this kind of bounding technique (the AO and MACD at time of publication) and instead use the Trandingview built-in versions!

We know this is a lot to read and digest, so please take your time and feel free to ask questions - we will do our best to answer! And as always, constructive feedback is always welcome!
aoOscillatorsRelative Strength Index (RSI)Stochastic RSI (STOCH RSI)TSIuo

オープンソーススクリプト

TradingViewの精神に則り、このスクリプトの作者は、トレーダーが理解し検証できるようにオープンソースで公開しています。作者に敬意を表します!無料で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。 お気に入りに登録してチャート上でご利用頂けます。

チャートでこのスクリプトを利用したいですか?


Everything you need to know about day trading from A to Z

And the most incredible day trading community to help support you along the way
他のメディア:

免責事項