Overall Statistics
using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
using MathNet.Numerics;

namespace QuantConnect.Algorithm.Examples
{
    public class QCUMovingAverageCross : QCAlgorithm
    {
        private const string Symbol1 = "VXX";
        private const string Symbol2 = "TVIX";
        public int myTimeFrame = 60;
        public decimal mySlippageValue = 0.0002m;

        
        public StandardDeviation stdDev;
        public SimpleMovingAverage mySMA;
        public List<double> myDataList1 = new List<double>();
        public List<double> myDataList2 = new List<double>();
       
        public override void Initialize()
        {
            SetStartDate(2012, 06, 01);
            SetEndDate(2017, 04, 07);
            SetWarmup(TimeSpan.FromMinutes(myTimeFrame));

            AddSecurity(SecurityType.Equity, Symbol1, Resolution.Minute);
            AddSecurity(SecurityType.Equity, Symbol2, Resolution.Minute);
            Securities[Symbol1].SlippageModel = new ConstantSlippageModel((decimal) mySlippageValue);
            Securities[Symbol2].SlippageModel = new ConstantSlippageModel((decimal) mySlippageValue);

            stdDev = new StandardDeviation(myTimeFrame);
            mySMA = new SimpleMovingAverage(myTimeFrame);
        }
        
        DateTime myDate;
        decimal myOpenPrice1;
        decimal myOpenPrice2;
        public void OnData(TradeBars data)
        {
        	if(this.Time != myDate)
        	{
        		myDate = this.Time;
        		myOpenPrice1 = data[Symbol1].Open;
        		myOpenPrice2 = data[Symbol2].Open;
        	}
        
        	var myDifference = Math.Abs(((myOpenPrice1 - data[Symbol1].Close) / myOpenPrice1) - (.5m *((myOpenPrice2 - data[Symbol2].Close) / myOpenPrice2)));
        	stdDev.Update(this.Time, myDifference);
        	mySMA.Update(this.Time, myDifference);
        	
            if (!stdDev.IsReady || !mySMA.IsReady) return;


           
            if (!Portfolio.Invested)
            {
                if (Math.Abs(myDifference) > ((2 * stdDev) + Math.Abs(mySMA)))
                {
        		    
                    if(((myOpenPrice1 - data[Symbol1].Close) / myOpenPrice1) > (.5m *((myOpenPrice2 - data[Symbol2].Close) / myOpenPrice2)))
                    {
                    	
                    	SetHoldings(Symbol1, .05); // +
                    	SetHoldings(Symbol2, -.1);
                    }
                    else
                    {
                    	
                    	SetHoldings(Symbol1, -.05);
                    	SetHoldings(Symbol2, .1); // +
                    }
                }
            }
            else
            {
            	if (Math.Abs(myDifference) < ((0.5m * stdDev) + Math.Abs(mySMA))){Liquidate();}
            }


        }
    }
}