How to implement flipflop logic for buy / sell signals using NaN and fill in python pandas?

I have two series of buy and sell signals in my python dataframe. Logic is the trigger I am trying to implement - I can only sell after I buy, and I cannot buy before I have already sold. So basically, 1 and -1 should only happen after they've already happened.

buy      = [ 0,  0,  0,  1,  0,  0,  1,  1,  1,  0,  0,  0,  1,  0]
sell     = [ 0,  0,  0, -1, -1,  0,  0,  0,  0,  0,  0,  0, -1,  0]
expected = [ 0,  0,  0,  1, -1,  0,  1,  0,  0,  0,  0,  0, -1,  0]

      

How can I do this without a for loop? Perhaps with vectors + fillna + NaN values ​​or merge?

+3
python numpy pandas dataframe


source to share


No one has answered this question yet

Check out similar questions:

637
How to remove Pandas DataFrame rows whose value in a specific column is NaN
381
How to check if any value is NaN in Pandas DataFrame
367
How to count NaN values ​​in a column in pandas DataFrame
363
How can I replace all NaN values ​​with Zero in pandas dataframe column
332
How to filter a Pandas dataframe using 'in' and 'not in' like in SQL
1
Python / Pandas concatenation problem with NaN data
1
Different conditions for buying and selling a signal
0
Numpy, vectorizing selection of rows that match buy / sell logic in quantitative analysis
0
How to manipulate a series of pandas with alternating logic without using a loop?
-2
How to perform a simple backtest of a signal in python pandas



All Articles
Loading...
X
Show
Funny
Dev
Pics