我想知道是否有一种有效的方法来为下面的代码生成输出。我正在通过在回撤值中定位峰值来寻找时间序列中的最大回撤
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
data={'time': list(np.arange(21)), 'price': [10,11,12,13,12,10,9,13,14,15,18,19,20,22,23,20,17,16,19,24,23]}
df = pd.DataFrame(data, columns = ['time', 'price'])
plt.plot(df['time'],df['price'])
def max_drawdown_list(X):
drawdowns = []
peak = X[0]
for x in X:
if x > peak:
peak = x
dd = peak - x
drawdowns.append(dd)
return drawdowns
def max_drawdown(prices):
peaks_index,_ = find_peaks(max_drawdown_list(prices), height = 0)
max_dd_list = max_drawdown_list(prices)
max_dd = [max_dd_list[i] for i in peaks_index]
return max_dd, peaks_index
max_drawdown(df['price'])