#### Discover more from All About Trading!

# 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.

Knowledge must be accessible to everyone. This is why, from now on, a purchase of either one of my new books *“ Contrarian Trading Strategies in Python” *or

*“*comes with free PDF copies of my first three books (Therefore, purchasing one of the new books gets you 4 books in total). The two new books listed above feature a lot of advanced indicators and strategies with a

**Trend Following Strategies in Python**”**GitHub**page. You can use the below link to purchase one of the two books (

*which one and make sure to include your*

**Please specify****Pay Kaabar using PayPal.Me***Go to paypal.me/sofienkaabar and type in the amount. Since it’s PayPal, it’s easy and secure. Don’t have a PayPal…*www.paypal.com

### 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.

Check out my weekly market sentiment report to understand the current positioning and to estimate the future direction of several major markets through complex and simple models working side by side. Find out more about the report through this link that covers the analysis between 07/08/2022 and 14/08/2022:

**Coalescence Report 7th August — 14th August 2022***THIS IS A FREE SAMPLE GIVEN TO FREE SUBSCRIBERS THIS WEEK. IF YOU WANT TO REGULARLY HAVE THIS REPORT, MAKE SURE TO…*coalescence.substack.com

### 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) / 13`

Let 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) / 13`

Now 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) / 13`

`plot(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) / 13
```

`plot(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 want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have *hands-on* detailed courses that I highly recommend.

**Learn Algorithmic Trading with Python Lumiwealth**

Learn how to create your own trading algorithms for stocks, options, crypto and more from the experts at Lumiwealth. Click to learn more

### Summary

To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.

I recommend you always follow the the below steps whenever you come across a trading technique or strategy:

*Have a critical mindset and get rid of any emotions.**Back-test it using real life simulation and conditions.**If you find potential, try optimizing it and running a forward test.**Always include transaction costs and any slippage simulation in your tests.**Always include risk management and position sizing in your tests.*

Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.