Python: How to populate Pandas column that depends on the previous value (previous row)?

I am building a financial app. My position depends on the previous position (previous row) and also on the 'signal' column (same row).

The DataFrame is called SPY.

position_arr = []
position = 0
for row in SPY['signal']:
    if row=='BUY' and position == 0:
        position = 1
    elif row=='SELL' and position == 0:
        position = -1
    elif row=='CLOSE SELL' and position == -1:
        position = 0
    elif row=='CLOSE BUY' and position == 1:
        position = 0
    position_arr.append(position)

SPY['position']=position_arr

Is there a better and more efficient way to do this?

Upvotes: 1

Views: 131

Answers (2)

Toterich
Toterich

Reputation: 585

You could shift the position column and use apply() on the column axis:

def apply_func(row):
    if row['signal']=='BUY' and row['pos_shifted'] == 0:
        position = 1
    elif row['signal']=='SELL' and row['pos_shifted'] == 0:
        position = -1
    elif row['signal']=='CLOSE SELL' and row['pos_shifted'] == -1:
        position = 0
    elif row['signal']=='CLOSE BUY' and row['pos_shifted'] == 1:
        position = 0
    return position

SPY['pos_shift'] = SPY['position'].shift()
SPY['position'] = SPY.apply(apply_func, axis=1)

Upvotes: 2

AndreyF
AndreyF

Reputation: 1838

You can use apply on 'signal' column and use a variable to hold the previous value.

prev_val = None  #if you don't know what should be the first value 

def check_condition(current_val):
    global prev_val
    val = 0
    if prev_val is not None:
        if current_val == 'BUY' and prev_val == 0:
            val = 1
        elif current_val == 'SELL' and prev_val == 0:
            val = -1
        elif current_val == 'CLOSE SELL' and prev_val == -1:
            val = 0
        elif current_val == 'CLOSE BUY' and prev_val == 1:
            val = 0
    else:  # handle the first row case separately
        val = 0  # TODO: what is the value for the first row?
    prev_val = val
    return val

df['position'] = df['signal'].apply(check_condition)

Upvotes: 1

Related Questions