ZigZag Indicator in Python
Here is an example of a zigzag indicator with buy and sell arrow plotting in Python that takes OHLC (Open-High-Low-Close) data:
import matplotlib.pyplot as plt
def zigzag(data, threshold):
# initialize variables
zigzag = []
last_high = data[0][2]
last_low = data[0][3]
# loop through the data
for i in range(1, len(data)):
# get the current bar's high and low
current_high = data[i][2]
current_low = data[i][3]
# check if the current high is greater than the last high
if current_high > last_high + threshold:
# add the current high to the zigzag
zigzag.append(current_high)
last_high = current_high
last_low = current_low
# check if the current low is lower than the last low
elif current_low < last_low - threshold:
# add the current low to the zigzag
zigzag.append(current_low)
last_high = current_high
last_low = current_low
return zigzag
def plot_zigzag(data, zigzag):
# initialize variables
buy_arrow = []
sell_arrow = []
# loop through the zigzag points
for i in range(1, len(zigzag)):
# check if the zigzag is going up
if zigzag[i] > zigzag[i-1]:
# add a buy arrow at the current zigzag point
buy_arrow.append(zigzag[i])
# check if the zigzag is going down
elif zigzag[i] < zigzag[i-1]:
# add a sell arrow at the current zigzag point
sell_arrow.append(zigzag[i])
# plot the zigzag points and arrows
plt.plot(zigzag, 'o', color='red')
plt.plot(buy_arrow, '^', color='green')
plt.plot(sell_arrow, 'v', color='red')
plt.show()
# example usage
data = [
[1, 100, 105, 95],
[2, 105, 110, 100],
[3, 95, 100, 90],
[4, 110, 115, 105],
[5, 90, 95, 85],
[6, 115, 120, 110]
]
zigzag = zigzag(data, 5)
plot_zigzag(data, zigzag)
This code defines a zigzag function that takes a list of OHLC data and a threshold as input and returns a list of zigzag points. The function initializes variables for the last high and low, and then loops through the data. If the current high is greater than the last high plus the threshold, the current high is added to the zigzag and the last high and low are updated. If the current low is lower than the last low minus the threshold, the current low is added to the zigzag and the last high and low are updated.
The code also defines a plot_zigzag function that takes OHLC data and a list of zigzag points as input and plots the zigzag points and buy and sell arrows on a chart. The function initializes lists for buy and sell arrows, and then loops through the zigzag points. If the zigzag is going up, a green buy arrow is added at the current zigzag point. If the zigzag is going down, a red sell arrow is added at the current zigzag point. The plot function from the matplotlib library is then used to plot the zigzag points and arrows on a chart, and the show function is used to display the chart.
Finally, the code includes an example usage of the zigzag and plot_zigzag functions, using a list of OHLC data and a threshold of 5. The zigzag function is called to get a list of zigzag points, and then the plot_zigzag function is called to plot the zigzag points and arrows on a chart.
I hope this helps to clarify the code. Let me know if you have any questions.
Comments
Post a Comment