#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.NinjaScript.Indicators; using System.ComponentModel.DataAnnotations; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class SonicRBot : Strategy { private ATR atr; private SMA longTermTrend; private OpeningRange openingRange; private SonicRDragon dragon; private bool lookingLong; private bool lookingShort; private bool waitingForLongBreak; private bool waitingForShortBreak; private double breakPrice; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Inspired by the Sonic R System created by sonicdeejay on the Forex Factory forums"; Name = "Sonic 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 = 100; IsInstantiatedOnEachOptimizationIteration = true; DragonPeriod = 34; ATRPeriod = 14; ATRMultiplier = 2.0; LongTermTrendPeriod = 20; OpeningRangePeriod = 3; LongOnly = true; } else if (State == State.Configure) { AddDataSeries(BarsPeriodType.Day, 1); // Data series required by the opening range indicator, must be loaded here. AddDataSeries(BarsPeriodType.Minute, 1); AddDataSeries(Instrument.FullName, BarsPeriod, "US Equities RTH"); } else if (State == State.DataLoaded) { atr = ATR(ATRPeriod); longTermTrend = SMA(BarsArray[1], LongTermTrendPeriod); openingRange = OpeningRange(OpeningRangePeriod, OpeningRangeBarType.Minutes); dragon = SonicRDragon(DragonPeriod); } } protected override void OnBarUpdate() { if (BarsInProgress != 0 || CurrentBars[1] < 0) return; if (CurrentBar < BarsRequiredToTrade) return; if (Position.MarketPosition != MarketPosition.Flat) { if (Position.MarketPosition == MarketPosition.Long && Close[0] < dragon.LowEMA[0]) ExitLong(); else if (Position.MarketPosition == MarketPosition.Short && Close[0] > dragon.HighEMA[0]) ExitShort(); return; } if (Open[0] < dragon.HighEMA[0] && Close[0] > dragon.HighEMA[0] && Close[0] > longTermTrend[0] && openingRange.ORH[0] > 0.0 && Close[0] > openingRange.ORH[0]) { lookingLong = true; lookingShort = false; waitingForLongBreak = false; waitingForShortBreak = false; } else if (Open[0] > dragon.LowEMA[0] && Close[0] < dragon.LowEMA[0] && Close[0] < longTermTrend[0] && openingRange.ORL[0] > 0.0 && Close[0] < openingRange.ORL[0]) { lookingShort = true; lookingLong = false; waitingForLongBreak = false; waitingForShortBreak = false; } if (lookingLong && Close[0] < Open[0]) { waitingForLongBreak = true; breakPrice = Close[1]; lookingLong = false; } else if (lookingShort && Close[0] > Open[0]) { waitingForShortBreak = true; breakPrice = Close[1]; lookingShort = false; } if (waitingForLongBreak && Close[0] > breakPrice) { SetTrailStop(CalculationMode.Ticks, (ATRMultiplier * atr[0]) / TickSize); EnterLong(); waitingForLongBreak = false; } else if (!LongOnly && waitingForShortBreak && Close[0] < breakPrice) { SetTrailStop(CalculationMode.Ticks, (ATRMultiplier * atr[0]) / TickSize); EnterShort(); waitingForShortBreak = false; } } public override string DisplayName { get { return Name; } } #region Properties [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Dragon Period", Description = "Period for the Sonic R Dragon", GroupName = "Sonic R Bot", Order = 1)] public int DragonPeriod { get; set; } [Range(1, int.MaxValue), NinjaScriptProperty] [Display(Name = "ATR Period", GroupName = "Sonic R Bot", Order = 2)] public int ATRPeriod { get; set; } [Range(0.1, double.MaxValue), NinjaScriptProperty] [Display(Name = "ATR Multiplier", GroupName = "Sonic R Bot", Order = 3)] public double ATRMultiplier { get; set; } [Range(1, int.MaxValue), NinjaScriptProperty] [Display(Name = "Long Term Trend Period", GroupName = "Sonic R Bot", Order = 4)] public int LongTermTrendPeriod { get; set; } [NinjaScriptProperty] [Display(Name = "Opening Range Period", GroupName = "Sonic R Bot", Order = 5)] public int OpeningRangePeriod { get; set; } [NinjaScriptProperty] [Display(Name = "Long Only", GroupName = "Sonic R Bot", Order = 6)] public bool LongOnly { get; set; } #endregion } }