Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trading Idea....See if someone can make it happen or if you think this is useless.

So I have been messing around with Rizm(they are lacking the features) and think I have found a very good trading idea but I just lack the knowledge to program it. Here it is

When the VIX close is > or = to 5% above its 10SMA you buy the SPY till the VIX is 5% below its 10SMA or 5days what ever comes first
When the VIX close is > or = to 5% below its 10SMA you sell the SPY till the VIX is 5% above its 10SMA or 5days what ever comes first

I have data to show that since 1989 when you follow this simple rule you will return have a 2.5 to 1 return on the weeks this set up takes place.

I think this would be best setup on a 1 day time frame.

Can anyone make this happen or have any experience with this type of system?

4 responses

I gave this one a shot. It's totally possible I messed up somewhere in my code, but I'm not seeing the kind of returns you mentioned.

Things of note:
- Wasn't sure how much to buy/sell on the days when conditions were met. Currently it's buying or selling 50 shares.
- VIX isn't a symbol in the quantopian universe, so I pulled in a CSV from quandl.

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.

Hello Ludin,

Could you upload the data set which shows the closing prices for VIX you mentioned since 1989?

Hi Saif

As Kathryn said, you can get it from Quandl
https://www.quandl.com/YAHOO/INDEX_VIX-VIX-S-P-500-Volatility-Index

A variation using VXX.
Tested on the last two years, performs adequately. Prior to this, not so much.
Note that the original logic had apparent issues. This version attempts to rectify those.

'''
 Contrarian style:  
 Long Entry: VXX close >= 5% above 10SMA  
  Long Exit: VXX <= 5% below 10SMA  
Short Entry: VXX close <= 5% below 10SMA  
 Short Exit: VXX >= 5% above 10SMA  
       Exit: 5 days

 Momentum style:  
 Long Entry: VXX close < 5% below 10SMA  
  Long Exit: VXX >= 5% above 10SMA  
Short Entry: VXX close > 5% above 10SMA  
 Short Exit: VXX >= 5% above 10SMA  
       Exit: 5 days

Original:  
When the VIX close is > or = to 5% above its 10SMA you buy the SPY  
    till the VIX is 5% below its 10SMA or 5days what ever comes first  
When the VIX close is > or = to 5% below its 10SMA you sell the SPY  
    till the VIX is 5% above its 10SMA or 5days what ever comes first  
'''

import numpy  
import talib

periodsToConsider = 4  
positionDuration = 4  
minimumPercentDeltaThreshold = .04

def initialize(context):  
    context.target = symbol('SPY')  
    context.signal = symbol('VXX')  
    context.openPositionDuration = -1  
    set_benchmark(symbol('SPY'))  
    set_commission(commission.PerTrade(cost=2))  
    schedule_function(func=HandleDataScheduled,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open(hours=0, minutes=1))

def handle_data(context, data):  
    pass

def HandleDataScheduled(context, data):  
    prices      = history(periodsToConsider, '1d', 'price')  
    targetPrice = prices[context.target][-1]  
    signalPrice = prices[context.signal][-1]  
    sma         = prices.apply(talib.SMA, timeperiod=periodsToConsider).iloc[-1]  
    signalSMA   = sma[context.signal]  
    pctDelta    = (signalPrice - signalSMA) / signalSMA

    isLong  = context.portfolio.positions_value > 0.0  
    isShort = context.portfolio.positions_value < 0.0  
    isBuyLongCondition    = False  
    isSellLongCondition   = False  
    isSellShortCondition  = False  
    isCoverShortCondition = False

    ### Contrarian style:  
    #if (pctDelta >= minimumPercentDeltaThreshold):  
    #    isBuyLongCondition = True  
    #if (pctDelta <= -minimumPercentDeltaThreshold):  
    #    isSellLongCondition = True  
    #if (pctDelta <= -minimumPercentDeltaThreshold):  
    #    isSellShortCondition = True  
    #if (pctDelta >= minimumPercentDeltaThreshold):  
    #    isCoverShortCondition = True

    ### Momentum style:  
    if (pctDelta <= -minimumPercentDeltaThreshold):  
        isBuyLongCondition = True  
    if (pctDelta >= minimumPercentDeltaThreshold):  
        isSellLongCondition = True  
    if (pctDelta >= minimumPercentDeltaThreshold):  
        isSellShortCondition = True  
    if (pctDelta <= -minimumPercentDeltaThreshold):  
        isCoverShortCondition = True

    if (isLong or isShort):  
        ### Period exit  
        context.openPositionDuration -= 1  
        if (context.openPositionDuration < 0):  
            order_target_percent(context.target, 0)  
            print("<<<  Time exit: {0} @ {1:<7.2f}".format(context.target.symbol, targetPrice))  
        ### Long exit  
        elif (isLong and isSellLongCondition):  
            order_target_percent(context.target, 0)  
            print("<<<  Long exit: {0} @ {1:<7.2f}".format(context.target.symbol, targetPrice))  
        ### Short exit  
        elif (isShort and isCoverShortCondition):  
            order_target_percent(context.target, 0)  
            print("<<< Short exit: {0} @ {1:<7.2f}".format(context.target.symbol, targetPrice))

    ### Long entry  
    if (not isLong and isBuyLongCondition):  
        order_target_percent(context.target, 1)  
        context.openPositionDuration = positionDuration  
        print("    Long entry: {0} @ {1:<7.2f} >>>".format(context.target.symbol, targetPrice))

    ### Short entry  
    elif (not isShort and isSellShortCondition):  
        order_target_percent(context.target, -1)  
        context.openPositionDuration = positionDuration  
        print("   Short entry: {0} @ {1:<7.2f} >>>".format(context.target.symbol, targetPrice))

    record(Target=targetPrice, Signal=signalPrice, SignalSMA=signalSMA, PctDelta=pctDelta)