Overall Statistics
namespace QuantConnect
{   
    public class YoniAmzROICFundamentals : QCAlgorithm
    {
    	// Notes
    	
		/* */
    	
        // User-editable Fields

        public  Dictionary<string, decimal> _tickerDictionary = new Dictionary<string, decimal> // ticker, weighted holdings
	    {
	        { "AAPL", 1.0m },
	        { "NFLX", 1.0m },
	        { "AMZN", 1.0m },
	        { "GOOGL", 1.0m },
	    };
		private double _polyA = 15.998;
		private double _polyB = 1.548;
		private double _polyC = 1.376;
		private int _warmupdays = 30;

        // Class Fields

        private DateTime _lastRebalanceTime = DateTime.MinValue;
        private TimeSpan _rebalanceInterval = TimeSpan.FromDays(30);
        private List<SecurityObject> _consideredSecuritiesList = new List<SecurityObject>();
        private List<SecurityObject> _winningSecuritiesList = new List<SecurityObject>();
        private List<SecurityObject> _losingSecuritiesList = new List<SecurityObject>();
        private bool _firstOnDataCall = true;

        // Initializer

        public override void Initialize()
        {
            SetStartDate(2015, 1, 5);
            SetEndDate(2015, 1, 6);
            SetCash(10000);

			foreach(KeyValuePair<string, decimal> entry in _tickerDictionary)
            {
            	string ticker = entry.Key;
            	decimal weightedholding = entry.Value;
            	
                AddSecurity(SecurityType.Equity, ticker, Resolution.Minute);
                Securities[ticker].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);
                Securities[ticker].SetLeverage(1);

				// ACTIVATE THESE
				//ROIC _roic = ROIC(entry.key, Resolution.Daily)
				//EnterpriseValue _enterprisevalue = ENTVAL(ticker, Resolution.Daily)
				//InvestedCapital _investedcapital = INVCAP(ticker, Resolution,Daily)
				
				// DEACTIVATE THESE
				double _roic = 0.0;
				double _enterprisevalue = 0.0;
				double _investedcapital = 0.0;
			
                _consideredSecuritiesList.Add(new SecurityObject { Ticker = ticker, HoldingsWeight = weightedholding, ROIC = _roic, EnterpriseValue = _enterprisevalue, InvestedCapital = _investedcapital });
                
                SetWarmup(_warmupdays);
            }
        }

        // Event-Driven Methods

        public void OnData(TradeBars data)
        {
        	// Abandon method if we're warming up the algorithm with historical data
            if (IsWarmingUp) return;
            
            // Abandon method if this is the first time the OnData() method is called (rules out first-of-day data errors)
            if (_firstOnDataCall)
            {
                _firstOnDataCall = false;
                _lastRebalanceTime = this.Time;
                return;
            }
            
			// Assess interval rules
			
            TimeSpan _timeSinceLastRebalance = this.Time.Subtract(_lastRebalanceTime);

            if (_timeSinceLastRebalance > _rebalanceInterval)
            {
                _lastRebalanceTime = this.Time;
                
				DefaultWinnersAndLosersToEmptyList();
				
				AssessWinnersAndLosers();
				
				TakeAction();
            }
        }

        // Rebalance Rules Methods

		public void DefaultWinnersAndLosersToEmptyList()
		{
			_winningSecuritiesList = new List<SecurityObject>();
			_losingSecuritiesList = new List<SecurityObject>();
		}
		
		public void AssessWinnersAndLosers()
		{
        	foreach (SecurityObject security in _consideredSecuritiesList)
            {
                security.DetermineValue(_polyA, _polyB, _polyC);
                
                if (security.IsOvervalued == false)
                {
                	_winningSecuritiesList.Add(security);
                }
                else // Overvalued == true
                {
                	_losingSecuritiesList.Add(security);
                }
            }
		}
		
		public void TakeAction()
		{
			// Sell our losing securities
			
			foreach (SecurityObject security in _losingSecuritiesList)
			{
				if (Portfolio[security.Ticker].HoldStock)
				{
					SetHoldings(security.Ticker, 0.0m);
				}
			}
			
			// Buy our winning securities
			
			decimal totalweightings = CalculateTotalWeightings();
			
			foreach (SecurityObject security in _winningSecuritiesList)
			{
				decimal percentageofportfolio = _tickerDictionary[security.Ticker] / totalweightings;
				SetHoldings(security.Ticker, Convert.ToDouble(percentageofportfolio));
			}
			
		}
		
		// Helper Methods
		
		public decimal CalculateTotalWeightings()
		{
			decimal calculatedtotalweightings = 0.0m;
			
			foreach (SecurityObject security in _winningSecuritiesList)
			{
				calculatedtotalweightings += _tickerDictionary[security.Ticker];
			}
			
			return calculatedtotalweightings;
		}
    }
}
namespace QuantConnect
{
    public class SecurityObject
    {
        // Fields
        private string _ticker;
        private bool _isovervalued = false;
        private decimal _holdingsweight = 0.0m;
        
        // ACTIVATE THESE
        //private ROIC _roic;
        //private EnterpriseValue _enterprisevalue;
        //private InvestedCapital _investedcapital;
        
        // DEACTIVATE THESE
        private double _roic;
        private double _enterprisevalue;
        private double _investedcapital;


        // Properties
        public string Ticker
        {
            get { return _ticker; }
            set
            {
                _ticker = value;
            }
        }
        
        public decimal HoldingsWeight
        {
            get { return _holdingsweight; }
            set
            {
                _holdingsweight = value;
            }
        }
        
        // ACTIVATE THESE
        
        //public ROIC ROIC
        //{
        //    get { return _roic; }
        //    set
        //    {
        //        _roic = value;
        //    }
        //}
        
        //public EnterpriseValue EnterpriseValue
        //{
        //    get { return _enterprisevalue; }
        //    set
        //    {
        //        _enterprisevalue = value;
        //    }
        //}
        
        //public InvestedCapital InvestedCapital
        //{
        //    get { return _investedcapital; }
        //    set
        //    {
        //        _investedcapital = value;
        //    }
        //}
        
        // DEACTIVATE THESE
        
        public double ROIC
        {
            get { return _roic; }
            set
            {
                _roic = value;
            }
        }
        
        public double EnterpriseValue
        {
            get { return _enterprisevalue; }
            set
            {
                _enterprisevalue = value;
            }
        }
        
        public double InvestedCapital
        {
            get { return _investedcapital; }
            set
            {
                _investedcapital = value;
            }
        }
        
        public bool IsOvervalued
        {
            get { return _isovervalued; }
            set
            {
                _isovervalued = value;
            }
        }

        // Methods
        public void DetermineValue(double polyA, double polyB, double polyC)
        {
        	double varX = ROIC;
        	double varY = (polyA * Math.Pow(varX, 2)) + (polyB * varX) + (polyC);
        	double actualY = EnterpriseValue / InvestedCapital;
        	
			if (actualY >= varY)
			{
				IsOvervalued = true;
			}
			else
			{
				IsOvervalued = false;
			}
        }
    }
}