Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using VIX as a volatility filter. How do i filter out days with high volitility.

Hi, i don't want to trade when the VIX is above 25 (for example). What is the best way to achieve this in the pipeline?

Should i add the VIX to each symbol using a factor/filter or is there another better way?

11 responses

If anyone is interested i just added it as a factor and created a column for the vix

Hey Kaya, do you have a Notebook available?

Also, if I understand VIX and look forward bias properly, adding a column for VIX to filter out HV days would not be useful if you are running re-balance daily.

Zoran, can you pls explain the look forward bias

I am also very new to all of this but if I remember properly from one of the related videos, you would not know if the day was high volatility before the day is over. So whatever you do to exclude this type of days would be using the information not available at relevant time when making the trading decisions. Imagine buying at market open just before market crushes with huge volatility at noon. You would not know of this volatility at 10am when your trades are being placed.
Thant is how I understand it but again, very new and learning so please confirm in the training materials.

I second Zoran's post. Don't do this if rebalance is running daily.
Beyond that, there's not an easy way/ I can't think of anything obvious to say "if the VIX is above X points, don't make any trades that day" purely within the Pipeline. Your best bet, in my opinion, is having a boolean that decides if your rebalance method runs associated with the value your Pipeline. That'd probably be the easiest, but you'd need to set it to check it either something like an hour after market open, or using yesterday's close.

I was using this in a daily strategy, my assumption is a Qantopian acts on the next bar following a signal (as this is how most backtesters do it)

Ok, so what you could do with that is filter your pipeline based on the VIX value column for yesterday's close. AKA in your pipeline forming method, add a filter to it that removes all securities that don't have a 'Vix' <= 25. This will achieve what you want, but you'll need to make sure that you run your rebalance daily. If you don't know how to do this, just go to the docs. There's an example of this that you can use and just swap out the variables and plug and play it.

I would use numpy.where to make the vix factor a boolean. Then use the boolean to put in front of your factors to switch them on and off. Multiply by 1 in the on state and multiply by 0 in the off state.

import numpy as np

class VIX_Switch(CustomFactor):  
    inputs = [cboe_vix.vix_close]  
    window_length = 1  
    def compute(self, today, assets, out,  vix):  
        out[:] = np.where(np.nan_to_num(vix[-1]) < 25, 1, 0)

VIX_Switch()*YourCustomFactor(mask=Universe).rank()  # Factor is on when Vix < 25 and off when vix >= 25  

Thanks Matthew and LM!

Mathew, that is how I was doing it. You mentioned using "yesterdays close of the vix", would this apply for all signals when using close price?
Most backtesters use the "next" bar after the signal to trade to avoid look-ahead bias.

LM could this approach (without the *yourcustomfactor) be a good way to analyze with alphalens?

https://www.quantopian.com/faq#backtester

Here it says that basically the signal is generated in the event loop and an order is placed, the order is executed in the next event loop. In my case this will be the next day. They do this to avoid look-ahead bias. Therefore there is no reason to look back a day when calculating signals.