Overall Statistics
using MathNet.Numerics;

namespace QuantConnect
{
	public class AnnualizedExponentialSlope : WindowIndicator<IndicatorDataPoint>
    {
        public AnnualizedExponentialSlope(int period)
            : base("AdjustedSlope" + period, period)
        {
        }

        public AnnualizedExponentialSlope(string name, int period)
            : base(name, period)
        {
        }

        protected override decimal ComputeNextValue(IReadOnlyWindow<IndicatorDataPoint> window, IndicatorDataPoint input)
        {
            if (window.Count < 3) return 0m;

            var xVals = new double[window.Count];
            var yVals = new double[window.Count];

            // load input data for regression
            for (int i = 0; i < window.Count; i++)
            {
                xVals[i] = i;
                // we want the log of our y values
                yVals[i] = Math.Log((double)window[window.Count - i - 1].Value);
            }

            //http://numerics.mathdotnet.com/Regression.html

            // solves y=a + b*x via linear regression
            var fit = Fit.Line(xVals, yVals);
            var intercept = fit.Item1;
            var slope = fit.Item2;

            // compute rsquared
            var rsquared = GoodnessOfFit.RSquared(xVals.Select(x => intercept + slope*x), yVals);

            // anything this small can be viewed as flat
            if (double.IsNaN(slope) || Math.Abs(slope) < 1e-25) return 0m;

            // trading days per year for us equities
            const int dayCount = 252;

            // annualize dy/dt
            var annualSlope = ((Math.Pow(Math.Exp(slope), dayCount)) - 1) * 100;

            // scale with rsquared
            annualSlope = annualSlope * rsquared;

            return (decimal) annualSlope;
        }
    }
}
namespace QuantConnect 
{   
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
    	private RateOfChangePercent ROCPyear;
    	private RateOfChange ROCPROCPyear;
    	//private ROCPRateOfChangePercent ROCPROCPyear;
    	private string symbol;
    	
        public override void Initialize() 
        {
            SetStartDate(2006, 1, 1);
            SetEndDate(DateTime.Now);
            SetCash(25000);
            symbol = "LNG";
            AddSecurity(SecurityType.Equity, symbol, Resolution.Daily);
            
            ROCPyear = new RateOfChangePercent(252); // 252 trading days in a US year
            RegisterIndicator(symbol, ROCPyear, Resolution.Daily, Field.Close);
            PlotIndicator("ROCP", true, ROCPyear);
            
            ROCPROCPyear = new RateOfChange(252).Of(ROCPyear);
            //ROCPROCPyear = new ROCPRateOfChangePercent(252);
            //RegisterIndicator(symbol, ROCPROCPyear, Resolution.Daily, Field.Close);
            PlotIndicator("ROCP", true, ROCPROCPyear);
        }

        //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 (!ROCPyear.IsReady) return;
        	
        	var quantity = Portfolio[symbol].Quantity;
        	if (quantity == 0 && ROCPyear > 20)
        	{
        		SetHoldings(symbol, .75);
        	}
        	if (quantity > 0) {
        		if (ROCPyear < 0) {
        			SetHoldings(symbol, 0);
        		}
        	}
        	if (quantity < 0) {
        		if (ROCPyear > 0) {
        			SetHoldings(symbol, 0);
        		}
        	}
        	if (quantity == 0 && ROCPyear < -20)
        	{
        			SetHoldings(symbol, -.75);
        	}
        }
    }
}