/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//http://data.quantconnect.com/backtests/10257/127006/f2a4307b17cff43f4a458122536109f0-log.txt
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),
new DateTime(2015, 8, 28),
};
// we'll hold some computed data in these guys
List<ETFSymbol> IndustryData = new List<ETFSymbol>();
List<ETFSymbol> RankedIndustryData = new List<ETFSymbol>();
public List<ETFSymbol> findRanking(List<ETFSymbol> ETFList)
{
int n = 1;
var rankedETFList = ETFList.OrderByDescending(o => o.ShortPerformance).ToList();
n = 1;
foreach (ETFSymbol ETF in rankedETFList)
{
ETF.totalRankAdjusted = n;
n++;
}
return rankedETFList;
}
/// <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, 1);
//SetEndDate(2015, 8, 28);
foreach (var symbol in IndustrySymbols)
{
// ideally we would use daily data
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
Securities[symbol].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);
var shortPerformance = ROCP(symbol, 10, Resolution.Daily); //6 m
var longPerformance = ROCP(symbol, 10, Resolution.Daily);// 8 m
var dailyReturn = ROCP(symbol, 1, Resolution.Daily);
var volitality = new StandardDeviation("4Month Vol ROCP", 10).Of(dailyReturn);
IndustryData.Add(new ETFSymbol
{
Symbol = symbol,
ShortPerformance = shortPerformance,
LongPerformance = longPerformance,
vol = volitality
});
}
}
/// <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());
RankedIndustryData = findRanking(IndustryData);
decimal TotalPortfolioValue = Portfolio.TotalPortfolioValue;
if (2009 == data.Time.Year && 12 == data.Time.Month && 31 == data.Time.Day)
{
foreach (ETFSymbol ETF in RankedIndustryData)
{
MarketOnCloseOrder(ETF.Symbol, (int)(TotalPortfolioValue / Securities[ETF.Symbol].Close - Portfolio[ETF.Symbol].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());
}
}
foreach (ETFSymbol ETF in RankedIndustryData)
{
//MarketOnCloseOrder(ETF.Symbol, (int)(TotalPortfolioValue / Securities[ETF.Symbol].Close - Portfolio[ETF.Symbol].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());
}
}
}
catch (Exception ex)
{
Error("OnTradeBar: " + ex.Message + "\r\n\r\n" + ex.StackTrace);
}
}
}
}