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,
 * 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.
*/
"""

from QuantConnect.Data.Market import TradeBar
from datetime import timedelta
from datetime import datetime
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d


class MyAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2018, 5, 6)   # Set Start Date
        self.SetEndDate(2018, 5, 14)    # Set End Date
        self.SetCash(100000)            # Strategy Cash
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
        self.symbol = self.AddEquity("SPY", Resolution.Second).Symbol

        # consolidate 60 seconds to for minute bars
        consolidator_minute = TradeBarConsolidator(60)
        consolidator_minute.DataConsolidated += self.OnMinuteData
        self.SubscriptionManager.AddConsolidator(self.symbol, consolidator_minute)

        # setup Rolling Window
        self.minute_rw = RollingWindow[TradeBar](2)

        # Schedule def to run 1 min after self.symbol opens
        self.Schedule.On(self.DateRules.EveryDay(),
                         self.TimeRules.AfterMarketOpen(self.symbol, 1),
                         Action(self.x_minute_after_open_market))

    def OnMinuteData(self, sender, bar):
        self.minute_rw.Add(bar)

    def x_minute_after_open_market(self):
        if not (self.minute_rw.IsReady): return
        
        currBar1 = self.minute_rw[0]  # Current bar has index 0.
        pastBar1 = self.minute_rw[1]  # Past bar has index 1.
        
        self.Log("Script fired at (s/b 09:31) : {0}".format(self.Time))
        
        self.Log("Past Bar Time on 1 min s/b FINAL 1 min of prior day")
        self.Log("Past Bar Price: Time: {0} Open: {1} High: {2} Low: {3} Close: {4}".format(pastBar1.Time, pastBar1.Open, pastBar1.High, pastBar1.Low, pastBar1.Close))
        
        self.Log("Current Bar Time on 1 min s/b FIRST 1 min of current day")
        self.Log("Current Bar Price: Time: {0} Open: {1} High: {2} Low: {3} Close: {4}".format(currBar1.Time, currBar1.Open, currBar1.High, currBar1.Low, currBar1.Close))