Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
13.181%
Drawdown
19.300%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0.915
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.143
Beta
-0.058
Annual Standard Deviation
0.147
Annual Variance
0.022
Information Ratio
-0.007
Tracking Error
0.215
Treynor Ratio
-2.31
Total Fees
$4.45
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Orders;


namespace QuantConnect.Algorithm.MyStrategy.ETFRotation
{
    /// <summary>
    /// ETF Global Rotation Strategy
    /// </summary>
    public class ETFRotation20150623 : QCAlgorithm
    {
        // we'll use this to tell us when the month has ended
        DateTime LastRotationTime = DateTime.MinValue;
        TimeSpan RotationInterval = TimeSpan.FromDays(1); //jjb last day of month, week
        public Identity SPY_Close, EDV_Close;
        //public Identity EDV_Close;
        //public CompositeIndicator<IndicatorDataPoint> SPY_Over_EDV;
        //public SimpleMovingAverage EMA_SPY_Over_EDV;
        
        
        public IndicatorBase<IndicatorDataPoint> SPY_Over_EDV, SMA_SPY_Over_EDV;
        
        public bool isTradeDay = false;

        private bool first = true;
        
        private string test_symbol = "SPY";



        List<string> IndustrySymbols = new List<string>
        {

            "SPY",

        };


        
        List<DateTime> LastBday = new List<DateTime> 
        { 
            new DateTime(2009, 12, 31),
            new DateTime(2010, 1, 29),
            new DateTime(2010, 2, 26),
            new DateTime(2010, 3, 31),
            new DateTime(2010, 4, 30),
            new DateTime(2010, 5, 31),
            new DateTime(2010, 6, 30),
            new DateTime(2010, 7, 30),
            new DateTime(2010, 8, 31),
            new DateTime(2010, 9, 30),
            new DateTime(2010, 10, 29),
            new DateTime(2010, 11, 30),
            new DateTime(2010, 12, 31),
            new DateTime(2011, 1, 31),
            new DateTime(2011, 2, 28),
            new DateTime(2011, 3, 31),
            new DateTime(2011, 4, 29),
            new DateTime(2011, 5, 31),
            new DateTime(2011, 6, 30),
            new DateTime(2011, 7, 29),
            new DateTime(2011, 8, 31),
            new DateTime(2011, 9, 30),
            new DateTime(2011, 10, 31),
            new DateTime(2011, 11, 30),
            new DateTime(2009, 12, 31),
            new DateTime(2010, 01, 29),
            new DateTime(2010, 02, 26),
            new DateTime(2010, 03, 31),
            new DateTime(2010, 4, 30),
            new DateTime(2010, 5, 28),
            new DateTime(2010, 6, 30),
            new DateTime(2010, 7, 30),
            new DateTime(2010, 8, 31),
            new DateTime(2010, 9, 30),
            new DateTime(2010, 10, 29),
            new DateTime(2010, 11, 30),
            new DateTime(2010, 12, 31),
            new DateTime(2011, 1, 31),
            new DateTime(2011, 2, 28),
            new DateTime(2011, 3, 31),
            new DateTime(2011, 4, 29),
            new DateTime(2011, 5, 31),
            new DateTime(2011, 6, 30),
            new DateTime(2011, 7, 29),
            new DateTime(2011, 8, 31),
            new DateTime(2011, 9, 30),
            new DateTime(2011, 10, 31),
            new DateTime(2011, 11, 30),
            new DateTime(2011, 12, 30),
            new DateTime(2012, 1, 31),
            new DateTime(2012, 2, 29),
            new DateTime(2012, 3, 30),
            new DateTime(2012, 4, 30),
            new DateTime(2012, 5, 31),
            new DateTime(2012, 6, 29),
            new DateTime(2012, 7, 31),
            new DateTime(2012, 8, 31),
            new DateTime(2012, 9, 28),
            new DateTime(2012, 10, 31),
            new DateTime(2012, 11, 30),
            new DateTime(2012, 12, 31),
            new DateTime(2013, 1, 31),
            new DateTime(2013, 2, 28),
            new DateTime(2013, 3, 28),
            new DateTime(2013, 4, 30),
            new DateTime(2013, 5, 31),
            new DateTime(2013, 6, 28),
            new DateTime(2013, 7, 31),
            new DateTime(2013, 8, 30),
            new DateTime(2013, 9, 30),
            new DateTime(2013, 10, 31),
            new DateTime(2013, 11, 29),
            new DateTime(2013, 12, 31),
            new DateTime(2014, 1, 31),
            new DateTime(2014, 2, 28),
            new DateTime(2014, 3, 31),
            new DateTime(2014, 4, 30),
            new DateTime(2014, 5, 30),
            new DateTime(2014, 6, 30),
            new DateTime(2014, 7, 31),
            new DateTime(2014, 8, 29),
            new DateTime(2014, 9, 30),
            new DateTime(2014, 10, 31),
            new DateTime(2014, 11, 28),
            new DateTime(2014, 12, 31),
            new DateTime(2015, 1, 30),
            new DateTime(2015, 2, 27),
            new DateTime(2015, 3, 31),
            new DateTime(2015, 4, 30),
            new DateTime(2015, 5, 29),
            new DateTime(2015, 6, 30),
            new DateTime(2015, 7, 31),

        };



