Quick scan for signal🙏🏻 Hey TV, this is QSFS, following:
^^ Quick scan for drift (QSFD)
^^ Quick scan for cycles (QSFC)
As mentioned before, ML trading is all about spotting any kind of non-randomness, and this metric (along with 2 previously posted) gonna help ya'll do it fast. This one will show you whether your time series possibly exhibits mean-reverting / consistent / noisy behavior, that can be later confirmed or denied by more sophisticated tools. This metric is O(n) in windowed mode and O(1) if calculated incrementally on each data update, so you can scan Ks of datasets w/o worrying about melting da ice.
^^ windowed mode
Now the post will be divided into several sections, and a couple of things I guess you’ve never seen or thought about in your life:
1) About Efficiency Ratios posted there on TV;
Some of you might say this is the Efficiency Ratio you’ve seen in Perry's book. Firstly, I can assure you that neither me nor Perry, just as X amount of quants all over the world and who knows who else, would say smth like, "I invented it," lol. This is just a thing you R&D when you need it. Secondly, I invite you (and mods & admin as well) to take a lil glimpse at the following screenshot:
^^ not cool...
So basically, all the Efficiency Ratios that were copypasted to our platform suffer the same bug: dudes don’t know how indexing works in Pine Script. I mean, it’s ok, I been doing the same mistakes as well, but loxx, cmon bro, you... If you guys ever read it, the lines 20 and 22 in da code are dedicated to you xD
2) About the metric;
This supports both moving window mode when Length > 0 and all-data expanding window mode when Length < 1, calculating incrementally from the very first data point in the series: O(n) on history, O(1) on live updates.
Now, why do I SQRT transform the result? This is a natural action since the metric (being a ratio in essence) is bounded between 0 and 1, so it can be modeled with a beta distribution. When you SQRT transform it, it still stays beta (think what happens when you apply a square root to 0.01 or 0.99), but it becomes symmetric around its typical value and starts to follow a bell-shaped curve. This can be easily checked with a normality test or by applying a set of percentiles and seeing the distances between them are almost equal.
Then I noticed that on different moving window sizes, the typical value of the metric seems to slide: higher window sizes lead to lower typical values across the moving windows. Turned out this can be modeled the same way confidence intervals are made. Lines 34 and 35 explain it all, I guess. You can see smth alike on an autocorrelogram. These two match the mean & mean + 1 stdev applied to the metric. This way, we’ve just magically received data to estimate alpha and beta parameters of the beta distribution using the method of moments. Having alpha and beta, we can now estimate everything further. Btw, there’s an alternative parameterization for beta distributions based on data length.
Now what you’ll see next is... u guys actually have no idea how deep and unrealistically minimalistic the underlying math principles are here.
I’m sure I’m not the only one in the universe who figured it out, but the thing is, it’s nowhere online or offline. By calculating higher-order moments & combining them, you can find natural adaptive thresholds that can later be used for anomaly detection/control applications for any data. No hardcoded thresholds, purely data-driven. Imma come back to this in one of the next drops, but the truest ones can already see it in this code. This way we get dem thresholds.
Your main thresholds are: basis, upper, and lower deviations. You can follow the common logic I’ve described in my previous scripts on how to use them. You just register an event when the metric goes higher/lower than a certain threshold based on what you’re looking for. Then you take the time series and confirm a certain behavior you were looking for by using an appropriate stat test. Or just run a certain strategy.
To avoid numerous triggers when the metric jitters around a threshold, you can follow this logic: forget about one threshold if touched, until another threshold is touched.
In general, when the metric gets higher than certain thresholds, like upper deviation, it means the signal is stronger than noise. You confirm it with a more sophisticated tool & run momentum strategies if drift is in place, or volatility strategies if there’s no drift in place. Otherwise, you confirm & run ~ mean-reverting strategies, regardless of whether there’s drift or not. Just don’t operate against the trend—hedge otherwise.
3) Flex;
Extension and limit thresholds based on distribution moments gonna be discussed properly later, but now you can see this:
^^ magic
Look at the thresholds—adaptive and dynamic. Do you see any optimizations? No ML, no DL, closed-form solution, but how? Just a formula based on a couple of variables? Maybe it’s just how the Universe works, but how can you know if you don’t understand how fundamentally numbers 3 and 15 are related to the normal distribution? Hm, why do they always say 3 sigmas but can’t say why? Maybe you can be different and say why?
This is the primordial power of statistical modeling.
4) Thanks;
I really wanna dedicate this to Charlotte de Witte & Marion Di Napoli, and their new track "Sanctum." It really gets you connected to the Source—I had it in my soul when I was doing all this ∞
Dimension
Musashi_Fractal_Dimension === Musashi-Fractal-Dimension ===
This tool is part of my research on the fractal nature of the markets and understanding the relation between fractal dimension and chaos theory.
To take full advantage of this indicator, you need to incorporate some principles and concepts:
- Traditional Technical Analysis is linear and Euclidean, which makes very difficult its modeling.
- Linear techniques cannot quantify non-linear behavior
- Is it possible to measure accurately a wave or the surface of a mountain with a simple ruler?
- Fractals quantify what Euclidean Geometry can’t, they measure chaos, as they identify order in apparent randomness.
- Remember: Chaos is order disguised as randomness.
- Chaos is the study of unstable aperiodic behavior in deterministic non-linear dynamic systems
- Order and randomness can coexist, allowing predictability.
- There is a reason why Fractal Dimension was invented, we had no way of measuring fractal-based structures.
- Benoit Mandelbrot used to explain it by asking: How do we measure the coast of Great Britain?
- An easy way of getting the need of a dimension in between is looking at the Koch snowflake.
- Market prices tend to seek natural levels of ranges of balance. These levels can be described as attractors and are determinant.
Fractal Dimension Index ('FDI')
Determines the persistence or anti-persistence of a market.
- A persistent market follows a market trend. An anti-persistent market results in substantial volatility around the trend (with a low r2), and is more vulnerable to price reversals
- An easy way to see this is to think that fractal dimension measures what is in between mainstream dimensions. These are:
- One dimension: a line
- Two dimensions: a square
- Three dimensions: a cube.
--> This will hint you that at certain moment, if the market has a Fractal Dimension of 1.25 (which is low), the market is behaving more “line-like”, while if the market has a high Fractal Dimension, it could be interpreted as “square-like”.
- 'FDI' is trend agnostic, which means that doesn't consider trend. This makes it super useful as gives you clean information about the market without trying to include trend stuff.
Question: If we have a game where you must choose between two options.
1. a horizontal line
2. a vertical line.
Each iteration a Horizontal Line or a Square will appear as continuation of a figure. If it that iteration shows a square and you bet vertical you win, same as if it is horizontal and it is a line.
- Wouldn’t be useful to know that Fractal dimension is 1.8? This will hint square. In the markets you can use 'FD' to filter mean-reversal signals like Bollinger bands, stochastics, Regular RSI divergences, etc.
- Wouldn’t be useful to know that Fractal dimension is 1.2? This will hint Line. In the markets you can use 'FD' to confirm trend following strategies like Moving averages, MACD, Hidden RSI divergences.
Calculation method:
Fractal dimension is obtained from the ‘hurst exponent’.
'FDI' = 2 - 'Hurst Exponent'
Musashi version of the Classic 'OG' Fractal Dimension Index ('FDI')
- By default, you get 3 fast 'FDI's (11,12,13) + 1 Slow 'FDI' (21), their interaction gives useful information.
- Fast 'FDI' cross will give you gray or red dots while Slow 'FDI' cross with the slowest of the fast 'FDI's will give white and orange dots. This are great to early spot trend beginnings or trend ends.
- A baseline (purple) is also provided, this is calculated using a 21 period Bollinger bands with 1.618 'SD', once calculated, you just take midpoint, this is the 'TDI's (Traders Dynamic Index) way. The indicator will print purple dots when Slow 'FDI' and baseline crosses, I see them as Short-Term cycle changes.
- Negative slope 'FDI' means trending asset.
- Positive most of the times hints correction, but if it got overextended it might hint a rocket-shot.
TDI Ranges:
- 'FDI' between 1.0≤ 'FDI' ≤1.4 will confirm trend following continuation signals.
- 'FDI' between 1.6≥ 'FDI' ≥2.0 will confirm reversal signals.
- 'FDI' == 1.5 hints a random unpredictable market.
Fractal Attractors
- As you must know, fractals tend orbit certain spots, this are named Attractors, this happens with any fractal behavior. The market of course also shows them, in form of Support & Resistance, Supply Demand, etc. It’s obvious they are there, but now we understand that they’re not linear, as the market is fractal, so simple trendline might not be the best tool to model this.
- I’ve noticed that when the Musashi version of the 'FDI' indicator start making a cluster of multicolor dots, this end up being an attractor, I tend to draw a rectangle as that area as price tend to come back (I still researching here).
Extra useful stuff
- Momentum / speed: Included by checking RSI Study in the indicator properties. This will add two RSI’s (9 and a 7 periods) plus a baseline calculated same way as explained for 'FDI'. This gives accurate short-term trends. It also includes RSI divergences (regular and hidden), deactivate with a simple check in the RSI section of the properties.
- BBWP (Bollinger Bands with Percentile): Efficient way of visualizing volatility as the percentile of Bollinger bands expansion. This line varies color from Iced blue when low volatility and magma red when high. By default, comes with the High vols deactivated for better view of 'FDI' and RSI while all studies are included. DDWP is trend agnostic, just like 'FDI', which make it very clean at providing information.
- Ultra Slow 'FDI': I noticed that while using BBWP and RSI, the indicator gets overcrowded, so there is the possibility of adding only one 'FDI' + its baseline.
Final Note: I’ve shown you few ways of using this indicator, please backtest before using in real trading. As you know trading is more about risk and trade management than the strategy used. This still a work in progress, I really hope you find value out of it. I use it combination with a tool named “Musashi_Katana” (also found in TradingView).
Best!
Musashi
FunctionMinkowskiDistanceLibrary "FunctionMinkowskiDistance"
Method for Minkowski Distance,
The Minkowski distance or Minkowski metric is a metric in a normed vector space
which can be considered as a generalization of both the Euclidean distance and
the Manhattan distance.
It is named after the German mathematician Hermann Minkowski.
reference: en.wikipedia.org
double(point_ax, point_ay, point_bx, point_by, p_value) Minkowsky Distance for single points.
Parameters:
point_ax : float, x value of point a.
point_ay : float, y value of point a.
point_bx : float, x value of point b.
point_by : float, y value of point b.
p_value : float, p value, default=1.0(1: manhatan, 2: euclidean), does not support chebychev.
Returns: float
ndim(point_x, point_y, p_value) Minkowsky Distance for N dimensions.
Parameters:
point_x : float array, point x dimension attributes.
point_y : float array, point y dimension attributes.
p_value : float, p value, default=1.0(1: manhatan, 2: euclidean), does not support chebychev.
Returns: float
ArrayMultipleDimensionPrototypeLibrary "ArrayMultipleDimensionPrototype"
A prototype library for Multiple Dimensional array methods
index_md_to_1d()
new_float(dimensions, initial_size) Creates a variable size multiple dimension array.
Parameters:
dimensions : int array, dimensions of array.
initial_size : float, default=na, initial value of the array.
Returns: float array
dimensions(id, value) set value of a element in a multiple dimensions array.
Parameters:
id : float array, multiple dimensions array.
value : float, new value.
Returns: float.
get(id) get value of a multiple dimensions array.
Parameters:
id : float array, multiple dimensions array.
Returns: float.
set(id) set value of a element in a multiple dimensions array.
Parameters:
id : float array, multiple dimensions array.
Returns: float.
Fractal DimensionJohn F Ehlers' Fractal Dimension
Description:
Fractal Dimension is a measure of how complicated a self similar shape is. For instance, a line is smaller and more basic than a square.
The lower the Fractal Dimension the closer a stock chart is to a straight line and therefore the stronger the trend. High readings on the other hand reveal a complex fractal; the shape of a range bound market.
These two different market types require very different strategies in order to maximize profits and minimize losses.