ninjatrader/strategies/trin/TRINBot.cs

115 lines
3.9 KiB
C#
Raw Normal View History

2024-09-21 13:32:21 +00:00
#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
2024-09-21 13:32:21 +00:00
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class TRINBot : Strategy
{
private RSI rsi;
2024-09-23 12:49:05 +00:00
private SMA longTermTrend;
2024-09-21 13:32:21 +00:00
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
2024-09-21 13:38:27 +00:00
Description = @"Taken from chapter 12 of Short Term Trading Strategies That Work";
2024-09-21 13:32:21 +00:00
Name = "TRIN 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;
LongTermTrendPeriod = 200;
2024-09-23 12:47:10 +00:00
RSIPeriod = 2;
RSISmoothing = 1;
RSIEntryThreshold = 50;
RSIExitThreshold = 65;
2024-09-23 13:09:55 +00:00
TRINEntryThreshold = 1.0;
DaysAboveThreshold = 3;
2024-09-21 13:32:21 +00:00
}
else if (State == State.Configure)
{
AddDataSeries("^TRIN", BarsPeriodType.Day, 1);
}
else if (State == State.DataLoaded)
{
2024-09-23 12:47:10 +00:00
rsi = RSI(RSIPeriod, RSISmoothing);
2024-09-23 12:49:05 +00:00
longTermTrend = SMA(LongTermTrendPeriod);
2024-09-21 13:32:21 +00:00
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBar < LongTermTrendPeriod || CurrentBars[1] < DaysAboveThreshold)
2024-09-21 13:32:21 +00:00
return;
if (Close[0] > longTermTrend[0] && rsi[0] < RSIEntryThreshold)
2024-09-21 13:32:21 +00:00
{
for (int i = 0; i < DaysAboveThreshold; i++)
{
if (Closes[1][i] <= TRINEntryThreshold)
return;
}
EnterLong();
2024-09-21 13:32:21 +00:00
}
else if (rsi[0] > RSIExitThreshold)
2024-09-21 13:32:21 +00:00
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Long-Term Trend Period", GroupName = "TRIN Bot", Order = 1)]
public int LongTermTrendPeriod { get; set; }
2024-09-23 12:47:10 +00:00
[NinjaScriptProperty]
[Display(Name = "RSI Period", GroupName = "TRIN Bot", Order = 2)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Smoothing", GroupName = "TRIN Bot", Order = 3)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Entry Threshold", GroupName = "TRIN Bot", Order = 4)]
public int RSIEntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Exit Threshold", GroupName = "TRIN Bot", Order = 5)]
public int RSIExitThreshold { get; set; }
2024-09-23 13:09:55 +00:00
[NinjaScriptProperty]
[Display(Name = "TRIN Entry Threshold", GroupName = "TRIN Bot", Order = 6)]
public double TRINEntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "Days Above Threshold", GroupName = "TRIN Bot", Order = 7)]
public int DaysAboveThreshold { get; set; }
2024-09-21 13:32:21 +00:00
}
}