Overall Statistics
/*
 *
*/

using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// Using rolling windows for efficient storage of historical data; which automatically clears after a period of time.
    /// </summary>
    /// <meta name="tag" content="using data" />
    /// <meta name="tag" content="history and warm up" />
    /// <meta name="tag" content="history" />
    /// <meta name="tag" content="warm up" />
    /// <meta name="tag" content="indicators" />
    /// <meta name="tag" content="rolling windows" />
    public class nvdahmarevdaily : QCAlgorithm
    {
        private RollingWindow<TradeBar> _window;
        private RollingWindow<IndicatorDataPoint> _hmaWin;
        /// <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(2013, 01, 01);  // Set Start Date
            SetEndDate(2019, 01, 16);    // Set End Date
            SetCash(10000);          // Set Strategy Cash

            // Find more symbols here: http://quantconnect.com/data
            AddEquity("NVDA", Resolution.Daily);

            // Creates a Rolling Window indicator to keep the 2 TradeBar
            _window = new RollingWindow<TradeBar>(2);    // For other security types, use QuoteBar

            // Creates an indicator and adds to a rolling window when it is updated
            var hma = HMA("NVDA", 25);
            hma.Updated += (sender, updated) => _hmaWin.Add(updated);
            _hmaWin = new RollingWindow<IndicatorDataPoint>(2);
        }

        /// <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)
        {
            // Add SPY TradeBar in rollling window
            _window.Add(data["NVDA"]);

            // Wait for windows to be ready.
            if (!_window.IsReady || !_hmaWin.IsReady) return;

            var currBar = _window[0];                   // Current bar had index zero.
            var pastBar = _window[1];                   // Past bar has index one.
            // Log($"Price: {pastBar.Time} -> {pastBar.Close} ... {currBar.Time} -> {currBar.Close}");

            var currhma = _hmaWin[0];                   // Current hma had index zero.
            var pasthma = _hmaWin[1];   // Oldest hma has index of window count minus 1.
            // Log($"hma: {pasthma.Time} -> {pasthma.Value} ... {currhma.Time} -> {currhma.Value}");

            var holdings = Portfolio["NVDA"].Quantity;

            if (holdings <= 0)
            {
                // if the fast is greater than the slow, we'll go long
                if (currhma > pasthma)
                {
                    // Log("buy  >> " + Securities["NVDA"].Price);
                    SetHoldings("NVDA", 1.0);
                }
            }

            if (holdings >= 0)
            {
	            if (currhma < pasthma)
	            {
	                // Log("sell short >> " + Securities["NVDA"].Price);
	                SetHoldings("NVDA", -1.0);
	            }
            }

            Plot("NVDA", "Price", Securities["NVDA"].Price);
        }
    }
}