编写一个Weis Wave脚本的解决方法如下所示:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def weis_wave(data, threshold=0.015):
df = pd.DataFrame(data, columns=['price'])
df['high_point'] = df['price'].rolling(window=3, center=True).max()
df['low_point'] = df['price'].rolling(window=3, center=True).min()
df['wave'] = df['high_point'].shift(-1) - df['low_point'].shift(-1)
df['threshold'] = df['price'] * threshold
df['wave_above_threshold'] = np.where(df['wave'] > df['threshold'], df['wave'], np.nan)
df['wave_below_threshold'] = np.where(df['wave'] < df['threshold'], df['wave'], np.nan)
wave_above_threshold = df['wave_above_threshold'].dropna()
wave_below_threshold = df['wave_below_threshold'].dropna()
plt.plot(df['price'])
plt.plot(df['high_point'], color='g', linestyle='--')
plt.plot(df['low_point'], color='r', linestyle='--')
plt.scatter(wave_above_threshold.index, wave_above_threshold, color='g', marker='^')
plt.scatter(wave_below_threshold.index, wave_below_threshold, color='r', marker='v')
plt.legend(['Price', 'High Point', 'Low Point', 'Above Threshold', 'Below Threshold'])
plt.show()
# 示例数据
data = [100, 102, 103, 101, 105, 104, 106, 107, 105, 103, 100, 98, 97, 96, 94, 92, 95, 97, 99, 98]
# 调用Weis Wave函数
weis_wave(data)
上述代码中,我们首先定义了一个名为weis_wave的函数,该函数接受一个数据列表和一个阈值作为输入参数。函数内的代码将数据转换为一个DataFrame对象,并计算出每个波动的高点和低点。然后,根据波动与阈值的关系,将波动分为两个类别:高于阈值和低于阈值。最后,使用matplotlib库绘制价格图表,并在图表中标记出高点、低点以及高于和低于阈值的波动点。
在示例中,我们传入了一个名为data的示例数据列表来调用weis_wave函数。你可以根据自己的需求修改数据列表和阈值来测试脚本的功能和效果。运行脚本后,将显示一个包含价格、高点、低点、高于阈值的波动点和低于阈值的波动点的图表。