波峰
import pandas as pd
from scipy.signal import find_peaks
import matplotlib.pyplot as plt
# 创建示例数据
data = {
'日期': ['2022-08-01',
'2022-08-02', '2022-08-03', '2022-08-04', '2022-08-05', '2022-08-08', '2022-08-09', '2022-08-10',
'2022-08-11', '2022-08-12', '2022-08-15', '2022-08-16', '2022-08-17', '2022-08-18', '2022-08-19',
'2022-08-22', '2022-08-23', '2022-08-24', '2022-08-25', '2022-08-26', '2022-08-29', '2022-08-30',
'2022-08-31', '2022-09-01', '2022-09-02', '2022-09-05', '2022-09-06', '2022-09-07', '2022-09-08',
'2022-09-09', '2022-09-13', '2022-09-14', '2022-09-15', '2022-09-16', '2022-09-19', '2022-09-20',
'2022-09-21', '2022-09-22', '2022-09-23', '2022-09-26', '2022-09-27', '2022-09-28', '2022-09-29'],
'收盘': [3.68, 3.66, 3.66, 3.67, 3.67, 3.69, 3.62, 3.43, 3.34, 3.41, 3.44, 3.45, 3.4, 3.39, 3.46, 3.48, 3.46, 3.51,
3.53, 3.44, 3.42, 3.44, 3.46, 3.48, 3.56, 3.47, 3.47, 3.58, 3.53, 3.55, 3.57, 3.68, 3.67, 3.73, 3.66,
3.68, 3.7, 3.62, 3.68, 3.7, 3.71, 3.62, 3.18]
}
# 检查数据长度
for key, value in data.items():
print(f"{key}: {len(value)}")
# 创建 DataFrame
df = pd.DataFrame(data)
# 将日期列转换为 datetime 类型
df['日期'] = pd.to_datetime(df['日期'])
# 检测波峰
peaks_indices, _ = find_peaks(df['收盘'])
# 获取波峰的日期和收盘价
peaks = df.iloc[peaks_indices]
peaks['波峰索引'] = peaks_indices
# 计算相邻波峰之间的距离
peaks['波峰距离'] = peaks['波峰索引'].diff().fillna(0)
# 显示结果
print("波峰数据:")
print(peaks[['日期', '收盘', '波峰索引', '波峰距离']])
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df['日期'], df['收盘'], label='收盘价')
plt.plot(peaks['日期'], peaks['收盘'], 'x', label='波峰', color='red')
for i, row in peaks.iterrows():
plt.text(row['日期'], row['收盘'], f"{row['波峰距离']:.0f}", color='blue', fontsize=9, verticalalignment='bottom')
plt.xlabel('日期')
plt.ylabel('收盘价')
plt.title('收盘价波峰及波峰距离')
plt.legend()
plt.show()
波谷
import pandas as pd
from scipy.signal import find_peaks
import matplotlib.pyplot as plt
# 创建示例数据
data = {
'日期': ['2022-08-01', '2022-08-02', '2022-08-03', '2022-08-04', '2022-08-05', '2022-08-08', '2022-08-09', '2022-08-10',
'2022-08-11', '2022-08-12', '2022-08-15', '2022-08-16', '2022-08-17', '2022-08-18', '2022-08-19',
'2022-08-22', '2022-08-23', '2022-08-24', '2022-08-25', '2022-08-26', '2022-08-29', '2022-08-30',
'2022-08-31', '2022-09-01', '2022-09-02', '2022-09-05', '2022-09-06', '2022-09-07', '2022-09-08',
'2022-09-09', '2022-09-13', '2022-09-14', '2022-09-15', '2022-09-16', '2022-09-19', '2022-09-20',
'2022-09-21', '2022-09-22', '2022-09-23', '2022-09-26', '2022-09-27', '2022-09-28', '2022-09-29'],
'收盘': [3.68, 3.66, 3.66, 3.67, 3.67, 3.69, 3.62, 3.43, 3.34, 3.41, 3.44, 3.45, 3.4, 3.39, 3.46, 3.48, 3.46, 3.51,
3.53, 3.44, 3.42, 3.44, 3.46, 3.48, 3.56, 3.47, 3.47, 3.58, 3.53, 3.55, 3.57, 3.68, 3.67, 3.73, 3.66,
3.68, 3.7, 3.62, 3.68, 3.7, 3.71, 3.62, 3.18]
}
# 创建 DataFrame
df = pd.DataFrame(data)
# 将日期列转换为 datetime 类型
df['日期'] = pd.to_datetime(df['日期'])
# 检测谷底(通过取负值检测波峰)
troughs_indices, _ = find_peaks(-df['收盘'])
# 获取谷底的日期和收盘价
troughs = df.iloc[troughs_indices]
# 显示结果
print("谷底数据:")
print(troughs[['日期', '收盘']])
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(df['日期'], df['收盘'], label='收盘价')
plt.plot(troughs['日期'], troughs['收盘'], 'o', label='谷底', color='red')
plt.xlabel('日期')
plt.ylabel('收盘价')
plt.title('收盘价谷底')
plt.legend()
plt.show()