如何根据条件制作范围/子数组,以便计算阈值/止损之前的回报? (在 numpy 或 pandas 中)

问题描述 投票:0回答:0

在 Excel 中,这将是一个偏移函数。考虑在 pandas 中,但它也可以在 numpy 中完成:

returns = df['close'].rolling(periods).shift(-periods).max() / df['close'] -1

..计算前瞻性时期内的最高潜在回报,并保持在所有低点。

滚动窗口固定在周期数

但相反,我想找到在低于某个阈值(止损)的任何低点之前实现的最大回报。因此它需要相同的移动滚动窗口但长度可变 - 直到满足条件的点(如果有的话)。

现在处于错误/伪代码中,因为我不知道如何创建相对或条件引用/窗口,即使它们在 Pandas/numpy 中是允许的——已经在文档中搜索了任何接近此的语法

V1 尝试将滚动窗口截断为第一个低于阈值损失的值,v2 尝试创建一个可变的相对索引

版本 1:

df['close'].rolling(periods).shift(-periods).truncate('close' < df['close'] * (1 - maximum_loss)).max() / df['close'] -1

版本 2

(df['close'][:min(周期, (df['close'] < df['close'] * (1 - maximum_loss)).index[0])].max() / df['close'] -1).shift(-periods)

感谢任何帮助!

python pandas numpy quantitative-finance
© www.soinside.com 2019 - 2024. All rights reserved.