posted this question here, but I haven't received an answer.
I have the following df2:
df2 = pd.DataFrame({"price":[200,205,210,208,206, 199, 192, 185, 165, 160, 161, 165, 168, 171, 169, 163, 161], "signal": [1,0,0,0,-1,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,1,0,0,0,-1,np.nan]})
# for signal, 1 means you opened a position, 0 means you're in the position, and -1 means you closed the position, NaN means you don't currently have a position
So I have open positions at df2[0:5] and df2[11:16]. For these two separate ranges (df2[0:5] and df2[11:16]), I want the separate cummax() of the price column.
Here's what I tried:
df2['trailing high'] = np.where(df2['signal'] != np.nan, df2['price'].cummax(), np.nan)
But the above just gives me the cummax() of the entire price column instead of giving me the cummax() of df2[0:5] and df2[11:16]. As you might have guessed, once I have the trailing cummax() of the price column, I'd use that to set a trailing stop loss where the trailing stop would continue to move higher as price moves higher, but would not move lower (hence the cummax()) as price moves lower.
The final df should look like this:
finaldf = pd.DataFrame({"price":[200,205,210,208,206, 199, 192, 185, 165, 160, 161, 165, 168, 171, 169, 163, 161], "signal": [1,0,0,0,-1,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,1,0,0,0,-1,np.nan], "trailing_high": [200, 205, 210, 210, 210, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 165, 168, 171, 171, 171, np.nan]})