简体   繁体   English

OHLC数据中需要移动平均高度,而无需创建其他列

[英]Need Moving Average Height in OHLC data without creating additional column

I need to find the Moving Average Height (abs(Close-Open)) for last 5 days. 我需要找到最近5天的移动平均高度(abs(Close-Open))。 I have daily OHLC data. 我每天都有OHLC数据。 An easy solution would be to first create a column Height and then calculate 5 days moving average using the rolling function. 一个简单的解决方案是首先创建一列“高度”,然后使用滚动功能计算5天移动平均值。 But this makes my code inefficient. 但这使我的代码效率低下。 I need to find the Moving average height without creating a new column. 我需要找到移动平均高度而无需创建新列。

My data looks like below and expected output is in column 'Average Height': 我的数据如下所示,预期输出在“平均高度”列中:

Date         Open    High   Low      Close  Average Height
01-01-2018  1763.95 1763.95 1725    1731.35 
02-01-2018  1736.2  1745.8  1725    1743.2  
03-01-2018  1741.1  1780    1740.1  1774.6  
04-01-2018  1779.95 1808    1770    1801.35 
05-01-2018  1801.1  1820.4  1795.6  1809.95 20.67
08-01-2018  1816    1827.95 1800    1825    15.95
09-01-2018  1823    1835    1793.9  1812.05 16.74
10-01-2018  1812.05 1823    1801.4  1816.55 10.94
11-01-2018  1825    1825.05 1798.55 1802.1  11.24
12-01-2018  1805    1820    1794    1804.95 9.48
15-01-2018  1809.9  1834.45 1792.45 1830    11.7
16-01-2018  1835    1857.45 1826.1  1850.25 12.56
17-01-2018  1850    1852.45 1826.2  1840.5  13.56
18-01-2018  1840.5  1852    1823.5  1839    9.28
19-01-2018  1828.25 1836.35 1811    1829.5  9.52

The current code I am using is as below snippet: 我正在使用的当前代码如下片段:

df['Avg Height'] = df[abs(df['Close'] - df['Open'])].rolling(window = 5).mean()

But of course, it looks incorrect since I am getting an error. 但是,当然,由于我遇到错误,它看起来不正确。

正确答案如下:

df['Avg Height'] = abs(df['Close'] - df['Open']).rolling(window = 5).mean()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM