np.where() causing RSI to end early

好久不见. 提交于 2019-12-08 07:00:22

问题


I initially began with one np.where() to run my RSI rules of sending a buy signal (1.0) when the RSI (rsip) dips below 20 (lower_bnd), and selling (0.0) above 80 (upper_bnd). The program bought correctly, but sold as soon as rsip went above 20. I would like for the program to hold onto the purchase until the RSI hits 80.

This was my original function:

signals['signal']= np.where(signals['rsip'] < lower_bnd, 1.0, 0.0)

Signals is a dataframe containing a series of signals and the RSI level.

I attempted to nest additional np.where() along with Boolean operators to ensure that the signal is held until it hits 80+:

signals['signal'] = np.where(signals['rsip']
            < lower_bnd, 1.0, np.where((signals['rsip'] 
            < upper_bnd) & (signals['signal']==1.0), 1.0, np.where(signals['rsip'] 
            > upper_bnd, 0.0,np.where((signals['rsip']
            < upper_bnd) & (signals['signal']==0.0), 0.0, 0.0))))

The above essentially says, " If lower than 20, buy(1.0); else if under 80 and signal is already =1.0, continue with the same signal; else if higher than 80, sell (0.0); else if lower than 80 and signal is already = 0.0, continue with 0.0"...

However I'm met with the error.

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

When I add .values[-1] to the end of signals['rsip'], every signal in the dataframe is a 1.0 which leads me to believe this is not the right approach...

I'm very lost after many hours of troubleshooting... I'd love to hear any ideas or solutions there are. Thanks for your time.

Edit: Using a slightly different with the approach by using this code:

signals['signal'] = np.where(signals.rsip
        <= lower_bnd, 1.0, np.where(((signals.rsip 
        > lower_bnd) & (signals.signal==1.0)), 1.0, np.where(((signals.rsip 
        >= upper_bnd) &(signals.signal==0.0)), 0.0, 0.0)))

I don't incur any error messages, however, it is working just as effectively as the original, singular np.where() statement.

来源:https://stackoverflow.com/questions/51624113/np-where-causing-rsi-to-end-early

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!