Numpy.where()

in short selling in trading, section:6, unit: 5 , i dont under stand whey we use the 1, -1 and 0 with below code

data['regime_breakout_252'] = np.where(data['rebased_high'] >= data['rolling_high'], 1, 0)

data['regime_breakdown_252'] = np.where(data['rebased_low'] <= data['rolling_low'], -1, 0)

The mentioned code says that when the rebased high is higher than the rolling high, we long the stock, which is represented by 1, otherwise do nothing which is represented by 0.



Similarly, in the second code, when the rebased high is lower than the rolling low, we sell the stock and is represented by -1.



1, and -1 are used to represent the buy and sell signal.



Can also refer to the documentation of the numpy.where() method.

 

thx Vibhu