Overall Statistics
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;
        }
    }

}
namespace QuantConnect 
{   

    public class AlwaysDataAlgorithm : QCAlgorithm
    {
        //consolidator
        private TimeSpan _barPeriod = TimeSpan.FromDays(1);
        private Consolidator _consolidator;
        
        //indicators
        SimpleMovingAverage _sma;
        
        //Data Required 
        List<string> _symbols = new List<string>() { "AAPL", "SPY", "IBM" };
        List<string> _forexSymbols = new List<string>
        {
            "EURUSD",
            "USDJPY",
            "EURGBP",
            "EURCHF",
            "USDCAD",
            "USDCHF",
            "AUDUSD",
            "NZDUSD",
        };
        
        TradeBars _bars = new TradeBars();
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            //Start and End Date range for the backtest:
            SetStartDate(2014, 12, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            
            //Cash allocation
            SetCash(25000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            foreach (var symbol in _symbols) {
                AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
                
                //Setup Consolidator bar
                _consolidator = new Consolidator(_barPeriod);
                //_sma = new SimpleMovingAverage(10);
            }
            //Add as many securities as you like. All the data will be passed into the event handler:
            foreach (var symbol in _forexSymbols) {
                AddSecurity(SecurityType.Forex, symbol, Resolution.Minute);
                
                //Setup Consolidator bar
                _consolidator = new Consolidator(_barPeriod);
                //_sma = new SimpleMovingAverage(10);
            }
            _symbols.AddRange(_forexSymbols);
        }

        //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) 
        {   
            UpdateBars(data);
            if (_bars.Count != _symbols.Count) return;
            
            foreach (var symbol in _symbols)
            {
                if (_bars.ContainsKey(symbol))
                {
                if (_consolidator.Update(_bars[symbol]))
                {
                    if(!Portfolio[symbol].Invested)
                    {
                        Buy(symbol, 1);
                    }
                    
                    //if (data.ContainsKey(symbol))
                    //{
                    //AlgoPro(_bars[symbol], symbol);
                    Log(" "+_bars[symbol].Time+" "+_bars[symbol].Symbol+" "+_bars[symbol].Close);
                    //}
                }
                }

            }
            
            
        }
        
        //Update the global "_bars" object
        private void UpdateBars(TradeBars data) 
        {
            foreach (var bar in data.Values)
            {
                if (!_bars.ContainsKey(bar.Symbol)) {
                    _bars.Add(bar.Symbol, bar);
                }
                _bars[bar.Symbol] = bar;
            }
        }
    }
}