Fibonacci Sequence in Trading with Python (2024)

Fibonacci extensions and retracements in Python using financial trading data from EOD Historical Data (EODHD APIs) data API
The Fibonacci sequence is fascinating and a very popular topic for writers, mathematicians, and the lik

If you want to learn how to install the EODHD APIs Python Financial Official Library and activate your API key, we recommend to start with exploring of our Documentation for it.

At a high level, it is a series of numbers in which each number (the Fibonacci numbers) are the sum of the two preceding numbers. The series starts with 1, 1, 2, 3, 5, 8, 13, 21, etc.

As the numbers get larger, the quotient between each successive pair of Fibonacci numbers is very close to 1.618, or its inverse 0.618. This is most commonly referred to as the Golden Ratio.

The Golden Ratio very interestingly appears in some patterns in nature, including spiral arrangements of leaves, number of flower petals, the structure of tree branches, and other plant life. It is also used to analyse artificial systems such as financial markets, which is the main focus on this article.

Golden ratio in Fibonacci sequence: 8/13 = 0.618 (61.8%), 21/34 = 0.618 (61.8%), 34/55 = 0.618 (61.8%), 55/89 = 0.618 (61.8%), etc.
Fibonacci Sequence in Trading with Python (1)

Mathematicians started investigating other ratios which may be connected to the Fibonacci sequence.

Fibonacci Retracement and Extensions for Trading in Python
Fibonacci Sequence in Trading with Python (2)

Fibonacci retracement measures the percentage of how much of a correction may happen after a price movement.

The Fibonacci retracement levels are 23.6%, 38.2%, 50%, 61.8%, 78.6%, 100% with extension levels at 138.2%, 161.8%, 200%, and 261.8%.

If we take the Fibonacci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89; and divide 55/89 we get 0.618, divide 34/89 we get 0.382, divide 21/89 we get 0.236, etc. The square root of 0.618 is 0.786. While not officially a Fibonacci ratio, 50% is also used.

This indicator is useful because it can be drawn between any two significant price points, such as a high and a low in trading. On trading graphs they look like support and resistance levels.

Fibonacci extensions use similar percentages but for a different purpose. They are a way to predict a target price to sell or find potential areas of support or resistance when the price is moving into unchartered territory.

Calculating Fibonacci levels in Python for trading

We’re going to need to retrieve some trading data. I’ll be using my trusty financial data API provider EODHD APIs, previously EOD Historical Data. I have a paid for account as I use it often but there is a free tier if you want to use that instead and follow along.

I’ll be using Google Colab notebooks to demonstrate this. It’s free and easy to use. It will allow you to run Python code snippets without having to install anything locally.

I use Python a lot for trading data analysis. I wrote an article previously to help those get set up. It has instructions on how to use Google Colab, which we will be using here. If you want to do this locally on your desktop, it will work fine as well.

Let’s start by retrieving some data to work with…

I’m going to use the S&P 500 (GSPC.INDC or ^GSPC) interday index using the EODHD API’s Python library in Google Colab. This index is amazing to trade but data providers never offer it for free. If you want to access this data you will need a subscription. If you want to try it out for free they have a demo API key which allows you to access Apple’s stock data using the AAPL stock code.
Fibonacci Sequence in Trading with Python (3)

We now have 207 days of S&P 500 data stored in a Pandas dataframe called “df”. We can now graph it like this.
Fibonacci Sequence in Trading with Python (4)

In order to perform our Fibonacci retracement calculations, we’re going to need the following:

  • The highest price in the focus range
  • The lowest price in the focus range
  • If the market is in an uptrend or downtrend

To get the high and low is easy…
Fibonacci Sequence in Trading with Python (5)

The S&P 500 currently visually looks like it’s in a downtrend. If you want to be sure you can do something like this.
Fibonacci Sequence in Trading with Python (6)

I’ve added two simple moving averages to our dataset, SMA50 and SMA200. The SMA50 is the 50-day rolling average and the SMA200 is the 200-day rolling average. The SMA50 is currently below the SMA200 on the 21st of October 2022, which is indicating a long term downtrend.

