From 8e923b7a412bfa7b6020be501684f72f36ca4a10 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sat, 1 Jun 2024 06:00:31 -0700 Subject: [PATCH] Add strategy that randomly places trades based on the current RSI value --- strategies/RandomBotRSI.cs | 116 +++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 strategies/RandomBotRSI.cs diff --git a/strategies/RandomBotRSI.cs b/strategies/RandomBotRSI.cs new file mode 100644 index 0000000..0f024da --- /dev/null +++ b/strategies/RandomBotRSI.cs @@ -0,0 +1,116 @@ +#region Using declarations +using System; +using System.ComponentModel.DataAnnotations; +using NinjaTrader.Cbi; +using NinjaTrader.NinjaScript.Indicators; +#endregion + +//This namespace holds Strategies in this folder and is required. Do not change it. +namespace NinjaTrader.NinjaScript.Strategies +{ + public class RandomBotRSI : Strategy + { + private Random RNG = new Random(); + + private ATR atr; + private RSI rsi; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Randomly places trades based on the current RSI value"; + Name = "Random Bot (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; + BarsRequiredToTrade = 20; + IsInstantiatedOnEachOptimizationIteration = true; + + PercentageChanceToEnterTrade = 25; + RSIPeriod = 14; + RSISmoothing = 3; + ATRPeriod = 14; + ATRMultiplier = 2; + } + else if (State == State.DataLoaded) + { + RNG = new Random(); + + atr = ATR(ATRPeriod); + rsi = RSI(RSIPeriod, RSISmoothing); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < BarsRequiredToTrade) + return; + + if (Position.MarketPosition != MarketPosition.Flat) + return; + + if (ShouldEnterTrade()) + { + double atrValue = atr[0]; + double stopLoss = ATRMultiplier * atrValue; + double profitTarget = ATRMultiplier * atrValue; + + if (rsi[0] > 80.0) + { + EnterLong(); + SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize); + SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize); + } + else if (rsi[0] < 20.0) + { + EnterShort(); + SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize); + SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize); + } + } + } + + private bool ShouldEnterTrade() + { + return RNG.Next(100) < PercentageChanceToEnterTrade; + } + + public override string DisplayName + { + get { return Name; } + } + + [Range(1, 100), NinjaScriptProperty] + [Display(Name = "Percentage Chance to Enter Trade", GroupName = "Random Bot (RSI)", Order = 1)] + public int PercentageChanceToEnterTrade { get; set; } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "RSI Period", GroupName = "Random Bot (RSI)", Order = 2)] + public int RSIPeriod { get; set; } + + [NinjaScriptProperty] + [Range(1, int.MaxValue)] + [Display(Name = "RSI Smoothing", GroupName = "Random Bot (RSI)", Order = 3)] + public int RSISmoothing { get; set; } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "ATR Period", GroupName = "Random Bot (RSI)", Order = 4)] + public int ATRPeriod { get; set; } + + [Range(0.1, double.MaxValue), NinjaScriptProperty] + [Display(Name = "ATR Multiplier", GroupName = "Random Bot (RSI)", Order = 5)] + public double ATRMultiplier { get; set; } + } +}