Overall Statistics
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Futures Example
    *
    *   QuantConnect allows importing generic data sources! This example demonstrates importing a futures
    *   data from the popular open data source Quandl.
    *
    *   QuantConnect has a special deal with Quandl giving you access to Stevens Continuous Futurs (SCF) for free. 
    *   If you'd like to download SCF for local backtesting, you can download it through Quandl.com.
    */
    public class VolatilityETN : QCAlgorithm
    {
        string shortTerm = "VXX";
        string longTerm = "VXZ";
        decimal IVTS = new decimal();
        string VIX = "YAHOO/INDEX_VIX";
        string VXV = "CBOEFE/INDEX_VXV";
        
        
        DateTime sampledToday = DateTime.Now;
        DateTime compareTime = DateTime.Now;
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            SetStartDate(2010,8,1);        
            SetEndDate(2012,9,1);
            SetCash(25000);
            AddSecurity(SecurityType.Equity, shortTerm, Resolution.Minute);
            AddSecurity(SecurityType.Equity, longTerm, Resolution.Minute);
            
            AddData<Quandl>(VXV, Resolution.Daily);
            AddData<Quandl>(VIX, Resolution.Daily);
        }

        
        /// <summary>
        /// Event - v3.0 DATA EVENT HANDLER: Basic template for user to override for receiving all subscription data in a single event
        /// </summary>
        /// <code>
        /// TradeBars bars = slice.Bars;
        /// Ticks ticks = slice.Ticks;
        /// TradeBar spy = slice["SPY"];
        /// List<Tick> aaplTicks = slice["AAPL"]
        /// Quandl oil = slice["OIL"]
        /// dynamic anySymbol = slice[symbol];
        /// DataDictionary<Quandl> allQuandlData = slice.Get<Quand>
        /// Quandl oil = slice.Get<Quandl>("OIL")
        /// </code>
        /// <param name="slice">The current slice of data keyed by symbol string</param>
        public void OnData(Slice data) 
        {   
            // add logic to have orders placed once / day
            if (sampledToday.Date == data.Time.Date) return;
            if (DateTime.Now.TimeOfDay <= System.TimeSpan.Parse("15:45:00")) return;
            Log("Time is "+ DateTime.Now.TimeOfDay);
            
            // gets all Quandl data from our 'Slice' object
            var quandls = data.Get<Quandl>();
            
            if (!quandls.ContainsKey(VIX) || !quandls.ContainsKey(VXV)) return;
         
            
            
            // IVTS = VIX / VXV
            IVTS = quandls[VIX].Value / quandls[VXV].Value;
            Log("IVTS is " + IVTS);
            
            // Add buy/sell logic
            
            int count = new int();
            
            if (IVTS <= .91m){
                count = 1;
            } else if ((0.91m < IVTS) && (IVTS <= 0.97m)) {
                count = 2;
            } else if (IVTS > .97m && IVTS <= 1.05m) {
                count = 3;
            } else {
                count = 4;
            }
            
            Log("Count is " + count);
            
            
            
            switch (count) 
            {
                case 1:
                    SetHoldings(shortTerm, -.6);
                    SetHoldings(longTerm, .4);
                    break;
                case 2:
                    SetHoldings(shortTerm, -.32);
                    SetHoldings(longTerm, .68);
                    break;
                case 3:
                    SetHoldings(shortTerm, -.25);
                    SetHoldings(longTerm, .75);
                    break;
                case 4:
                    SetHoldings(shortTerm, -.10);
                    SetHoldings(longTerm, .90);
                    break;
                default:
                    Log("there is an error");
                    break;
            }
            
            sampledToday = DateTime.Now;
            
        }
    }
}