Coding the Fibonacci Moving Average in TradingView.
How to Code & Use the Fibonacci Moving Average in Pine Script.

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. Having already discussed how to code it in Python, it is now time to code in in TradingView using their in-house language, Pine Script.
I have just published a new book after the success of my previous one “New Technical Indicators in Python”. It features a more complete description and addition of structured trading strategies with a GitHub page dedicated to the continuously updated code. If you feel that this interests you, feel free to visit the below Amazon link, or if you prefer to buy the PDF version, you could contact me on LinkedIn.
The Fibonacci Moving Average — Intuition
The Fibonacci sequence is an amazing series that keeps impressing us everyday. It is applied almost everywhere with mathematically beautiful properties. In trading and technical analysis, the sequence is often used in trying to predict where will the market react. It can also serve as inputs in the parameters of some indicators.
We will use the sequence to create the Fibonacci Moving Average, my favorite reactionary overlay indicator. We need to first understand the concept of moving averages.
Moving averages help us confirm and ride the trend. They are the most known technical indicator and this is because of their simplicity and their proven track record of adding value to the analyses. We can use them to find support and resistance levels, stops and targets, and to understand the underlying trend. This versatility makes them an indispensable tool in our trading arsenal.

Another even more dynamic moving average is the exponential one. Its idea is to give more weight to the more recent values so that it reduces the lag between the price and the average.

Notice how the exponential moving average is closer to prices than the simple one when the trend is strong. This is because it gives more weight to the latest values so that the average does not stay very far.
Now that we have understood the Exponential Moving Average, we will proceed to create the Fibonacci Moving Average.
If you want to support me and the articles I regularly publish, please consider subscribing to my DAILY Newsletter (A Free Plan is Available) via the below link. It features my Medium articles, more trading strategies, coding lessons related to research and analysis and subscribers get a free PDF copy of my book. You can expect 5–7 articles per week with your subscription. This would help me continue sharing my research. Thank you!
Coding the Fibonacci Moving Average Step-By-Step
We want to create the FMA on Solana values loaded on the TradingView platform. Note that you must create an account to be able to view the charts, the good news is that it is free. Locate the Chart button on the home screen and then choose any asset you would like to calculate the indicator on. Now, on the bottom of the screen, locate Pine Editor and warm up your fingers to do some coding.

The first step is to specify the version of Pine Script. In our case it is 4. We must set the overlay argument to true because we want to chart the moving average in the same panel as the price. Setting it to false will put the moving average in another panel which makes it difficult to analyze.
//@version=4
study("Fibonacci Moving Average", overlay = true)Now, following the Fibonacci sequence, we can take the first 13 values as lookback periods. Note that we can actually tweak the indicator by adding and removing lookback periods. For this study, we will be using 13 lookback periods using this Fibonacci list: {5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597}. Remember, we will be using the exponential moving average for this step, therefore, we use the ema() function applied on the highs.
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)Then, we will take the simple average of the thirteen exponential moving averages we have calculated. This will give us the first Fibonacci moving average applied on the highs.
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) / 13Let us redo the whole thing using the lows instead of the highs. After all, we are making a zone.
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)The last step is to calculate the thirteen exponential moving averages applied on the lows so that we obtain the last piece of the puzzle.
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) / 13Now that we have calculated the Fibonacci moving average, let us plot it. We can also use a cool color trick which outputs a blue moving average zone if the market price is above the moving average and an orange moving average if the market price is below it. For this, we use the conditional “?” which states that if the previous condition is true, then do the following unless do this other thing (After the two dots).
plot(fib_high, color = (fib_high < close) ? color.blue:color.orange)
plot(fib_low, color = (fib_low < close) ? color.blue:color.orange)
Et voilà, the FMA is ready for analysis. This will form a series of indicators and strategies created in Pine Script in the future where the complexity rises in each article. Make sure to read the documentation. Meanwhile, here is the full code. You can check out my profile in TradingView for other indicators.
//@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)As stated above, I also use another Fibonacci Moving Average which uses the following sequence: {2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181}. It is up to the trader and her preferences.
If you are also interested by more technical indicators and using Python to create strategies, then my best-selling book on Technical Indicators may interest you:
Conclusion
Remember to always do your back-tests. You should always believe that other people are wrong. My indicators and style of trading may work for me but maybe not for you.
I am a firm believer of not spoon-feeding. I have learnt by doing and not by copying. You should get the idea, the function, the intuition, the conditions of the strategy, and then elaborate (an even better) one yourself so that you back-test and improve it before deciding to take it live or to eliminate it. My choice of not providing specific Back-testing results should lead the reader to explore more herself the strategy and work on it more.
Medium is a hub to many interesting reads. I read a lot of articles before I decided to start writing. Consider joining Medium using my referral link!
To sum up, are the strategies I provide realistic? Yes, but only by optimizing the environment (robust algorithm, low costs, honest broker, proper risk management, and order management). Are the strategies provided only for the sole use of trading? No, it is to stimulate brainstorming and getting more trading ideas as we are all sick of hearing about an oversold RSI as a reason to go short or a resistance being surpassed as a reason to go long. I am trying to introduce a new field called Objective Technical Analysis where we use hard data to judge our techniques rather than rely on outdated classical methods.
One Last Word
I have recently started an NFT collection that aims to support different humanitarian and medical causes. The Society of Light is a set of limited collectibles which will help make the world slightly better as each sale will see a percentage of it sent directly to the charity attributed to the avatar. As I always say, nothing better than a bullet list to outline the benefits of buying these NFT’s:
- High-potential gain: By concentrating the remaining sales proceedings on marketing and promoting The Society of Light, I am aiming to maximize their value as much as possible in the secondary market. Remember that trading in the secondary market also means that a portion of royalties will be donated to the same charity.
- Art collection and portfolio diversification: Having a collection of avatars that symbolize good deeds is truly satisfying. Investing does not need to only have selfish needs even though there is nothing wrong with investing to make money. But what about investing to make money, help others, and collect art?
- Donating to your preferred cause(s): This is a flexible way of allocating different funds to your charities.
- A free copy of my book in PDF: Any buyer of any NFT will receive a free copy of my latest book shown in the link of the article.
