#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.NinjaScript.Indicators; using System.ComponentModel.DataAnnotations; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class SonicRBot : Strategy { private ATR atr; 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 = 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 = 100; IsInstantiatedOnEachOptimizationIteration = true; DragonPeriod = 34; ATRPeriod = 14; ATRMultiplier = 2.0; } else if (State == State.DataLoaded) { atr = ATR(ATRPeriod); dragon = SonicRDragon(DragonPeriod); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBar < BarsRequiredToTrade) return; if (Position.MarketPosition != MarketPosition.Flat) return; if (Open[0] < dragon.HighEMA[0] && Close[0] > dragon.HighEMA[0]) { lookingLong = true; lookingShort = false; waitingForLongBreak = false; waitingForShortBreak = false; } else if (Open[0] > dragon.LowEMA[0] && Close[0] < dragon.LowEMA[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; } double atrValue = atr[0]; double stopLoss = ATRMultiplier * atrValue; double profitTarget = ATRMultiplier * atrValue; if (waitingForLongBreak && Close[0] > breakPrice) { SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize); SetProfitTarget(CalculationMode.Ticks, profitTarget / TickSize); EnterLong(); waitingForLongBreak = false; } else if (waitingForShortBreak && Close[0] < breakPrice) { SetStopLoss(CalculationMode.Ticks, stopLoss / TickSize); SetProfitTarget(CalculationMode.Ticks, profitTarget / 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; } #endregion } }