Overall Statistics
using System;
using QuantConnect.Data.Market;

namespace QuantConnect.Indicators
{
    /// <summary>

    /// SumTrueRange is defined as the maximum of the following:
    ///   High - Low
    ///   ABS(High - PreviousClose)
    ///   ABS(Low  - PreviousClose)
    ///  Summed over a N peridos window
    /// VMplus:
    ///   
    /// SUM(ABS(High - previous.Low), N periods)
    /// VMminus:
    ///   
    ///  SUM(ABS(High - previous.High), N periods)
    ///  Vortex Indicator:
    ///  VMplus / SumTrueRange - VMminus / SumTrueRange
    ///
    ///
    
    /// </summary>
    public class Vortex : TradeBarIndicator
    {

        private Sum _Sum;
                private int _period;
                
        /// <summary>
        /// Gets the Sumtrue,VMplus and VMminus range which is the more volatile calculation to be smoothed by this indicator
        /// </summary>
        public IndicatorBase<TradeBar> SumTrueRange { get; private set; }
        public IndicatorBase<TradeBar> VMplus { get; private set; } 
                public IndicatorBase<TradeBar> VMminus { get; private set; }
                
        /// <summary>
        /// Gets a flag indicating when this indicator is ready and fully initialized
        /// </summary>
        
        public override bool IsReady
        {
            get { return Samples > _period; }
        }

        /// <summary>
        /// Creates a new AverageTrueRange indicator using the specified period and moving average type
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">The smoothing period used to smooth the true range values</param>

        public Vortex(string name, int period)
            : base(name)
        {
           _period = period;

            TradeBar previous = null;
            
            SumTrueRange = new FunctionalIndicator<TradeBar>(name + "_SumTrueRange", currentBar =>
            {
                // in our ComputeNextValue function we'll just call the ComputeTrueRange
                var nextValue = ComputeSumTrueRange(previous, currentBar);
                previous = currentBar;
                return nextValue;
            }   // in our IsReady function we just need at least two sample
            , trueRangeIndicator => trueRangeIndicator.Samples >= _period
            );
            
            VMplus = new FunctionalIndicator<TradeBar>(name + "_SumTrueRange", currentBar =>
            {
                // in our ComputeNextValue function we'll just call the ComputeTrueRange
                var nextValue = ComputeVMplus(previous, currentBar);
                previous = currentBar;
                return nextValue;
            }   // in our IsReady function we just need at least two sample
            , trueRangeIndicator => trueRangeIndicator.Samples >= _period
            );
            
            VMminus = new FunctionalIndicator<TradeBar>(name + "_SumTrueRange", currentBar =>
            {
                // in our ComputeNextValue function we'll just call the ComputeTrueRange
                var nextValue = ComputeVMminus(previous, currentBar);
                previous = currentBar;
                return nextValue;
            }   // in our IsReady function we just need at least two sample
            , trueRangeIndicator => trueRangeIndicator.Samples >= _period
            );
            
            
            
   
            
            
            
        }

        /// <summary>
        /// Creates a new AverageTrueRange indicator using the specified period and moving average type
        /// </summary>
        /// <param name="period">The smoothing period used to smooth the true range values</param>
        /// <param name="movingAverageType">The type of smoothing used to smooth the true range values</param>
        public Vortex(int period)
            : this("Vortex" + period, period)
        {
        }


        public decimal ComputeSumTrueRange(TradeBar previous, TradeBar current)
        {
            var range1 = current.High - current.Low;
            if (previous == null)
            {
                return 0m;
            }

            var range2 = Math.Abs(current.High - previous.Close);
            var range3 = Math.Abs(current.Low - previous.Close);

            return SumTrueRange.Current + Math.Max(range1, Math.Max(range2, range3));
        }
                
        public decimal ComputeVMplus(TradeBar previous, TradeBar current)
        {
            
            if (previous == null)
            {
                return 0;
            }

            var range = Math.Abs(current.High - previous.Low);
            
                        
            return Current + range;
        }
                
        public decimal ComputeVMminus(TradeBar previous, TradeBar current)
        {
            
            if (previous == null)
            {
                return 0m; 
            }

            var range = Math.Abs(current.Low - previous.High);

            return Current + range;
        }
                
                
                
        /// <summary>
        /// Computes the next value of this indicator from the given state
        /// </summary>
        /// <param name="input">The input given to the indicator</param>
        /// <returns>A new value for this indicator</returns>
        protected override decimal ComputeNextValue(TradeBar input)
        {
            // compute the true range and then sum it
            SumTrueRange.Update(input);
            VMplus.Update(input);
            VMminus.Update(input);
            
            decimal VIplus= VMplus / SumTrueRange;
            decimal VIminus= VMminus / SumTrueRange;
            return VIplus - VIminus;
        }

        /// <summary>
        /// Resets this indicator to its initial state
        /// </summary>
        public override void Reset()
        {
            
            SumTrueRange.Reset();
            VMplus.Reset();
            VMminus.Reset();
            base.Reset();
        }
    }
}
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {
			
            //Start and End Date range for the backtest:
            SetStartDate(2013, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            
            //Cash allocation
            SetCash(25000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
        }

        //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) 
        {   
            // "TradeBars" object holds many "TradeBar" objects: it is a dictionary indexed by the symbol:
            // 
            //  e.g.  data["MSFT"] data["GOOG"]
            
            if (!Portfolio.HoldStock) 
            {
                int quantity = (int)Math.Floor(Portfolio.Cash / data["SPY"].Close);
                
                //Order function places trades: enter the string symbol and the quantity you want:
                Order("SPY",  quantity);
                
                //Debug sends messages to the user console: "Time" is the algorithm time keeper object 
                Debug("Purchased SPY on " + Time.ToShortDateString());
                
                //You can also use log to send longer messages to a file. You are capped to 10kb
                //Log("This is a longer message send to log.");
            }
        }
    }
}