Overall Statistics
 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

using System;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.Examples
    /// <summary>
    /// Basic template algorithm simply initializes the date range and cash
    /// </summary>
    public class MultipleTimeFrameAlgorithm : QCAlgorithm

        public ExponentialMovingAverage Weekly_EMA20;
        public ExponentialMovingAverage Weekly_EMA50;
        public SimpleMovingAverage Daily_SMA21;
        public SimpleMovingAverage Daily_Close;

        /// <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(2002, 01, 01);  //Set Start Date
            SetEndDate(2010, 01, 01);    //Set End Date
            SetCash(100000);             //Set Strategy Cash

            // Find more symbols here: http://quantconnect.com/data
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);

            // we can easily create our daily indicator using the helper function 'SMA'
            // this is because we have defined the Resolution.Daily
            Daily_SMA21 = SMA("SPY", 21, Resolution.Daily);
            // a one day SMA is the same as the close!
            Daily_Close = SMA("SPY", 1, Resolution.Daily);

            // this will produce weekly bars from our minute SPY data stream
            var weeklyConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(7));

            // define our weekly EMAs
            Weekly_EMA20 = new ExponentialMovingAverage("W_EMA20", 20);
            Weekly_EMA50 = new ExponentialMovingAverage("W_EMA50", 50);

            // register the weekly EMAs to use our weekly consolidator, the x => x.Value is selecting what value gets sent
            // into the indicator, here we select the .Value property which is an alias for TradeBar.Close
            RegisterIndicator("SPY", Weekly_EMA20, weeklyConsolidator, x => x.Value);
            RegisterIndicator("SPY", Weekly_EMA50, weeklyConsolidator, x => x.Value);

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
            // wait for our 20 week ema to ready up
            if (!Weekly_EMA20.IsReady)

            // we're in an uptrend and don't have a position
            if (!Portfolio["SPY"].Invested && Weekly_EMA20 > Weekly_EMA50)
                // we closed above the SMA 21, buy!
                if (Daily_Close > Daily_SMA21)
                    MarketOrder("SPY", 1500);
                    Debug("Purchased SPY@" + data["SPY"].Value);

            // we've crossed back below our SMA and have an open position, so liquidate our holdings
            if (Portfolio["SPY"].Invested && Daily_Close < Daily_SMA21)
                Debug("Liquidated SPY@" + data["SPY"].Value);