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

namespace NinjaTrader.NinjaScript.Strategies
{
    public class TenDayLowBot : Strategy
    {
        private MIN lows;
        private SMA shortTermTrend;
        private SMA longTermTrend;
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description = @"Strategy taken from chapter 2 of Short Term Trading Strategies That Work";
                Name = "10 Day Low 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;

                LowPeriod = 10;
                ShortTermTrendPeriod = 10;
                LongTermTrendPeriod = 200;
            }
            else if (State == State.DataLoaded)
            {
                lows = MIN(Low, LowPeriod);
                shortTermTrend = SMA(ShortTermTrendPeriod);
                longTermTrend = SMA(LongTermTrendPeriod);
            }
        }

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

            if (Low[0] == lows[0] && Close[0] > longTermTrend[0])
                EnterLong();

            if (Close[0] > shortTermTrend[0])
                ExitLong();
        }

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

        [NinjaScriptProperty]
        [Display(Name = "Low Period", GroupName = "10-Day Low Bot", Order = 1)]
        public int LowPeriod { get; set; }

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

        [NinjaScriptProperty]
        [Display(Name = "Long Term Trend Period", GroupName = "10-Day Low Bot", Order = 3)]
        public int LongTermTrendPeriod { get; set; }
    }
}