如何精确计算一系列点的移动平均斜率?

2024-10-01 00:19:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做的是计算一个简单的移动平均线,在一段特定的时间内,股票价格。我参考了很多在线资源,它们都建议使用rolling_mean函数来计算移动平均值

我是这样做的:


def getEODData(symbol):
    api_result = requests.get('http://api.marketstack.com/v1/eod?access_key='+apikey+'&symbols='+symbol+'&limit=2500')
    api_response = api_result.json()
    df=pd.DataFrame.from_dict(api_response['data'])
    df=df.iloc[::-1]
    timeshort=66

    if not df.empty:
        
        df['SMA']=df.iloc[:,3].rolling(window=timeshort).mean() 
        slope_short=((df['SMA'][0]-df['SMA'][timeshort])/timeshort)
        slope_short_deg = math.atan(slope_short) * 180 / math.pi
        print(slope_short_deg)

我做了df. iloc[::-1],因为滚动平均值计算的第一个66周期是NaN,所以我翻转了数据帧,以便可以得到最新日期的移动平均值

以下是翻转后的外观:

open       high        low      close     volume  adj_high  adj_low  ...  adj_volume  split_factor  symbol  exchange                      date          SMA     SMA_long
1791   568.0000   568.0000   552.9200   558.4600    13100.0    568.00   552.92  ...     13100.0           2.0    GOOG      XNAS  2014-03-27T00:00:00+0000          NaN          NaN
1790   561.2000   566.4300   558.6700   559.9900    41100.0    566.43   558.67  ...     41100.0           1.0    GOOG      XNAS  2014-03-28T00:00:00+0000          NaN          NaN
1789   566.8900   567.0000   556.9300   556.9700    10800.0    567.00   556.93  ...     10800.0           1.0    GOOG      XNAS  2014-03-31T00:00:00+0000          NaN          NaN
1788   558.7100   568.4500   558.7100   567.1600     7900.0    568.45   558.71  ...      7900.0           1.0    GOOG      XNAS  2014-04-01T00:00:00+0000          NaN          NaN
1787   565.1060   604.8300   562.1900   567.0000   146700.0    604.83   562.19  ...    146700.0           1.0    GOOG      XNAS  2014-04-02T00:00:00+0000          NaN          NaN
...         ...        ...        ...        ...        ...       ...      ...  ...         ...           ...     ...       ...                       ...          ...          ...
4     2402.7200  2419.7000  2384.5000  2395.1699  1648353.0       NaN      NaN  ...         NaN           1.0    GOOG      XNAS  2021-05-03T00:00:00+0000  2134.197117  1724.360315
3     2369.7400  2379.2600  2311.7000  2354.2500  1686545.0       NaN      NaN  ...         NaN           1.0    GOOG      XNAS  2021-05-04T00:00:00+0000  2141.638632  1728.445849   
2     2368.4199  2382.2000  2351.8850  2356.7400  1090275.0       NaN      NaN  ...         NaN           1.0    GOOG      XNAS  2021-05-05T00:00:00+0000  2149.532571  1732.516758   
1     2350.6399  2382.7100  2342.3381  2381.3501   978908.0       NaN      NaN  ...         NaN           1.0    GOOG      XNAS  2021-05-06T00:00:00+0000  2156.805300  1736.588853   
0     2400.0000  2416.4099  2390.0000  2398.6899  1163600.0       NaN      NaN  ...         NaN           1.0    GOOG      XNAS  2021-05-07T00:00:00+0000  2163.944389  1740.744544   

现在我尝试运行google股票,它给出的输出是80.47 deg。然后我去了一个名为tradingview的网站验证我的结果,结果是这样的:

enter image description here

(此图表的设置->;图表的时间段-1天和移动平均周期-66)

我为66条的斜率画了红线,正如你所看到的,这与80 deg很不接近

然后我想到用np.polyfit()来找到这样的斜率:

 y=np.array(df['SMA'][-(timeshort):])
 x= range(0, len(y))
 sl, b=np.polyfit(x,y,1)
 sl=math.atan(sl) * 180 / math.pi

但这也给出了79 deg的输出

我做错了什么?我怎样才能得到网站的坡度

任何帮助都将不胜感激


Tags: apidfmathnansymbolslope平均值goog
1条回答
网友
1楼 · 发布于 2024-10-01 00:19:25

抱歉耽搁了, 让我们看看下面的代码片段:

>>> import math
>>> slope_short_deg = math.atan(1) * 180 / math.pi
>>> slope_short_deg
45.0
>>> slope_short_deg = math.atan(2) * 180 / math.pi
>>> slope_short_deg
63.43494882292201
>>> slope_short_deg = math.atan(3) * 180 / math.pi
>>> slope_short_deg
71.56505117707799

如您所见,当math.atan()的输入参数为1时,结果的阶数为45,因此它似乎将x元素视为1,通过增加输入参数,阶数增加

您也可以使用math.atan2(y, x)并传递x参数

你可以说x参数是蜡烛的日期,这是确定的,你可以看到日期大小是相对的,你可以通过滚动来改变它,图表中线条的程度也会改变

因此,您可以选择一个x号,并根据您的策略形成一个与之相关的条件

相关问题 更多 >