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

Fibonacci Retracement Trading Strategy In Python? ›

Fibonacci Retracement and Extensions for Trading in Python

How to do Fibonacci sequence 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.

Is Fibonacci retracement a good 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 best combination with Fibonacci retracement? ›

Combining the Fibonacci Retracement with Bollinger Bands

Bollinger Bands is a powerful breakout indicator. If the price has crossed the upper or lower Bollinger Band at the same time when key Fibonacci Levels have been crossed, then the likelihood of a breakout is confirmed.

What is the best Fibonacci retracement setup? ›

Which Are the Best Fibonacci Retracement Settings? The most commonly-used Fibonacci retracement levels are at 23.6%, 38.2%, 61.8%, and 78.6%.

What does Fibonacci () do in Python? ›

The Fibonacci series in python is a mathematical sequence that starts with 0 and 1, with each subsequent number being the sum of the two preceding ones. In Python, generating the Fibonacci series is not only a classic programming exercise but also a great way to explore recursion and iterative solutions.

Do professional traders use Fibonacci? ›

Traders use Fibonacci retracement levels to identify potential support and resistance levels where an asset's price may find a floor or ceiling after a significant move up or down.

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.

What is the golden level of Fibonacci retracement? ›

The Fibonacci retracement levels are 23.6%, 38.2%, 61.8%, and 78.6%. While not officially a Fibonacci ratio, 50% is also used. The indicator is useful because it can be drawn between any two significant price points, such as a high and a low.

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.

Can I use Fibonacci retracement for scalping? ›

Trading against the trend might be risky for some, but because scalpers seek to trade small price movements in any direction, these Fibonacci retracement levels can make for an interesting Fibonacci scalping strategy.

How do you use Fibonacci retracement for beginners? ›

Finding Fibonacci Retracement Levels

In order to find these Fibonacci retracement levels, you have to find the recent significant Swing Highs and Swings Lows. Then, for downtrends, click on the Swing High and drag the cursor to the most recent Swing Low. For uptrends, do the opposite.

What are the best Fibonacci zones? ›

Which Fibonacci ratios are most commonly used for these time zones? The most common ratios are 23.6%, 38.2%, 50%, 61.8%, and 78.6%.

How do you write a Fibonacci sequence? ›

The sequence follows the rule that each number is equal to the sum of the preceding two numbers. The Fibonacci sequence begins with the following 14 integers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ...

How do you write a Fibonacci sequence code? ›

For example this code is the best recursive function code for fibonacci. Remember : recursive function take lot of time and memory , so don't input big numbers for it . def fibonacci(n): if n <= 1: return n else: return fibonacci( n-1)+fibonacci (n -2) x=int(input()) print(fibonacci(x)) This code gives us fibonacci .

What is the formula for Fibonacci sequence? ›

Fibonacci sequence, the sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21, …, each of which, after the second, is the sum of the two previous numbers; that is, the nth Fibonacci number Fn = Fn 1 + Fn 2.

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 5885

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.