//+------------------------------------------------------------------+
//| HistogramMacd Cross.mq5 |
//| Copyright 2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#include <Trade\Trade.mqh>
CTrade trade ;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
input int fastema = 12 ;
input int slowema = 26 ;
input int signalsma = 9;
input double LotSize = 0.1;
input int Slippage = 10;
int sl_point = 600;
int tp_point = 1000;
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
// MACD histogram cross zero
string macdhistogramzerocross() {
// creat buffer
double macdbuffer[] ;
ArraySetAsSeries(macdbuffer , true);
int macdhandle =
iMACD(_Symbol,PERIOD_CURRENT,fastema,slowema,signalsma,PRICE_CLOSE);
if (macdhandle == INVALID_HANDLE){
Print("Invalid macd handle") ;
return ("Invalid macd handle");
}
CopyBuffer(macdhandle, 0, 0, 3 ,macdbuffer );
// calculate the zero cross
double previousmacdhistogram = macdbuffer[1];
double secondpreviousmacdhistogram = macdbuffer[2];
if(secondpreviousmacdhistogram < 0){
if(previousmacdhistogram > 0){
Print("macd histogram zero cross detected");
// return "above zero"
return("abovezero");
}
return ("no cross");
}
if(secondpreviousmacdhistogram > 0){
if(previousmacdhistogram <0){
Print("macd histogram zero cross");
return ("below zero");
}
return ("no cross");
}
return ("no cross");
void OpenBuy()
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl = ask - sl_point * _Point;
double tp = ask + tp_point * _Point;
if(trade.PositionOpen(_Symbol, ORDER_TYPE_BUY, LotSize, ask, sl, 0, "Histogram
BUY"))
Print("✅ Mở BUY mới");
else
Print("❌ Lỗi mở BUY: ", GetLastError());
}
//+------------------------------------------------------------------+
//| Hàm mở lệnh SELL |
//+------------------------------------------------------------------+
void OpenSell()
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double sl = bid + sl_point * _Point;
double tp = bid - tp_point * _Point;
if(trade.PositionOpen(_Symbol, ORDER_TYPE_SELL, LotSize, bid, sl, 0, "Histogram
SELL"))
Print("✅ Mở SELL mới");
else
Print("❌ Lỗi mở SELL: ", GetLastError());
}
//+------------------------------------------------------------------+
//| Hàm đóng vị thế hiện tại |
//+------------------------------------------------------------------+
void CloseCurrentPosition()
{
ulong ticket = PositionGetInteger(POSITION_TICKET);
double volume = PositionGetDouble(POSITION_VOLUME);
ENUM_POSITION_TYPE posType =
(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
MqlTradeRequest req;
MqlTradeResult res;
ZeroMemory(req);
ZeroMemory(res);
req.action = TRADE_ACTION_DEAL;
req.symbol = _Symbol;
req.volume = volume;
req.position = ticket;
req.magic = 123456;
req.deviation = Slippage;
if(posType == POSITION_TYPE_BUY)
{
req.type = ORDER_TYPE_SELL;
req.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
}
else if(posType == POSITION_TYPE_SELL)
{
req.type = ORDER_TYPE_BUY;
req.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
}
if(!OrderSend(req, res))
Print("❌ Lỗi đóng lệnh: ", res.retcode);
else
Print("✅ Đã đóng lệnh.");
}
void OnTick()
{
//--- check histogram cross
macdhistogramzerocross();
}
//+------------------------------------------------------------------+