Overall Statistics
Total Trades
70
Average Win
1.00%
Average Loss
-1.05%
Compounding Annual Return
6.259%
Drawdown
7.900%
Expectancy
0.504
Net Profit
19.997%
Sharpe Ratio
0.901
Loss Rate
23%
Win Rate
77%
Profit-Loss Ratio
0.95
Alpha
-0.056
Beta
5.97
Annual Standard Deviation
0.07
Annual Variance
0.005
Information Ratio
0.616
Tracking Error
0.07
Treynor Ratio
0.011
Total Fees
$1701.60
namespace QuantConnect.Algorithm.CSharp
{
    public class LearningFear : QCAlgorithm
    {
    	///Set to true when you want to do a 3 year back test up to one year in the past
     	public bool Inbound = true;
     	
     	private int LongP = 25;
     	private int ShortP = 4;

		private RelativeStrengthIndex _rsiLong;
		private RelativeStrengthIndex _rsiShort;
		private SimpleMovingAverage _SMA200;

        public override void Initialize()
        {
        	if(Inbound) {
				SetStartDate(DateTime.Now.Date.AddDays(-4*365));  //Set Start Date 4 years in the past
            	SetEndDate(DateTime.Now.Date.AddDays(-1*365));  //Set End Date end date 1 year in the past leaving 1 year for out of bounds forward testing
        	}
        	else
        	{
	            SetStartDate(DateTime.Now.Date.AddDays(-1*365));  //Set Start Date 4 years in the past
	            SetEndDate(DateTime.Now);  //Set End Date end date 1 year in the past leaving 1 year for out of bounds forward testing
        	}

        	SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin);
        	
            SetCash(1000000);             //Set Strategy Cash
            
            AddEquity("SPY", Resolution.Daily);
            
            SetWarmUp(TimeSpan.FromDays(200+50));
            _rsiLong = RSI("SPY",LongP,MovingAverageType.Simple, Resolution.Daily);
            _rsiShort = RSI("SPY",ShortP,MovingAverageType.Simple, Resolution.Daily);
			_SMA200 = SMA("SPY",200,Resolution.Daily);
            
            //setup chart
            var rsiPlot = new Chart("RSI Chart");
            var RSIFast = new Series("Fast", SeriesType.Line,0);
            var RSISlow = new Series("Slow", SeriesType.Line,0);
            var SMASlow = new Series("SMA200", SeriesType.Line,0);
            //TODO add go long go short markers
            rsiPlot.AddSeries(RSIFast);
            rsiPlot.AddSeries(RSISlow);
            AddChart(rsiPlot);

        }

        /// 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(!_rsiLong.IsReady) return;
        	if(!_rsiShort.IsReady) return;
        	if(!_SMA200.IsReady) return;
        	
        	
        	Plot("RSI Chart","Fast", _rsiShort.Current);
        	Plot("RSI Chart","Slow", _rsiLong.Current);
        	Plot("RSI Chart","SMA200", _SMA200.Current);

    		//we are in an uptrend
        	if(data["SPY"].Price > _SMA200.Current)
        	{
				if(_rsiShort.Current < 25 && !Portfolio["SPY"].IsLong)
        		{
        			//go long over sold
        			SetHoldings("SPY",1);
        		}
    		}
		    if(_rsiShort.Current > 65 && Portfolio["SPY"].IsLong)
    		{
    			//go long over sold
    			SetHoldings("SPY",0);
    		}
        }
    }
}