Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie needs help!

Python program gurus,

I am trying to test a simple algorithm.

Step 1. Every trading day, 15 minutes before close (this is important, as the market closes at different times on some holidays), read the current market value of vix, vxv, and the one- and two-month vix futures. These are available from the following links:

VIX: http://finance.yahoo.com/q?s=%5EVIX
VXV: http://finance.yahoo.com/q?s=%5EVXV
One- and two-month vix futures: http://www.cboe.com/micro/vix/vixfuturesprices.aspx

Step 2: Depending on the ratio of these 4 things, go long or short the SPY.

The idea is that if VIX is too low, buy SPRO and short the market. If it is too high, buy UPRO and go long the market. I need the program to paper trade (and may be trade for real too), and not backtest. I have already backtested in Excel.

Any help and sample program will be most appreciated!

Thanks!

1 response

Hey Apratim,
It doesn't look like Quantopian currently has those securities built-in, though they can probably be added in the future. Unfortunately, there is also no way it import external data during a trading day, since data is only loaded when the market is about to open. However, I think there is one way that could work. Since those indices don't change during the day, you can manually create those indices in Quantopian using fetch_csv. Host a .csv on a server and set it to update the securities and universe it defines every morning before the market opens with the new indices. See "Using Fetcher to create a custom universe" here. Note that the universe size is limited to 200 here, which means the sum of the size of both indices cannot exceed 200. But you can have a universe that updates every morning according to an external file.

This is pretty cumbersome, and you might run into problems doing this, but it's an idea. Another simpler option might be trying to generate something very similar to those indices within Quantopian. You could use set_universe to find all stocks that have been volatile in the past X days, etc. You would need to figure out how to quantify volatility, but this seems realistic.

As far as doing something a few minutes before the market closes and accounting for early closes, you can use this:

import pandas as pd  
import datetime  
from zipline.utils.tradingcalendar import get_early_closes  

def initialize(context):  
    # Find what days the market closes early  
    start_date = datetime.date(2002, 1, 3)  
    end_date = datetime.date.today()  
    context.early_closes = get_early_closes(start_date,end_date).date

def handle_data(context, data):  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')

    if exchange_time.date() in context.early_closes and exchange_time.hour == 12 and exchange_time.minute == 45:  
        end_day(context)  
    elif exchange_time.date() not in context.early_closes and exchange_time.hour == 15 and exchange_time.minute == 45:  
        end_day(context)  

Hopefully that's helpful, let me know what you decide to do and if you have any more questions!

Gus

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.