# Formulas for a Fibonacci Weighted Moving Average

#### Jman831

##### Member
Hi there, I came up with the idea for a Fibonacci Weighted Moving Average, but I need help figuring out the math since I'll have to make it from scratch. Basically, it would work like a weighted moving average but with Fibonacci numbers instead of how the original weighted moving average works. For example, a 6 period weighted moving average would multiply the furthest period by 1, the second furthest by 2, the third furthest by 3 etc. until you get to the most recent period which is multiplied by 6. Then you'd divide the sum of those numbers by the sum of the weights which is 21 (1 + 2 + 3 + 4 + 5 + 6 = 21). I need a formula that would do the same thing but with Fibonacci numbers using only an input of the number of periods you'd like to use. So the furthest period would be multiplied by 1, the second furthest by 2, the third furthest by 3, the fourth furthest by 5, the fifth furthest by 8, etc. based on the number of periods you input. Any help from someone good with math who can come up with a thinkscript formula would be very helpful. Thanks in advance!

i made a study that calculates the nth term, in the Fibonacci sequence.
i think this could be put in a fold loop and do what you are looking for.

it might be a day or 2 till i get back to it, so posting what i made, in case someone else wants to work on it.

Thanks for posting! I think this would work with what I'm looking for but I'm not sure how to implement it. I'll leave you to it if/when you have time. And I'll be sure to give credit where credit is due if I repost the result.

If you could point me in a direction that would be of great help because I can't find it in the search.

Yeah I did and this isn't what I'm looking for. Lol. I'm pretty sure this combines Fibonacci length EMAs which I already have a similar indicator that does the same thing. Also that code I'm pretty sure is for TradingView not ToS. I'm looking for something where it weights based on Fibonacci numbers not a combination of Fibonacci length averages.

So unfortunately I couldn't figure out a way to utilize that code the way I need. It returns values for every bar on the chart. But, I made an 18 period Fibonacci Weighted Average with this code:

Code:
``````def close1 = close[17];
def close2 = close[16] * 2;
def close3 = close[15] * 3;
def close4 = close[14] * 5;
def close5 = close[13] * 8;
def close6 = close[12] * 13;
def close7 = close[11] * 21;
def close8 = close[10] * 34;
def close9 = close[9] * 55;
def close10 = close[8] * 89;
def close11 = close[7] * 144;
def close12 = close[6] * 233;
def close13 = close[5] * 377;
def close14 = close[4] * 610;
def close15 = close[3] * 987;
def close16 = close[2] * 1597;
def close17 = close[1] * 2584;
def close18 = close * 4181;
def avg = (close1 + close2 + close3 + close4 + close5 + close6 + close7 + close8 + close9 + close10 + close11 + close12 + close13 + close14 + close15 + close16 + close17 + close18) / 10944;

plot FibonacciWeightedAvg = avg;``````

This is essentially what I want it to do, but with this code the length isn't adjustable from the indicator settings. If I want to adjust the length I have to manually edit the code and add lines of code until I get the length I want. If there's a way, I'd like to develop a version that allows adjustable length so I can compare different lengths.

This is what the result looks like so far without any other indicators on:

this is from trading view i tried modifying but im lacking codes

