From 8d4a805b6ec2994376495a6963b689d7bfe3b069 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 27 Aug 2024 12:43:08 -0700 Subject: [PATCH] Add ConnorsRSI Bot --- strategies/connors-rsi/ConnorsRSIBot.cs | 68 +++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 strategies/connors-rsi/ConnorsRSIBot.cs diff --git a/strategies/connors-rsi/ConnorsRSIBot.cs b/strategies/connors-rsi/ConnorsRSIBot.cs new file mode 100644 index 0000000..6cd01c6 --- /dev/null +++ b/strategies/connors-rsi/ConnorsRSIBot.cs @@ -0,0 +1,68 @@ +#region Using declarations +using NinjaTrader.Cbi; +using NinjaTrader.NinjaScript.Indicators; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class ConnorsRSIBot : Strategy + { + private ConnorsRSI connorsRSI; + + private SMA longTermTrend; + private SMA shortTermTrend; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @""; + Name = "Connors RSI 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.DataLoaded) + { + connorsRSI = ConnorsRSI(3, 1, 2, 1, 100); + + longTermTrend = SMA(200); + shortTermTrend = SMA(5); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < 200) + return; + + if (Close[0] > longTermTrend[0] && connorsRSI[0] < 15) + EnterLong(); + else if (Close[0] < longTermTrend[0] && connorsRSI[0] > 85) + EnterShort(); + + if (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0]) + ExitLong(); + else if (Position.MarketPosition == MarketPosition.Short && Close[0] < shortTermTrend[0]) + ExitShort(); + } + + public override string DisplayName + { + get { return Name; } + } + } +}