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 ;
RSIPeriod = 2 ;
RSISmoothing = 1 ;
2024-07-27 17:46:59 +00:00
CumulativeRSIPeriod = 2 ;
2024-07-18 19:18:07 +00:00
CumulativePeriod = 2 ;
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)]
[Display(Name = "RSI Period", Description = "The period for the RSI calculation", Order = 1, GroupName = "Cumulative RSI Bot")]
public int RSIPeriod { get ; set ; }
[Range(1, int.MaxValue)]
[Display(Name = "RSI Smoothing", Description = "The smoothing period for the RSI calculation", Order = 2, GroupName = "Cumulative RSI Bot")]
public int RSISmoothing { get ; set ; }
[Range(1, int.MaxValue)]
2024-07-27 17:46:59 +00:00
[Display(Name = "Cumulative RSI Period", Description = "The period for the cumulative RSI calculation", Order = 3, GroupName = "Cumulative RSI Bot")]
public int CumulativeRSIPeriod { get ; set ; }
[Range(1, int.MaxValue)]
[Display(Name = "Cumulative Period", Description = "The number of RSI values to add together", Order = 4, GroupName = "Cumulative RSI Bot")]
2024-07-18 19:18:07 +00:00
public int CumulativePeriod { get ; set ; }
[Range(1, int.MaxValue)]
2024-07-27 17:46:59 +00:00
[Display(Name = "SMA Period", Description = "The period for the moving average", Order = 5, GroupName = "Cumulative RSI Bot")]
2024-07-18 19:18:07 +00:00
public int SMAPeriod { get ; set ; }
[Range(1, 100)]
2024-07-27 17:46:59 +00:00
[Display(Name = "Entry Threshold", Description = "The cumulative RSI threshold for entering a long position", Order = 6, GroupName = "Cumulative RSI Bot")]
2024-07-18 19:18:07 +00:00
public int EntryThreshold { get ; set ; }
[Range(1, 100)]
2024-07-27 17:46:59 +00:00
[Display(Name = "Exit Threshold", Description = "The RSI threshold for exiting a long position", Order = 7, GroupName = "Cumulative RSI Bot")]
2024-07-18 19:18:07 +00:00
public int ExitThreshold { get ; set ; }
#endregion
}
}