4

我有一个包含每日 OHLCV 数据的 DataFrame。

我可以计算范围:

s['Range'] = s['High'] - s['Low']

简单的。现在我想计算一个我称之为s['OIR'](OIR = Open-In-Range)的新列

['OIR']列检查我们是否在区间内开盘,它通过测试我们是否开盘高于昨天的低点和低于昨天的高点来做到这一点。我需要参考前面的行,但我不太确定该怎么做。返回值将是真/假。

谢谢。


编辑:我是 StackExchange 和 Python 的新手。不确定在哪里放置样本数据。这是数据框的图像。

http://i47.tinypic.com/142eb2a.png


示例数据:字典转换为 DataFrame

{'High': {<Timestamp: 2007-03-02 00:00:00>: 1384.5,
  <Timestamp: 2007-03-05 00:00:00>: 1373.0},
 'Last': {<Timestamp: 2007-03-02 00:00:00>: 1365.0,
  <Timestamp: 2007-03-05 00:00:00>: 1351.5},
 'Low': {<Timestamp: 2007-03-02 00:00:00>: 1364.25,
  <Timestamp: 2007-03-05 00:00:00>: 1350.5},
 'OIR': {<Timestamp: 2007-03-02 00:00:00>: False,
  <Timestamp: 2007-03-05 00:00:00>: False},
 'Open': {<Timestamp: 2007-03-02 00:00:00>: 1378.5,
  <Timestamp: 2007-03-05 00:00:00>: 1356.75},
 'Range': {<Timestamp: 2007-03-02 00:00:00>: 20.25,
 <Timestamp: 2007-03-05 00:00:00>: 22.5},
 'Volume': {<Timestamp: 2007-03-02 00:00:00>: 1706906,
 <Timestamp: 2007-03-05 00:00:00>: 1984041}}

回答:

s['OIR'] = ((s['Open'] < s['High'].shift(1)) & (s['Open'] > s['Low'].shift(1)))
4

1 回答 1

7

以您建议的方式引用以前的行最好使用以下Series.shift()功能完成:

In [1]: df = DataFrame(randn(10,3),columns=['O','L','H'])

In [2]: df
Out[2]:
          O         L         H
0  0.605412  0.739866 -0.280222
1 -0.707852  0.785651  0.855183
2 -0.087119  0.518924  0.932167
3 -0.913352  0.369825  1.277771
4  0.434593 -2.942903  0.802413
5  0.075669 -0.135914  1.374454
6  1.112062  0.314946  0.882468
7 -0.706078 -0.202243  0.838088
8 -1.668152  0.414585  0.809932
9  1.452937 -0.048245  0.635499

In [3]: df['OIR'] = ((df.L.shift() <= df.O) & (df.O <= df.H.shift()))

In [4]: df
Out[4]:
          O         L         H    OIR
0  0.605412  0.739866 -0.280222  False
1 -0.707852  0.785651  0.855183  False
2 -0.087119  0.518924  0.932167  False
3 -0.913352  0.369825  1.277771  False
4  0.434593 -2.942903  0.802413   True
5  0.075669 -0.135914  1.374454   True
6  1.112062  0.314946  0.882468   True
7 -0.706078 -0.202243  0.838088  False
8 -1.668152  0.414585  0.809932  False
9  1.452937 -0.048245  0.635499  False
于 2013-01-24T02:45:30.320 回答