//+------------------------------------------------------------------+ //| MACD_EA.mq4 | //| Copyright 2023, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2023, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ enum Money_Management { Equity_Percent = 1, // % Equity Fixed_Lot_Size = 2, // Fixed Lot Size Fixed_Money = 3, // Fix Money Based Options }; enum Trading_Direction { Both = 1, // Both of Long or Short Only_Long = 2, Only_Short = 3, }; sinput string macd = "MACD"; // === MACD Inputs === input int macd_fast_ema = 35; // MACD Input FAST EMA input int macd_slow_ema = 90; // MACD Input Slow EMA input int macd_signal = 9; // MACD Signal Period sinput string ea = "EA"; // === EA Inputs === input int MAGIC_NUMBER = 12345; // MAGIC NUMBER input int TP = 50; // TP in Points input int SL = 30; // SL in Points sinput string ts_s = "Trailing StopLoss"; // === Trailing StopLoss === input bool ts = true; // Trailing Stop Loss input int ts_trigger = 30; // TS Trail SL input int ts_first_tp = 50; // Breakeven input Money_Management money_op = Equity_Percent; // Money Management options input double lot_size = 0.1; // Fixed Lot Size input double lot_percent = 1; // Equity * N input double lot_money = 10000; // Fixed Money sinput string to_s = "Trading Option"; // === Trading Option === input Trading_Direction to = Both; // Trading Option string macd_name = "MACD"; double PIP = 0.0; double last_bar = 0.0; int OnInit() { //--- PIP = Point()*(Digits%2==1 ? 10 : 1); last_bar = 0.0; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ datetime bartime = NULL; void OnTick() { //--- bool buy_en = false; bool sell_en = false; switch(to) { case Both: buy_en = true; sell_en = true; break; case Only_Long: buy_en = true; sell_en = false; break; case Only_Short: buy_en = false; sell_en = true; break; default: buy_en = true; sell_en = true; } double spread = MarketInfo(_Symbol, MODE_SPREAD); if(bartime != iTime(_Symbol, PERIOD_CURRENT, 0)) { double lot = 0.0; double dLotSize = MarketInfo(Symbol(), MODE_LOTSIZE); switch(money_op) { case Equity_Percent: lot = AccountEquity() * lot_percent / dLotSize; break; case Fixed_Lot_Size: lot = lot_size; case Fixed_Money: lot = lot_money / dLotSize; } lot = NormalizeDouble(lot, 2); double black_bar = iMACD(_Symbol, PERIOD_CURRENT, macd_fast_ema, macd_slow_ema, macd_signal, PRICE_CLOSE, 0, 1); double black_bar_1 = iMACD(_Symbol, PERIOD_CURRENT, macd_fast_ema, macd_slow_ema, macd_signal, PRICE_CLOSE, 0, 2); if(black_bar > 0 && black_bar_1 <0) { // Long Position double stop_loss = NormalizeDouble(Bid - SL * PIP, Digits); double take_profit = NormalizeDouble(Ask + TP * PIP, Digits); for(int i = OrdersTotal() - 1; i >= 0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderSymbol() != _Symbol) continue; if(OrderMagicNumber() != MAGIC_NUMBER) continue; if(OrderType() != OP_SELL) continue; int check = OrderClose(OrderTicket(), OrderLots(), Ask, 2); if(check < 0) Print(GetLastError()); } // Two Trade at the same time. if(buy_en) { int check = OrderSend(_Symbol, OP_BUY, lot, Ask, 2, stop_loss, take_profit, "Long MACD", MAGIC_NUMBER, 0, clrBlue); check = OrderSend(_Symbol, OP_BUY, lot, Ask, 2, stop_loss, NULL, "Long MACD", MAGIC_NUMBER,0, clrBlue); if( check < 0) Print(GetLastError(), " ", spread); } } if(black_bar < 0 && black_bar_1 > 0) { for(int i = OrdersTotal() - 1; i >= 0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderSymbol() != _Symbol) continue; if(OrderMagicNumber() != MAGIC_NUMBER) continue; if(OrderType() != OP_BUY) continue; int check = OrderClose(OrderTicket(), OrderLots(), Bid, 2); if(check < 0) Print(GetLastError()); } // Short Position double stop_loss = NormalizeDouble(Ask + SL * PIP, Digits); double take_profit = NormalizeDouble(Bid - TP * PIP, Digits); if(sell_en) { int check = OrderSend(_Symbol, OP_SELL, lot, Bid, 2, stop_loss, take_profit, "Short MACD", MAGIC_NUMBER, 0, clrRed); check = OrderSend(_Symbol, OP_SELL, lot, Bid, 2, NULL, NULL, "Short MACD", MAGIC_NUMBER, 0, clrRed); if(check < 0) Print(GetLastError(), " ", spread); } } if(ts) // Trailing Stoploss Enabled { for(int i = OrdersTotal() - 1; i >= 0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderSymbol() != _Symbol) continue; if(OrderMagicNumber() != MAGIC_NUMBER) continue; Trailing_Stop(OrderTicket(), ts_trigger, ts_first_tp); } } last_bar = black_bar; } bartime = iTime(_Symbol, PERIOD_CURRENT, 0); Comment(spread); } //+------------------------------------------------------------------+ void Trailing_Stop(int ticket, int Trail, int First_TP) { if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) { if(OrderStopLoss() != NULL) { if(OrderType() == OP_BUY && Bid - OrderStopLoss() > Trail * PIP) { int check = OrderModify(ticket, OrderOpenPrice(), Bid - Trail * PIP, OrderTakeProfit(), 0); } if(OrderType() == OP_SELL && OrderStopLoss() - Ask > Trail * PIP) { int check = OrderModify(ticket, OrderOpenPrice(), Ask + Trail * PIP, OrderTakeProfit(), 0); } } else { if(Bid - OrderOpenPrice() > First_TP * PIP) { int check = OrderModify(ticket, OrderOpenPrice(), Bid - Trail * PIP, OrderTakeProfit(), 0); } if(OrderOpenPrice() - Ask > First_TP * PIP) { int check = OrderModify(ticket, OrderOpenPrice(), Ask + Trail * PIP, OrderTakeProfit(), 0); } } } }