gpt4 book ai didi

python - Pandas 如何让我的专栏将新价格附加到标价,持有或什么都不做时保持相同的列表,全部出售为 Nan=N

转载 作者:行者123 更新时间:2023-12-03 19:04:29 25 4
gpt4 key购买 nike

我有这个来自黄金价格的数据框

    Date      Open          High        Low         Close       Long  20High      LongPrice
x/x/x 569.799988 575.299988 568.000000 572.500000 1 575.299988 NaN
x/x/x 571.500000 574.200012 565.000000 567.400024 0 575.299988 NaN
x/x/x 568.400024 574.000000 567.500000 570.200012 0 575.299988 NaN
x/x/x 569.500000 571.000000 550.599976 551.000000 0 575.299988 NaN
x/x/x 551.000000 553.299988 545.500000 550.099976 0 575.299988 NaN
x/x/x 553.299988 566.000000 549.900024 564.500000 0 575.299988 NaN
x/x/x 561.900024 561.900024 548.000000 550.200012 0 575.299988 NaN
x/x/x 548.500000 549.500000 540.000000 539.000000 -1 575.299988 NaN
x/x/x 538.000000 546.000000 535.500000 545.900024 -1 575.299988 NaN
x/x/x 544.900024 545.000000 538.000000 539.700012 0 575.299988 NaN

我为他们创造了功能

  • 做多 = 1 --> 买入
  • Long = 0 --> 什么也不做或只是持有
  • Long = -1 --> 全部卖出

会变成这样

  Date    Open          High        Low         Close       Long  20High      LongPrice
x/x/x 569.799988 575.299988 568.000000 572.500000 1 575.299988 [575.299988]
x/x/x 571.500000 575.299988 565.000000 567.400024 1 575.299988 [575.299988,575.299988]
x/x/x 568.400024 574.000000 567.500000 570.200012 0 575.299988 [575.299988,575.299988]
x/x/x 569.500000 571.000000 550.599976 551.000000 0 575.299988 [575.299988,575.299988]
x/x/x 551.000000 553.299988 545.500000 550.099976 0 575.299988 [575.299988,575.299988]
x/x/x 553.299988 566.000000 549.900024 564.500000 0 575.299988 [575.299988,575.299988]
x/x/x 561.900024 561.900024 548.000000 550.200012 0 575.299988 [575.299988,575.299988]
x/x/x 548.500000 549.500000 540.000000 539.000000 -1 575.299988 NaN
x/x/x 538.000000 546.000000 535.500000 545.900024 -1 575.299988 NaN
x/x/x 544.900024 577.000000 538.000000 560.700015 1 577.000000 [577.000000]

但我不确定为什么以及我在我的代码中做错了什么,它无法使数据框像我展示的上面的数据框一样(我将使用 LongPrice 中的数据来计算利润)

def TurtleBuyPrice(df):

df = df.copy()
df = df.reset_index()
x = []
for index,row in df.iterrows():
if index == 0:
if row['Long'] == 0 or -1:
continue
else:
df['LongPrice'][index] = [row["20High"]]
elif row['Long'] in [1]:
if df['LongPrice'][index-1] == np.nan:
df['LongPrice'][index] = [row["20High"]]
else:
df['LongPrice'][index] = df['LongPrice'][index-1]+[row["20High"]]
elif row['Long'] in [0]:
df['LongPrice'][index] = df['LongPrice'][index-1]
elif row['Long'] in [-1]:
df['LongBuySell'][index] = np.nan
return df

如果有人有更好的想法让数据保持这样,请提供一些建议

最佳答案

使用 shiftcumsum 的一种方法:

s = df["Long"].ne(-1)
s2 = df["20High"].apply(lambda x: [x]) * df["Long"]
df["LongPrice"] = s2.groupby(s.ne(s.shift()).cumsum()).apply(pd.Series.cumsum)
print(df)

输出:

    Date        Open        High         Low       Close  Long      20High  \
0 x/x/x 569.799988 575.299988 568.000000 572.500000 1 575.299988
1 x/x/x 571.500000 574.200012 565.000000 567.400024 1 575.299988
2 x/x/x 568.400024 574.000000 567.500000 570.200012 0 575.299988
3 x/x/x 569.500000 571.000000 550.599976 551.000000 0 575.299988
4 x/x/x 551.000000 553.299988 545.500000 550.099976 0 575.299988
5 x/x/x 553.299988 566.000000 549.900024 564.500000 0 575.299988
6 x/x/x 561.900024 561.900024 548.000000 550.200012 0 575.299988
7 x/x/x 548.500000 549.500000 540.000000 539.000000 -1 575.299988
8 x/x/x 538.000000 546.000000 535.500000 545.900024 -1 575.299988
9 x/x/x 544.900024 545.000000 538.000000 539.700012 1 575.299988

LongPrice
0 [575.299988]
1 [575.299988, 575.299988]
2 [575.299988, 575.299988]
3 [575.299988, 575.299988]
4 [575.299988, 575.299988]
5 [575.299988, 575.299988]
6 [575.299988, 575.299988]
7 []
8 []
9 [575.299988]

关于python - Pandas 如何让我的专栏将新价格附加到标价,持有或什么都不做时保持相同的列表,全部出售为 Nan=N,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63989191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com