| Overall Statistics |
|
Total Trades 37 Average Win 7.96% Average Loss -2.23% Compounding Annual Return 8.880% Drawdown 20.700% Expectancy 2.301 Net Profit 204.682% Sharpe Ratio 0.796 Loss Rate 28% Win Rate 72% Profit-Loss Ratio 3.57 Alpha 0.02 Beta 3.583 Annual Standard Deviation 0.113 Annual Variance 0.013 Information Ratio 0.623 Tracking Error 0.113 Treynor Ratio 0.025 Total Fees $0.00 |
namespace QuantConnect
{
public class switchAlgo : QCAlgorithm
{
private MovingAverageConvergenceDivergence macd;
public override void Initialize()
{
SetBrokerageModel(BrokerageName.Alpaca, AccountType.Cash);
SetStartDate(2005,1,1);// # Set Start Date
SetEndDate(2018,2,1);// # Set End Date
SetCash(100000) ;// # Set Strategy Cash
SetWarmUp(150);
AddEquity("SPY", Resolution.Daily);
AddEquity("TLT", Resolution.Daily);
// Construct MACD Indicator
macd = MACD("SPY",50, 150, 9, MovingAverageType.Simple, Resolution.Daily);
}
public override void OnData(Slice data)
{
// Return if MACD indicator is not ready
if (!macd.IsReady) return;
// Set weight
var weight = 0.80;
// Liquidate TLT and allocate 80% of portfolio value to SPY
if (macd.Fast > macd.Slow){
if(Portfolio["TLT"].Invested) Liquidate("TLT");
if(!Portfolio["SPY"].Invested) SetHoldings("SPY", weight);
}
// Liquidate SPY and allocate 80% of portfolio value to TLT
else if (macd.Slow > macd.Fast){
if (Portfolio["SPY"].Invested) Liquidate("SPY");
if (!Portfolio["TLT"].Invested) SetHoldings("TLT", weight);
}
}
}
}