Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
VIX Futures Strategy / Trouble Importing .csv

Hello,
Trying to backrest a strategy which goes long the ETF 'SVXY' when the VIX futures are in backwardation. This is indicated by a negative value in the 5th column titled "ContBackw". The code currently throws an error when attempting to record the values.
I am having some trouble importing my CSV file containing the data and implementing this strategy. Any help would be appreciated.

Code is as follows:

# Put any initialization logic here.  The context object will be passed to  
# the other methods in your algorithm.  
def initialize(context):

    import datetime  
    import pytz  
    import pandas as pd  
    import numpy as np  
    import zipline  
    import math  
    import talib  
    url = 'https://dl.dropboxusercontent.com/u/1172699/data.csv'  
    fetch_csv(url,  
               date_column = 'date',  
               date_format = '%m/%d/%y',  
               symbol = 'ContBackw')  


    pass  
# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    # data[sid(X)] holds the trade event data for that security.  
    # context.portfolio holds the current portfolio state.

    # Place orders with the order(SID, amount) method.

    # TODO: implement your own logic here.  
    current_bwd = data['ContBackw']['ContBackw']  
    # plot it  
    record(bwd=current_bwd)  
2 responses

Hi Reed,

Take a look at the following code:

import datetime  
import pytz  
import pandas as pd  
import numpy as np  
import zipline  
import math  
import talib  


def initialize(context):  
    url = 'https://dl.dropboxusercontent.com/u/1172699/data.csv'  
    fetch_csv(url,  
               date_column = 'date',  
               date_format = '%m/%d/%y',  
               symbol = 'ContBackw')  
    #: To set your universe of securities so that your algorithm can run  
    sid(24)  

def handle_data(context, data):  
    #: Make sure that we first have data for the security  
    if 'ContBackw' in data['ContBackw']:  
        current_bwd = data['ContBackw']['ContBackw']  
        # plot it  
        record(bwd=current_bwd)  

You'll notice that I added in two things:

sid(24) #Your algorithm requires at least one security in order for it to run handle_data so you can initialize it anywhere, this won't affect your algorithm

and

if 'ContBackw' in data['ContBackw'] #There's a bit of double checking you need to do before directly calling 'ContbackW', first you need to make sure that you have data for it

Let me know if you have any questions

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.

I'm curious, what did you end up using for a sell order in this algo? Vix futures have always been of interest to me and this looks like an interesting strategy.