I have this data frame from gold price
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
and I made funtion for them be like
- Long = 1 --> Buy
- Long = 0 --> Do nothing or just hold
- Long = -1 --> Sell all
and it will be like this
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]
But I am not sure why and what i did wrong in my code that it isn't working to make dataframe be like above dataframe that i show example (I will use data in LongPrice for calculate profit as well)
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
If somebody have better idea to keep data be like this one please give some advise pls