Overall Statistics
using QuantConnect.Indicators;
using System;
using System.Collections.Concurrent;
namespace QuantConnect 
{   
    public class VolatilityEffect : QCAlgorithm
    {
    	public readonly ConcurrentDictionary<Symbol, StandardDeviation> _symbolsData = new ConcurrentDictionary<Symbol, StandardDeviation>();
    	public int _month = 0;
        public override void Initialize() 
        {
        	// backtest parameters
        	UniverseSettings.Leverage = 2.0m;
			UniverseSettings.Resolution = Resolution.Daily;
            SetStartDate(2005, 5, 1);         
            SetEndDate(2013, 1, 1);
            SetCash(100000);
            AddUniverse( coarse =>
            		{
            			return
            			( 	
            				from cf in coarse
            				let std = _symbolsData.GetOrAdd(cf.Symbol, sym => new StandardDeviation(600))
            				where std.Update(cf.EndTime,cf.Price)
							where cf.Price > 10.0m
            				orderby cf.DollarVolume descending
            				select cf.Symbol
            			).Take(100);
            		}
            	);
        }

        public override void OnData(Slice data) 
        {
			if(Time.Month != _month)
			{
				_month = Time.Month;
				foreach(var sym in Portfolio.Keys)
				{
					Liquidate(sym);
				}
				var leastVolatile = (from _symbol in data.Keys
									orderby _symbolsData[_symbol]
									select _symbol).Take(2);
				foreach(var entry in leastVolatile)
				{
					SetHoldings(entry,0.5);
				}
			}
        }
    }
}