ninjatrader/strategies/CumulativeRSIBot.cs

103 lines
4.1 KiB
C#
Raw Normal View History

#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;
CumulativeRSIPeriod = 2;
CumulativePeriod = 2;
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 = "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)]
[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")]
public int CumulativePeriod { get; set; }
[Range(1, int.MaxValue)]
[Display(Name = "SMA Period", Description = "The period for the moving average", Order = 5, GroupName = "Cumulative RSI Bot")]
public int SMAPeriod { get; set; }
[Range(1, 100)]
[Display(Name = "Entry Threshold", Description = "The cumulative RSI threshold for entering a long position", Order = 6, GroupName = "Cumulative RSI Bot")]
public int EntryThreshold { get; set; }
[Range(1, 100)]
[Display(Name = "Exit Threshold", Description = "The RSI threshold for exiting a long position", Order = 7, GroupName = "Cumulative RSI Bot")]
public int ExitThreshold { get; set; }
#endregion
}
}