Fibonacci Retracement Explained and Modelled in Python

James Peter Webb
JPA-Quant-Articles
Published in
3 min readMar 16, 2021

Technical analysis was widely frowned upon in industry, but with a resurgence in mathematical assets, is there a new opportunity for it’s come back?

The Fibonacci illustrated by the cross section of a pearl nautilus.
The Fibonacci sequence as seen by a cross section of a pearl nautilus.

The famous Fibonacci sequence is defined simply as ‘each element is the sum of the two previous elements, beginning: 0, 1’. This creates a sequence which begins:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 …

Introduction

The Fibonacci sequence is frequently observed in nature, mathematics, and many modern approximations derived from the closely related ‘golden ratio’. It can be observed in the natural arrangement of the leaves of many plants, and the family tree of a honey bee, and many mathematicians believe it also has applications to the price action of an asset — queue Fibonacci retracement.

This model of technical analysis uses the 3 ratios derived from the Fibonacci sequence, to establish hypothetical support and resistance lines of an asset. These ratios are: 0.618, 0.382, and 0.236, and are calculated by the interelement relationship within the series, of one element divided by each of it’s 3 preceding elements respectively.

The Code

I will be illustrating this concept with cryptocurrencies as they follow this trend format quite clearly, but this works for any asset who’s historical prices you have.

Code adapted from that of Computer Science YouTube

This function takes in a ticker (in my example it will take a currency-cryptocurrency pair, but I assure it works with all assets provided the relevant suffix is added for non-US exchanges), scrapes Yahoo Finance for data and packs it into a data frame.

Once done, you calculate your Fibonacci constants, these are then used to calculate each level of retracement. Once this function is called with a valid ticker input you will obtain something akin to the following.

This output shows a repetitive breakout and retracement for Bitcoin. December last year, price action rose to; and broke through, the initial resistance, setting a fourth level retracement at $23,650. Bull run continues and Bitcoin breaks $40,000, and then pulls back to it’s current 50% retracement at $30,800, cycle repeats forming a third and second levels at $38,000 and $46,900 respectively. Bitcoin currently sits at $51,150 at the time of publishing.

How is this information used?

As with all technical indicators, this information isn’t to blindly followed, but can offer insight into the potential ‘pseudo-randomness’ of price path, particularly for cyclical assets.

Many traders will use this as a buy indicator seeking assets that have pulled back to a retracement level, such as a stock pulling back 23.6%, a trader would long the stock as they would believe that the stock has bottomed out at a support line, and will retrace upwards. Similarly this information can be used as a sell indicator as a price drops below a support, a trader may believe it will retract to the next Fibonacci level, and the trader may short.

For instance, Very Boring Company (VBC, fictional asset) may be perceived to behave cyclically, trader Jim sees a pull back of 25%, and believes VBC won’t bottom out until it hit’s the next support at 38.2% total drop, so Jim assumes a short position, and closes out once this line is hit, then upon confirmation, reassumes an opposing position in VBC.

Reference:

The following article was a helpful reference for this project:

If you enjoyed this quick read or found any of this useful, feel free to reach out to me on LinkedIn

https://www.linkedin.com/in/jpawebb/

--

--

James Peter Webb
JPA-Quant-Articles

Analyst at CryptoCompare, MSc FinTech and Investment, BSc Chemistry: Follow my projects in quantitative analysis and Christian finance.