From b21523e6f14fcbd1ee7fea2626e38f32d5c411d5 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sun, 4 Aug 2024 07:01:14 -0700 Subject: [PATCH] Add strategy based on Williams %R --- strategies/WilliamsPercentRBot.cs | 106 ++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 strategies/WilliamsPercentRBot.cs diff --git a/strategies/WilliamsPercentRBot.cs b/strategies/WilliamsPercentRBot.cs new file mode 100644 index 0000000..d2db09c --- /dev/null +++ b/strategies/WilliamsPercentRBot.cs @@ -0,0 +1,106 @@ +#region Using declarations +using NinjaTrader.Cbi; +using NinjaTrader.Data; +using NinjaTrader.NinjaScript.Indicators; +using System.ComponentModel.DataAnnotations; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class WilliamsPercentRBot : Strategy + { + private WilliamsR williamsR; + + private SMA longTermTrend; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Williams %R Bot"; + Name = "Williams %R Bot"; + Calculate = Calculate.OnBarClose; + EntriesPerDirection = 1; + EntryHandling = EntryHandling.AllEntries; + IsExitOnSessionCloseStrategy = false; + 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; + + WilliamsPercentRPeriod = 2; + EntryThreshold = -90; + ExitThreshold = -30; + UseNewHighExit = false; + UseTrendFilter = false; + TrendFilterPeriod = 20; + } + else if (State == State.Configure) + { + AddDataSeries(BarsPeriodType.Day, 1); + } + else if (State == State.DataLoaded) + { + williamsR = WilliamsR(WilliamsPercentRPeriod); + + longTermTrend = SMA(BarsArray[1], TrendFilterPeriod); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < WilliamsPercentRPeriod) + return; + + if (UseTrendFilter && CurrentBars[1] < TrendFilterPeriod) + return; + + if (williamsR[0] < EntryThreshold) + { + if (UseTrendFilter && Close[0] < longTermTrend[0]) + return; + + EnterLong(); + } + else if (williamsR[0] > ExitThreshold || (UseNewHighExit && Close[0] > High[1])) + ExitLong(); + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "Williams %R Period", GroupName = "Williams %R Bot", Order = 1)] + public int WilliamsPercentRPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Entry Threshold", GroupName = "Williams %R Bot", Order = 2)] + public int EntryThreshold { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Exit Threshold", GroupName = "Williams %R Bot", Order = 3)] + public int ExitThreshold { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Use New High Exit", GroupName = "Williams %R Bot", Order = 4)] + public bool UseNewHighExit { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Use Trend Filter", GroupName = "Williams %R Bot", Order = 5)] + public bool UseTrendFilter { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Trend Filter Period", GroupName = "Williams %R Bot", Order = 6)] + public int TrendFilterPeriod { get; set; } + } +}