Fibonacci Retracement Levels
Calculate the Fibonacci Retracement Levels for a given price series.
Fibonacci Retracement Levels are horizontal price levels, derived from ratios found in the Fibonacci sequence, that traders watch as potential support (during a pullback within an uptrend) or resistance (during a bounce within a downtrend) zones. For every date, the swing high and swing low are taken as the rolling maximum high and rolling minimum low over the specified window, and the retracement levels are derived from that high/low pair.
The formula is as follows:
\[\text{Uptrend (retracing down from the high):}\;\; \text{Level} = \text{High} - \text{Ratio} \cdot (\text{High} - \text{Low})\] \[\text{Downtrend (retracing up from the low):}\;\; \text{Level} = \text{Low} + \text{Ratio} \cdot (\text{High} - \text{Low})\]Also known as: Fibonacci retracement, Fib levels, retracement levels.
Fibonacci Retracement Levels in Python
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_fibonacci_retracement_levels as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(tickers=["AAPL", "MSFT"])
toolkit.technicals.get_fibonacci_retracement_levels().xs("AAPL", level=1, axis="columns")
Which returns:
Note that the columns sort lexicographically by their label (so “100.0%” sorts right after “0.0%”, ahead of “23.6%”), matching the sorting convention used by every other multi-column indicator in this module (e.g. Pivot Points’ Resistance/Support levels).
| Date | 0.0% | 100.0% | 23.6% | 38.2% | 50.0% | 61.8% | 78.6% |
|---|---|---|---|---|---|---|---|
| 2022-12-16 | 150.92 | 133.73 | 146.863 | 144.353 | 142.325 | 140.297 | 137.409 |
| 2022-12-19 | 150.92 | 131.32 | 146.294 | 143.433 | 141.12 | 138.807 | 135.514 |
| 2022-12-20 | 150.92 | 129.89 | 145.957 | 142.887 | 140.405 | 137.923 | 134.39 |
| 2022-12-21 | 150.92 | 129.89 | 145.957 | 142.887 | 140.405 | 137.923 | 134.39 |
| 2022-12-22 | 150.92 | 129.89 | 145.957 | 142.887 | 140.405 | 137.923 | 134.39 |
| 2022-12-23 | 149.97 | 129.64 | 145.172 | 142.204 | 139.805 | 137.406 | 133.991 |
| 2022-12-27 | 149.97 | 128.72 | 144.955 | 141.852 | 139.345 | 136.838 | 133.268 |
| 2022-12-28 | 149.97 | 125.87 | 144.282 | 140.764 | 137.92 | 135.076 | 131.027 |
| 2022-12-29 | 149.97 | 125.87 | 144.282 | 140.764 | 137.92 | 135.076 | 131.027 |
| 2022-12-30 | 149.97 | 125.87 | 144.282 | 140.764 | 137.92 | 135.076 | 131.027 |
Parameters
The function accepts the following parameters:
- period (str, optional): The time period to consider for historical data. Can be “daily”, “weekly”, “quarterly”, or “yearly”. Defaults to “daily”.
- close_column (str, optional): The column name for closing prices in the historical data. Defaults to “Adj Close”.
- window (int, optional): The number of periods over which the rolling swing high (maximum) and swing low (minimum) are determined. Defaults to 14.
- levels (list[float] | None, optional): The Fibonacci ratios to calculate levels for. Defaults to the standard [0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0].
- trend (str, optional): Whether to compute retracement levels for an “uptrend” (levels measured down from the high — the conventional direction, used when a prior move was up and price is now pulling back) or a “downtrend” (levels measured up from the low, used when a prior move was down and price is now bouncing). Defaults to “uptrend”.
- rounding (int | None, optional): The number of decimals to round the results to. Defaults to 4.
- growth (bool, optional): Whether to calculate the growth of the retracement levels. Defaults to False.
- lag (int | list[int], optional): The lag to use for the growth calculation.
- standardize (bool, optional): Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False. Defaults to 1.