How to end a function if certain condition is met in pandas

I have this dataset:

Close   X    SL   
60.6    0
57.8   100  55.4   
59.6    0   55.4   
54.1    0   55.4   
53.6    0   55.4
54.5    0   55.4
66.1   100  62.3
66.0    0   62.3

I want to add a new Remarks column has should have BUY when X=100 followed by three days of HOLD and two days of SELL. I also want to add Stopped Out option when Close < SL. This can show up anytime after BUY and should last for only two rows.

Desired behaviour:

Close   X    SL   Remarks
60.6    0
57.8   100  55.4   BUY
59.6    0   55.4   HOLD
54.1    0   55.4   Stopped Out
53.6    0   55.4   Stopped Out
54.5    0   55.4
66.1   100  62.3   BUY
66.0    0   62.3   HOLD

Note that that the function should break only if Stopped Out appears, else it should go as usual.

Here’s my current code:

cond = [
    (df['X'] == 100),
    (df['X'].shift(1) == 100),
    (df['X'].shift(2) == 100),
    (df['X'].shift(3) == 100),
    (df['X'].shift(4) == 100),
    (df['X'].shift(4) == 100),
]
choices = ['BUY', 'HOLD', 'HOLD', 'HOLD', 'SELL','SELL']
df['Remarks'] = np.select(cond, choices, default='')

buy = df['X'].eq(100).cumsum()
df['stop'] = df.groupby(buy, sort=False).apply(lambda g: g['Close'].lt(g['SL']).cumsum().cumsum()).array

df['Remarks'] = df['Remarks'].mask(df['stop'].isin([1, 2]), 'Stopped Out')

However, it gives me this outcome:

Close   X    SL   Remarks
60.6    0
57.8   100  55.4   BUY
59.6    0   55.4   HOLD
54.1    0   55.4   Stopped Out
53.6    0   55.4   HOLD
54.5    0   55.4   SELL
66.1   100  62.3   BUY
66.0    0   62.3   HOLD

It seems to carry on the function even after Stopped Out.

Answer

The following code, inspired by your code, does what you asked:

cond = [
    (df['X'] == 100),
    (df['X'].shift(1) == 100),
    (df['X'].shift(2) == 100),
    (df['X'].shift(3) == 100),
    (df['X'].shift(4) == 100),
    (df['X'].shift(5) == 100),
]
choices = ['BUY', 'HOLD', 'HOLD', 'HOLD', 'SELL','SELL']
df['Remarks'] = np.select(cond, choices, default='')

buy = df['X'].eq(100).cumsum()

df['stop'] = df.groupby(buy, sort=False).apply(lambda g: g['Close'].lt(g['SL']).cumsum()).values

df['Remarks'] = df['Remarks'].mask(df['stop'].isin([1, 2]), 'Stopped Out')
df['Remarks'] = df['Remarks'].mask(df['stop'].isin([3, 10]), '')
df = df.drop('stop', axis=1)

Example:

    Close    X    SL      Remarks
0    60.6    0   NaN             
1    57.8  100  55.4          BUY
2    59.4    0  55.4         HOLD
3    54.1    0  55.4  Stopped Out
4    53.6    0  55.4  Stopped Out
5    54.5    0  55.4             
6    66.1  100  62.3          BUY
7    66.0    0  62.3         HOLD
8    67.0    0  62.3         HOLD
9    68.0  100  64.3          BUY
10   67.0    0  64.3         HOLD
11   66.0    0  64.3         HOLD
12   67.0    0  64.3         HOLD
13   66.0    0  64.3         SELL
14   65.0    0  64.3         SELL
15   64.4    0  64.3             
16   63.3    0  64.3  Stopped Out