The question we are trying to answer now is how far this downtrend will go and potential places it may improve. This is where Fibonacci retracement levels can be really useful for Trading in Python.

The calculations are as follows:

Uptrend Retracement = High - ((High - Low) * Percentage)Uptrend Extension = High + ((High - Low) * Percentage)Downtrend Retracement = Low + ((High - Low) * Percentage)Downtrend Extension = Low -((High - Low) * Percentage)

We will be using the “Downtrend Retracement” formula.

Low + ((High - Low) * Percentage)23.6% = 3577.03 + ((4796.5601 - 3577.03) * 0.236)23.6% = 3577.03 + (1219.5301 * 0.236)23.6% = 3577.03 + (1219.5301 * 0.236)23.6% = 3577.03 + 287.809103623.6% = 3864.8391036

We then repeat this process with the other Fibonacci percentages. To do this programmatically we can do this.
Fibonacci Sequence in Trading with Python (7)

For your convenience, here is the Google Colab code.

Low = df["adjusted_close"].min()High = df["adjusted_close"].max()Diff = High - LowFib100 = HighFib764 = Low + (Diff * 0.764)Fib618 = Low + (Diff * 0.618)Fib50 = Low + (Diff * 0.5)Fib382 = Low + (Diff * 0.382)Fib236 = Low + (Diff * 0.236)Fib0 = LowFib100, Fib764, Fib618, Fib50, Fib382, Fib236, Low

And this is how we plot it in Matplotlib.

plt.figure(figsize=(30,10))plt.plot(df["adjusted_close"], color="black", label="Price")plt.axhline(y=Fib100, color="limegreen", linestyle="-", label="100%")plt.axhline(y=Fib764, color="slateblue", linestyle="-", label="76.4%")plt.axhline(y=Fib618, color="mediumvioletred", linestyle="-", label="61.8%")plt.axhline(y=Fib50, color="gold", linestyle="-", label="50%")plt.axhline(y=Fib236, color="darkturquoise", linestyle="-", label="23.6%")plt.axhline(y=Fib0, color="lightcoral", linestyle="-", label="0%")plt.ylabel("Price")plt.xticks(rotation=90)plt.title("S&P 500 (^GSPC) Daily")plt.legend()plt.show()

Fibonacci Sequence in Trading with Python (8)

Fibonacci extensions would be calculated like this (for an uptrend).

Fib2618 = High + (Diff * 2.618)Fib2000 = High + (Diff * 2)Fib1618 = High + (Diff * 1.618)Fib1382 = High + (Diff * 1.382)Fib1000 = High + (Diff * 1)Fib618 = High + (Diff * 0.618)

The Pandas dataframe is showing 207 days of data, but maybe we are only interested in data from mid-August. The Fibonacci retracement calculation only needs a high and low value. We can truncate our data as follows.

df_subset = df.tail(48)plt.figure(figsize=(30,10))plt.plot(df_subset["adjusted_close"], color="black", label="Price")plt.ylabel("Price")plt.xticks(rotation=90)plt.title("S&P 500 (^GSPC) Daily")plt.legend()plt.show()

Fibonacci Sequence in Trading with Python (9)

We will calculate our new levels like this.

Low = df_subset["adjusted_close"].min()High = df_subset["adjusted_close"].max()Diff = High - LowFib100 = HighFib764 = Low + (Diff * 0.764)Fib618 = Low + (Diff * 0.618)Fib50 = Low + (Diff * 0.5)Fib382 = Low + (Diff * 0.382)Fib236 = Low + (Diff * 0.236)Fib0 = LowFib100, Fib764, Fib618, Fib50, Fib382, Fib236, Low

And plot our new data.

plt.figure(figsize=(30,10))plt.plot(df_subset["adjusted_close"], color="black", label="Price")plt.axhline(y=Fib100, color="limegreen", linestyle="-", label="100%")plt.axhline(y=Fib764, color="slateblue", linestyle="-", label="76.4%")plt.axhline(y=Fib618, color="mediumvioletred", linestyle="-", label="61.8%")plt.axhline(y=Fib50, color="gold", linestyle="-", label="50%")plt.axhline(y=Fib236, color="darkturquoise", linestyle="-", label="23.6%")plt.axhline(y=Fib0, color="lightcoral", linestyle="-", label="0%")plt.ylabel("Price")plt.xticks(rotation=90)plt.title("S&P 500 (^GSPC) Daily")plt.legend()plt.show()

