Overall Statistics
using System;
using System.Collections;
using System.Collections.Generic; 
using QuantConnect.Securities;  
using QuantConnect.Models;   

namespace QuantConnect 
{   
    // Name your algorithm class anything, as long as it inherits QCAlgorithm
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        decimal open = 0m;
        decimal close = 0m;
        private Consolidator oneDayConsol;
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            SetStartDate(2014, 12, 01);         
            SetEndDate(2014, 12, 03); 
            SetCash(1000);
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Second);
            
            oneDayConsol = new Consolidator(TimeSpan.FromDays(1));
            
            Log("ACTUAL VALUES:");
            Log("12/01/14: Open: 206.40, Close: 205.76");
            Log("12/02/14: Open: 205.81, Close: 207.09");
            //Log("12/03/14: Open: 207.30, Close: 207.89");
            Log("====================================");
            Log("EVALUATED VALUES:");
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        {   
            
            TradeBar SPY = data["SPY"];
            
            
            if (SPY.Time.Hour == 9 && SPY.Time.Minute == 30 && SPY.Time.Second == 00) {
                open = Math.Round(SPY.Price,2);
            }
            else if (SPY.Time.Hour == 15 && SPY.Time.Minute == 59 && SPY.Time.Second == 59) {
                close = Math.Round(SPY.Price,2);
                Log("TIME: Open: " + open.ToString() + " Close: " + close.ToString());
            }
            
            
            if (oneDayConsol.Update(SPY))
            {
                Log("CONSOL: Open: " + Math.Round(oneDayConsol.Bar.Open,2).ToString() + " Close: " + Math.Round(oneDayConsol.Bar.Close,2).ToString());
                Log("------------------------------------");
            }
            
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;

namespace QuantConnect 
{
    
    /*
    *   TimeSpanConsolidator Helper Routine: Assemble generic timespan bar lengths: e.g. 10 minutes:
    *
    *   1. Setup the new Consolidator class with the timespan period:
    *   var _consolidator = new Consolidator(TimeSpan.FromMinutes(10));
    *
    *   2. Add in the data with the update routine. It will return true when bar ready
    *   if (_consolidator.Update(data["MSFT"])) {   UseBar    }
    */
    public class Consolidator 
    {
        private TradeBar _resultBar;
        private TradeBar _workingBar;
        private DateTime _start;
        private TimeSpan _period;
        
        //Result:
        public TradeBar Bar
        {
            get
            {
                return _resultBar;
            }
        }
        
        //Constructor: Set the period we'd like to scan
        public Consolidator(TimeSpan span) 
        {
            this._period = span;
            this._resultBar = new TradeBar();
            this._workingBar = new TradeBar(new DateTime(), "", Decimal.Zero, Decimal.MinValue, Decimal.MaxValue, 0, 0);
        }
        
        //Submit this bar, return true if we've started a new one.
        public bool Update(TradeBar newBar)
        {
            //Intialize:
            if (_start == new DateTime()) 
            {
                _start = newBar.Time;
            }
            
            //While we're less than end date, keep adding to this bar:
            if (newBar.Time < (_start + _period))
            {
                //Building bar:
                AddToBar(newBar);
                return false;
            } 
            else 
            {
                //Completed bar: start new one:
                _resultBar = _workingBar;
                //Create a new bar:
                _workingBar = new TradeBar(newBar.Time, newBar.Symbol, Decimal.Zero, Decimal.MinValue, Decimal.MaxValue, 0, 0);
                //Start of this bar:
                _start = newBar.Time;
                AddToBar(newBar);
                return true;
            }
        }
        
        //Add to a tradebar
        private void AddToBar(TradeBar newBar)
        {
            //Add this data to working bar:
            if (_workingBar.Time == new DateTime()) _workingBar.Time = newBar.Time;
            if (_workingBar.Symbol == "") _workingBar.Symbol = newBar.Symbol;
            if (_workingBar.Open == Decimal.Zero) _workingBar.Open = newBar.Open;
            if (newBar.High > _workingBar.High) _workingBar.High = newBar.High;
            if (newBar.Low < _workingBar.Low) _workingBar.Low = newBar.Low;
            _workingBar.Close = newBar.Close;
            _workingBar.Volume = newBar.Volume;
        }
    }

}