mercredi 10 juin 2020

Python - Error with IF Statements:Dataframe new column not returning proper value when IF is True

I have a loop with multiple if statements. If the first statement is met, I want to return the multiplication between an int or float that I coded in the line multiplied by the number of shares in one of the columns. I want the loop to run row by row. However, my IF statements are not reading correctly, as the first line item should be returning 3, and not 30. See the image attached. Thanks, Guillaume

    trading['position size'] = 0

    for idx, row in trading.iterrows():
        i = trading['Close Price']
        x = trading['20-day SMA']
        y = trading['200-day SMA']
        z = trading['num of shrs']
        if (i > x) and (i > y):
            trading['position size'] = (1*z)
        elif (i > x) and (i < y) or (i < x) and (i > y):
            trading['position size'] = (0.5*z)
        elif (i < x) and (i < y):
            trading['position size'] = (0.1*z)
        else:
            trading['position size'] = np.nan

enter image description here

Aucun commentaire:

Enregistrer un commentaire