Here is the code
protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"MACD cross over strategy. Long order if MACD crosses over Avg AND price close over 21 EMA. Short order if MACD crosses below Avg AND price close is below 21 EMA. Profit target is small (i.e. 2 points max) with quicker time frame (i.e. 5 min, 10 min)"; Name = "MACDEMA21"; 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; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; ProfitTarget = 8; StopLoss = 20; EMALength = 21; OrderSize = 5; } else if (State == State.Configure) { } else if (State == State.DataLoaded) { MACD1 = MACD(Close, 12, 26, 9); EMA1 = EMA(Close, Convert.ToInt32(EMALength)); SetProfitTarget("", CalculationMode.Ticks, ProfitTarget); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; // Set 1 if ((CrossAbove(MACD1.Default, MACD1.Avg, 1)) && (Close[0] > EMA1[0])) { EnterLong(Convert.ToInt32(OrderSize), @"Long"); } // Set 2 if ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) && (Close[0] < EMA1[0])) { EnterLong(Convert.ToInt32(OrderSize), @"Short"); } }
- Didn't take the profit even though both of them had 2 point gain? And
- Why did both of them stay active for so long even though there were multiple MACD cross-overs in the other direction?
Comment