【问题标题】:create a new column in pandas and assign value from existing column if condition is true如果条件为真,则在 pandas 中创建一个新列并从现有列中赋值
【发布时间】:2020-08-25 21:16:04
【问题描述】:

我有 15 分钟的股票数据蜡烛图并且有一个短信号 - 如果信号 = 0,我想创建一个新的止损列,然后止损 = 下一个第二根蜡烛的高点,即(df['high'] .shift(-2) )

                        open     high      low    close  signal
date                                                           
2020-01-01 09:15:00  1452.50  1457.00  1449.20  1452.50     NaN
2020-01-01 09:30:00  1452.30  1454.40  1450.00  1451.45     NaN
2020-01-01 09:45:00  1450.50  1454.80  1450.00  1453.75     NaN
2020-01-01 10:00:00  1453.70  1453.70  1450.10  1450.70     0.0
2020-01-01 10:15:00  1450.70  1453.00  1450.50  1452.20     NaN
2020-01-01 10:30:00  1452.00  1452.00  1446.75  1446.85     NaN
2020-01-01 10:45:00  1447.60  1449.00  1445.50  1447.10     NaN
2020-01-01 11:00:00  1446.75  1449.00  1446.55  1447.65     NaN

在这个例子中: 2020-01-01 10:00:00 做空信号的止损为 1452.00
这是 2020-01-01 10:30:00 的最高点

【问题讨论】:

标签: python pandas time-series stock


【解决方案1】:

让我们试试np.where(condition, answer if condition is true, answer if condition is false)

df['stop-loss']=np.where(df.signal==0,df.high.shift(-2),'')

在这种情况下,您没有指定如果为 false 的条件应该是什么,所以我放在那里''

                        open    high      low    close  signal stop-loss
date                                                                    
2020-01-01 09:15:00  1452.50  1457.0  1449.20  1452.50     NaN          
2020-01-01 09:30:00  1452.30  1454.4  1450.00  1451.45     NaN          
2020-01-01 09:45:00  1450.50  1454.8  1450.00  1453.75     NaN          
2020-01-01 10:00:00  1453.70  1453.7  1450.10  1450.70     0.0    1452.0
2020-01-01 10:15:00  1450.70  1453.0  1450.50  1452.20     NaN          
2020-01-01 10:30:00  1452.00  1452.0  1446.75  1446.85     NaN          
2020-01-01 10:45:00  1447.60  1449.0  1445.50  1447.10     NaN          
2020-01-01 11:00:00  1446.75  1449.0  1446.55  1447.65     NaN          

根据您在 cmets 中的其他问题。假设数据框是

                  open    high      low    close  signal
date                                                          
2020-01-01 09:15:00  1452.50  1457.0  1449.20  1452.50     NaN
2020-01-01 09:30:00  1452.30  1454.4  1450.00  1451.45     NaN
2020-01-01 09:45:00  1450.50  1454.8  1450.00  1453.75     NaN
2020-01-01 10:00:00  1453.70  1453.7  1450.10  1450.70     0.0
2020-01-01 10:15:00  1450.70  1453.0  1450.50  1452.20     NaN
2020-01-01 10:30:00  1452.00  1452.0  1446.75  1446.85     1.0
2020-01-01 10:45:00  1447.60  1449.0  1445.50  1447.10     NaN
2020-01-01 11:00:00  1446.75  1449.0  1446.55  1447.65     NaN

使用np.select([conditons],[choices], alternative)

conditions=[df.signal==0,df.signal==1]
choices=[df.high.shift(-2),df.low.shift(-2)]
df['stop-loss']=np.select(conditions, choices,'')



                   open    high      low    close  signal stop-loss
date                                                                    
2020-01-01 09:15:00  1452.50  1457.0  1449.20  1452.50     NaN          
2020-01-01 09:30:00  1452.30  1454.4  1450.00  1451.45     NaN          
2020-01-01 09:45:00  1450.50  1454.8  1450.00  1453.75     NaN          
2020-01-01 10:00:00  1453.70  1453.7  1450.10  1450.70     0.0    1452.0
2020-01-01 10:15:00  1450.70  1453.0  1450.50  1452.20     NaN          
2020-01-01 10:30:00  1452.00  1452.0  1446.75  1446.85     1.0   1446.55
2020-01-01 10:45:00  1447.60  1449.0  1445.50  1447.10     NaN          
2020-01-01 11:00:00  1446.75  1449.0  1446.55  1447.65     NaN        

【讨论】:

  • 如果我也有一个 Long 信号 1 和那个 df.low.shift(-2) 的止损,有没有办法在 np.where 中添加两者?
  • 请尝试 np.select conditions=[df.signal==0,df.signal==1] choices=[df.high.shift(-2),df.low.shift(-2)] df['stop-loss']=np.select(conditions, choices,'')
  • 这有帮助吗?如果满意,请为答案投票,以便其他人将来可以自信地使用您的问题和我的答案。
猜你喜欢
  • 2021-02-15
  • 2021-12-22
  • 2019-01-18
  • 2020-08-10
  • 1970-01-01
  • 2021-04-12
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多