The code and idea is quite simple, the algorithm pulls daily settlement prices for VIX front month and second month futures and calculates the ratio of the Front Month/Second Month. If this ratio is below a certain threshold, in my case I chose 0.90, short VXX, otherwise close out all positions and move to cash. I'm testing this strategy and the results look really nice, but I'm hoping for input to help reduce the drawdown. Also, if anyone has spotted errors in my code or any biases, please let me know!

Clone Algorithm

358

Loading...

There was an error loading this backtest.

Backtest from
to
with
initial capital

Cumulative performance:

Algorithm
Benchmark

Custom data:

Total Returns

--

Alpha

--

Beta

--

Sharpe

--

Sortino

--

Max Drawdown

--

Benchmark Returns

--

Volatility

--

Returns | 1 Month | 3 Month | 6 Month | 12 Month |

Alpha | 1 Month | 3 Month | 6 Month | 12 Month |

Beta | 1 Month | 3 Month | 6 Month | 12 Month |

Sharpe | 1 Month | 3 Month | 6 Month | 12 Month |

Sortino | 1 Month | 3 Month | 6 Month | 12 Month |

Volatility | 1 Month | 3 Month | 6 Month | 12 Month |

Max Drawdown | 1 Month | 3 Month | 6 Month | 12 Month |

# Algorithm uses front month and second month settlement prices for VIX futures to # determine whether or not to short the ETF VXX. Current threshold is 0.95 representing # the percentage contango of F1/F2. If the settlements F1/F2 is below 0.95, short VXX, # otherwise move to cash. import datetime import numpy as np import pandas as pd # Post Function for fetch_csv where futures data from Quandl is standardized def rename_col(df): df = df.rename(columns={'Close': 'price','Trade Date': 'Date'}) df = df.fillna(method='ffill') df = df[['price', 'Settle','sid']] # Shifting data by one day to avoid forward-looking bias return df.shift(1) def initialize(context): # Pulling front month VIX futures data fetch_csv('http://www.quandl.com/api/v1/datasets/CHRIS/CBOE_VX1.csv', date_column='Trade Date', date_format='%Y-%m-%d', symbol='v1', post_func=rename_col) # Pulling second month VIX futures data fetch_csv('http://www.quandl.com/api/v1/datasets/CHRIS/CBOE_VX2.csv', date_column='Trade Date', date_format='%Y-%m-%d', symbol='v2', post_func=rename_col) # Declaring VXX as the stock for shorting context.stock = sid(38054) # Scheduling the order function to occur everyday 5 minutes after market open schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours = 0, minutes = 5)) def my_rebalance(context, data): # Calculating the contango ratio of the front and second month VIX Futures settlements last_ratio = data.current('v1','Settle')/data.current('v2','Settle') # Specifying the contango ratio threshold to short VXX threshold = 0.90 log.info(context.portfolio.cash) if last_ratio < threshold: if context.stock in context.portfolio.positions: log.info("Already in - %.2f" %last_ratio) else: # Calculating the number of shares of VXX to short to reach the target # portfolio percentage allocation target_pct = 1 target_mv = context.portfolio.portfolio_value*target_pct shares = np.floor(target_mv/data.current(context.stock,'price')) order(context.stock,-shares) log.info("Short VXX - %.2f" %last_ratio) record(shares=shares) else: # If the contango ratio is above specified threshold, purchase back VXX shares and remain in cash order_target_percent(context.stock,0) log.info("Move to cash - %.2f" %last_ratio) record(ratio=last_ratio)