Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pulling daily VIX index values

I'm trying to pull the daily value of VIX from quandl and then use it to test a trading plan. I've been able to get a dataframe with the daily VIX, but I can't figure out how to pull the actual number from this. Here is my code:

"""
This is a template algorithm on Quantopian for you to adapt and fill in.  
"""
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline.factors import AverageDollarVolume  
from quantopian.pipeline.data.quandl import yahoo_index_vix as cboe_vix  
def initialize(context):  
    """  
    Called once at the start of the algorithm.  
    """  
    # Rebalance every day, 1 hour after market open.  
    schedule_function(my_rebalance, date_rules.every_day())  
    pipe = Pipeline()  
    attach_pipeline(pipe, name='VIX_pipeline')  
    pipe.add(cboe_vix.close.latest, 'vix')  
def before_trading_start(context, data):  
    """  
    Called every day before market open.  
    """  
    context.VIX = pipeline_output('VIX_pipeline')  
    context.spy = sid(8554)

    print context.VIX  
    # These are the securities that we are interested in trading each day.  
    #context.security_list = context.output.index  
def my_rebalance(context,data):  
    """  
    Execute orders according to our schedule_function() timing.  
    if context.VIX < 14:  
        order_target_percent(context.spy, 0.1)  
    if context.VIX > 30:  
        order_target_percent(context.spy, -0.1)  
        """  
    pass  
def handle_data(context,data):  
    """  
    Called every minute.  
    """  
    pass

How can I get the value of VIX into a variable I can use in the if statements?

Thanks.

1 response

Hi Matthew,

.iloc indexes into a Pandas DataFrame. So you can do context.VIX.iloc[0, 0] to get a single value out of the DataFrame.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.