Fibonacci Sequence in Trading with Python (10)

As you can see the Fibonacci retracement levels form a sort of support and resistance.

EODHD API’s Python Library

I pushed an update to the EODHD API’s Python library to make this functionality easily available to everyone. It’s available from version 1.0.8.

Retrieve our data as before, or any data you like.
Fibonacci Sequence in Trading with Python (11)

Two important points to mention here:

  1. I used the -U argument for my PIP install to make sure that my current “eodhd” library upgrades to the latest version
  2. I confirmed the version I’m using on Google Colab is 1.0.8

So now the new exciting part!

from eodhd import EODHDGraphsgraphs = EODHDGraphs()

We can how render all our graphs as follows….

graphs.fibonacci_retracement(df, "downtrend", "adjusted_close")

Fibonacci Sequence in Trading with Python (12)

graphs.fibonacci_retracement(df, "uptrend", "adjusted_close")

Fibonacci Sequence in Trading with Python (13)

graphs.fibonacci_extension(df, "uptrend", "adjusted_close")

Fibonacci Sequence in Trading with Python (14)

graphs.fibonacci_extension(df, "downtrend", "adjusted_close")

Fibonacci Sequence in Trading with Python (15)

By default, the graphs will display without saving the graph. I’ve added two optional arguments to both functions.

  • save_file=”fibonacci.png” ← default is “” which doesn’t save a file
  • quiet=True ← default is False which displays the graph

This is how you can save the graph without displaying it.

I hope you enjoyed this article. If you did, please consider following me for future articles and clapping for the article as that helps remunerates me for my efforts 🙂

Fibonacci Sequence in Trading with Python (2024)
Top Articles
Is Selling Feet Pics Illegal? Legal Insights and Boundaries
THE BEST WAY TO CHARGE AN ELECTRIC SCOOTER
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Terrorist Usually Avoid Tourist Locations
Tabc On The Fly Final Exam Answers
Retro Ride Teardrop
Flights to Miami (MIA)
CA Kapil 🇦🇪 Talreja Dubai on LinkedIn: #businessethics #audit #pwc #evergrande #talrejaandtalreja #businesssetup…
Carter Joseph Hopf
The Blind Showtimes Near Showcase Cinemas Springdale
Hmr Properties
Bfg Straap Dead Photo Graphic
2 Corinthians 6 Nlt
Vistatech Quadcopter Drone With Camera Reviews
3S Bivy Cover 2D Gen
U Arizona Phonebook
Craigslist Prescott Az Free Stuff
Cbssports Rankings
Heart Ring Worth Aj
Air Traffic Control Coolmathgames
Craigslist Apartments Baltimore
TeamNet | Agilio Software
Bra Size Calculator & Conversion Chart: Measure Bust & Convert Sizes
Wku Lpn To Rn
Weather October 15
Ncal Kaiser Online Pay
Big Boobs Indian Photos
Srjc.book Store
Hannah Jewell
Duke Energy Anderson Operations Center
Gideon Nicole Riddley Read Online Free
Gabrielle Enright Weight Loss
404-459-1280
Tendermeetup Login
Vanessa West Tripod Jeffrey Dahmer
SOC 100 ONL Syllabus
Property Skipper Bermuda
Sunrise Garden Beach Resort - Select Hurghada günstig buchen | billareisen.at
Sabrina Scharf Net Worth
Clausen's Car Wash
Emily Browning Fansite
Fairbanks Auto Repair - University Chevron
N33.Ultipro
Gt500 Forums
Kate Spade Outlet Altoona
Gander Mountain Mastercard Login
Bonecrusher Upgrade Rs3
The top 10 takeaways from the Harris-Trump presidential debate
Smoke From Street Outlaws Net Worth
Www Ventusky
Morbid Ash And Annie Drew
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6396

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.