From a03bde4b15f1fd1fe641a1e5df02fd3752237e93 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sun, 16 Jun 2024 05:30:30 -0700 Subject: [PATCH] Add trend breakout strategy taken from chapter 3 of Kaufman Constructs Trading Systems --- strategies/TrendBreakout.cs | 115 ++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 strategies/TrendBreakout.cs diff --git a/strategies/TrendBreakout.cs b/strategies/TrendBreakout.cs new file mode 100644 index 0000000..b04ab76 --- /dev/null +++ b/strategies/TrendBreakout.cs @@ -0,0 +1,115 @@ +#region Using declarations +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; +using System.Windows.Media; +using System.Xml.Serialization; +using NinjaTrader.Cbi; +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using NinjaTrader.Gui.SuperDom; +using NinjaTrader.Gui.Tools; +using NinjaTrader.Data; +using NinjaTrader.NinjaScript; +using NinjaTrader.Core.FloatingPoint; +using NinjaTrader.NinjaScript.Indicators; +using NinjaTrader.NinjaScript.DrawingTools; +using NinjaTrader.Gui.PropertiesTest; +#endregion + +//This namespace holds Strategies in this folder and is required. Do not change it. +namespace NinjaTrader.NinjaScript.Strategies +{ + public class TrendBreakout : Strategy + { + private ATR atr; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Taken from Chapter 3 of Kaufman Constructs Trading Systems"; + Name = "Trend Breakout"; + 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; + + Period = 120; + ATRPeriod = 14; + ATRMultiplier = 2; + } + else if (State == State.DataLoaded) + { + atr = ATR(ATRPeriod); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < Period) + return; + + double highestHigh = MAX(High, Period)[1]; + double lowestLow = MIN(Low, Period)[1]; + + /*double atrValue = atr[0]; + double stopLoss = ATRMultiplier * atrValue; + double profitTarget = ATRMultiplier * atrValue;*/ + + if (Close[0] > highestHigh) + { + EnterLong(); + //SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize); + //SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize); + } + + /*if (Close[0] < lowestLow) + { + EnterShort(); + SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize); + SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize); + }*/ + + if (Position.MarketPosition == MarketPosition.Long && Close[0] < lowestLow) + ExitLong(); + } + + public override string DisplayName + { + get { return Name; } + } + + #region Properties + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "Period", Description = "Period for highest high and lowest low", Order = 1, GroupName = "Trend Breakout")] + public int Period { get; set; } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "ATR Period", GroupName = "Trend Breakout", Order = 2)] + public int ATRPeriod { get; set; } + + [Range(0.1, double.MaxValue), NinjaScriptProperty] + [Display(Name = "ATR Multiplier", GroupName = "Trend Breakout", Order = 3)] + public double ATRMultiplier { get; set; } + #endregion + } +}