问题描述
我想要做的是计算指定时间段内股票价格的简单移动平均线。我参考了很多在线资源,他们都推荐使用 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
用于 rolling_mean 计算,所以我翻转了数据框,以便我可以获得最新日期的移动平均值。>
这是翻转后的样子:
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
。然后我去了一个叫 Tradeview 的网站来验证我的结果,结果是这样的:
(此图表的设置 -> 图表的时间段 - 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
的输出。
我做错了什么?我怎样才能获得像网站一样的斜率?
任何帮助将不胜感激。
解决方法
抱歉耽搁了, 让我们看看下面的代码片段:
"result": [
{
"firstname": "John","lastname": "Doe",},{
"firstname": "Max","lastname": "Mustermann",}
]
可以看到,当 math.atan() 的输入参数为 1 时,结果的度数为 45,因此它似乎将 >>> 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
元素视为 1,并通过增加输入参数,度数增加。
您也可以使用 x
并传递 math.atan2(y,x)
参数。
你可能会说 x
参数是蜡烛的日期,这没关系,你可以看到日期大小是相对的,你可以通过滚动来改变它,图表中线条的度数将是改变了。
因此您可以选择一个 x
数字并根据您的策略形成一个与之相关的条件。