        // we'll hold some computed data in these guys


        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            SetCash(100000);
            SetStartDate(2009, 12, 1);
            SetEndDate(2015, 8, 26);




                AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
                Securities["SPY"].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);


        }



        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
        {


            try
            {

                
                
                
                if (first)
                {
                    first = false;
                    LastRotationTime = data.Time;
                    return;
                }
                
                if(2015 == data.Time.Year && 8 == data.Time.Month && 26 == data.Time.Day)
                {
                	Debug("Time: " + Time.Year.ToString() + "/" + Time.Month.ToString() + "/" +Time.Day.ToString() +": " + Portfolio.TotalPortfolioValue.ToString());
                
                	Debug("Price: " + Securities["SPY"].Close.ToString());
                }
                
				 
                
                isTradeDay = false;
                foreach (DateTime tradeDay in LastBday)
                {

                    if (tradeDay.Year == data.Time.Year && tradeDay.Month == data.Time.Month && tradeDay.Day == data.Time.Day)
                    {
                        isTradeDay = true;
                        break;
                    }
                    
                }
                

                if (!isTradeDay)
                {
                    return;
                }

                var delta = data.Time.Subtract(LastRotationTime);
                
                if (delta <= RotationInterval)
                {
                    return;
                }
                
                if ( data.Time.Hour == 15 && data.Time.Minute >= 49)
                {

                    LastRotationTime = data.Time;

                        Debug("Time: " + Time.Year.ToString() + "/" + Time.Month.ToString() + "/" +Time.Day.ToString() +": " + Portfolio.TotalPortfolioValue.ToString());
                        
                             

                        decimal TotalPortfolioValue = Portfolio.TotalPortfolioValue;
                        if (2009 == data.Time.Year && 12 == data.Time.Month && 31 == data.Time.Day)
                        {


                           MarketOnCloseOrder("SPY", (int)(TotalPortfolioValue / Securities["SPY"].Close - Portfolio["SPY"].Quantity));

                           //Debug(ETF.Symbol + "  Rank: " + ETF.totalRankAdjusted.ToString() + ", PerfShort: " + ETF.ShortPerformance.ToString() + ", PerfLong: " + ETF.LongPerformance.ToString() + ", Vol: " + ETF.vol.ToString() + ", Holding: " + Portfolio[ETF.Symbol].Quantity.ToString()  + ", Cash: " + Portfolio.Cash.ToString());
                                                   
                        
                        }


                           //MarketOnCloseOrder(ETF.Symbol, (int)(TotalPortfolioValue / Securities[ETF.Symbol].Close - Portfolio[ETF.Symbol].Quantity));

                           Debug("SPY: " + " Holding: " + Portfolio["SPY"].Quantity.ToString()  + ", Cash: " + Portfolio.Cash.ToString());
                                                   
                        
 

                }
            }
            catch (Exception ex)
            {
                Error("OnTradeBar: " + ex.Message + "\r\n\r\n" + ex.StackTrace);
            }
        }
    }


}