我正在尝试计算数据框中股票价格的价格变化,该数据框中包含单独列中的进入和退出信号,并且只想获取这两者之间范围的盈亏。
Date Price Entry Signal Exit Signal Price Change
2024-01-18 3595.0 NaN NaN
2024-01-19 3280.0 Entry NaN
2024-01-22 3505.0 NaN NaN
2024-01-23 3945.0 NaN Exit (3945 / 3280- 1)
2024-01-24 4075.0 NaN NaN
2024-03-07 10270.0 Entry NaN
2024-03-08 8120.0 NaN nan
2024-03-11 6620.0 NaN Exit (6620 / 10270 - 1)
到目前为止,我依赖于 for 循环遍历每一行,并有一个名为position的变量,其中我递增1(一旦触发Entry)并重置为0(一旦触发Exit),但必须有一种更智能/更快的方法鉴于这是一个数据框。
代码
cond = df[['Entry Signal', 'Exit Signal']].notna().any(axis=1)
df['Price Change'] = df.loc[cond, 'Price'].pct_change().where(df['Exit Signal'].notna())
df:
Date Price Entry Signal Exit Signal Price Change
0 2024-01-18 3595.0 None None NaN
1 2024-01-19 3280.0 Entry None NaN
2 2024-01-22 3505.0 None None NaN
3 2024-01-23 3945.0 None Exit 0.202744
4 2024-01-24 4075.0 None None NaN
5 2024-03-07 10270.0 Entry None NaN
6 2024-03-08 8120.0 None None NaN
7 2024-03-11 6620.0 None Exit -0.355404
示例代码
import pandas as pd
data = {"Date": ["2024-01-18", "2024-01-19", "2024-01-22", "2024-01-23", "2024-01-24", "2024-03-07", "2024-03-08", "2024-03-11"], "Price": [3595.0, 3280.0, 3505.0, 3945.0, 4075.0, 10270.0, 8120.0, 6620.0], "Entry Signal": [None, "Entry", None, None, None, "Entry", None, None], "Exit Signal": [None, None, None, "Exit", None, None, None, "Exit"]}
df = pd.DataFrame(data)