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

namespace NinjaTrader.NinjaScript.Strategies
{
    public class ConnorsRSIBot : Strategy
    {
        private ConnorsRSI connorsRSI;

        private SMA longTermTrend;
        private SMA shortTermTrend;
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description = @"Strategy based on the ConnorsRSI indicator developed by Larry Connors";
                Name = "Connors RSI Bot";
                Calculate = Calculate.OnBarClose;
                EntriesPerDirection = 1;
                EntryHandling = EntryHandling.AllEntries;
                IsExitOnSessionCloseStrategy = true;
                ExitOnSessionCloseSeconds = 30;
                IsFillLimitOnTouch = false;
                MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                OrderFillResolution = OrderFillResolution.Standard;
                Slippage = 0;
                StartBehavior = StartBehavior.WaitUntilFlat;
                TimeInForce = TimeInForce.Gtc;
                TraceOrders = false;
                RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                StopTargetHandling = StopTargetHandling.PerEntryExecution;
                BarsRequiredToTrade = 20;
                IsInstantiatedOnEachOptimizationIteration = true;

                RSIPeriod = 3;
                RSISmoothing = 1;
                StreakRSIPeriod = 2;
                StreakRSISmoothing = 1;
                PercentRankPeriod = 100;
                LongTermTrendPeriod = 200;
                ShortTermTrendPeriod = 5;
            }
            else if (State == State.DataLoaded)
            {
                connorsRSI = ConnorsRSI(RSIPeriod, RSISmoothing, StreakRSIPeriod, StreakRSISmoothing, PercentRankPeriod);

                longTermTrend = SMA(LongTermTrendPeriod);
                shortTermTrend = SMA(ShortTermTrendPeriod);
            }
        }

        protected override void OnBarUpdate()
        {
            if (CurrentBar < 200)
                return;
            
            if (Close[0] > longTermTrend[0] && connorsRSI[0] < 15)
                EnterLong();
            else if (Close[0] < longTermTrend[0] && connorsRSI[0] > 85)
                EnterShort();

            if (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0])
                ExitLong();
            else if (Position.MarketPosition == MarketPosition.Short && Close[0] < shortTermTrend[0])
                ExitShort();
        }

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

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

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

        [NinjaScriptProperty]
        [Display(Name = "Streak RSI Period", GroupName = "ConnorsRSI Bot", Order = 3)]
        public int StreakRSIPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Streak RSI Smoothing", GroupName = "ConnorsRSI Bot", Order = 4)]
        public int StreakRSISmoothing { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Percent Rank Period", GroupName = "ConnorsRSI Bot", Order = 5)]
        public int PercentRankPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Long Term Trend Period", GroupName = "ConnorsRSI Bot", Order = 6)]
        public int LongTermTrendPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Short Term Trend Period", GroupName = "ConnorsRSI Bot", Order = 7)]
        public int ShortTermTrendPeriod { get; set; }
    }
}