Fibonacci Retracement Trading Strategy In Python (2024)

Mathematics and Econometrics This Blog

Mathematics and Econometrics

5 min read

ByIshan Shah

Fibonacci trading tools are used for determining support/resistance levels or to identify price targets. It is the presence of Fibonacci series in nature which attracted technical analysts’ attention to use Fibonacci for trading. Fibonacci numbers work like magic in finding key levels in any widely traded security. In this article, I will explain one of the famous Fibonacci trading strategy: retracement to identify support level.

Fibonacci sequence

Fibonacci sequence is a series of numbers, starting with zero and one, in which each number is the sum of the previous two numbers.

Fibonacci Retracement Trading Strategy In Python (1)

The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610……It extends to infinity and can be summarized using below formula:

Xn = Xn-1 + Xn-2

What are some interesting facts about the Fibonacci sequence?

There are some interesting properties of the Fibonacci sequence. Divide any number in the sequence by the previous number; the ratio is always approximately 1.618.

Xn/Xn-1 = 1.61855/34 = 1.61889/55 = 1.618144/89 = 1.618

1.618 is known as the golden ratio. I would suggest searching for the golden ratio examples on the Google images and you will be pleasantly astonished by the relevance of the ratio in nature.

Similarly, divide any number in the sequence by the next number; the ratio is always approximately 0.618.

Xn/Xn+1 = 0.61834/55 = 0.61855/89 = 0.61889/144 = 0.618

0.618 expressed in percentage is 61.8%.The square root of 0.618 is 0.786 (78.6%).

Similar consistency is found when any number in the sequence is divided by a number two places right to it.

Xn/Xn+2 = 0.38213/34 = 0.38221/55 = 0.38234/89 = 0.382

0.382 expressed in percentage is 38.2%

Also, there is consistency when any number in the sequence is divided by a number three places right to it.

Xn/Xn+3 = 0.23621/89 = 0.23634/144 = 0.23655/233 = 0.236

0.236 expressed in percentage terms is 23.6%.

The ratios 23.6%, 38.2%, 61.8%, and 78.6% are known as the Fibonacci ratios.

Fibonacci retracement trading strategy

The Fibonacci ratios, 23.6%, 38.2%, and 61.8%, can be applied for time series analysis to find support level. Whenever the price moves substantially upwards or downwards, it usually tends to retrace back before it continues to move in the original direction. For example, if the stock price has moved from $200 to $250, then it is likely to retrace back to $230 before it continues to move upward. The retracement level of $230 is forecasted using the Fibonacci ratios.

Fibonacci Retracement Trading Strategy In Python (2)

We can arrive at $230 by using a simple mathTotal up move = $250 - $200 = $5038.2% of up move = 38.2% * 50 = $19.1Retracement forecast = $250 - $19.1 = $230.9

Any price level below $230 provides a good opportunity for the traders to enter into new positions in the direction of the trend. Likewise, we can calculate for 23.6%, 61.8% and the other Fibonacci ratios.

How to find Fibonacci retracement levels?

As we now know, retracements are the price movements that go against the original trend. To forecast the Fibonacci retracement level we should first identify the total up move or total down move. To mark the move, we need to pick the most recent high and low on the chart.

Let’s take an example of Exxon Mobil to understand the Fibonacci retracement construction

# To import stock pricesfrom pandas_datareader import data as pdr# To plotimport matplotlib.pyplot as pltdf = pdr.get_data_google('XOM','2017-08-01', '2017-12-31')fig, ax = plt.subplots()ax.plot(df.Close, color='black')

Fibonacci Retracement Trading Strategy In Python (3)

As seen from the graph, there is substantial upward movement in the price from September 2017 to end of October 2017. In this case, the minimum price is $76 and maximum price is $84. The $8 is the total up move.

price_min = 76 #df.Close.min()
price_max = 84 #df.Close.max()

The retracement levels for Fibonacci ratios of 23.6%, 38.2% and 61.8% are calculated as follows:

