Back to Community
Using context in Customfactor to store data

I like to get historic cboe_vix.vix_close data. Vladimir gave a workaround solution (post.

Has anyone tried to pass context into Customfactor to store data? Something like this:

class VIX(CustomFactor):
#inputs = [cboe_vix.vix_close, con]
window_length = 1000
def compute(self, today, assets, out, vix, con):
con.vix = vix.ravel()
out[:] = vix[-1:]

Then when make pipeline, we call VIX and pass as inputs cboe_vix.vix_close and the context (back test only). I am just wondering if it is possible at all.

3 responses

As I mention before, you may create your own CustomFactor using as much data as avalable and use pipeline to get the results.
Here are CustomFactors for 100 and 1000 day moving averages of VIX.
Try them.

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline, CustomFactor  
from quantopian.pipeline.data.quandl import cboe_vix  
import pandas as pd  
# --------------------  
MA_F, MA_S = 100, 1000  
# --------------------  
def initialize(context):  
    columns = {'VixClose':  GetVIX(), 'VixMavgF': GetVixMavgF(), 'VixMavgS': GetVixMavgS()}  
    attach_pipeline(Pipeline(columns), 'vix_pipeline')  

def before_trading_start(context, data):  
    output = pipeline_output('vix_pipeline')  
    vix_last = output['VixClose'].iloc[-1]  
    vix_mavg_fast = output['VixMavgF'].iloc[-1]  
    vix_mavg_slow = output['VixMavgS'].iloc[-1]  

    record(vix_last = vix_last, vix_mavg_fast = vix_mavg_fast, vix_mavg_slow = vix_mavg_slow)  

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

class GetVixMavgF(CustomFactor):  
    inputs = [cboe_vix.vix_close]  
    window_length = MA_F  
    def compute(self, today, assets, out, close):  
        close = close.ravel()  
        mean_close = pd.Series(close).rolling(MA_F).mean()  
        out[:] = mean_close.iloc[-1]  

class GetVixMavgS(CustomFactor):  
    inputs = [cboe_vix.vix_close]  
    window_length = MA_S  
    def compute(self, today, assets, out, close):  
        close = close.ravel()  
        mean_close = pd.Series(close).rolling(MA_S).mean()  
        out[:] = mean_close.iloc[-1]  

Vladimir,

Much appreciate again. Maybe I was not clear for what I want. I like to pull historical data to study relationship with some patterns, not making moving average. Your first proposed solution may help with a little bit of overhead to get the entire data set .

Something like this may work:

def initialize(context):  
    context.vix # Defined here

class VIX(CustomFactor, context):  
    inputs = [cboe_vix.vix_close, context.vix]  
    window_length = 1000  
    context.vix.window_safe = True # Manually flag this as True if it is window safe

    def compute(self, today, assets, out, vix, con):  
        con.vix = vix.ravel()  
        out[:] = vix[-1:]