Home > Back-end >  Python - iterate over pandas DataFrame until condition is met, then continue iterating until a diffe
Python - iterate over pandas DataFrame until condition is met, then continue iterating until a diffe

Time:04-28

I have a pandas DataFrame (new_df) of the following form:

index   open      high      low       close       buy     sell    sl_price    tp_price
9124    1.12593   1.12690   1.12340   1.12519     0.0     0.0       
9125    1.12519   1.12714   1.12401   1.12542     1.0     0.0     1.11289     1.13794
9126    1.12542   1.12551   1.12462   1.12477     0.0     0.0       
9127    1.12477   1.12504   1.12390   1.12395     0.0     0.0       
9128    1.12395   1.12450   1.12326   1.12392     0.0     0.0       
9129    1.12392   1.12554   1.12050   1.12077     0.0     0.0       
9130    1.12077   1.12201   1.11085   1.12068     0.0     0.0       
9131    1.12068   1.12160   1.11860   1.11957     0.0     0.0       

'open' 'high' 'low' 'close' columns are all prices. The 'buy' and 'sell' columns contain either 0 or 1. 'sl_price' and 'tp_price' are nan, unless 'buy' = 1 or 'sell' = 1, then they are also prices.

I want to iterate through the 'buy' column until I see a 1, at which point I want to record the associated sl_price and tp_price, then go down row by row, and if I first see a 'low' value which is less than 'sl_price', I want to append -1 to a blank list called long_trades. But if I first see a 'high' value which is greater than 'tp_price', I want to append 1 to the long_trades list. Whichever of those 2 conditions are met first, I want to append the 1 or -1 to long_trades list, then have the program go back to searching for 'buy' = 1, and repeat the process over again.

It's safe to assume that both those two conditions, (low < sl_price and high > tp_price) won't ever be met at the same time (row).

I hope that makes sense. Hopefully my attempt below clears up what I'm trying to do a bit. I imagine the answer will be some combo of for/while loops but I can't wrap my head around the logic.

Here's what I've come up with:

long_trades = []

for i in range(len(new_df['buy'])):
    
    trade_on = False
    
    if new_df['buy'][i] == 1:
        trade_on = True
        sl, tp = new_df['sl_price'][i], new_df['tp_price'][i]
        
        while trade_on == True:

            if new_df['low'][i] < sl:
                long_trades.append(-1)
                trade_on = False
            
            if new_df['high'][i] > tp:
                long_trades.append(1)
                trade_on = False

But when I run this the console just runs forever and the long_trades list never populates and I have to stop the console interrupting. I think it's getting stuck in an infinite loop but I cant see how.

Thanks for your time!

CodePudding user response:

Your code gets stuck in the while loop since it never updates i so you're just checking the same row over and over.

        while trade_on == True:
            # code gets stuck here because i isn't updated

            if new_df['low'][i] < sl:
                long_trades.append(-1)
                trade_on = False
            
            if new_df['high'][i] > tp:
                long_trades.append(1)
                trade_on = False

So instead, we could use just loop and break explicitly like this:

        for j in range(i, len(new_df['buy'])):          
            # I am assuming here you want to go back to the row
            # where you first encountered a 1 after this loop

            if new_df['low'][j] < sl:
                long_trades.append(-1)
                break
            
            if new_df['high'][j] > tp:
                long_trades.append(1)
                break
  • Related