Back to Community
Feeding custom data to talib.ROC ?


I would like to feed talib.ROC function the averages of the 14 period EMA based on 15m bar highs and the 14 period EMA based on 15m bar lows. I have not been able to accomplish such a goal. Although I am quite confident that if there is no quick solution I can simply code my own ROC function to work with what I have, I would like to avoid such an inconvenience if possible.

Is there a way to feed the talib.ROC function custom data, in this case the average of 2 emas?
I suspect that one solution may be converting the ema_mid list into a pandas dataframe but I am not sure how to make that work.

Thanks for any and all help!

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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: 59667e76e407575418304939
There was a runtime error.
4 responses

See if this works for you.

highs_15m = data.history(context.spylong,'high',390,'1m').resample('15T', label='right', closed='right').last()  
lows_15m = data.history(context.spylong,'low',390,'1m').resample('15T', label='right', closed='right').last()  

highlow = 14  
rocperiod = 12  

ema_high = talib.EMA(highs_15m.dropna(), timeperiod=highlow)  
ema_low = talib.EMA(lows_15m.dropna(), timeperiod=highlow)  
ema_mid = (ema_high + ema_low) / 2.0  
roc18 = talib.ROC(ema_mid, timeperiod=rocperiod)

A few things. You don't need to iterate with a for loop to get the average of the two ema series. You can manipulate numpy series directly. Also, the 'rocperiod' needs to be 12 or less. Otherwise you will need to fetch more data. Remember that 'roc18' is a numpy series. Not sure what you want to do with it but you may want just the last value of the series (ie roc18[-1]). That will be a scaler value.

Good luck.

Thank you for that, Dan; that was exactly what I needed although I'm curious to ask:

say, I wanted to feed the talib.ROC a list of scalar values, e.g. [1,2,4,8,16,32,64], to calculate the period=3 RoC.
Is there a way I can convert a regular list of scalar values into a pandas dataframe or whatever it needs to be to fit into talib.ROC?

The talib functions expect a numpy array as their inputs. Moreover the values of the array should be real (not integer numbers). A numpy array can be made many ways but a simple one is the 'array' method (see

my_np_array = np.array([1.0,2.0,4.0,8.0,16.0,32.0,64.0])  
roc18 = talib.ROC(my_np_array, timeperiod=rocperiod)

Notice the decimal values in the array. This will ensure it's a real array. Also, ensure 'rocperiod' is less than or equal to the length of the input array.

Hope that helps.

Ah, I see. I attempted that but one of the answers on stackoverflow told me to use np.asarray() instead of np.array().
Thank you again Dan for your miracles.