2024-07-20 12:39:13 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.NinjaScript.Indicators;
|
|
|
|
using System;
|
2024-09-09 15:05:35 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-07-20 12:39:13 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public class TwoPeriodRSI : Strategy
|
|
|
|
{
|
2024-09-09 14:06:10 +00:00
|
|
|
private SMA longTermTrend;
|
|
|
|
private SMA shortTermTrend;
|
2024-07-20 12:39:13 +00:00
|
|
|
private RSI rsi;
|
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
|
|
|
Description = @"Taken from chapter 9 of Short Term Trading Strategies That Work";
|
|
|
|
Name = "2 Period RSI";
|
|
|
|
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;
|
|
|
|
IsInstantiatedOnEachOptimizationIteration = true;
|
2024-09-09 15:05:35 +00:00
|
|
|
|
2024-09-09 15:09:53 +00:00
|
|
|
RSIPeriod = 2;
|
|
|
|
RSISmoothing = 1;
|
2024-09-09 15:13:57 +00:00
|
|
|
LongEntryThreshold = 5;
|
|
|
|
ShortEntryThreshold = 95;
|
2024-09-09 15:05:35 +00:00
|
|
|
LongTermTrendPeriod = 200;
|
2024-09-09 15:07:37 +00:00
|
|
|
ShortTermTrendPeriod = 5;
|
2024-07-20 12:39:13 +00:00
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
2024-09-09 15:05:35 +00:00
|
|
|
longTermTrend = SMA(LongTermTrendPeriod);
|
2024-09-09 15:07:37 +00:00
|
|
|
shortTermTrend = SMA(ShortTermTrendPeriod);
|
2024-09-09 15:09:53 +00:00
|
|
|
rsi = RSI(RSIPeriod, RSISmoothing);
|
2024-07-20 12:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
2024-09-09 15:07:37 +00:00
|
|
|
if (CurrentBar < Math.Max(LongTermTrendPeriod, ShortTermTrendPeriod))
|
|
|
|
return;
|
2024-07-20 12:39:13 +00:00
|
|
|
|
|
|
|
// Entries
|
|
|
|
int quantity = (int)Math.Floor(1000000 / Close[0]);
|
2024-09-09 15:13:57 +00:00
|
|
|
if (Close[0] > longTermTrend[0] && rsi[0] < LongEntryThreshold)
|
2024-07-20 12:39:13 +00:00
|
|
|
{
|
|
|
|
if (Position.MarketPosition != MarketPosition.Long)
|
|
|
|
EnterLong(quantity);
|
|
|
|
}
|
2024-09-09 15:13:57 +00:00
|
|
|
else if (Close[0] < longTermTrend[0] && rsi[0] > ShortEntryThreshold)
|
2024-07-20 12:39:13 +00:00
|
|
|
{
|
|
|
|
if (Position.MarketPosition != MarketPosition.Short)
|
|
|
|
EnterShort(quantity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exits
|
2024-09-09 14:06:10 +00:00
|
|
|
if (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0])
|
2024-07-20 12:39:13 +00:00
|
|
|
ExitLong();
|
2024-09-09 14:06:10 +00:00
|
|
|
else if (Position.MarketPosition == MarketPosition.Short && Close[0] < shortTermTrend[0])
|
2024-07-20 12:39:13 +00:00
|
|
|
ExitShort();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Name; }
|
|
|
|
}
|
2024-09-09 15:05:35 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-09-09 15:09:53 +00:00
|
|
|
[Display(Name = "RSI Period", GroupName = "2 Period RSI", Order = 1)]
|
|
|
|
public int RSIPeriod { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "RSI Smoothing", GroupName = "2 Period RSI", Order = 2)]
|
|
|
|
public int RSISmoothing { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-09-09 15:13:57 +00:00
|
|
|
[Display(Name = "Long Entry Threshold", GroupName = "2 Period RSI", Order = 3)]
|
|
|
|
public int LongEntryThreshold { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Short Entry Threshold", GroupName = "2 Period RSI", Order = 4)]
|
|
|
|
public int ShortEntryThreshold { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Long Term Trend Period", GroupName = "2 Period RSI", Order = 5)]
|
2024-09-09 15:05:35 +00:00
|
|
|
public int LongTermTrendPeriod { get; set; }
|
2024-09-09 15:07:37 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2024-09-09 15:13:57 +00:00
|
|
|
[Display(Name = "Short Term Trend Period", GroupName = "2 Period RSI", Order = 6)]
|
2024-09-09 15:07:37 +00:00
|
|
|
public int ShortTermTrendPeriod { get; set; }
|
2024-07-20 12:39:13 +00:00
|
|
|
}
|
|
|
|
}
|