检查数据框中的值是否会增加或减少一定百分比



我有一个OHLC数据框架,例如:

<表类> 指数 打开 关闭高低tbody><<tr>2021-03-23 10:00:00 + 01421.1001424.5001427.7201422.6502021-03-23 11:00:00 + 01424.5001421.4801422.4001411.8902021-03-23 12:00:00 + 01421.4801435.1701443.9801433.7802021-03-23 13:00:00 + 01435.1701440.8601443.1901437.5902021-03-23 14:00:00 + 01440.8601438.9201443.5701435.2002021-03-23 15:00:00 + 01438.9201435.9901444.8401435.0602021-03-23 16:00:00 + 01435.9901441.9201446.6101441.450

我认为最好的方法和你现在的做法没有太大的不同:

from datetime import timedelta
def check(x, change=0.01):
time = x.name
price = ohlc.loc[time, 'close']
while True:
if time not in ohlc.index:          # If we reach the end
return 0
high = ohlc.loc[time, 'high']
low = ohlc.loc[time, 'low']
if high > (1.0 + change) * price:   # Upper thresh broken
return 1
elif low < 1.0 - change) * price:   # Lower thresh broken
return -1
time = time + timedelta(hours=1)    # Time update
ohlc['check'] = ohlc.apply(check, axis=1)

如果您担心的是效率,那么使用这种方法会稍微更有效率一些,因为它只会在需要突破阈值时向前看。或者,您可以修改while循环,将其限制为100小时,将每行检查的次数限制为100次:

endtime = time + timedelta(hours=100)
while time < endtime:
# etc

相关内容

  • 没有找到相关文章

最新更新