vendredi 26 novembre 2021

value: error when using if else and a flag statement in python

Im trying to set a flag on my if else statement. I want it to be 1 if it crossover the upper thres but only the first cross after the previous signal and the same if it crossunder the lower thres (-1 on first cross under) . otherwise i want it to be 0.

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

| as is | what i want |

| -------- | ----------- | | 1 | 1 | | 1 | -1 | | 1 | 0 | | 1 | 1 |

def is_triggered(row, flag):
    if  flag == -1 and  ((row.D >  row.UPPER ) and (row.D_PREV  < row.UPPER)) :
        flag = 1
        
    if  flag == -1 and  ((row.D <=  row.UPPER ) and (row.D_PREV  >= row.UPPER)) :
        flag = 1    
    
    else:
        flag=0
    
    return flag

flag=-1
df['SIG'] = is_triggered(df, flag)

Aucun commentaire:

Enregistrer un commentaire