From fea62f40df6f3658d1edf64e421734625831cff4 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Wed, 18 Dec 2024 07:45:35 -0800 Subject: [PATCH] Initial commit of Engulfing strategy --- strategies/engulfing/EngulfingBot.cs | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 strategies/engulfing/EngulfingBot.cs diff --git a/strategies/engulfing/EngulfingBot.cs b/strategies/engulfing/EngulfingBot.cs new file mode 100644 index 0000000..763995e --- /dev/null +++ b/strategies/engulfing/EngulfingBot.cs @@ -0,0 +1,51 @@ +#region Using declarations +using NinjaTrader.Cbi; +using NinjaTrader.NinjaScript.Indicators; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class EngulfingBot : Strategy + { + private SMA LongTermTrend; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Name = "Engulfing Bot"; + Description = @"Simple strategy based on the engulfing candle pattern"; + Calculate = Calculate.OnBarClose; + EntriesPerDirection = 1; + } + else if (State == State.DataLoaded) + { + LongTermTrend = SMA(200); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < BarsRequiredToTrade) + return; + + if (High[0] > High[1] && Close[0] < Low[1]) + { + if (Close[0] >= LongTermTrend[0]) + EnterLong(); + else + EnterShort(); + } + + if (Position.MarketPosition == MarketPosition.Long && (Close[0] > Position.AveragePrice || Close[0] > High[1])) + ExitLong(); + else if (Position.MarketPosition == MarketPosition.Short && (Close[0] < Position.AveragePrice || Close[0] < Low[1])) + ExitShort(); + } + + public override string DisplayName + { + get { return Name; } + } + } +}