# Fibonacci Levels considering original trend as upward movediff = price_max - price_minlevel1 = price_max - 0.236 * difflevel2 = price_max - 0.382 * difflevel3 = price_max - 0.618 * diffprint "Level", "Price"print "0 ", price_maxprint "0.236", level1print "0.382", level2print "0.618", level3print "1 ", price_minax.axhspan(level1, price_min, alpha=0.4, color='lightsalmon')ax.axhspan(level2, level1, alpha=0.5, color='palegoldenrod')ax.axhspan(level3, level2, alpha=0.5, color='palegreen')ax.axhspan(price_max, level3, alpha=0.5, color='powderblue')plt.ylabel("Price")plt.xlabel("Date")plt.legend(loc=2)plt.show()

Output:Fibonacci Retracement Trading Strategy In Python (4)

Fibonacci Retracement Trading Strategy In Python (5)

The first retracement level at 23.6% is $82.10, the second retracement level at 38.6% is $80.90, and the next retracement level at 61.8% is $79.05. During mid-November, the Exxon Mobil stock price went down to $80.40, (falling below 38.6% retracement level) and then continued the upward movement.

How to use the Fibonacci retracement trading strategy?

The retracement levels can be used in a situation where you wanted to buy a particular stock but you have not been able to because of a sharp run-up in the stock price. In such a situation, wait for the price to correct to Fibonacci retracement levels such as 23.6%, 38.2%, and 61.8% and then buy the stock.The ratios 38.2% and 61.8% are the most important support levels.

This Fibonacci retracement trading strategy is more effective over a longer time interval and like any indicator, using the strategy with other technical indicators such as RSI, MACD, and candlestick patterns can improve the probability of success.

Good luck with Fibonacci trading :)

Next Step

There are many sources for getting data from various cryptocurrencies available on the net. In our post 'Cryptocurrencies Trading Strategy With Data Extraction Technique' learn the use of python library coinmarketcap to fetch data. To learn more Quantitative trading strategies, you can go through the

Quantitative Trading Strategies and Models course

.

Disclaimer:All investments and trading in the stock market involve risk. Any decisions to place trades in the financial markets, including trading in stock or options or other financial instruments is a personal decision that should only be made after thorough research, including a personal risk and financial assessment and the engagement of professional assistance to the extent you believe necessary. The trading strategies or related information mentioned in this article is for informational purposes only.

Download Python code

  • Fibonacci Retracement Trading Strategy Python Code

Login to Download

 

Mega Quant Sale 2024

Fibonacci Retracement Trading Strategy In Python (2024)

FAQs

How to calculate Fibonacci retracement in Python? ›

Initial Concept: The Basic Fibonacci Retracement Function

Our journey began with a simple Python function to calculate Fibonacci retracement levels. The basic premise was straightforward — input the high and low prices of a stock, and the function computes the key Fibonacci levels: 23.6%, 38.2%, 50%, 61.8%, and 100%.

Do professional traders use Fibonacci retracement? ›

Every foreign exchange trader will use Fibonacci retracements at some point in their trading career. Some will use them just some of the time, while others will apply them regularly.

Is Fibonacci a good trading strategy? ›

That said, many traders find success using Fibonacci ratios and retracements to place transactions within long-term price trends. Fibonacci retracement can become even more powerful when used in conjunction with other indicators or technical signals.

What is the most effective Fibonacci retracement level? ›

The most popular (or commonly watched) Fibonacci Retracements are 61.8% and 38.2%. Sometimes these percentages are rounded to 62% and 38%, respectively.

How do you solve Fibonacci series in Python? ›

The simplest way to print Fibonacci numbers is using a while loop in Python. We initialize two variables, a and b, with 0 and 1,, representing the series' starting numbers. Inside the while loop, we print the current term and update the variables by adding them. This continues recursively to generate the sequence.

How do you predict Fibonacci retracement? ›

