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

namespace NinjaTrader.NinjaScript.Strategies
{
    public class TwoPeriodRSIHighs : Strategy
    {
        private RSI rsi;
        private SMA longTermTrend;

        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name = "2-Period RSI Highs";
                Description = @"Based on chatper 11 of How Markets Really Work (2012) by Larry Connors";
                Calculate = Calculate.OnBarClose;
                EntriesPerDirection = 1;

                RSIPeriod = 2;
                RSISmoothing = 1;
                RSIEntry = 85;
                LongTermTrendPeriod = 200;
                DaysToExit = 5;
            }
            else if (State == State.DataLoaded)
            {
                rsi = RSI(RSIPeriod, RSISmoothing);
                longTermTrend = SMA(LongTermTrendPeriod);
            }
        }

        protected override void OnBarUpdate()
        {
            if (CurrentBar < RSIPeriod)
                return;

            if (rsi[0] > RSIEntry && Close[0] > longTermTrend[0])
                EnterLong();

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

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

        [NinjaScriptProperty]
        [Display(Name = "RSI Period", GroupName = "2-Period RSI Highs", Order = 1)]
        public int RSIPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "RSI Smoothing", GroupName = "2-Period RSI Highs", Order = 2)]
        public int RSISmoothing { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "RSI Entry", GroupName = "2-Period RSI Highs", Order = 3)]
        public int RSIEntry { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Long-Term Trend Period", GroupName = "2-Period RSI Highs", Order = 4)]
        public int LongTermTrendPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Days to Exit", GroupName = "2-Period RSI Highs", Order = 5)]
        public int DaysToExit { get; set; }
    }
}