Overall Statistics
Total Trades
1
Average Win
96.42%
Average Loss
0%
Compounding Annual Return
37.618%
Drawdown
14.200%
Expectancy
0
Net Profit
96.417%
Sharpe Ratio
1.77
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.01
Beta
1.622
Annual Standard Deviation
0.19
Annual Variance
0.036
Information Ratio
1.643
Tracking Error
0.082
Treynor Ratio
0.207
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        //Initialize the data and resolution you require for your strategy:
        LogReturn returnIndicator;
        SequentialIndicator<IndicatorDataPoint> sma;
        
        RelativeStrengthIndex rsi;
        SequentialIndicator<IndicatorDataPoint> emaOfRsi;
        
        public override void Initialize()
        {
			
            //Start and End Date range for the backtest:
            SetStartDate(2013, 1, 1);         
            //SetEndDate(2013, 1, 28);         
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            
            //Cash allocation
            SetCash(25000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            
            //Set up Indicators:
            returnIndicator = new LogReturn("SPY");
            
            //
            // when using sequential indicators it is only necessary to subscribe the 'outter' indicator
            // this is because the sequential indicator takes the output of one and pumps it into the other
            // so we want to register the indicator that is the most derived, in this case, sma.
            // we want to send data into sma, then he will control sending data into the returnIndicator
            //
            //RegisterIndicator("SPY", returnIndicator, Resolution.Minute, x => x.Value);
            
            // define an SMA 5 of our returnIndicator and register this new indicator.
            sma = new SimpleMovingAverage(5).Of(returnIndicator);
            RegisterIndicator("SPY", sma, Resolution.Minute, x => x.Value);
            
            
            // we want to create the momentum of the EMA, so in this case, our EMA is our inner indicator
            // and the momentum is our outter indicator.
            
            // define the indicators
            rsi = new RelativeStrengthIndex(14);
            emaOfRsi = new ExponentialMovingAverage(14).Of(rsi);
            
            // now we only need to register the outter, momOfEma, since it will control sending data into our ema
            RegisterIndicator("SPY",emaOfRsi, Resolution.Daily, x => x.Value);
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        {
            // "TradeBars" object holds many "TradeBar" objects: it is a dictionary indexed by the symbol:
            // 
            //  e.g.  data["MSFT"] data["GOOG"]
            
            if (!Portfolio.HoldStock) 
            {
                //Order function places trades: enter the string symbol and the quantity you want:
                SetHoldings("SPY",  1.0);
                
                //Debug sends messages to the user console: "Time" is the algorithm time keeper object 
                Log("Purchased SPY on " + Time.ToShortDateString());
            }
        }
        
        public override void OnEndOfDay()
        {
            // don't plot until ready
            if (!returnIndicator.IsReady) return;
            Plot("Return","Instantaneous",returnIndicator);
            
            // don't plot until ready
            if (!emaOfRsi.IsReady) return;
            Plot("Return","5 Period Average",sma);
            Plot("EMA of RSI", emaOfRsi, rsi);
        }
    }
}
using System;
using QuantConnect.Data.Market;

namespace QuantConnect.Indicators
{
    /// <summary>
    /// This indicator is meant to compute log returns.
    /// </summary>
    public class LogReturn : Indicator
    {

        public LogReturn()
            : this(string.Format("Return"))
        {
        }

        decimal _last;
        public decimal Return;

        public LogReturn(String name)
            : base(name)
        {
            _last = 0m;
            Return = 0m;
        }

        /// <summary>
        /// Gets a flag indicating when this indicator is ready and fully initialized
        /// </summary>
        public override bool IsReady
        {
            get { return Samples > 1; }
        }
        public decimal total;
        
        protected override decimal ComputeNextValue(IndicatorDataPoint input)
        {
            if (IsReady && _last != 0)
            {
                Return = (decimal)Math.Log((double)(input / _last));
            }
            _last = input;
            return Return*10000m;
        }
        /// <summary>
        /// Resets this indicator to its initial state
        /// </summary>
        public override void Reset()
        {
            Return = 0m;
            base.Reset();
        }
    }
}