Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
namespace QuantConnect 
{   
    /*
    *   Basic Template Algorithm
    *
    *   The underlying QCAlgorithm class has many methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full base class can be found at:
    *   https://github.com/QuantConnect/Lean/tree/master/Algorithm
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
    	private SecurityChanges _changes = SecurityChanges.None;
    	private int _todayCount = 0;
    	private DateTime _lastDate = DateTime.MinValue;
    	
        public override void Initialize() 
        {

        	// backtest parameters
            SetStartDate(2017, 2, 1);
            SetEndDate(2017, 2, 7);
            
            // cash allocation
            SetCash(25000);
            
            UniverseSettings.Resolution = Resolution.Minute;
            AddUniverse(CoarseSelectionFunction, FineSelectionFunction);
        }
        public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
        {
        	Debug("COURSE SELECTION BEGIN");
                
        	var filtered = (from cf in coarse 
			where cf.Symbol.Value == "SPY"
            select cf.Symbol);
                        
            return filtered;     
        }
        
        public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
        {
        	Debug("FINE SELECTION BEGIN");
        	
        	var filtered = (from cf in fine 
			where cf.Symbol.Value == "SPY"
            select cf.Symbol);
                        
            return filtered;  			
        }
        
        public void OnData(TradeBars data) 
        {
        	if(Time.Date != _lastDate){
        		_lastDate = Time.Date;
        		_todayCount = 0;
        	}
        	
        	_todayCount ++;
        	if(_todayCount > 5)
        		return;

        	//Get just this bar.
        	var spyBar = data.Values.Where(e=>e.Symbol.Value=="SPY").FirstOrDefault();
        	if(spyBar != null){
        		Debug($"ONDATA {spyBar.Symbol}: open={spyBar.Open};  close={spyBar.Close}; endtime={spyBar.EndTime}");
        	}
        	
        	if (_changes == SecurityChanges.None) return;
        	
            if (!Portfolio.HoldStock) 
            {
                SetHoldings("SPY", 1);
                Debug("Purchased SPY on " + Time.ToShortDateString());
            }
            _changes = SecurityChanges.None;
        }
        
        // this event fires whenever we have changes to our universe
        public override void OnSecuritiesChanged(SecurityChanges changes)
        {

            _changes = changes;
            
            if(changes.AddedSecurities.Count > 0){
            	Debug($"Added {changes.AddedSecurities.Count} securities at : {Time}");
            }
            
            foreach (var sec in changes.AddedSecurities)
            {
            	Securities[sec.Symbol].SetDataNormalizationMode(DataNormalizationMode.Raw);
            	var symbolData = sec.Symbol;
                var consolidator = (IDataConsolidator)new TradeBarConsolidator(TimeSpan.FromMinutes(1440));
                consolidator.DataConsolidated += (sender, baseData) =>
                {
	                var bar = (IBaseDataBar)baseData;
                	if(bar.Symbol.Value == "SPY"){
	                    Debug($"CONSOLIDATOR {bar.Symbol}: open={bar.Open};  close={bar.Close}; endtime={bar.EndTime}");
                	}
                };

                SubscriptionManager.AddConsolidator(sec.Symbol, consolidator);
            }
        }        
    }
}