namespace QuantConnect.Rotation
{
/*
* QuantConnect University - Global Rotation by Michael Handschuh
*
* From a list of ETF's which look at the global markets; always select the
* best performing ETF assuming its momentum will continue.
*
* Symbols are ranked by an objective function.
*
*/
public class QCUGlobalRotation : QCAlgorithm
{
// we'll use this to tell us when the month has ended
DateTime LastRotationTime = DateTime.MinValue;
TimeSpan RotationInterval = TimeSpan.FromDays(30);
// these are the growth symbols we'll rotate through
List<string> GrowthSymbols = new List<string>
{
"MDY", // US S&P mid cap 400
"IEV", // iShares S&P europe 350
"EEM", // iShared MSCI emerging markets
"ILF", // iShares S&P latin america
"EPP" // iShared MSCI Pacific ex-Japan
};
// these are the safety symbols we go to when things are looking bad for growth
List<string> SafetySymbols = new List<string>
{
"EDV", // Vangaurd TSY 25yr+
"SHY" // Barclays Low Duration TSY
};
// we'll hold some computed data in these guys
List<SymbolData> SymbolData = new List<SymbolData>();
public override void Initialize()
{
SetCash(3000);
SetStartDate(2001, 1, 1);
foreach (var symbol in GrowthSymbols.Union(SafetySymbols))
{
// ideally we would use daily data
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
var oneMonthPerformance = MOM(symbol, 30, Resolution.Daily);
var threeMonthPerformance = MOM(symbol, 90, Resolution.Daily);
SymbolData.Add(new SymbolData
{
Symbol = symbol,
OneMonthPerformance = oneMonthPerformance,
ThreeMonthPerformance = threeMonthPerformance
});
}
}
private bool first = true;
public void OnData(TradeBars data)
{
try
{
// the first time we come through here we'll need to do some things such as allocation
// and initializing our symbol data
if (first)
{
first = false;
LastRotationTime = data.Time;
return;
}
var delta = data.Time.Subtract(LastRotationTime);
if (delta > RotationInterval)
{
LastRotationTime = data.Time;
// pick which one is best from growth and safety symbols
var orderedObjScores = SymbolData.OrderByDescending(x => x.ObjectiveScore).ToList();
foreach (var orderedObjScore in orderedObjScores)
{
Log(">>SCORE>>" + orderedObjScore.Symbol + ">>" + orderedObjScore.ObjectiveScore);
}
var bestGrowth = orderedObjScores.First();
if (bestGrowth.ObjectiveScore > 0)
{
if (Portfolio[bestGrowth.Symbol].Quantity == 0)
{
Log("PREBUY>>LIQUIDATE>>");
Liquidate();
}
Log(">>BUY>>" + bestGrowth.Symbol + "@" + (100 * bestGrowth.OneMonthPerformance).ToString("00.00"));
decimal qty = Portfolio.Cash / Securities[bestGrowth.Symbol].Close;
MarketOrder(bestGrowth.Symbol, (int)qty);
}
else
{
// if no one has a good objective score then let's hold cash this month to be safe
Log(">>LIQUIDATE>>CASH");
Liquidate();
}
}
}
catch (Exception ex)
{
Error("OnTradeBar: " + ex.Message + "\r\n\r\n" + ex.StackTrace);
}
}
}
class SymbolData
{
public string Symbol;
public Momentum OneMonthPerformance { get; set; }
public Momentum ThreeMonthPerformance { get; set; }
public decimal ObjectiveScore
{
get
{
// we weight the one month performance higher
decimal weight1 = 100;
decimal weight2 = 75;
return (weight1 * OneMonthPerformance + weight2 * ThreeMonthPerformance) / (weight1 + weight2);
}
}
}
}
How do I set up this algorithm to trade live?
It says time error code line 69,73,76.
Thank You
Alexandre Catarino
The errors on line 69, 73 and 76 are warnings about an obsolete property of the TradeBars object. They will not prevent you from running the algorithm in backtesting to live mode, but you can remove this warning messages with the following changes:
# Line 69 and 76 LastRotationTime = Time; # Line 73 var delta = Time.Subtract(LastRotationTime);
In order to run this algorithm in live mode, there is a light grey button that looks like a switch on the top of the terminal page that says BACKTEST. When you click on it, it will open a dialog that will help you configure QuantConnect to connect to your brokerage. There is also an option to test algorithm with paper trading.
Evan Winokurzew
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!