Python 实现通达信的 zig 函数
通达信的zig
函数是用来绘制市场走势中高低点指标的方法,在 Python 中可以使用第三方库numpy
和matplotlib
搭配实现类似通达信zig
函数的功能,使用numpy
和matplotlib
前需确保其已经安装。
如下是实现步骤:
- 初始化高点和低点列表、当前最高点和最低点,以及趋势方向。
- 遍历数据中的每个数据点。
- 更新当前最高点和最低点。
- 根据当前趋势方向和波动范围判断是否出现高点或低点。
- 返回识别出的高点和低点列表。
- 绘制走势图。
如下是代码示例:
import numpy as np
import matplotlib.pyplot as plt
# 定义简化版的 zig 算法
def zig(data, threshold):
# 高点列表,低点列表,当前最高点,当前最低点,趋势方向,1表示上涨,-1表示下跌
high_points = []
low_points = []
high = float('-inf')
low = float('inf')
direction = 0
for i in range(len(data)):
value = data[i]
# 更新最高点和最低点
if value > high:
high = value
if value < low:
low = value
# 判断趋势方向
if direction == 0:
if value - low >= threshold:
direction = 1
high_points.append([i, high])
low = value
# 判断为上涨
elif direction == 1:
if value > high:
high = value
elif high - value >= threshold:
direction = -1
low_points.append([i, low])
high = value
low = value
# 判断为下降
elif direction == -1:
if value < low:
low = value
elif value - low >= threshold:
direction = 1
high_points.append([i, high])
high = value
low = value
# 返回数据
return high_points, low_points
def draw_zip_picture():
# 生成测试数据
data = np.random.randn(200).cumsum()
# 执行 zig 算法
threshold = 0.5
high_points, low_points = zig(data, threshold)
# 绘制 zig 线
plt.plot(data, label='Original')
for point in high_points:
plt.plot(point[0], point[1], 'ro')
for point in low_points:
plt.plot(point[0], point[1], 'go')
# 生成并展示图片
plt.legend()
plt.show()
draw_zip_picture()
上述代码通过draw_zip_picture
函数实现类似通达信zig
函数的功能,在函数内部通过调用自定义函数zip
获取高低点数据列表,调用matplotlib
中的plot
方法将获取到的高低点数据绘制为图表。
请注意,这只是一个简单的示例,如果你需要更精确的通达信zig
函数的功能,建议使用通达信软件或相关的开源库来实现。