Overall Statistics
Total Trades
142
Average Win
7.66%
Average Loss
-2.01%
Compounding Annual Return
1.903%
Drawdown
57.900%
Expectancy
0.193
Net Profit
37.806%
Sharpe Ratio
0.195
Loss Rate
75%
Win Rate
25%
Profit-Loss Ratio
3.81
Alpha
0.032
Beta
0.018
Annual Standard Deviation
0.169
Annual Variance
0.029
Information Ratio
-0.188
Tracking Error
0.258
Treynor Ratio
1.855
Total Fees
$1223.39
/*
 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
 * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// Shows some ways to keep track of an indicator's history
    /// </summary>
    public class HistoricalIndicatorAlgorithm : QCAlgorithm
    {
        private const string Symbol = "SPY";
        public RollingWindow<IndicatorDataPoint> EmaHistory;

        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            SetStartDate(1998, 01, 02);  //Set Start Date
            SetEndDate(2015, 01, 01);    //Set End Date
            SetCash(100000);             //Set Strategy Cash
            // Find more symbols here: http://quantconnect.com/data
            AddSecurity(SecurityType.Equity, Symbol, Resolution.Daily);

            // create a new EMA and track it's history, you can access
            // the current value of the EMA using EmaHistory[0],
            // the previous value using EmaHistory[1], ect...
            EmaHistory = HistoryTrackerImpl.Track(EMA(Symbol, 100), 3);
        }

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">Slice object keyed by symbol containing the stock data</param>
        public override void OnData(Slice data)
        {
        	if (!EmaHistory.IsReady) return;
        	
        	var current = EmaHistory[0];
        	
        	// all historical values are less than the current, uptrend
        	if (Portfolio[Symbol].Quantity <= 0 && EmaHistory.All(x => current >= x))
        	{
        		SetHoldings(Symbol, 1);
        	}
			// all historical values are greater than the current, downtrend
        	else if (Portfolio[Symbol].Quantity >= 0 && EmaHistory.All(x => current <= x))
        	{
        		SetHoldings(Symbol, -1);
        	}
        	
        }
    }

    /// <summary>
    /// Provides helper methods to track the history of indicators and consolidators
    /// </summary>
    public static class HistoryTrackerImpl
    {
        /// <summary>
        /// Track an indicator's history
        /// </summary>
        /// <typeparam name="T">The indicator's input type</typeparam>
        /// <param name="indicator">The indicator to track</param>
        /// <param name="historyLength">The length of history to keep, defaults to 8</param>
        /// <returns>A rolling window of the requested length that will automatically update as the indicator does</returns>
        public static RollingWindow<IndicatorDataPoint> Track<T>(IndicatorBase<T> indicator, int historyLength = 8)
            where T : BaseData
        {
            // create a rolling window of the requested length
            var window = new RollingWindow<IndicatorDataPoint>(historyLength);
            // wire up an event so that each time the indicator gets updated, we add the new value to our window
            indicator.Updated += (sender, args) => window.Add(indicator.Current);
            // return the window now that it's wired up to the indicator
            return window;
        }

        /// <summary>
        /// Track an consolidator's history
        /// </summary>
        /// <typeparam name="T">The consolidator's output type</typeparam>
        /// <param name="consolidator">The consolidator to track</param>
        /// <param name="historyLength">The length of history to keep, defaults to 8</param>
        /// <returns>A rolling window of the requested length that will automatically update as the consolidator does</returns>
        public static RollingWindow<T> Track<T>(DataConsolidator<T> consolidator, int historyLength = 8)
            where T : BaseData
        {
            // create a rolling window of the requested length
            var window = new RollingWindow<T>(historyLength);
            // wire up and event so that each time the consolidator gets updated, we add the new value to our window
            consolidator.DataConsolidated += (sendar, args) => window.Add((T)args);
            // return the window now that it's ired up to the indicator
            return window;
        }

        /// <summary>
        /// Track an consolidator's history
        /// </summary>
        /// <typeparam name="T">The consolidator's output type</typeparam>
        /// <param name="consolidator">The consolidator to track</param>
        /// <param name="historyLength">The length of history to keep, defaults to 8</param>
        /// <returns>A rolling window of the requested length that will automatically update as the consolidator does</returns>
        public static RollingWindow<T> Track<T>(IDataConsolidator consolidator, int historyLength = 8) where T : class
        {
            // create a rolling window of the requested length
            var window = new RollingWindow<T>(historyLength);
            // wire up and event so that each time the consolidator gets updated, we add the new value to our window
            consolidator.DataConsolidated += (sendar, args) => window.Add(args as T);
            // return the window now that it's ired up to the indicator
            return window;
        }
    }
}