// Calculate MACD values double macd_main[], macd_signal[]; ArraySetAsSeries(macd_main, true); ArraySetAsSeries(macd_signal, true); int counted_bars = IndicatorCounted(); int limit = Bars - counted_bars; if (limit > Bars - 2) limit = Bars - 2; int macd_handle = iMACD(NULL, 0, MACD_FAST_EMA, MACD_SLOW_EMA, MACD_SIGNAL_SMMA, PRICE_CLOSE); if (macd_handle <= 0) { Print("Failed to initialize MACD indicator!"); return (-1); } int macd_count = CopyBuffer(macd_handle, 0, 0, limit, macd_main); int signal_count = CopyBuffer(macd_handle, 1, 0, limit, macd_signal); // Check for bullish setup if (macd_main[0] > 0 && macd_signal[0] > 0 && macd_main[1] <= 0 && macd_signal[1] <= 0) { double highestHigh = iHigh(NULL, 0, MODE_HIGH, 2); double currentClose = iClose(NULL, 0, 0); double entryPrice = NormalizeDouble(highestHigh + (currentClose - highestHigh) * 1.1, Digits); // Open a long trade ticket = OrderSend(Symbol(), OP_BUY, LotSize, entryPrice, 0, entryPrice - StopLoss * Point, entryPrice + TakeProfit * Point, "Scalping EA", 0, 0, clrGreen); } // Check for bearish setup if (macd_main[0] < 0 && macd_signal[0] < 0 && macd_main[1] >= 0 && macd_signal[1] >= 0) { double lowestLow = iLow(NULL, 0, MODE_LOW, 2); double currentClose = iClose(NULL, 0, 0); double entryPrice = NormalizeDouble(lowestLow - (lowestLow - currentClose) * 1.1, Digits); // Open a short trade ticket = OrderSend(Symbol(), OP_SELL, LotSize, entryPrice, 0, entryPrice + StopLoss * Point, entryPrice - TakeProfit * Point, "Scalping EA", 0, 0, clrRed); } return (0);
generate functionSun, 09 Jul 2023
Java
Generate More