在 Pandas 中设计追踪止损列

时间:2021-04-18 09:13:07

标签: python pandas

我用熊猫创建了一个数据框并计算了盈利或亏损的百分比: 我希望我可以设计 2 列,如进入市场和追踪追踪止损回测,例如 5% 像:

earning/losing    entered market        trail
             0                 0            0
             1                 1            1
             2                 1            2
             3                 1            3
             7                 1            7
             4                 1            7
             5                 1            7
             8                 1            8
             2                 0            0
             5                 0            0
             4                 0            0

我曾尝试使用 numpy 条件创建 like 但我无法完成 其余条件:

condition = [(df['earning/losing'] > 0) & (df['earning/losing'] >df['earning/losing'].shift(-1)) & (df['earning/losing'] - df['earning/losing'].shift(-1) < 5),]
value = [df.earning/losing,]
df['trail'] = np.select(condition,value,default = 0)

我觉得如果我可以创建一个像trail这样的列,那么我就可以判断尾随情况了,

但是我不知道如何在pandas中创建trail列

谁能帮帮我?非常感谢!

0 个答案:

没有答案