From 593b02afa9016bc90a9fa213e52840f4cf969980 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 16 Jul 2024 11:08:52 -0700 Subject: [PATCH] Initial commit of Triple Moving Average bot --- strategies/TripleMovingAverageBot.cs | 97 ++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 strategies/TripleMovingAverageBot.cs diff --git a/strategies/TripleMovingAverageBot.cs b/strategies/TripleMovingAverageBot.cs new file mode 100644 index 0000000..8529241 --- /dev/null +++ b/strategies/TripleMovingAverageBot.cs @@ -0,0 +1,97 @@ +#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 TripleMovingAverageBot : 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 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; + BarsRequiredToTrade = 20; + IsInstantiatedOnEachOptimizationIteration = true; + + FastMAPeriod = 10; + MediumMAPeriod = 20; + SlowMAPeriod = 100; + LongTermTrendPeriod = 20; + } + 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] > mediumMA[0] && mediumMA[0] > slowMA[0] && Close[0] > longTermTrend[0]) + EnterLong(); + else if (fastMA[0] < mediumMA[0] && mediumMA[0] < slowMA[0]) + ExitLong(); + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "Fast MA Period", GroupName = "Triple Moving Average Bot", Order = 1)] + public int FastMAPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Medium MA Period", GroupName = "Triple Moving Average Bot", Order = 2)] + public int MediumMAPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Slow MA Period", GroupName = "Triple Moving Average Bot", Order = 3)] + public int SlowMAPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Long Term Trend Period", GroupName = "Triple Moving Average Bot", Order = 4)] + public int LongTermTrendPeriod { get; set; } + } +}