Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-54.273%
Drawdown
19.900%
Expectancy
0
Net Profit
0%
Sharpe Ratio
-0.808
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.455
Beta
-0.234
Annual Standard Deviation
0.498
Annual Variance
0.248
Information Ratio
-0.338
Tracking Error
0.523
Treynor Ratio
1.72
Total Fees
$1.87
using MathNet.Numerics.Statistics;

namespace QuantConnect 
{   
    public class TradingTheOdds : QCAlgorithm
    {
    	RollingWindow<double> _closingPrices;
    	ExponentialMovingAverage _ema;
    	string _symbolGSPC = "YAHOO/INDEX_GSPC";
    	string _symbolVIX = "SPDJ/SPVIXSTR";

    	
        public override void Initialize() 
        {
        	// Minimum Start Date: 2011-3-1
            SetStartDate(2015, 1, 1);         
            SetEndDate(2015,2,1);
            SetCash(10000);
            //AddSecurity(SecurityType.Equity, "SPY", Resolution.Daily);
            AddSecurity(SecurityType.Equity, "XIV", Resolution.Daily);
            AddSecurity(SecurityType.Equity, "VXX", Resolution.Daily);
            
            AddData<Quandl>(_symbolGSPC, Resolution.Daily);
            AddData<QuandlVixContract>(_symbolVIX, Resolution.Daily);
            
            //Initialize:
            _closingPrices = new RollingWindow<double>(5);
            _ema = new ExponentialMovingAverage("AvgVixLessASTD", 5);
        }

        public override void OnData(Slice data) 
        {   
        	Log("Slice is invoked.");
        	try
        	{
	        	//Save off the closing prices
	        	//_closingPrices.Add((double)data.Bars["SPY"].Close);
	        	var gspc = data.Get<Quandl>(_symbolGSPC);
	        	_closingPrices.Add((double)gspc.Value);
	        	
	        	//Wait till we've got a week data:
	        	if (!_closingPrices.IsReady) return;
	        	
	        	//Save Vix price:
	        	var vix = data.Get<Quandl>(_symbolVIX);
	        	
	        	//Calculate the annualized standard deviation of the last 2 days
	        	//standard deviation((LN(todaysclose/yesterdaysclose), LN(yesterdaysclose/twodaysagoclose)) * 100 * sqrt(252))
	        	var samples = new List<double>() { 
	        		Math.Log(_closingPrices[0]/_closingPrices[1]), 
	        		Math.Log(_closingPrices[1]/_closingPrices[2]) 
	        	};
	        	var astd = (decimal) (samples.StandardDeviation() * Convert.ToDouble(Math.Pow(252, 0.5)) * 100);
	        	
	        	//Calculate the raw unaveraged value:
	        	_ema.Update(new IndicatorDataPoint(Time, vix.Value - astd));
	        	
	    		//More than 5 EMA points:
	        	if (_ema.IsReady) 
	        	{
	        		Log(string.Format("EMA: {0:0.00}\tVIX: {1:0.00}\tGSPC: {2:0.00}\tASTD: {3:0.00}", _ema, vix.Value, gspc.Value, astd));
	        		
	        		if (_ema > 1.0m)
	        		{
	        			if (Portfolio["XIV"].HoldStock == false)
	        			{
	        				SetHoldings("XIV", 1.0, true);
	        			}
	        		}
	        		else
	        		{
	        			if (Portfolio["VXX"].HoldStock == false)
	        			{
	        				SetHoldings("VXX", 1.0, true);
	        			}
	        		}
	        	}
        	}
        	catch (Exception e)
        	{
        		Log(e.ToString());
        	}
        }
        
    }
}
namespace QuantConnect
{
    public class QuandlVixContract : Quandl
    {
        public QuandlVixContract() : base(valueColumnName: "S&P 500 VIX Short-Term Index MCAP") 
        {
        }
    }
}