To predict market reversals using Fibonacci retracement levels, you can analyze potential bounce-back or breakout points after a correction. In an uptrend, if the price retraces to the 38.2% level, it suggests a potential continuation of the upward momentum, making it a plausible entry or stop-loss point.

Which timeframe is best for Fibonacci retracement? ›

What time frame is best for Fibonacci retracement? The 30-60 minute candlestick chart is best suited to analyse the Fibonacci retracements to watch the daily market swings closely.

What is the golden ratio in the Fibonacci sequence? ›

The golden ratio, also known as the golden number, golden proportion, or the divine proportion, is a ratio between two numbers that equals approximately 1.618. Usually written as the Greek letter phi, it is strongly associated with the Fibonacci sequence, a series of numbers wherein each number is added to the last.

How do you master Fibonacci retracement? ›

We can create Fibonacci retracements by taking a peak and trough (or two extreme points) on a chart and dividing the vertical distance by the above key Fibonacci ratios. Once these trading patterns​ are identified, horizontal lines can be drawn and then used to identify possible support and resistance levels.

What are the best Fibonacci levels to take profit? ›

The most commonly used Fibonacci extension levels are 138.2 and 161.8. The rules for take profit orders are very individual, but most traders use it as follows: A 50, 61.8 or 78.6 retracement will often go to the 161 Fibonacci extension after breaking through the 0%-level.

Can you use Fibonacci for day trading? ›

Tips for Using Fibonacci Retracements in Day Trading

Day traders who use this tool typically start by looking at the numbers on a long term chart first. Draw the lines on a weekly chart to establish key levels. Then, go to the daily chart to look at retracement levels and only then go to intraday retracement levels.

What is the golden Fibonacci level? ›

Needless to say, the series extends to infinity. There are few interesting properties of the Fibonacci series. Divide any number in the series by the previous number; the ratio is always approximately 1.618. The ratio of 1.618 is considered as the Golden Ratio, also referred to as the Phi.

What is the best zone for Fibonacci retracement? ›

Fibonacci retracement lines can be created when you divide the vertical distance between the high and low points by the key Fibonacci ratios. Horizontal lines are drawn on the trading chart​ at the 23.6%, 38.2% and 61.8% retracement levels. Some traders also like to use the 50.0% ratio.

What is 100% Fibonacci retracement? ›

A Fibonacci retracement forecast is created by taking two extreme points on a chart and dividing the vertical distance by Fibonacci ratios. 0% is considered to be the start of the retracement, while 100% is a complete reversal to the original price before the move.

How do you draw accurate Fibonacci retracement? ›

Drawing Fibonacci retracements in a downtrend

To plot Fibonacci retracement levels in a downtrend in a down trending market condition, you need to select the most recent significant swing high and low points. Start with the swing high point, and then drag the cursor down to the swing low point.

How to calculate Fibonacci retracement? ›

How do you calculate Fibonacci retracement levels? UR = Low price + ((High price - Low price) × percentage) in a downtrend market, percentage can be any of the Fibonacci retracement levels: 23.6%, 38.2%, 50%, 61.8%, 76.4%, 100%, 138.2%, or 161.8%.

How do you pull Fibonacci retracement? ›

We can create Fibonacci retracements by taking a peak and trough (or two extreme points) on a chart and dividing the vertical distance by the above key Fibonacci ratios. Once these trading patterns​ are identified, horizontal lines can be drawn and then used to identify possible support and resistance levels.

How do you calculate 61.8 retracement? ›

The key Fibonacci ratio of 61.8% is found by dividing one number in the series by the number that follows it. For example, 21 divided by 34 equals 0.6176, and 55 divided by 89 equals about 0.61798. The 38.2% ratio is discovered by dividing a number in the series by the number located two spots to the right.

How do you check if a number is in the Fibonacci sequence Python? ›

To check if the given number is a Fibonacci number in Python, use the following property, i.e., A number is Fibonacci if and only if 5n^2 + 4 or 5n^2 – 4 is a perfect square.

Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 6351

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.