ninjatrader/strategies/connors-rsi/ConnorsRSIBot.cs
2024-08-27 12:43:08 -07:00

69 lines
2.3 KiB
C#

#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; }
}
}
}