| Overall Statistics |
|
Total Trades 1135 Average Win 0.70% Average Loss -0.58% Compounding Annual Return -3.561% Drawdown 28.900% Expectancy -0.060 Net Profit -19.562% Sharpe Ratio -0.278 Loss Rate 57% Win Rate 43% Profit-Loss Ratio 1.20 Alpha -0.023 Beta 0.016 Annual Standard Deviation 0.079 Annual Variance 0.006 Information Ratio -0.735 Tracking Error 0.152 Treynor Ratio -1.389 Total Fees $1135.00 |
namespace QuantConnect
{
/*
* Little algorithm that goes long in a stock when VIX goes down
* Likewise, goes short when VIX goes up
* [By JPB for Stephen Oehler]
*/
public class VolatilityETN : QCAlgorithm
{
// Initialise tickers
string ticker = "SPY";
string volindex = "YAHOO/INDEX_VIX";
// Initialise moving averages
ExponentialMovingAverage emaFast;
ExponentialMovingAverage emaSlow;
// Current trading direction
int _dir = 0;
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
SetStartDate(2010, 1, 1);
SetEndDate(2016, 1, 1);
SetCash(25000);
// Add the SPY
AddSecurity(SecurityType.Equity, ticker, Resolution.Hour);
// Add the VIX (from Quandl)
AddData<Quandl>(volindex, Resolution.Daily);
// Create moving averages based on VIX
emaFast = EMA(volindex, 1);
emaSlow = EMA(volindex, 5);
//Manually creating and subscribing indicator for data updates.
//This allows the indicator to automatically be the right value.
var SlowConsolidator = new DynamicDataConsolidator(1, TimeSpan.FromDays(1));
RegisterIndicator(volindex, emaFast, SlowConsolidator, x => x.Value);
var FastConsolidator = new DynamicDataConsolidator(1, TimeSpan.FromDays(1));
RegisterIndicator(volindex, emaSlow, FastConsolidator, x => x.Value);
}
//Data Event Handler for Quandl data
public void OnData(Quandl data)
{
// Leave this for now (don't need it for logic)
}
// Handle data in OnData(Slice data) event
public void OnData(TradeBars data)
{
// Before OnData() is called, moving averages are updated
// So we can now simply use them in our trading logic
// Trading logic will be that VIX is the inverse of SPY
// So if moving average of VIX goes up, we expect SPY to go down
// buy/sell logic
if (_dir != -1 && emaFast > emaSlow) { // moving average VIX up && not currently short
Liquidate(ticker); // Liquidate current position
Order(ticker, -100); // go SHORT
_dir = -1;
} else if (_dir != 1 && emaFast < emaSlow){ // moving average VIX down && not currently long
Liquidate(ticker); // Liquidate current position
Order(ticker, 100); // go LONG
_dir = 1;
}
}
}
}