2024-07-18 19:18:07 +00:00
#region Using declarations
using NinjaTrader.Cbi ;
using NinjaTrader.NinjaScript.Indicators ;
using System ;
using System.ComponentModel.DataAnnotations ;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class CumulativeRSIBot : Strategy
{
private CumulativeRSI cumulativeRSI ;
private RSI rsi ;
private SMA sma ;
protected override void OnStateChange ( )
{
if ( State = = State . SetDefaults )
{
Description = @"Credit to Larry Connors and his book Short Term Trading Strategies That Work" ;
Name = "Cumulative 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 ;
2024-07-27 17:46:59 +00:00
CumulativeRSIPeriod = 2 ;
2024-07-18 19:18:07 +00:00
CumulativePeriod = 2 ;
2024-07-27 17:48:32 +00:00
RSIPeriod = 2 ;
RSISmoothing = 1 ;
2024-07-18 19:18:07 +00:00
SMAPeriod = 200 ;
EntryThreshold = 35 ;
ExitThreshold = 65 ;
}
else if ( State = = State . DataLoaded )
{
2024-07-27 17:46:59 +00:00
cumulativeRSI = CumulativeRSI ( CumulativeRSIPeriod , CumulativePeriod ) ;
2024-07-18 19:18:07 +00:00
rsi = RSI ( RSIPeriod , RSISmoothing ) ;
sma = SMA ( SMAPeriod ) ;
}
}
protected override void OnBarUpdate ( )
{
if ( CurrentBar < Math . Max ( SMAPeriod , Math . Max ( RSIPeriod , CumulativePeriod ) ) )
return ;
if ( Close [ 0 ] > sma [ 0 ] & & cumulativeRSI [ 0 ] < EntryThreshold )
EnterLong ( ) ;
if ( rsi [ 0 ] > ExitThreshold & & Position . MarketPosition = = MarketPosition . Long )
ExitLong ( ) ;
}
public override string DisplayName
{
get { return Name ; }
}
#region Properties
[Range(1, int.MaxValue)]
2024-07-27 17:49:39 +00:00
[Display(Name = "Cumulative RSI Period", Description = "The period for the cumulative RSI calculation", GroupName = "Cumulative RSI Bot", Order = 1)]
2024-07-27 17:48:32 +00:00
public int CumulativeRSIPeriod { get ; set ; }
2024-07-18 19:18:07 +00:00
[Range(1, int.MaxValue)]
2024-07-27 17:49:39 +00:00
[Display(Name = "Cumulative Period", Description = "The number of RSI values to add together", GroupName = "Cumulative RSI Bot", Order = 2)]
2024-07-27 17:48:32 +00:00
public int CumulativePeriod { get ; set ; }
2024-07-18 19:18:07 +00:00
[Range(1, int.MaxValue)]
2024-07-27 17:49:39 +00:00
[Display(Name = "RSI Period", Description = "The period for the RSI calculation", GroupName = "Cumulative RSI Bot", Order = 3)]
2024-07-27 17:48:32 +00:00
public int RSIPeriod { get ; set ; }
2024-07-27 17:46:59 +00:00
[Range(1, int.MaxValue)]
2024-07-27 17:49:39 +00:00
[Display(Name = "RSI Smoothing", Description = "The smoothing period for the RSI calculation", GroupName = "Cumulative RSI Bot", Order = 4)]
2024-07-27 17:48:32 +00:00
public int RSISmoothing { get ; set ; }
2024-07-18 19:18:07 +00:00
[Range(1, int.MaxValue)]
2024-07-27 17:49:39 +00:00
[Display(Name = "SMA Period", Description = "The period for the moving average", GroupName = "Cumulative RSI Bot", Order = 5)]
2024-07-18 19:18:07 +00:00
public int SMAPeriod { get ; set ; }
[Range(1, 100)]
2024-07-27 17:49:39 +00:00
[Display(Name = "Entry Threshold", Description = "The cumulative RSI threshold for entering a long position", GroupName = "Cumulative RSI Bot", Order = 6)]
2024-07-18 19:18:07 +00:00
public int EntryThreshold { get ; set ; }
[Range(1, 100)]
2024-07-27 17:49:39 +00:00
[Display(Name = "Exit Threshold", Description = "The RSI threshold for exiting a long position", GroupName = "Cumulative RSI Bot", Order = 7)]
2024-07-18 19:18:07 +00:00
public int ExitThreshold { get ; set ; }
#endregion
}
}