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
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// 1. adding SMA 
    /// </summary>
    public class FindValue1 : QCAlgorithm
    {
        private const int _numberOfSymbolsCoarse = 100;
        private const int _numberOfSymbolsFine = 5;
		private const decimal _investmentAmt = .2m;
		
		private const int _minVolume = -1; //200000;
		private const decimal _minPrice = -1; // 1m;
		private const decimal _maxPrice = -1; //100;
		private const decimal _maxPbRatio = -1; // .99m;
		private const decimal _maxPeRatio = -1; //18.49m;
		private const decimal _yieldGrowthMinPct = -1; // .3m;
		private const string _sortType = "PB";
		private const string _sortDir = "DESC";
		
		private const int _smaPeriod = 50;
		
		//private const int _minVolume = 
		
        // initialize our changes to nothing
        private SecurityChanges _changes = SecurityChanges.None;

        public override void Initialize()
        {
            UniverseSettings.Resolution = Resolution.Daily;

            SetStartDate(2018, 05, 01);
            SetEndDate(2018, 06, 01);
            SetCash(25000);

            // this add universe method accepts two parameters:
            // - coarse selection function: accepts an IEnumerable<CoarseFundamental> and returns an IEnumerable<Symbol>
            // - fine selection function: accepts an IEnumerable<FineFundamental> and returns an IEnumerable<Symbol>
            AddUniverse(CoarseSelectionFunction, FineSelectionFunction);
            
            
            foreach (Security stock in Securities.Values)
        	{
        		Debug("SECURITY = " + stock.Symbol);	
        	}
        }

        // sort the data by daily dollar volume and take the top 'NumberOfSymbolsCoarse'
        public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
        {
            // Has Fundamentals, Min/Max Price filters
            var filteredSorted = coarse
                .Where(x => x.HasFundamentalData
                		&& (_minVolume < 0 || x.Volume >= _minVolume)
                		&& (_minPrice < 0 || x.Price >= _minPrice)
                		&& (_maxPrice < 0 || x.Price <= _maxPrice))
                .OrderByDescending(x => x.DollarVolume);

            // take the top entries from our sorted collection
            var filtered = filteredSorted.Take(_numberOfSymbolsCoarse);

			//Debug("COURSE COUNT = " + filtered.Count());
			
            // we need to return only the symbol objects
            return filtered.Select(x => x.Symbol);
        }

        // sort the data by P/E ratio and take the top 'NumberOfSymbolsFine'
        public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
        {
        	//Price/Book filter
        	var filtered = fine.Where(e => (_maxPbRatio < 0 || ( e.ValuationRatios.PBRatio <= _maxPbRatio && e.ValuationRatios.PBRatio > 0m)));

 			if(_maxPeRatio >= 0){
				//PE filter        	
	        	filtered = filtered.Where(e => e.ValuationRatios.PERatio <= _maxPeRatio);
 			}
        	
        	if(_yieldGrowthMinPct >= 0){
	        	//EPS Growth - proj this yr vs last yr
	        	//Forward > Yield && (Forward / Yield -1 > .33) _yieldGrowthMinPct
				filtered = filtered.Where(e => e.ValuationRatios.ForwardEarningYield > 0 && e.ValuationRatios.EarningYield > 0);
								//&& (( e.ValuationRatios.ForwardEarningYield /  e.ValuationRatios.EarningYield) -1) >=  _yieldGrowthMinPct);
				
				filtered = filtered.Where(e => (( e.ValuationRatios.ForwardEarningYield /  e.ValuationRatios.EarningYield) -1) >=  _yieldGrowthMinPct);
        	}
			
			//Price Performance 52 weeks – 35.14 and above ()
			
			//% Price off 50 day SMA : -1% and below  ()
			
            // sort descending by P/E ratio
            if(_sortType == "PE" && _sortDir == "ASC"){
	            filtered = filtered.OrderBy(x => x.ValuationRatios.PERatio);
            }else if (_sortType == "PE" && _sortDir == "DESC"){
            	filtered = filtered.OrderByDescending(x => x.ValuationRatios.PERatio);
            }else if (_sortType == "PB" && _sortDir == "ASC"){
            	filtered = filtered.OrderBy(x => x.ValuationRatios.PBRatio);
            }else if(_sortType == "PB" && _sortDir == "DESC"){
            	filtered = filtered.OrderByDescending(x => x.ValuationRatios.PBRatio);
            }
            
            // take the top entries from our sorted collection
            filtered = filtered.Take(_numberOfSymbolsFine);

			Debug("FINE COUNT = " + filtered.Count());
		
            // we need to return only the symbol objects
            return filtered.Select(x => x.Symbol);
        }

        //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)
        {
        	
        	/*
            // if we have no changes, do nothing
            if (_changes == SecurityChanges.None) return;

            // liquidate removed securities
            foreach (var security in _changes.RemovedSecurities)
            {
                if (security.Invested)
                {
                    Liquidate(security.Symbol);
                    Debug("Liquidated Stock: " + security.Symbol.Value);
                }
            }

            // we want 50% allocation in each security in our universe
            foreach (var security in _changes.AddedSecurities)
            {
                SetHoldings(security.Symbol, _investmentAmt); //0.5m);
                Debug("Purchased Stock: " + security.Symbol.Value);
            }

            _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("Securities added: " + string.Join(",", changes.AddedSecurities.Select(x => x.Symbol.Value)));
            }
            if (changes.RemovedSecurities.Count > 0)
            {
                Debug("Securities removed: " + string.Join(",", changes.RemovedSecurities.Select(x => x.Symbol.Value)));
            }
            
        }
    }
}