ninjatrader/strategies/cumulative-rsi/CumulativeRSIBot.cs

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