Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with talib linear regression

Any help would be greatly appreciated. I believe I need to modify the OBV data to pass it through the LINEARREG and LINEARREG_SLOPE functions?... I am getting this error:

TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got numpy.float64)
There was a runtime error on line 73.

Code below:

def record_OBV(context, data):
hist_price = data.history(context.spy, 'price', 200, '1d')
hist_volume = data.history(context.spy, 'volume', 200, '1d')
obv = talib.OBV(hist_price, hist_volume)[-1]
log.info(obv)
obv_trendline = talib.LINEARREG(obv, timeperiod=28)[-1]
obv_slope = talib.LINEARREG_SLOPE(obv, timeperiod=28)[-1]
record(OBV = obv, OBV_Trendline = obv_trendline, OBV_Slope = obv_slope)

3 responses

I made this update - obv_np = pandas.Series(obv, dtype=numpy.float64)

This got rid of my - TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got numpy.float64)

My code will now run, but - log.info(obv_slope) - returns only - nan - values?

Code is below, any help would be greatly appreciated, thanks!

import talib  
import numpy  
import pandas

def initialize(context):  
    context.spy = sid(8554)  
    schedule_function(record_OBV,  
                      date_rules.every_day(),  
                      time_rules.market_close())

def record_OBV(context, data):  
    hist_price = data.history(context.spy, 'price', 200, '1d')  
    hist_volume = data.history(context.spy, 'volume', 200, '1d')  
    obv = talib.OBV(hist_price, hist_volume)[-1]  
    log.info(obv)  
    obv_np = pandas.Series(obv, dtype=numpy.float64)  
    obv_trendline = talib.LINEARREG(obv_np, timeperiod=14)[-1]  
    obv_slope = talib.LINEARREG_SLOPE(obv_np, timeperiod=14)[-1]  
    log.info(obv_slope)  

This may help you:

# Record OBV  
import talib  
# -----------------------------  
sec, period = symbol('SPY'), 28  
# -----------------------------  
def initialize(context):  
    schedule_function(record_OBV, date_rules.every_day(), time_rules.market_close())

def record_OBV(context, data):  
    prices = data.history(sec, 'price', period, '1d')  
    volumes = data.history(sec, 'volume', period, '1d')  
    obv = talib.OBV(prices, volumes)  

    obv_trendline = talib.LINEARREG(obv, period)  
    obv_slope = talib.LINEARREG_SLOPE(obv, period)  

    log.info(obv_trendline[-1])  
    log.info(obv_slope[-1])  

    record(OBV_Trendline = obv_trendline[-1]/100000, OBV_Slope = obv_slope[-1]/5000)  
    record(OBV = obv[-1]/100000)  

Very helpful, problem solved, thank you very much!