Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using BBands with CustomFactor

Hello,

I have been trying to use the talib bbands library with in a custom facteur class.
It may come from the fact I am not sure what output is exected from a pipeline.

I have the error:
AssertionError: real has wrong dimensions

Could you please help me understan why?

I am trying to set a trading rule that takes into account the bollinger band width on the VIX to trade XIV.

import pandas as pd
import numpy as np
import talib as ta
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor, Latest
from quantopian.pipeline.data.quandl import cboe_vix, cboe_vxv, cboe_vxd, cboe_vvix
from quantopian.research import run_pipeline

class GetVIX(CustomFactor):
window_length = 1
def compute(self, today, assets, out, vix):
out[:] = vix[-1]

class GetVIXbolls(CustomFactor):
window_length = 20
def compute(self, today, assets, out, vix):
upper, middle, lower = ta.BBANDS(vix, timeperiod = self.window_length, nbdevup = 2, nbdevdn = 2, matype=0)

    out[:] = upper - lower

def make_pipeline():
pipe = Pipeline()
pipe.add(GetVIX(inputs = [cboe_vix.vix_close]), 'Vixclose')
pipe.add(GetVIXbolls(inputs = [cboe_vix.vix_close]), 'vixmean')
return pipe

def initialize(context):
pipe = make_pipeline()

result = run_pipeline(make_pipeline(), '2015-03-05', '2015-05-05')
result.head()

Many thanks

3 responses

Hello

If that can help anyone, I had the answer from another post here, BBANDS as much talib functions only takes 1D arrays!
an np.squeeze does the job here

class GetVIXbolls(CustomFactor):
window_length = 20
def compute(self, today, assets, out, vix):
a = np.squeeze(vix)
upper, middle, lower = ta.BBANDS(a, timeperiod = self.window_length, nbdevup = 2, nbdevdn = 2, matype=0)

out[:] = (upper - lower)[-1] # BBANDS returns arrays of the resutlt from the sliding window over the input array and only retunr non Nans if the sliding windowis fully filled  

still I found very hard to find proper specs of the talib libs, if anyone knows where to find with good cooking examples I would be very grateful!
Thank you

These should all have clonable backtests that use talib. Adjust for any particular talib indicator. Click 'Tools' and then 'Past year' to sort by date. There's also Q's built-in search where you can specify backtests.

https://www.google.com/search?q="import+talib"+"clone+algorithm"+bollinger+site%3Aquantopian.com