我正在使用Pyalgotrade(通常與ta-lib指標結合使用),但我缺少一個函數來找到數據系列中的局部最大值和最小值 。Python Pyalgotrade在Dataseries中查找最近的峯值(最小值或最大值)
我知道最小和最大函數,但這不是我正在尋找的。 E.例如, MIN(低,count = 5)會給我5個小節中的最低值,但這不會超出5個值。我正在尋找一個函數,在特定時間段內返回「本地低點」的值,即該值在該日前後兩天較低。
例
Series [2,3,2,1,3,4,5,6,6]
MIN(5) -> returns 3, but the lowest value is on the left border of the observed window
and the day before, the value was even lower!
whatiamlookingfor() -> should return 1,
because it is the last local low over a +/-2 days period
我希望這是清楚明白我的意思。其他任何功能都可能是我忽視的功能。
編輯
要查找的數據系列的低,我想出了這樣的事情...
def min_peak(series, interval):
reference = -1
left = reference - interval
while -reference < len(series):
if min(series[left:]) == min(series[reference:]):
return min(series[left:])
reference -= 1
left -= 1
...但我不是很滿意這個解決方案,因爲我認爲向後解析這個系列效率不高。我假設內置的本地運行可能會更快。
親切的問候,
啤酒
當然,解決方案很簡單,但使用優化軟件包或外部庫(如ta-lib)通常在執行過程中會快一個數量級。我的希望是,有一些我可以重複使用 – Ale
@Ale我更新了我的答案,希望它可以有任何幫助... – superhero