Course Name: Short Selling in Trading, Section No: 6, Unit No: 14, Unit type: Notebook
In the function, we have the following line of code:
df['regime_sma'+''+str(short_term)+''+str(long_term)] = np.where(sma_st>= sma_lt,1,np.where(sma_st < sma_lt,-1,np.nan))
However, it can be more simply represented as:
df['regime_sma'+''+str(short_term)+''+str(long_term)] = np.where(sma_st>= sma_lt,1,-1)
Can you please confirm my understanding is correct?
Thank you,
Ryan
In the context of the code that you are talking, your understanding is correct. Both codes will generate the same output.
But let's check another scenario where your data contains nan values.
array_1 = np.array([1, 5, 4, 10, np.nan])
array_2 = np.array([1, 3, 6, 12, 4])
np.where(array_1 >= array_2, 1.0, np.where(array_1 < array_2, -1.0, np.nan))
[ 1. 1. -1. -1. nan]
np.where(array_1 >= array_2, 1.0, -1.0)
[ 1. 1. -1. -1. -1.]
You can clearly observe the difference in the output of the last element.