Overall Statistics
Total Trades
87
Average Win
1.02%
Average Loss
-0.74%
Compounding Annual Return
7.648%
Drawdown
4.700%
Expectancy
0.612
Net Profit
21.810%
Sharpe Ratio
1.357
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
1.39
Alpha
0.072
Beta
0.017
Annual Standard Deviation
0.055
Annual Variance
0.003
Information Ratio
-0.768
Tracking Error
0.124
Treynor Ratio
4.309
Total Fees
$87.00
using QuantConnect.Algorithm;
using QuantConnect.Indicators;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;

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
    {
        string symbol = "SPY";
        private LogReturn logr14;
        private LogReturn logr30;
        private LogReturn lastLogr;
        
        private DateTime previous;
        //RegisterIndicator(symbol, logr, null);
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {
            //Start and End Date range for the backtest:
            SetStartDate(2013, 1, 1);         
            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, symbol, Resolution.Daily);
            
            logr14 = new LogReturn(14);
            logr30 = new LogReturn(30);
            lastLogr = logr14;
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        	// "TradeBars" object holds many "TradeBar" objects: it is a dictionary indexed by the symbol:
        	// 
        	//  e.g.  data["MSFT"] data["GOOG"]
        public void OnData(TradeBars data) 
        {   
        	// update Indicators
        	decimal price = data[symbol].Close;
        	IndicatorDataPoint datum = new IndicatorDataPoint(data[symbol].Time, price);
        	logr14.Update(datum);
        	logr30.Update(datum);
        	
        	// wait for the indicators to fully initialize
            if (!logr30.IsReady) return;
            
            // stock is moving in an upwards trend
            if (logr14.Current.Value > logr30.Current.Value)
            {
            	if (!Portfolio.HoldStock)
            	{
            		int quantity = (int)Math.Floor(Portfolio.Cash / data[symbol].Close);
                	Order(symbol,  quantity);
            	}
            }
            // stock is moving in a downwards trend
            else if (logr30.Current.Value > logr14.Current.Value)
            {
            	Liquidate(symbol);
            }
            
            Plot(symbol, logr14);
            Plot(symbol, logr30);
        }
    }
}
namespace QuantConnect {
    /// <summary>
    ///     Represents the LogReturn indicator (LOGR)
    ///      - log returns are useful for identifying price convergence/divergence in a given period
    ///      - logr = log (current price / last price in period)
    /// </summary>
    public class LogReturn : WindowIndicator<IndicatorDataPoint>
    {
        /// <summary>
        ///     Initializes a new instance of the LogReturn class with the specified name and period
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">The period of the LOGR</param>
        public LogReturn(string name, int period)
            : base(name, period)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the LogReturn class with the default name and period
        /// </summary>
        /// <param name="period">The period of the SMA</param>
        public LogReturn(int period)
            : base("LOGR" + period, period)
        {
        }

        /// <summary>
        ///     Computes the next value for this indicator from the given state.
        ///      - logr = log (current price / last price in period)
        /// </summary>
        /// <param name="window">The window of data held in this indicator</param>
        /// <param name="input">The input value to this indicator on this time step</param>
        /// <returns>A new value for this indicator</returns>
        protected override decimal ComputeNextValue(IReadOnlyWindow<IndicatorDataPoint> window, IndicatorDataPoint input)
        {
        	decimal valuef = input;

            decimal value0 = !window.IsReady
               	? window[window.Count - 1]
               	: window.MostRecentlyRemoved;
                
            decimal logr = (decimal)Math.Log((double)(valuef / value0));
            
            return logr;
        }
	}
}