Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
using System;
using System.CodeDom;
using QuantConnect.Algorithm;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Custom;
using QuantConnect.Indicators;

namespace Sandbox.DualConsolidation
{
    public class DualConsolidation : QCAlgorithm
    {
        private const string VIX = "CBOE/VIX";
        private const string VXV = "CBOE/VXV";
        private SimpleMovingAverage smaVIX;
        private SimpleMovingAverage smaVXV;
        private IndicatorBase<IndicatorDataPoint> ratio_VXV_VIX;
        
        private string vxx = "VXX"; 
        private string xiv = "XIV"; 

        public override void Initialize()
        {
            SetStartDate(2015, 11, 11);
            SetEndDate(2018,2,15);
			SetCash(25000);
            // request data
            AddData<QuandlVix>(VIX);
            AddData<QuandlVxV>(VXV);
            Log(VIX);

            // define data sources for our functional indicator, these are really just 'identities' of the closing price
            smaVIX = SMA(VIX, 10);
            smaVXV = SMA(VXV, 10);

            // the functional indicator takes as arguments two functions,
            // the first is a ComputeNextValue function
            // the second is an IsReady function
            ratio_VXV_VIX = new FunctionalIndicator<IndicatorDataPoint>("ratio",
                point => RatioIndicator_ComputeNextValue(point, smaVXV, smaVIX),
                ratioIndicator => RatioIndicator_IsReady(ratioIndicator, smaVXV, smaVIX)
                );

            // we register to the VXV and VIX symbols, so when either of these gets data updates our indicator will recompute
            var identityConsolidator = new IdentityDataConsolidator<Quandl>();
            RegisterIndicator(VXV, ratio_VXV_VIX, identityConsolidator, x => x.Value);
            RegisterIndicator(VIX, ratio_VXV_VIX, identityConsolidator, x => x.Value);
            
            AddSecurity(SecurityType.Equity, vxx, Resolution.Minute);
            AddSecurity(SecurityType.Equity, xiv, Resolution.Minute);
            
          //  var currentPrice = data[xiv].Close;
        }

        private DateTime previous;
        public void OnData(Quandl data)
        {
        	if (IsWarmingUp) return;
            if (previous.Date != data.Time.Date && smaVIX.IsReady && smaVXV.IsReady && ratio_VXV_VIX.IsReady)
            {
                previous = data.Time;
                Plot("Data", smaVIX, smaVXV);
                Plot("Ratio", ratio_VXV_VIX);
                
                if (ratio_VXV_VIX < 0.923m) 
                {
					Liquidate(xiv);
					SetHoldings(xiv, -1);
				} else 
				{
					
                	Liquidate(xiv);
					SetHoldings(xiv, 1);
			//		StopMarketOrder(xiv, 1, price * (1m * .05));
				}
            }
        }

        /// <summary>
        /// This code is run as part of the ratio_VXV_VIX functional indicator
        /// </summary>
        /// <remarks>
        /// This is the ComputeNextValue function implementation for IndicatorBase
        /// </remarks>
        private decimal RatioIndicator_ComputeNextValue(IndicatorDataPoint data,
            IndicatorBase<IndicatorDataPoint> vxv,
            IndicatorBase<IndicatorDataPoint> vix)
        {
            return vxv / vix;
        }

        /// <summary>
        /// This code is run as part of the ratio_VXV_VIX functional indicator
        /// </summary>
        /// <remarks>
        /// This is the IsReady function implementation for IndicatorBase
        /// </remarks>
        private bool RatioIndicator_IsReady(IndicatorBase<IndicatorDataPoint> functionalIndicator,
            IndicatorBase<IndicatorDataPoint> vxv,
            IndicatorBase<IndicatorDataPoint> vix)
        {
            return vxv.IsReady && vix.IsReady;
        }
    }
    
    public class QuandlVix : Quandl {
    public QuandlVix() : base(valueColumnName: "vix close") { }
  }
  
  public class QuandlVxV : Quandl {
    public QuandlVxV() : base(valueColumnName: "vxv close") { }
  }
}