144 lines
5.2 KiB
C#
144 lines
5.2 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 RSITwentyFiveBot : Strategy
|
|
{
|
|
private SMA longTermTrend;
|
|
private RSI rsi;
|
|
|
|
protected override void OnStateChange()
|
|
{
|
|
if (State == State.SetDefaults)
|
|
{
|
|
Description = @"Taken from chapter 3 of High Probability ETF Trading (2009) by Larry Connors";
|
|
Name = "RSI 25 / 75 Bot";
|
|
Calculate = Calculate.OnBarClose;
|
|
EntriesPerDirection = 2;
|
|
IsInstantiatedOnEachOptimizationIteration = true;
|
|
|
|
LongTermTrendPeriod = 200;
|
|
RSIPeriod = 4;
|
|
RSISmoothing = 1;
|
|
EnableLongTrades = true;
|
|
LongRSIEntry = 25;
|
|
LongRSIExit = 55;
|
|
EnableShortTrades = true;
|
|
ShortRSIEntry = 75;
|
|
ShortRSIExit = 45;
|
|
EnableAggressiveEntries = true;
|
|
AggressiveLongRSIEntry = 20;
|
|
AggressiveShortRSIEntry = 80;
|
|
}
|
|
else if (State == State.DataLoaded)
|
|
{
|
|
longTermTrend = SMA(LongTermTrendPeriod);
|
|
rsi = RSI(RSIPeriod, RSISmoothing);
|
|
}
|
|
}
|
|
|
|
protected override void OnBarUpdate()
|
|
{
|
|
if (CurrentBar < Math.Max(LongTermTrendPeriod, RSIPeriod))
|
|
return;
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
public override string DisplayName
|
|
{
|
|
get { return Name; }
|
|
}
|
|
|
|
[NinjaScriptProperty]
|
|
[Range(1, int.MaxValue)]
|
|
[Display(Name = "Long-Term Trend Period", GroupName = "RSI 25 / 75 Bot", Order = 1)]
|
|
public int LongTermTrendPeriod { get; set; }
|
|
|
|
[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; }
|
|
|
|
[NinjaScriptProperty]
|
|
[Display(Name = "Enable Long Trades", GroupName = "RSI 25 / 75 Bot", Order = 4)]
|
|
public bool EnableLongTrades { get; set; }
|
|
|
|
[NinjaScriptProperty]
|
|
[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)]
|
|
public bool EnableShortTrades { get; set; }
|
|
|
|
[NinjaScriptProperty]
|
|
[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)]
|
|
public bool EnableAggressiveEntries { get; set; }
|
|
|
|
[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; }
|
|
}
|
|
}
|