ninjatrader/strategies/connors-rsi/ConnorsRSIBot.cs

69 lines
2.3 KiB
C#
Raw Normal View History

2024-08-27 19:43:08 +00:00
#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)
{
2024-08-28 19:15:45 +00:00
Description = @"Strategy based on the ConnorsRSI indicator developed by Larry Connors";
2024-08-27 19:43:08 +00:00
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; }
}
}
}