ninjatrader/strategies/tps/TPSBot.cs

169 lines
5.5 KiB
C#
Raw Permalink Normal View History

2024-09-30 14:11:53 +00:00
#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class TPSBot : Strategy
{
private SMA longTermTrend;
private RSI rsi;
private int tradeCount = 0;
private double lastEntryPrice = 0.0;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "TPS Bot";
Description = @"Taken from chapter 8 of High Probability ETF Trading (2009) and Buy the Fear, Sell the Greed (2018) by Larry Connors";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 4;
LongTermTrendPeriod = 200;
RSIPeriod = 2;
RSISmoothing = 1;
EnableLongTrades = true;
LongRSIEntry = 25;
LongRSIExit = 70;
EnableShortTrades = true;
ShortRSIEntry = 75;
ShortRSIExit = 30;
TotalPositionSize = 10000;
}
else if (State == State.DataLoaded)
{
longTermTrend = SMA(LongTermTrendPeriod);
rsi = RSI(RSIPeriod, RSISmoothing);
AddChartIndicator(longTermTrend);
AddChartIndicator(rsi);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(LongTermTrendPeriod, RSIPeriod))
return;
if (EnableLongTrades)
{
if (Position.MarketPosition == MarketPosition.Long && rsi[0] > LongRSIExit)
ExitTrade(true);
else if (Close[0] > longTermTrend[0])
{
if (Position.MarketPosition == MarketPosition.Flat && rsi[0] < LongRSIEntry && rsi[1] < LongRSIEntry)
EnterTrade(true);
else if (Position.MarketPosition == MarketPosition.Long && Close[0] < lastEntryPrice)
EnterTrade(true);
}
}
if (EnableShortTrades)
{
if (Position.MarketPosition == MarketPosition.Short && rsi[0] < ShortRSIExit)
ExitTrade(false);
else if (Close[0] < longTermTrend[0])
{
if (Position.MarketPosition == MarketPosition.Flat && rsi[0] > ShortRSIEntry && rsi[1] > ShortRSIEntry)
EnterTrade(false);
else if (Position.MarketPosition == MarketPosition.Short && Close[0] > lastEntryPrice)
EnterTrade(false);
}
}
}
private void EnterTrade(bool isLong)
{
tradeCount += 1;
int quantity = CalculateQuantity(tradeCount * 10.0);
if (isLong)
EnterLong(quantity);
else
EnterShort(quantity);
lastEntryPrice = Close[0];
}
private void ExitTrade(bool isLong)
{
if (isLong)
ExitLong();
else
ExitShort();
tradeCount = 0;
lastEntryPrice = 0.0;
}
private int CalculateQuantity(double percent)
{
double dollarAmount = TotalPositionSize * (percent / 100.0);
int quantity = Convert.ToInt32(Math.Floor(dollarAmount / Close[0]));
return quantity > 0 ? quantity : 1;
}
2024-10-01 12:36:22 +00:00
public override string DisplayName
{
get { return Name; }
}
2024-09-30 14:11:53 +00:00
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Long-Term Trend Period", GroupName = "TPS Bot", Order = 1)]
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "RSI Period", GroupName = "TPS Bot", Order = 2)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "RSI Smoothing", GroupName = "TPS Bot", Order = 3)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Display(Name = "Enable Long Trades", GroupName = "TPS Bot", Order = 4)]
public bool EnableLongTrades { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "Long RSI Entry", GroupName = "TPS Bot", Order = 5)]
public int LongRSIEntry { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "Long RSI Exit", GroupName = "TPS Bot", Order = 6)]
public int LongRSIExit { get; set; }
[NinjaScriptProperty]
[Display(Name = "Enable Short Trades", GroupName = "TPS Bot", Order = 7)]
public bool EnableShortTrades { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "Short RSI Entry", GroupName = "TPS Bot", Order = 8)]
public int ShortRSIEntry { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "Short RSI Exit", GroupName = "TPS Bot", Order = 9)]
public int ShortRSIExit { get; set; }
[NinjaScriptProperty]
[Range(0.1, double.MaxValue)]
[Display(Name = "Total Position Size", GroupName = "TPS Bot", Order = 10)]
public double TotalPositionSize { get; set; }
#endregion
}
}