RedK Compound Ratio Moving Average (CoRa_Wave)

1063 vizualizações

Compound Ratio Weighted Average (CoRa_Wave) is a moving average where the weights increase in a "logarithmically linear" way - from the furthest point in the data to the current point - the formula to calculate these weights work in a similar way to how "compound ratio" works - you start with an initial amount, then add a consistent "ratio of the cumulative prior sum" each period until you reach the end amount. The result is, the "step ratio" between the weights is consistent - This is not the case with linear-weights moving average ( WMA ), or EMA

- for example, if you consider a Weighted Moving Average ( WMA ) of length 5, the weights will be (from the furthest point towards the most current) 1, 2, 3, 4, 5 -- we can see that the ratio between these weights are inconsistent. in fact, the ratio between the 2 furthest points is 2:1, but the ratio between the most recent points is 5:4 -- the ratio is inconsistent, and in fact, more recent points are not getting the best weights they should/can get to counter-act the lag effect. Using the Compound ratio approach addresses that point.

a key advantage here is that we can significantly reduce the "tail weight" - which is "relatively" large in other MAs and would be main cause for lag - giving more weights to the most recent data points - and in a way that is consistent, reliable and easy to "code"

- the outcome is, a moving average line that suffers very little lag regardless of the length, and that can be relied on to track the price movements and swings closely.

other features:
- An accelerator, or multiplier, has been added to further increase the "aggressiveness" of the moving average line, giving even more weights to the more recent points - the multiplier will have more effect between 1 and 5, then will have a diminishing effect after that - note that a multiplier of 0 (which effectively causes a comp. ratio of 0 to be applied) will produce a Simple Moving Average line :)

- We also added the ability to use an "automatic smoothing" mechanism, that user can over-ride by manually choosing how much smoothing is used. This gives more flexibility to how we can leverage this Moving Average in our charting.

- User can also select the Resolution and Source price for the CoRa_Wave. by default, they will be set to "same as chart" and hlc3

here are the formulas for our Compound Ratio moving average:
Compound Weight ratio    r = (A/P)^1/t - 1
Weight at time t         A = P(1 + r)^t 
                           = Start_val * (1 + r) ^ index
index in the above formula is 0 for the furthest point out

Here's how CoRa_Wave compares to other common moving averages all set to the same length (20)

Proposed Usage
- CoRa_Wave can be used for any scenarios where we need a moving average that closely tracks the price, trend, swings with high responsiveness and little lag
- MA Cross-over scenarios - against another CoRa_Wave or any other MA
- below is a quick example scenario for how to utilize 2 CoRa_Wave lines of same length (one for open and one for closing price) to track swings and trends
- get as creative as you need :)

Code is commented - please feel free to leverage or customize further as you need.

👉 if you are interested in other moving averages i posted before, please check out the FiMA and the v_Wave ...
Script de código aberto

No verdadeiro espírito TradingView, o autor deste script o publicou com código aberto, para que os traders possam compreendê-lo e verificá-lo. Um brinde ao autor! Você pode usá-lo gratuitamente, mas a reutilização deste código em uma publicação é regida pelas Regras da Casa. Você pode favoritá-lo para utilizá-lo em um gráfico.

Quer usar esse script no gráfico?


500 moedas
RedKTrader PineCoders
@PineCoders, thank you!
This is a great moving average! I compared it against them all. I use the JMA as a reference for fast/low lag and it was pretty much a toss up (JMA in green). I also tried changing the wma to other types like hma or vwma, but there wasn't really much difference that couldn't be compensated for with the start weighting or comp multiplier.

By the way, here's a function for the whole thing (auto smoothing) for anyone looking to integrate it into other scripts:

crwma(source, length, Start_Wt, r_multi) =>
numerator = 0.0, denom = 0.0, c_weight = 0.0, s = 0 , crwma =0.0
//Start_Wt = 1.0 // Start Wight is an input in this version - can also be set to a basic value here.
End_Wt = length // use length as initial End Weight to calculate base "r"
s := max(round(sqrt(length)),1)
r = pow((End_Wt / Start_Wt),(1 / (length - 1))) - 1
base = 1 + r * r_multi
for i = 0 to length -1
c_weight := Start_Wt * pow(base,(length - i))
numerator := numerator + source * c_weight
denom := denom + c_weight
crwma := wma(int(numerator / denom), s)
RedKTrader AtomOfScent
@AtomOfScent, great ... thank you :)

i suggest you don't to use int() in the last line. that will change the moving average calculation and impact the smoothing.
+1 Resposta
AtomOfScent RedKTrader
@RedKTrader, thanks for the heads up, I double-checked an the result seems the same, but without int it breaks for some reason.

original (white, transp=50), function with int (black, transp=50), function w/o int (green)
RedKTrader AtomOfScent
@AtomOfScent, something is fishy here.. plot your code again on something with a smaller price range (<50) and you'll see what i mean.

i tried having both (aqua with int and white is with no int) and here's my result - mine didn't produce any error in either cases.
AtomOfScent RedKTrader
@RedKTrader, actually after posting that both functions displayed broken on symbols with a low price (i.e. Cardano), while working fine on Bitcoin etc.
cora_line = wma(f_adj_crwma(data, length, 0.01, r_multi), max(round(sqrt(length)),1)) works fine so hard to say what the issue is. 🤷🏻‍♂️
RedKTrader AtomOfScent
@AtomOfScent, cross posts :)

pls DM me if you need me to share my version with you .
Good thing to explore more, thanks for sharing knowledge..
Início Rastreador de Fundamentos Rastreador Forex Rastreador de Criptomoedas Calendário Econômico Sobre Recursos gráficos Preços Indique um Amigo Regras da Casa Central de Ajuda Soluções para Sites & Corretoras Widgets Soluções em Gráficos Biblioteca de Gráficos Lightweight Blog & Notícias Twitter