Overall Statistics
Total Trades
2
Average Win
0.16%
Average Loss
-0.03%
Compounding Annual Return
0.11%
Drawdown
2.100%
Expectancy
1.985
Net Profit
0.127%
Sharpe Ratio
0.051
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
4.97
Alpha
0.035
Beta
-0.234
Annual Standard Deviation
0.031
Annual Variance
0.001
Information Ratio
-0.97
Tracking Error
0.144
Treynor Ratio
-0.007
-no value-
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 = "YAHOO/INDEX_VIX";
        private const string VXV = "CBOEFE/INDEX_VXV";
        private SimpleMovingAverage smaVIX;
        private SimpleMovingAverage smaVXV;
        private IndicatorBase<IndicatorDataPoint> ratio_VXV_VIX;

        public override void Initialize()
        {
            SetStartDate(2014, 01, 01);

            // request data
            AddData<Quandl>(VIX);
            AddData<Quandl>(VXV);

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

            // 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);
        }

        private DateTime previous;
        public void OnData(Quandl data)
        {
            if (Portfolio[data.Symbol].Quantity == 0)
            {
                MarketOrder(data.Symbol, 100);
            }

            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);
            }
        }

        /// <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;
        }
    }
}