From 1a17b9bd95f61131296f026665a631fb1e072be2 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sat, 11 May 2024 06:03:52 -0700 Subject: [PATCH] Add another opening range bot --- strategies/OpeningRangeBot.cs | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 strategies/OpeningRangeBot.cs diff --git a/strategies/OpeningRangeBot.cs b/strategies/OpeningRangeBot.cs new file mode 100644 index 0000000..d76921c --- /dev/null +++ b/strategies/OpeningRangeBot.cs @@ -0,0 +1,72 @@ +#region Using declarations +using NinjaTrader.Cbi; +using NinjaTrader.Data; +using NinjaTrader.NinjaScript.Indicators; +#endregion + +//This namespace holds Strategies in this folder and is required. Do not change it. +namespace NinjaTrader.NinjaScript.Strategies +{ + public class OpeningRangeBot : Strategy + { + private const int PrimaryBars = 0; + + private OpeningRange OR; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Strategy based on opening range breakouts"; + Name = "Opening Range 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; + } + else if (State == State.Configure) + { + AddDataSeries(BarsPeriodType.Minute, 1); + AddDataSeries(Instrument.FullName, new BarsPeriod + { BarsPeriodType = BarsPeriodType.Minute, Value = 5 }, "US Equities RTH"); + } + else if (State == State.DataLoaded) + { + OR = OpeningRange(30, OpeningRangeBarType.Minutes); + } + } + + protected override void OnBarUpdate() + { + // TODO: Make this timezone-agnostic. + if (ToTime(Time[0]) <= 70000) + return; + + if (PrimaryBars != BarsInProgress) + return; + + if (Open[0] < OR.ORH[0] && Close[0] > OR.ORH[0]) + { + SetStopLoss(CalculationMode.Price, Low[0]); + EnterLong(); + } + else if (Open[0] > OR.ORL[0] && Close[0] < OR.ORL[0]) + { + SetStopLoss(CalculationMode.Price, High[0]); + EnterShort(); + } + } + } +}