Back to Community
Removing NAN from Talib ATR Function

I am trying to remove the NaN values that are being created by talib.ATR. It will run for a little bit, but then fails because of NaNs on line 107. I have checked the stop losses and they seem right, but I cannot figure out how to remove the Nan. It gets a NA value from the ATR calculation.

I have tried to dropna() from the history statements, "if atr_stop==float("NaN"): continue", but none of those statements will work. It would be great if someone could tell me what is wrong with the algorithm.

Thanks,

Eric

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 590a38740f696b61e8cc43a1
There was a runtime error.
2 responses

These added three lines will avoid the nans and you might want to see if ATR values are any more accurate with forward and back fill instead of dropna().

            highs    = data.history(stock, 'high' , 25, '1d').ffill().bfill()  
            lows     = data.history(stock, 'low'  , 25, '1d').ffill().bfill()  
            closes   = data.history(stock, 'close', 25, '1d').ffill().bfill()  
            if np.all(np.isnan(highs)):  continue  
            if np.all(np.isnan(lows)):   continue  
            if np.all(np.isnan(closes)): continue  

It still will not run, I think it has something to do with adding positions that it does not know about. I get these logs right before it fails. I know I shouldn't have shorts, and my leverage is kind of different to get the equal weights.

new_weight=1/(existing+new-stopped_out)

Then I rebalance it every day based on the amount of positions it has. That is why I append to the context.exist from context.portfolio.positions
The problem is it will never sell back the total amount of the initial position if I am always having to reweigh it.

2005-04-26 14:00 WARN Your order for -29 shares of MBG failed to fill by the end of day and was canceled.
2005-10-27 14:00 WARN Your order for -1 shares of PKZ failed to fill by the end of day and was canceled.
2005-12-21 14:00 WARN Your order for -2 shares of PHS failed to fill by the end of day and was canceled.
2005-12-23 14:00 WARN Your order for -1 shares of GP failed to fill by the end of day and was canceled.
2006-01-27 14:00 WARN Your order for 3 shares of IVX failed to fill by the end of day and was canceled.
2006-03-23 14:00 WARN Your order for -6 shares of CEPH failed to fill by the end of day and was canceled.
2006-03-23 14:00 WARN Your order for -1 shares of IMDC failed to fill by the end of day and was canceled.
2006-04-03 14:00 WARN Your order for -2 shares of ABGX failed to fill by the end of day and was canceled.
2006-04-10 14:00 WARN Your order for 1 shares of BR failed to fill by the end of day and was canceled.
2006-04-19 14:00 WARN Your order for 1 shares of BR failed to fill by the end of day and was canceled.
2006-04-20 14:00 WARN Your order for 1 shares of BR failed to fill by the end of day and was canceled.
2006-04-21 14:00 WARN Your order for 1 shares of BR failed to fill by the end of day and was canceled.
2006-05-02 14:00 WARN Your order for -1 shares of BR failed to fill by the end of day and was canceled.
End of logs.