TradingView
RedKTrader
١٣ أيار مايو ٢٠٢٠ ١٩:١٠

TA Basics: Creating a Fibonacci Weighted Moving Average 

Apple Inc.NASDAQ

الوصف

In the previous 2 posts in this series, we played around with simple math concepts to create a zero-lag moving average that can deliver fast response and less lag - that we can use to enable better trend following, or as filter / signal.

here we take a step further - instead of using equal weight for the moving average (as in the Simple moving average) or linear weights (as in the weighted moving average), we get to pick THE MAGIC SEQUENCE, Fibonacci.
we will use the Fibonacci Sequence as weights to produce our moving average - so practically, we create a "Fibonacci Weighted Moving Average" (let's call it FiMA) - and compare the result with other commonly-used moving averages of the same length

in a Fibonacci moving average, the data will be weighted based on the Fibonacci Series starting from 1 (for the furthest data point)
so for example, if we use a length of 10, the weights will be 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 with the 55 being the weight applied to the most recent / current bar's selected value (close, hl2, hc3..etc) and moving backward

before i posted this script, i searched around to see if someone else has already wrote this - i found a couple, but the approach we use here in this code is different - i can't claim it to be more efficient - i honestly don't know - but the resulting code here, IMHO, is more compact and easier to integrate in other studies that you may like to put together to leverage this idea, to create your own indicators and strategies.

the reason the code here is more compact, is that it utilizes a shorter formula to calculate the FIb(n) - i included the source where i found that formula, and i tested it before using it in the code.
i also added an optional "extra smoothing" for the resulting MA, by simply calling the fima() function a second time (so like doing a 2-pass filter), with a smaller length on the result of the 1st pass. keep this smoothing small not to produce too much lag.


i like the outcome when compared to other moving averages - it has a fast response to data/trend change and less overshoot - but honesty i didn't see any real "Fibonacci Magic" :) .. but i'll leave the final judgement to those who use it - this is more of an experimental code in all cases - please feel free to use, change and share feedback.

ملاحظات الأخبار

- no code change - just updated the commentary to explain the build up of the FiMA() function

ملاحظات الأخبار

- Just a quick update with the following notes

1. we removed the round() function from the calculation of the Fib Weights. it only makes a small difference until Fib(9) but it causes a side effect of the FiMA behaving erratically after length = 92 (this is where the function hits the maximum integer value in Pine)

2. thanks to feedback - yes, as the FiMA length increases, the Fib ratios become too large .. at around length = 15 to 20, the last 5 data points will always have ~91% of the sum of weights - any length changes after that will have no effect on the FiMA line plot. if this MA is used, it's suggested that it is used only for short lengths below 15 - otherwise, a weighted moving average will be a lot more efficient.
at the end, this was an experimental TA exercise - maybe someone gets inspired and makes something out of it :)

- thanks to those who provided feedback and helped diagnose the behavior with high value of lengths
التعليقات
Honestcowboy
Glad to have come accross your scripts. The compactness and simplicity is very inspiring. Love this idea always loved the fibonacci sequence
RedKTrader
@Honestcowboy, thank you - appreciate your feedback :)
RedKTrader
big thank you to @xel_arjona and @AtomOfScent for helping diagnose the FiMA behavior with large lengths and for helping get to a better v2 :)
tradearcher
Heya!

Nice Fibonacci calculation trick! I implemented one similarly, labeled fwma, around the same time for my Pandas TA Library. Just never got around to making a TV version. Like you, I also noticed like you that a longer length really makes little difference if any. XD
RedKTrader
@tradearcher, thanks for the notes.. i guess this ended up being more of a learning and exploring exercise .. there are other MA's that i worked on that proved to be more useful.
all the best to you.
tradearcher
@RedKTrader,

No doubt other MAs are "more" useful. Yeah it was mainly in jest and exploratory... a fun thought experiment.
Best of luck!
AtomOfScent
After being impressed with CRWMA I thought I'd see what other cool stuff you've made and wasn't disappointed. :)
I noticed something a bit peculiar about this one. There doesn't seem to be much change at all with lengths between 10-90 (though it's unpredictable). If you ever revisit this one and find a way to retain the concept with a more linear length response it would make it quite a bit more usable.

Example: Two instances with lengths 10 and 90 look the same while changing the length parameter.
imgur.com/cBRrUDX
RedKTrader
@AtomOfScent, thanks for your feedback and for your effort validating these scripts..

with Fib series as weight, as the length gets longer (starting from around 20), the weights become significantly high - example, Fib(10) = 55, but Fib(20) = 6,765 and Fib(30) = 832,040 --

you can see this clearly if you create a quick spreadsheet table and test with various length series and watch how the weights increase significantly - with such big weights, the weight will be concentrated at the last 5 points or so in the series (representing > 90% of the total weight) - and the resulting moving average wouldn't change as the length changes.. this is a major difference from other MAs - and the reason why this FibMA is not really practical and was more of an experimental / educational piece - rather than a tradeable tool

then i don't know what happens above length 90, and why does the MA starts changing again :) - maybe we break the TV limits :) as Fib(90) = 2.88007E+18

all the best,
AtomOfScent
@RedKTrader, thanks for the explanation. This was certainly a worthy experiment :)
RedKTrader
المزيد