#region Using declarations
using NinjaTrader.NinjaScript.Indicators;
using System;
using System.ComponentModel.DataAnnotations;
#endregion

namespace NinjaTrader.NinjaScript.Strategies
{
    public class ConnorsVIXReversal1 : Strategy
    {
        private const int PrimaryBars = 0;
        private const int VIXBars = 1;
        
        private MAX ShortTermHighs;
        private SMA LongTermTrend;
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name = "Connors VIX Reversal 1";
                Description = @"Taken from Chapter 2 of Connors on Advanced Trading Strategies (1998) by Larry Connors";
                Calculate = Calculate.OnBarClose;
                EntriesPerDirection = 1;

                ShortTermHighPeriod = 15;
                LongTermTrendPeriod = 200;
                DaysToExit = 3;
            }
            else if (State == State.Configure)
            {
                AddDataSeries("^VIX");
            }
            else if (State == State.DataLoaded)
            {
                ShortTermHighs = MAX(Highs[VIXBars], ShortTermHighPeriod);
                LongTermTrend = SMA(LongTermTrendPeriod);
            }
        }

        protected override void OnBarUpdate()
        {
            if (PrimaryBars != BarsInProgress)
                return;

            if (CurrentBar < Math.Max(LongTermTrendPeriod, ShortTermHighPeriod))
                return;

            if (Highs[VIXBars][0] == ShortTermHighs[0] && Closes[VIXBars][0] < Opens[VIXBars][0] && Close[0] > LongTermTrend[0])
                EnterLong();

            if (BarsSinceEntryExecution(PrimaryBars, "", 0) >= DaysToExit)
                ExitLong();
        }

        public override string DisplayName
        {
            get { return Name; }
        }

        [NinjaScriptProperty]
        [Display(Name = "Short-Term High Period", GroupName = "Connors VIX Reversal 1 Bot", Order = 1)]
        public int ShortTermHighPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Long-Term Trend Period", GroupName = "Connors VIX Reversal 1 Bot", Order = 2)]
        public int LongTermTrendPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Days to Exit", GroupName = "Connors VIX Reversal 1 Bot", Order = 3)]
        public int DaysToExit { get; set; }
    }
}