vendredi 2 août 2019

Python strange if statement behaviour

I have an abstract class in which there are 3 states:

class AbstractStrategy:
    SELL = -1
    HOLD = 0
    BUY = 1

I have another child class which inherits from my parent class:

class ChildStrategy(AbstractStrategy):

I have an if statement in this child class as follows:

        if current_state != self.HOLD and state == self.HOLD:
            buy_sell_list[i] = current_state
            state = current_state
        else:
            buy_sell_list[i] = current_state

        current_state = state

This code generates one of the 3 trade signals: Buy, Hold or Sell. However, even though the if statement result is false, it sometimes enters the statement and executes the code.

What I mean is; this statement is actually false because current_state is not HOLD:

if current_state != self.HOLD and state == self.HOLD:

but when I debug the code, it enters the if block and executes the code in it. When I evaluate the if statement in PyCharm, I get false response, yet, it enters into it.

When I change the statement to;

if current_state != 0 and state == 0:

then it does not enter into the if statement (works correct).

What might cause this?

PS: if I change the code like this, it works correct:

            if current_state != int(self.HOLD) and state == int(self.HOLD):
                buy_sell_list[i] = current_state
                state = current_state
            else:
                buy_sell_list[i] = current_state

            current_state = state

Aucun commentaire:

Enregistrer un commentaire