TA-lib python,如何使用 MAVP - 具有可变周期的移动平均线

问题描述

real = MAVP(close,periods,minperiod=2,maxperiod=30,matype=0)

我正在尝试使用此方法,但它会引发错误,因为句点参数, 如何将这种方法用于像这样的 Dataframe

enter image description here

解决方法

这个 period 参数必须作为您想要获取的周期数组传递。我从雅虎财经获得了苹果股票价格,并获得了整个期间的移动平均线。

import yfinance as yf
data = yf.download("AAPL",start="2020-01-01",end="2021-01-01")

data.head()
    Open    High    Low     Close   Adj Close   Volume
Date                        
2020-01-02  74.059998   75.150002   73.797501   75.087502   74.333511   135480400
2020-01-03  74.287498   75.144997   74.125000   74.357498   73.610840   146322800
2020-01-06  73.447502   74.989998   73.187500   74.949997   74.197395   118387200
2020-01-07  74.959999   75.224998   74.370003   74.597504   73.848442   108872000
2020-01-08  74.290001   76.110001   74.290001   75.797501   75.036385   132079200

import talib as ta
import numpy as np

data.reset_index(drop=False,inplace=True)
periods = data.Date
real = ta.MAVP(data.Close,periods,minperiod=2,maxperiod=30,matype=0)

real
0             NaN
1             NaN
2             NaN
3             NaN
4             NaN
          ...    
248    131.465004
249    134.330002
250    135.779999
251    134.294998
252    133.205002
Length: 253,dtype: float64