diff --git a/strategies/TripleMovingAverageSlopeBot.cs b/strategies/TripleMovingAverageSlopeBot.cs new file mode 100644 index 0000000..0af6277 --- /dev/null +++ b/strategies/TripleMovingAverageSlopeBot.cs @@ -0,0 +1,110 @@ +#region Using declarations +using NinjaTrader.Cbi; +using NinjaTrader.Data; +using NinjaTrader.NinjaScript.Indicators; +using System; +using System.ComponentModel.DataAnnotations; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class TripleMovingAverageSlopeBot : Strategy + { + private EMA fastMA; + private EMA mediumMA; + private EMA slowMA; + private SMA longTermTrend; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @""; + Name = "Triple Moving Average Slope Bot"; + Calculate = Calculate.OnBarClose; + EntriesPerDirection = 1; + EntryHandling = EntryHandling.AllEntries; + IsExitOnSessionCloseStrategy = false; + 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; + IsInstantiatedOnEachOptimizationIteration = true; + + FastMAPeriod = 100; + MediumMAPeriod = 200; + SlowMAPeriod = 300; + LongTermTrendPeriod = 20; + LongOnly = false; + } + else if (State == State.Configure) + { + AddDataSeries(BarsPeriodType.Day, 1); + } + else if (State == State.DataLoaded) + { + fastMA = EMA(FastMAPeriod); + mediumMA = EMA(MediumMAPeriod); + slowMA = EMA(SlowMAPeriod); + + longTermTrend = SMA(BarsArray[1], LongTermTrendPeriod); + } + } + + protected override void OnBarUpdate() + { + if (BarsInProgress != 0) + return; + + if (CurrentBar < Math.Max(FastMAPeriod, Math.Max(MediumMAPeriod, SlowMAPeriod))) + return; + + if (CurrentBars[1] < 0) + return; + + if (fastMA[0] > fastMA[1] && mediumMA[0] > mediumMA[1] && slowMA[0] > slowMA[1] && Close[0] > longTermTrend[0]) + EnterLong(); + else if (fastMA[0] < fastMA[1] && mediumMA[0] < mediumMA[1]) + ExitLong(); + + if (!LongOnly) + { + if (fastMA[0] < fastMA[1] && mediumMA[0] < mediumMA[1] && slowMA[0] < slowMA[1] && Close[0] < longTermTrend[0]) + EnterShort(); + else if (fastMA[0] > fastMA[1] && mediumMA[0] > mediumMA[1]) + ExitShort(); + } + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "Fast MA Period", GroupName = "Triple Moving Average Slope Bot", Order = 1)] + public int FastMAPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Medium MA Period", GroupName = "Triple Moving Average Slope Bot", Order = 2)] + public int MediumMAPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Slow MA Period", GroupName = "Triple Moving Average Slope Bot", Order = 3)] + public int SlowMAPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Long Term Trend Period", GroupName = "Triple Moving Average Slope Bot", Order = 4)] + public int LongTermTrendPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Long Only", GroupName = "Triple Moving Average Slope Bot", Order = 5)] + public bool LongOnly { get; set; } + } +}