Overall Statistics
Total Trades
240
Average Win
1.76%
Average Loss
-0.69%
Compounding Annual Return
12.835%
Drawdown
30.400%
Expectancy
1.839
Net Profit
451.332%
Sharpe Ratio
0.848
Loss Rate
20%
Win Rate
80%
Profit-Loss Ratio
2.56
Alpha
0.229
Beta
-7.402
Annual Standard Deviation
0.127
Annual Variance
0.016
Information Ratio
0.718
Tracking Error
0.127
Treynor Ratio
-0.015
Total Fees
$0.00
namespace QuantConnect 
{   
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
    	public string[] Symbols = {"SPY","TLT","GLD"};
    	
    	private Dictionary<string, RelativeStrengthIndex> rsi = new Dictionary<string, RelativeStrengthIndex>();
    	private Dictionary<string, SimpleMovingAverage> smoothedRSI = new Dictionary<string, SimpleMovingAverage>();
    	RollingWindow<string> TopSymbol;
    	RollingWindow<string> BottomSymbol;
    	
        public int SmoothLen = 120;
        public decimal Max = .5m;
        public decimal Mid = .3m;
        public decimal Min = .2m;
        public decimal Lev = 1.5m;
    	
        public override void Initialize() 
        {
            SetStartDate(2004, 1, 1);
            SetEndDate(DateTime.Now);
            SetCash(10000000);
            
			foreach (var symbol in Symbols)
			{
				AddSecurity(SecurityType.Equity,symbol,Resolution.Daily);
            	rsi[symbol]=RSI(symbol, 14, MovingAverageType.Wilders);
            	smoothedRSI[symbol]=new SimpleMovingAverage(SmoothLen).Of(rsi[symbol]);
            	
            	Securities[symbol].FeeModel = new ConstantFeeTransactionModel(0);
            	Securities[symbol].SetLeverage(5);
            
            	var history = History(symbol, SmoothLen+15);
            	foreach (var tradeBar in history)
            	{
            		rsi[symbol].Update(tradeBar.EndTime, tradeBar.Close);
            		smoothedRSI[symbol].Update(tradeBar.EndTime, rsi[symbol]);
        	 	}
			}
			TopSymbol = new RollingWindow<string>(2);
			BottomSymbol = new RollingWindow<string>(2);
        }
       
        public void OnData(TradeBars data) 
        {
        	foreach (var symbol in Symbols){if (!data.ContainsKey(symbol)) return;}
        	
        	DateTime Track = Time;
			
        	////////////  Entry Shit /////////////
        	var MaxMomentum = Math.Max(smoothedRSI["SPY"], Math.Max(smoothedRSI["TLT"], smoothedRSI["GLD"]));
        	var MinMomentum = Math.Min(smoothedRSI["SPY"], Math.Min(smoothedRSI["TLT"], smoothedRSI["GLD"]));
        	
        	string TopTicker = "SPY";
        	if (smoothedRSI["TLT"] == MaxMomentum){TopTicker = "TLT";} else if (smoothedRSI["GLD"] == MaxMomentum){TopTicker = "GLD";}
        	TopSymbol.Add(TopTicker); if(!TopSymbol.IsReady) return;
        	string BottomTicker = "SPY";
        	if (smoothedRSI["TLT"] == MinMomentum){BottomTicker = "TLT";} else if (smoothedRSI["GLD"] == MinMomentum){BottomTicker = "GLD";}
        	BottomSymbol.Add(BottomTicker); if(!BottomSymbol.IsReady) return;
        	string MiddleTicker = "SPY";
        	foreach (var symbol in Symbols){if (symbol != TopTicker && symbol != BottomTicker){MiddleTicker = symbol;}}
        
        	if (TopSymbol[0]!=TopSymbol[1] || BottomSymbol[0]!=BottomSymbol[1] || !Portfolio.Invested)
        	{
        		SetHoldings(TopTicker, Max*Lev, false, "Overweight " + TopTicker);
        		SetHoldings(MiddleTicker, Mid*Lev, false, "Equalweight " + MiddleTicker);
        		SetHoldings(BottomTicker, Min*Lev, false, "Underweight " + BottomTicker);
        	}
        }
    }
}