lundi 10 mai 2021

IF statement / For Loop dilemma for formula

So I have been stuck on this problem for a while now. I am trying to replicate this formula below within Python:

 FINAL UPPERBAND = IF( (Current BASICUPPERBAND < Previous FINAL
 UPPERBAND) or (Previous Close > Previous FINAL UPPERBAND))
                     THEN (Current BASIC UPPERBAND) ELSE Previous FINALUPPERBAND)

My problem is that is throwing me off is how to create FINAL UPPERBAND when it is used within the If statement too. For reference BASIC UPPERBAND and CLOSES are their own dataframes which is making it a bit trickier too. The only way around this I can think is a for loop but the dataframes are pretty big so going to take ages.

I have tried other things such as pandas.where apply and things like this final_uppers[(basic_uppers < basic_uppers.shift(1))] = basic_uppers which when I add together give me mostly the correct answers but about 1/4 are wrong. And I believe this is due to filling the rows not in order. This is what I have now, but like I say there are still values that are incorrect:

final_uppers = pd.DataFrame(columns=closes.columns, index=closes.index)
final_uppers = final_uppers.fillna(0)
final_uppers[(basic_uppers < basic_uppers.shift(1))] = basic_uppers
final_uppers[(closes.shift(1) > final_uppers.shift(1))] = basic_uppers
final_uppers = final_uppers.replace(to_replace=0, method='ffill')

Any help would be appreciated, or if someone could help me with looping through just to test how long it would take using a loop. But any ideas of how to get around this would be appreciated. :)

Aucun commentaire:

Enregistrer un commentaire