2024-09-24 13:39:23 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.NinjaScript.Indicators;
|
2024-09-24 13:53:39 +00:00
|
|
|
using System;
|
2024-09-24 13:51:55 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-09-24 13:39:23 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public class RSITwentyFiveBot : Strategy
|
|
|
|
{
|
2024-09-24 13:49:30 +00:00
|
|
|
private SMA longTermTrend;
|
2024-09-24 13:51:11 +00:00
|
|
|
private RSI rsi;
|
2024-09-24 13:49:30 +00:00
|
|
|
|
2024-09-24 13:39:23 +00:00
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
2024-09-24 13:46:35 +00:00
|
|
|
Description = @"Taken from chapter 3 of High Probability ETF Trading (2009) by Larry Connors";
|
2024-09-24 13:39:23 +00:00
|
|
|
Name = "RSI 25 / 75 Bot";
|
|
|
|
Calculate = Calculate.OnBarClose;
|
2024-09-24 14:50:57 +00:00
|
|
|
EntriesPerDirection = 2;
|
2024-09-24 13:39:23 +00:00
|
|
|
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-09-24 13:49:30 +00:00
|
|
|
|
|
|
|
LongTermTrendPeriod = 200;
|
2024-09-24 13:51:11 +00:00
|
|
|
RSIPeriod = 4;
|
|
|
|
RSISmoothing = 1;
|
2024-09-24 13:55:11 +00:00
|
|
|
EnableLongTrades = true;
|
2024-09-24 14:35:31 +00:00
|
|
|
LongRSIEntry = 25;
|
|
|
|
LongRSIExit = 55;
|
2024-09-24 13:55:11 +00:00
|
|
|
EnableShortTrades = true;
|
2024-09-24 14:35:31 +00:00
|
|
|
ShortRSIEntry = 75;
|
|
|
|
ShortRSIExit = 45;
|
2024-09-24 13:55:11 +00:00
|
|
|
EnableAggressiveEntries = true;
|
2024-09-24 14:35:31 +00:00
|
|
|
AggressiveLongRSIEntry = 20;
|
|
|
|
AggressiveShortRSIEntry = 80;
|
2024-09-24 13:39:23 +00:00
|
|
|
}
|
2024-09-24 13:49:30 +00:00
|
|
|
else if (State == State.DataLoaded)
|
2024-09-24 13:39:23 +00:00
|
|
|
{
|
2024-09-24 13:49:30 +00:00
|
|
|
longTermTrend = SMA(LongTermTrendPeriod);
|
2024-09-24 13:51:11 +00:00
|
|
|
rsi = RSI(RSIPeriod, RSISmoothing);
|
2024-09-24 13:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
2024-09-24 13:53:39 +00:00
|
|
|
if (CurrentBar < Math.Max(LongTermTrendPeriod, RSIPeriod))
|
|
|
|
return;
|
2024-09-24 14:40:51 +00:00
|
|
|
|
|
|
|
if (EnableLongTrades)
|
|
|
|
{
|
|
|
|
if (Close[0] > longTermTrend[0])
|
|
|
|
{
|
|
|
|
if (Position.MarketPosition == MarketPosition.Flat && rsi[0] < LongRSIEntry)
|
|
|
|
EnterLong();
|
|
|
|
else if (Position.MarketPosition == MarketPosition.Long && rsi[0] < AggressiveLongRSIEntry &&
|
|
|
|
EnableAggressiveEntries)
|
|
|
|
EnterLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Position.MarketPosition == MarketPosition.Long && rsi[0] > LongRSIExit)
|
|
|
|
ExitLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnableShortTrades)
|
|
|
|
{
|
|
|
|
if (Close[0] < longTermTrend[0])
|
|
|
|
{
|
|
|
|
if (Position.MarketPosition == MarketPosition.Flat && rsi[0] > ShortRSIEntry)
|
|
|
|
EnterShort();
|
|
|
|
else if (Position.MarketPosition == MarketPosition.Short && rsi[0] > AggressiveShortRSIEntry &&
|
|
|
|
EnableAggressiveEntries)
|
|
|
|
EnterShort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Position.MarketPosition == MarketPosition.Short && rsi[0] < ShortRSIExit)
|
|
|
|
ExitShort();
|
|
|
|
}
|
2024-09-24 13:39:23 +00:00
|
|
|
}
|
2024-09-24 13:47:21 +00:00
|
|
|
|
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Name; }
|
|
|
|
}
|
2024-09-24 13:49:30 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
|
|
|
[Display(Name = "Long-Term Trend Period", GroupName = "RSI 25 / 75 Bot", Order = 1)]
|
|
|
|
public int LongTermTrendPeriod { get; set; }
|
2024-09-24 13:51:11 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
|
|
|
[Display(Name = "RSI Period", GroupName = "RSI 25 / 75 Bot", Order = 2)]
|
|
|
|
public int RSIPeriod { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
|
|
|
[Display(Name = "RSI Smoothing", GroupName = "RSI 25 / 75 Bot", Order = 3)]
|
|
|
|
public int RSISmoothing { get; set; }
|
2024-09-24 13:55:11 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Enable Long Trades", GroupName = "RSI 25 / 75 Bot", Order = 4)]
|
|
|
|
public bool EnableLongTrades { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-09-24 14:35:31 +00:00
|
|
|
[Range(0.1, double.MaxValue)]
|
|
|
|
[Display(Name = "Long RSI Entry", GroupName = "RSI 25 / 75 Bot", Order = 5)]
|
|
|
|
public double LongRSIEntry { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(0.1, double.MaxValue)]
|
|
|
|
[Display(Name = "Long RSI Exit", GroupName = "RSI 25 / 75 Bot", Order = 6)]
|
|
|
|
public double LongRSIExit { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Enable Short Trades", GroupName = "RSI 25 / 75 Bot", Order = 7)]
|
2024-09-24 13:55:11 +00:00
|
|
|
public bool EnableShortTrades { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-09-24 14:35:31 +00:00
|
|
|
[Range(0.1, double.MaxValue)]
|
|
|
|
[Display(Name = "Short RSI Entry", GroupName = "RSI 25 / 75 Bot", Order = 8)]
|
|
|
|
public double ShortRSIEntry { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(0.1, double.MaxValue)]
|
|
|
|
[Display(Name = "Short RSI Exit", GroupName = "RSI 25 / 75 Bot", Order = 9)]
|
|
|
|
public double ShortRSIExit { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Enable Aggressive Entries", GroupName = "RSI 25 / 75 Bot", Order = 10)]
|
2024-09-24 13:55:11 +00:00
|
|
|
public bool EnableAggressiveEntries { get; set; }
|
2024-09-24 14:35:31 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(0.1, double.MaxValue)]
|
|
|
|
[Display(Name = "Aggressive Long RSI Entry", GroupName = "RSI 25 / 75 Bot", Order = 11)]
|
|
|
|
public double AggressiveLongRSIEntry { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(0.1, double.MaxValue)]
|
|
|
|
[Display(Name = "Aggressive Short RSI Entry", GroupName = "RSI 25 / 75 Bot", Order = 12)]
|
|
|
|
public double AggressiveShortRSIEntry { get; set; }
|
2024-09-24 13:39:23 +00:00
|
|
|
}
|
|
|
|
}
|