// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API MACD Crossover indicator provided by njardim@email.com on Dec 2015.
//
//    Based on the original MACD Crossover indicator from cTrade.
//
// -------------------------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BarTimes : Indicator
    {
        [Output("Signal", LineColor = "Blue", PlotType = PlotType.Line, LineStyle = LineStyle.Lines, Thickness = 1)]
        public IndicatorDataSeries Signal { get; set; }

        public IndicatorDataSeries Times { get; set; }
        private MovingAverage TimesMa;

        protected override void Initialize()
        {
            Times = CreateDataSeries();
            TimesMa = Indicators.MovingAverage(Times, 200, MovingAverageType.Simple );
        }

        public override void Calculate(int index)
        {
            if( index>0 )
            {
                var t0 = Bars.OpenTimes[index];
                var t1 = Bars.OpenTimes[index-1];
                if( t0.ToLocalTime().Date == t1.ToLocalTime().Date )
                    Times[index] = (t0-t1).TotalMinutes;
                else
                    Times[index] = 0;
            }
            
            Signal[index] = Times[index]>0? TimesMa.Result[index] / Times[index] : 0;
        }
    }
}
