📅  最后修改于: 2023-12-03 15:29:37.177000             🧑  作者: Mango
Bill Williams Fractals is a trading indicator designed by Bill Williams in his book "Trading Chaos". The indicator uses fractals to identify significant market reversal points.
In this tutorial, we will be using Python and the Pandas library to create a Bill Williams Fractals indicator for trading analysis.
First, let's import the necessary libraries:
import pandas as pd
import matplotlib.pyplot as plt
Next, let's download some historical data for the stock we want to analyze. For this tutorial, we will be using Apple Inc (AAPL) historical data from Yahoo Finance.
data = pd.read_csv("https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1262322000&period2=1630966800&interval=1d&events=history&includeAdjustedClose=true")
In order to calculate the fractals, we need to identify certain price patterns in the data. The pattern we are looking for is a series of at least five consecutive bars, where the highest high is in the middle, and the two bars on either side have lower highs. The same must be true for lows.
First, let's identify the highs and lows:
data["High Fractal"] = data["High"].rolling(5).apply(lambda x: x[2] == max(x), raw=True)
data["Low Fractal"] = data["Low"].rolling(5).apply(lambda x: x[2] == min(x), raw=True)
Next, let's filter out the false positives:
data.loc[(data["High Fractal"] == True) & (data["High Fractal"].shift(1) == False) & (data["High Fractal"].shift(2) == False) & (data["High Fractal"].shift(3) == False) & (data["High Fractal"].shift(4) == False), "High Fractal"] = True
data.loc[(data["Low Fractal"] == True) & (data["Low Fractal"].shift(1) == False) & (data["Low Fractal"].shift(2) == False) & (data["Low Fractal"].shift(3) == False) & (data["Low Fractal"].shift(4) == False), "Low Fractal"] = True
Finally, let's visualize the fractals on a graph:
plt.plot(data["Close"])
plt.plot(data.loc[data["High Fractal"] == True]["High"], marker="w", linestyle="")
plt.plot(data.loc[data["Low Fractal"] == True]["Low"], marker="o", linestyle="")
plt.show()
In this tutorial, we learned how to calculate Bill Williams Fractals using Python and the Pandas library. We also visualized the fractals on a graph. This indicator can be useful in identifying significant market reversal points for trading analysis.