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

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

        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name = "2-Period RSI";
                Description = @"Taken from chapter 9 of Short Term Trading Strategies That Work";
                Calculate = Calculate.OnBarClose;
                EntriesPerDirection = 1;

                RSIPeriod = 2;
                RSISmoothing = 1;
                LongEntryThreshold = 5;
                ShortEntryThreshold = 95;
                LongTermTrendPeriod = 200;
                ShortTermTrendPeriod = 5;
                UseFixedPositionSizing = false;
                FixedPositionSize = 10000;
                LongOnly = false;
            }
            else if (State == State.DataLoaded)
            {
                longTermTrend = SMA(LongTermTrendPeriod);
                shortTermTrend = SMA(ShortTermTrendPeriod);
                rsi = RSI(RSIPeriod, RSISmoothing);
            }
        }

        protected override void OnBarUpdate()
        {
            if (CurrentBar < Math.Max(LongTermTrendPeriod, ShortTermTrendPeriod))
                return;

            int quantity;
            if (UseFixedPositionSizing)
                quantity = (int)Math.Floor(FixedPositionSize / Close[0]);
            else
                quantity = 1;

            if (Close[0] > longTermTrend[0] && rsi[0] < LongEntryThreshold)
            {
                if (Position.MarketPosition != MarketPosition.Long)
                    EnterLong(quantity);
            }
            else if (Close[0] < longTermTrend[0] && rsi[0] > ShortEntryThreshold && !LongOnly)
            {
                if (Position.MarketPosition != MarketPosition.Short)
                    EnterShort(quantity);
            }

            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 = "2-Period RSI", Order = 1)]
        public int RSIPeriod { get; set; }

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

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

        [NinjaScriptProperty]
        [Display(Name = "Short Entry Threshold", GroupName = "2-Period RSI", Order = 4)]
        public int ShortEntryThreshold { get; set; }

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

        [NinjaScriptProperty]
        [Display(Name = "Short-Term Trend Period", GroupName = "2-Period RSI", Order = 6)]
        public int ShortTermTrendPeriod { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Use Fixed Position Sizing", GroupName = "2-Period RSI", Order = 7)]
        public bool UseFixedPositionSizing { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Fixed Position Size", GroupName = "2-Period RSI", Order = 8)]
        public int FixedPositionSize { get; set; }

        [NinjaScriptProperty]
        [Display(Name = "Long Only", GroupName = "2-Period RSI", Order = 9)]
        public bool LongOnly { get; set; }
    }
}