//@version=4
study("Fibonacci Moving Average", overlay = true)hfib_ma_1 = ema(high, 5)
hfib_ma_2 = ema(high, 8)
hfib_ma_3 = ema(high, 13)
hfib_ma_4 = ema(high, 21)
hfib_ma_5 = ema(high, 34)
hfib_ma_6 = ema(high, 55)
hfib_ma_7 = ema(high, 89)
hfib_ma_8 = ema(high, 144)
hfib_ma_9 = ema(high, 233)
hfib_ma_10 = ema(high, 377)
hfib_ma_11 = ema(high, 610)
hfib_ma_12 = ema(high, 987)
hfib_ma_13 = ema(high, 1597)fib_high = (hfib_ma_13 + hfib_ma_12 + hfib_ma_11 + hfib_ma_10 + hfib_ma_9 + hfib_ma_8 + hfib_ma_7 + hfib_ma_6 + hfib_ma_5 + hfib_ma_4 + hfib_ma_3 + hfib_ma_2 + hfib_ma_1) / 13plot(fib_high, color = (fib_high < close) ? color.blue : color.orange)lfib_ma_1 = ema(low, 5)
lfib_ma_2 = ema(low, 8)
lfib_ma_3 = ema(low, 13)
lfib_ma_4 = ema(low, 21)
lfib_ma_5 = ema(low, 34)
lfib_ma_6 = ema(low, 55)
lfib_ma_7 = ema(low, 89)
lfib_ma_8 = ema(low, 144)
lfib_ma_9 = ema(low, 233)
lfib_ma_10 = ema(low, 377)
lfib_ma_11 = ema(low, 610)
lfib_ma_12 = ema(low, 987)
lfib_ma_13 = ema(low, 1597)fib_low = (lfib_ma_13 + lfib_ma_12 + lfib_ma_11 + lfib_ma_10 + lfib_ma_9 + lfib_ma_8 + lfib_ma_7 + lfib_ma_6 + lfib_ma_5 + lfib_ma_4 + lfib_ma_3 + lfib_ma_2 + lfib_ma_1) / 13plot(fib_low, color = (fib_low < close) ? color.blue : color.orange)

@Bidtheoffer this is from trading view i tried modifying but im lacking codes

Here is a possible TOS conversion

The image is what was used by the Tradingview author

Ruby:
``````#Fibonacci Moving Average
#Sofien-Kaabar
#Oct 21, 2021
#//@version=4
#The Fibonacci Moving Average is a powerful indicator that takes into account many underlying moving averages to give out an approximate short-term/long-term view of the markets. Its strength lies with dynamic support and resistance levels. I have created this indicator in order to improve trend-following entry positions.

#study("Fibonacci Moving Average", overlay = true)

script ema {
input price = high;
input length = 5;
plot emaavg = expaverage(price, length);
}
def hfib_ma_1 = ema(high, 5);
def hfib_ma_2 = ema(high, 8);
def hfib_ma_3 = ema(high, 13);
def hfib_ma_4 = ema(high, 21);
def hfib_ma_5 = ema(high, 34);
def hfib_ma_6 = ema(high, 55);
def hfib_ma_7 = ema(high, 89);
def hfib_ma_8 = ema(high, 144);
def hfib_ma_9 = ema(high, 233);
def hfib_ma_10 = ema(high, 377);
def hfib_ma_11 = ema(high, 610);
def hfib_ma_12 = ema(high, 987);
def hfib_ma_13 = ema(high, 1597);
plot fib_high = (hfib_ma_13 + hfib_ma_12 + hfib_ma_11 + hfib_ma_10 + hfib_ma_9 + hfib_ma_8 + hfib_ma_7 + hfib_ma_6 + hfib_ma_5 + hfib_ma_4 + hfib_ma_3 + hfib_ma_2 + hfib_ma_1) / 13;
#, color = (fib_high < close) ? color.blue : color.orange)
def lfib_ma_1 = ema(low, 5);
def lfib_ma_2 = ema(low, 8);
def lfib_ma_3 = ema(low, 13);
def lfib_ma_4 = ema(low, 21);
def lfib_ma_5 = ema(low, 34);
def lfib_ma_6 = ema(low, 55);
def lfib_ma_7 = ema(low, 89);
def lfib_ma_8 = ema(low, 144);
def lfib_ma_9 = ema(low, 233);
def lfib_ma_10 = ema(low, 377);
def lfib_ma_11 = ema(low, 610);
def lfib_ma_12 = ema(low, 987);
def lfib_ma_13 = ema(low, 1597);
plot fib_low = (lfib_ma_13 + lfib_ma_12 + lfib_ma_11 + lfib_ma_10 + lfib_ma_9 + lfib_ma_8 + lfib_ma_7 + lfib_ma_6 + lfib_ma_5 + lfib_ma_4 + lfib_ma_3 + lfib_ma_2 + lfib_ma_1) / 13;
#color = (fib_low < close) ? color.blue : color.orange)``````

87k+ Posts
305 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?