ninjatrader/strategies/cumulative-rsi/CumulativeRSIBot.cs

103 lines
4.1 KiB
C#

#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;
CumulativeRSIPeriod = 2;
CumulativePeriod = 2;
RSIPeriod = 2;
RSISmoothing = 1;
SMAPeriod = 200;
EntryThreshold = 35;
ExitThreshold = 65;
}
else if (State == State.DataLoaded)
{
cumulativeRSI = CumulativeRSI(CumulativeRSIPeriod, CumulativePeriod);
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 = "Cumulative RSI Period", Description = "The period for the cumulative RSI calculation", GroupName = "Cumulative RSI Bot", Order = 1)]
public int CumulativeRSIPeriod { get; set; }
[Range(1, int.MaxValue)]
[Display(Name = "Cumulative Period", Description = "The number of RSI values to add together", GroupName = "Cumulative RSI Bot", Order = 2)]
public int CumulativePeriod { get; set; }
[Range(1, int.MaxValue)]
[Display(Name = "RSI Period", Description = "The period for the RSI calculation", GroupName = "Cumulative RSI Bot", Order = 3)]
public int RSIPeriod { get; set; }
[Range(1, int.MaxValue)]
[Display(Name = "RSI Smoothing", Description = "The smoothing period for the RSI calculation", GroupName = "Cumulative RSI Bot", Order = 4)]
public int RSISmoothing { get; set; }
[Range(1, int.MaxValue)]
[Display(Name = "SMA Period", Description = "The period for the moving average", GroupName = "Cumulative RSI Bot", Order = 5)]
public int SMAPeriod { get; set; }
[Range(1, 100)]
[Display(Name = "Entry Threshold", Description = "The cumulative RSI threshold for entering a long position", GroupName = "Cumulative RSI Bot", Order = 6)]
public int EntryThreshold { get; set; }
[Range(1, 100)]
[Display(Name = "Exit Threshold", Description = "The RSI threshold for exiting a long position", GroupName = "Cumulative RSI Bot", Order = 7)]
public int ExitThreshold { get; set; }
#endregion
}
}