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.Collections;
using System.Collections.Generic;
using System.Globalization;
using QuantConnect.Algorithm;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators; 
using QuantConnect.Securities;

namespace QuantConnect
{
   
    public class UVXYwithCatango : QCAlgorithm
    {
        private string symbol="UVXY";
        private string spy_symbol="SPY";
        private double percent=0.8; // total use 20% of cash to short uvxy in the total Portfolio;
    	// private  int quantity=500;
    	private  int trigger=0;
    	private  int vixtrigger=0;
        decimal currCatango;
        RelativeStrengthIndex rsi;


        public override void Initialize()
        {
            SetCash(1000000);
            SetStartDate(2011,01,01);
            SetEndDate(2015, 03, 31);
			AddData<VIX>("VIX");
			AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
			AddSecurity(SecurityType.Equity, spy_symbol, Resolution.Minute);



		//	rsi = RSI("SPY",14, MovingAverageType.Simple, Resolution.Daily);
            // SetWarmUp(TimeSpan.FromDays(30));
            Schedule.On(DateRules.EveryDay("UVXY"), TimeRules.AfterMarketOpen("UVXY",0), () =>
            {
                trigger=-1;
                //Log(Time.ToString()+"");
            });
            
           Schedule.On(DateRules.EveryDay("UVXY"), TimeRules.BeforeMarketClose("UVXY",0), () =>
            {
                trigger=1;
                //Log(Time.ToString()+"");
            });
        }
        
         public void OnData(VIX data)
		{
			
			//currCatango=(data.vix_n/data.vix_f)-1; 
			//Log("current catango: "+currCatango+ data.Time+"\t"+currCatango+"\t"+Convert.ToDouble(currCatango));
			//Log(data.Value+"");
			currCatango=data.Value;
			if(Convert.ToDouble(currCatango)<=0.05)
			{
			    //Log("current catango: "+currCatango+ data.Time+"\t"+currCatango+"\t"+Convert.ToDouble(currCatango));

				vixtrigger=1;
			}
			else
			vixtrigger=-1;

		}
        
       public void OnData(TradeBars data) 
        {   
          //  if (!rsi.IsReady) return;
        	// if(Time.Hour==9&&Time.Minute==35)
        	// Log(Time.ToString()+""+"HELLO");
        	double avaiblecash=Convert.ToDouble(percent*Convert.ToDouble(Portfolio.Cash));
        	double price=Convert.ToDouble(Portfolio[symbol].Price);
            int quantity=Convert.ToInt32(avaiblecash/price);
        	 //int quantity=(percent*Convert.ToDouble(Portfolio.Cash))/Convert.ToDouble(Portfolio[symbol].Price);
        	
           if(trigger==-1&&vixtrigger==-1&&!Portfolio.HoldStock)
           {
           	 Log(currCatango+"rsi"+rsi);
           //	Log(Portfolio[symbol].Price+"");
           	 Order(symbol,-quantity);
           //	 Log(Time.ToString()+""+"short"+quantity);
           }
           else if (trigger==1&&Portfolio.HoldStock)
           {
           	 Liquidate("UVXY");
           	 //Order(symbol,Portfolio.quantity);
           	// Log(Time.ToString()+""+"close"+quantity);

           }
        }



        public class VIX : BaseData
    {
        public decimal Catango = 0;
        public decimal vix_n=0;
        public decimal vix_f=0;
        string format ="yyyy-MM-dd";
        public string url="";
		CultureInfo provider = CultureInfo.InvariantCulture;

 
        public VIX()
        {    
            this.Symbol = "VIX";
        }

    public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
        {
            return new SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile);
        }

      public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
        {
            var index = new VIX();

            try
            {
            	string[] data = line.Split(',');
                //Dates must be in the format YYYY-MM-DD. If your data source does not have this format, you must use
                //DateTime.ParseExact() and explicit declare the format your data source has.
                string dateString = data[0].Replace("$","");
                index.Time = DateTime.ParseExact(dateString, format, provider);
                index.Symbol = "VIX";
                index.vix_n=Convert.ToDecimal(data[1]);
                index.vix_f=Convert.ToDecimal(data[2]);
                index.Value=(index.vix_n/index.vix_f)-1;
            }
            catch
            {
            	

            }
            return index;
        }
    }
    }
    
    
    
    
    
    
    
}