| Overall Statistics |
|
Total Trades 882 Average Win 1.98% Average Loss -1.08% Compounding Annual Return 14.243% Drawdown 15.800% Expectancy 0.428 Net Profit 523.348% Sharpe Ratio 0.945 Loss Rate 50% Win Rate 50% Profit-Loss Ratio 1.83 Alpha 0.135 Beta 0.064 Annual Standard Deviation 0.15 Annual Variance 0.022 Information Ratio 0.188 Tracking Error 0.228 Treynor Ratio 2.207 Total Fees $13699.32 |
namespace QuantConnect.Algorithm.CSharp
{
using System.Drawing;
public class QuantumHorizontalShield : QCAlgorithm
{
private MomentumPercent
spyMomentum, qqqMomentum, eq4Momentum, tltMomentum;
public ExponentialMovingAverage
emaFastSPY, emaSlowSPY, emaFastQQQ, emaSlowQQQ, emaFastTLT, emaSlowTLT;
public AverageTrueRange
atrSPY, atrQQQ, atrTLT;
//
private int momPer, emaSlowPer, emaFastPer, atrPer;
public string setHold;
//
public override void Initialize()
{
SetStartDate(2006, 1, 1); //Set Start Date
SetCash(100000); //Set Strategy Cash
momPer = 21;
emaFastPer = 34;
emaSlowPer = 195;
atrPer = 21;
AddEquity("SPY", Resolution.Daily);
AddEquity("QQQ", Resolution.Daily);
AddEquity("TLT", Resolution.Daily);
spyMomentum = MOMP("SPY", momPer, Resolution.Daily);
qqqMomentum = MOMP("QQQ", momPer, Resolution.Daily);
tltMomentum = MOMP("TLT", momPer, Resolution.Daily);
emaFastSPY = EMA("SPY", emaFastPer, Resolution.Daily);
emaSlowSPY = EMA("SPY", emaSlowPer, Resolution.Daily);
emaFastQQQ = EMA("QQQ", emaFastPer, Resolution.Daily);
emaSlowQQQ = EMA("QQQ", emaSlowPer, Resolution.Daily);
emaFastTLT = EMA("TLT", emaFastPer, Resolution.Daily);
emaSlowTLT = EMA("TLT", emaSlowPer, Resolution.Daily);
atrSPY = ATR("SPY", atrPer, MovingAverageType.Simple);
atrQQQ = ATR("QQQ", atrPer, MovingAverageType.Simple);
atrTLT = ATR("TLT", atrPer, MovingAverageType.Simple);
SetBenchmark("SPY");
SetWarmUp(emaSlowPer);
var spyPlot = new Chart("SPY");
spyPlot.AddSeries(new Series("Buy", SeriesType.Scatter, "1", Color.Green, ScatterMarkerSymbol.Triangle));
spyPlot.AddSeries(new Series("Sell", SeriesType.Scatter, "1", Color.Red, ScatterMarkerSymbol.TriangleDown));
spyPlot.AddSeries(new Series("Price", SeriesType.Line, 0));
spyPlot.AddSeries(new Series("EMASlow", SeriesType.Line, 0));
spyPlot.AddSeries(new Series("EMAFast", SeriesType.Line, 0));
spyPlot.AddSeries(new Series("ATR", SeriesType.Line, 0));
spyPlot.AddSeries(new Series("MOMP", SeriesType.Line, 0));
AddChart(spyPlot);
var qqqPlot = new Chart("QQQ");
qqqPlot.AddSeries(new Series("Buy", SeriesType.Scatter, "1", Color.Green, ScatterMarkerSymbol.Triangle));
qqqPlot.AddSeries(new Series("Sell", SeriesType.Scatter, "1", Color.Red, ScatterMarkerSymbol.TriangleDown));
qqqPlot.AddSeries(new Series("Price", SeriesType.Line, 0));
qqqPlot.AddSeries(new Series("EMASlow", SeriesType.Line, 0));
qqqPlot.AddSeries(new Series("EMAFast", SeriesType.Line, 0));
qqqPlot.AddSeries(new Series("ATR", SeriesType.Line, 0));
qqqPlot.AddSeries(new Series("MOMP", SeriesType.Line, 0));
AddChart(qqqPlot);
var tltPlot = new Chart("TLT");
tltPlot.AddSeries(new Series("Price", SeriesType.Line, 0));
tltPlot.AddSeries(new Series("EMASlow", SeriesType.Line, 0));
tltPlot.AddSeries(new Series("EMAFast", SeriesType.Line, 0));
tltPlot.AddSeries(new Series("ATR", SeriesType.Line, 0));
tltPlot.AddSeries(new Series("MOMP", SeriesType.Line, 0));
AddChart(tltPlot);
}
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// Slice object keyed by symbol containing the stock data
public override void OnData(Slice data)
{
if (IsWarmingUp)
return;
// auto plot our indicators on our symbol chart
if (data.ContainsKey("SPY"))
{
Plot("SPY", "EMASlow", emaSlowSPY);
Plot("SPY", "EMAFast", emaFastSPY);
Plot("SPY", "ATR", atrSPY);
Plot("SPY", "MOMP", spyMomentum);
try {Plot("SPY", "Price", data.Bars["SPY"].Close);}
catch {}
}
if (data.ContainsKey("QQQ"))
{
Plot("QQQ", "EMASlow", emaSlowQQQ);
Plot("QQQ", "EMAFast", emaFastQQQ);
Plot("QQQ", "ATR", atrQQQ);
Plot("QQQ", "MOMP", qqqMomentum);
try {Plot("QQQ", "Price", data["QQQ"].Close);}
catch {}
}
if (data.ContainsKey("TLT"))
{
Plot("TLT", "EMASlow", emaSlowTLT);
Plot("TLT", "EMAFast", emaFastTLT);
Plot("TLT", "ATR", atrTLT);
Plot("TLT", "MOMP", tltMomentum);
try {Plot("TLT", "Price", data["TLT"].Close);}
catch {}
}
if (spyMomentum/atrSPY > qqqMomentum/atrQQQ
& spyMomentum/atrSPY > tltMomentum/atrTLT)
{
if (Portfolio["SPY"].Quantity == 0)
{
if (Portfolio["QQQ"].Quantity > 0)
{
Liquidate("QQQ");
Plot("QQQ", "Sell", -1);
}
if (Portfolio["TLT"].Quantity > 0)
{
Liquidate("TLT");
}
//
try
{
if (data.Bars["SPY"].Close > emaFastSPY | data.Bars["SPY"].Close > emaSlowSPY)
{
SetHoldings("SPY", 0.99);
Plot("SPY", "Buy", 1);
}
}
catch
{
SetHoldings("SPY", 0.99);
setHold = "SPY";
Plot("SPY", "Buy", 1);
}
}
else
{
try
{
if (data.Bars["SPY"].Close < emaFastSPY & data.Bars["SPY"].Close < emaSlowSPY)
{
Liquidate("SPY");
Plot("SPY", "Sell", -1);
SetHoldings("TLT", 0.99);
setHold = "TLT";
}
}
catch {}
}
}
else if (qqqMomentum/atrQQQ > spyMomentum/atrSPY
& qqqMomentum/atrQQQ > tltMomentum/atrTLT)
{
if (Portfolio["QQQ"].Quantity == 0)
{
if (Portfolio["SPY"].Quantity > 0)
{
Liquidate("SPY");
Plot("SPY", "Sell", -1);
}
if (Portfolio["TLT"].Quantity > 0)
{
Liquidate("TLT");
}
//
try
{
if (data.Bars["QQQ"].Close > emaFastQQQ | data.Bars["QQQ"].Close > emaSlowQQQ)
{
SetHoldings("QQQ", 0.99);
setHold = "QQQ";
Plot("QQQ", "Buy", 1);
}
}
catch
{
SetHoldings("QQQ", 0.99);
setHold = "QQQ";
Plot("QQQ", "Buy", 1);
}
}
else
{
try
{
if (data.Bars["QQQ"].Close < emaFastQQQ & data.Bars["QQQ"].Close < emaSlowQQQ)
{
Liquidate("QQQ");
Plot("QQQ", "Sell", 1);
SetHoldings("TLT", 0.99);
setHold = "TLT";
}
}
catch
{
}
}
}
else
{
if (Portfolio["TLT"].Quantity == 0)
{
if (Portfolio["SPY"].Quantity > 0)
{
Liquidate("SPY");
Plot("SPY", "Sell", -1);
}
if (Portfolio["QQQ"].Quantity > 0)
{
Liquidate("QQQ");
Plot("QQQ", "Sell", -1);
}
//
try
{
if (data.Bars["TLT"].Close > emaFastTLT | data.Bars["TLT"].Close > emaSlowTLT)
{
SetHoldings("TLT", 0.99);
setHold = "TLT";
}
}
catch
{
SetHoldings("TLT", 0.99);
setHold = "TLT";
}
}
else
{
try
{
if (data.Bars["TLT"].Close < emaFastTLT & data.Bars["TLT"].Close < emaSlowTLT)
{
Liquidate("TLT");
setHold = "";
}
}
catch {}
}
}
}
}
}