Reputation: 43
I have a log of events from an algorithmic trading bot loaded into a dataframe that looks like this:
datetime_long high low trade_direction trade_entry trade_exit
159 2021-02-05 10:15:00 88.915 88.6150 LE 1.0 0.0
160 2021-02-05 10:30:00 89.395 88.7800 LX 0.0 1.0
172 2021-02-05 13:30:00 89.090 88.9000 LE 1.0 0.0
177 2021-02-05 14:45:00 89.410 89.1900 LX 0.0 1.0
206 2021-02-08 15:30:00 88.885 88.6600 LE 1.0 0.0
207 2021-02-08 15:45:00 89.080 88.7700 LX 0.0 1.0
"LE" indicates a long entry, "LX" a long exit, "SE" short entry and "SX" short exit. The goal would be to get each "exit" on the same row as the prior "entry" so that I could perform some simple arithmetic computations like determining profit, average hold time, etc.
How does one go about "shifting" only exits up one row so that my entries and corresponding exits are on the same line?
Upvotes: 0
Views: 59
Reputation: 29982
You can use .str.contains()
to check if elements in Series contains value.
X_mask = df['trade_direction'].str.contains('X')
X_previous_mask = X_mask.shift(-1).fillna(False)
E_mask = df['trade_direction'].str.contains('E')
You can use
# Entry Rows where Entry next row is Exit
X_previous_E_mask = X_previous_mask & E_mask
# Entry and Exit Rows where Entry next row is Exit
X_and_previous_E_mask = X_previous_E_mask | X_mask
At last, you can use boolean indexing to select rows.
df[mask]
Upvotes: 1