Overall Statistics
Total Trades
3
Average Win
0%
Average Loss
0%
Compounding Annual Return
411.781%
Drawdown
2.400%
Expectancy
0
Net Profit
0%
Sharpe Ratio
4.223
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.007
Beta
1.264
Annual Standard Deviation
0.256
Annual Variance
0.065
Information Ratio
2.498
Tracking Error
0.093
Treynor Ratio
0.854
Total Fees
$16.91
/*
 * 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 System;
using System.Collections.Generic;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Securities;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// Basic template algorithm simply initializes the date range and cash
    /// </summary>
    public class CustomUniverseTriggerTimesAlgorithm : QCAlgorithm
    {
        /// <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, 10, 07);  //Set Start Date
            SetEndDate(2013, 10, 11);    //Set End Date
            SetCash(100000);             //Set Strategy Cash
            // Find more symbols here: http://quantconnect.com/data
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour);
			
			UniverseSettings.Resolution = Resolution.Second;
			
            AddUniverse(new PreMarketDailyUsEquityUniverse(UniverseSettings, SecurityInitializer, TimeSpan.FromMinutes(10), dateTime =>
            {
                Debug("Universe selection trigger time: " + dateTime);
                // here's where you can do your custom selection logic, such as call a web service
                return new List<string> {"AIG", "BAC", "IBM"};
            }));
        }

        /// <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 void OnData(TradeBars data)
        {
            if (!Portfolio.Invested)
            {
            	foreach (var item in data.Values.Where(x=>!x.Symbol.Equals("SPY")))
            	{
            		SetHoldings(item.Symbol, 1m / 3);
                	Debug("Purchased Stock: " + item.Symbol);
            	}
            }
            
        }
    }

    /// <summary>
    /// Specifies a universe which fires before us-equity market open each day
    /// </summary>
    public class PreMarketDailyUsEquityUniverse : UserDefinedUniverse
    {
        private readonly TimeSpan _timeBeforeMarketOpen;

        public PreMarketDailyUsEquityUniverse(UniverseSettings universeSettings, ISecurityInitializer securityInitializer, TimeSpan timeBeforeMarketOpen, Func<DateTime, IEnumerable<string>> selector)
            : base(CreateConfiguration(), universeSettings, securityInitializer, TimeSpan.MaxValue, selector)
        {
            _timeBeforeMarketOpen = timeBeforeMarketOpen;
        }

		// this configuration is used internally, so we'll create a us-equity configuration
        private static SubscriptionDataConfig CreateConfiguration()
        {
            // use us-equity market hours for 'exchange is open' logic
            var marketHoursDbEntry = MarketHoursDatabase.FromDataFolder().GetEntry(QuantConnect.Market.USA, null, SecurityType.Equity);
            // this is the time zone the data is in, now in our case, our unvierse doesn't have 'data'
            var dataTimeZone = marketHoursDbEntry.DataTimeZone;
            var exchangeTimeZone = marketHoursDbEntry.ExchangeHours.TimeZone;
            var symbol = Symbol.Create("pre-market-daily-us-equity-universe", SecurityType.Equity, QuantConnect.Market.USA);
            return new SubscriptionDataConfig(typeof (Tick), symbol, Resolution.Daily, dataTimeZone, exchangeTimeZone, false, false, true);
        }

        /// <summary>
        /// This funtion is used to determine at what times to fire the selection function
        /// </summary>
        /// <param name="startTimeUtc">The start of the interval (first date in backtest, launch time in live)</param>
        /// <param name="endTimeUtc">The end of the interval (EOD last date in backtest, <see cref="Time.EndOfTime"/> in live</param>
        /// <param name="marketHoursDatabase">A market hours database instance for resolving market hours</param>
        /// <returns>The date time trigger times in UTC</returns>
        public override IEnumerable<DateTime> GetTriggerTimes(DateTime startTimeUtc, DateTime endTimeUtc, MarketHoursDatabase marketHoursDatabase)
        {
            // convert times to local
            var startTimeLocal = startTimeUtc.ConvertFromUtc(TimeZones.NewYork);
            var endTimeLocal = endTimeUtc.ConvertFromUtc(TimeZones.NewYork);

            // get the us-equity market hours
            var exchangeHours = marketHoursDatabase.GetExchangeHours(QuantConnect.Market.USA, null, SecurityType.Equity);

            // loop over each tradeable date in our time frame
            foreach (var tradeableDate in Time.EachTradeableDay(exchangeHours, startTimeLocal, endTimeLocal))
            {
                // get the market open time for this date
                var marketOpen = exchangeHours.GetNextMarketOpen(tradeableDate, false);

                // subtract out how much time before market open we'd like to fire
                yield return marketOpen - _timeBeforeMarketOpen;
            }
        }
    }
}