last ...
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| ZmqBridgeEA.mq5 |
|
||||
//| پل ZeroMQ بین MetaTrader 5 و سرویس C# |
|
||||
//| |
|
||||
//| کانال داده: PUB → tcp://*:5555 |
|
||||
//| کانال دستور: REP → tcp://*:5556 |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Zmq Bridge"
|
||||
#property version "1.01"
|
||||
#property strict
|
||||
|
||||
#include <Trade\Trade.mqh>
|
||||
#include <Zmq/Zmq.mqh>
|
||||
|
||||
//--- Context اشتراکی (نام یکتا برای جلوگیری از تداخل) ---
|
||||
Context g_ctx("__zmq_bridge_1405__");
|
||||
|
||||
//--- سوکتها ---
|
||||
Socket g_pub(g_ctx, ZMQ_PUB); // انتشار داده
|
||||
Socket g_rep(g_ctx, ZMQ_REP); // دریافت دستور
|
||||
|
||||
CTrade g_trade;
|
||||
|
||||
//--- ثابتهای پورت و Magic ---
|
||||
#define PUB_PORT "tcp://*:5555"
|
||||
#define REP_PORT "tcp://*:5556"
|
||||
#define EXPERT_MAGIC 20251119
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| مقداردهی اولیه |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit()
|
||||
{
|
||||
//--- اتصال سوکتها (توجه: * برای شنود روی تمام اینترفیسها) ---
|
||||
if (!g_pub.bind(PUB_PORT))
|
||||
{
|
||||
Print("خطا در bind سوکت PUB روی ", PUB_PORT);
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
if (!g_rep.bind(REP_PORT))
|
||||
{
|
||||
Print("خطا در bind سوکت REP روی ", REP_PORT);
|
||||
return INIT_FAILED;
|
||||
}
|
||||
|
||||
//--- پیکربندی لایه معاملات ---
|
||||
g_trade.SetExpertMagicNumber(EXPERT_MAGIC);
|
||||
g_trade.SetDeviationInPoints(10);
|
||||
g_trade.SetTypeFillingBySymbol(_Symbol);
|
||||
|
||||
Print("ZmqBridgeEA با موفقیت راهاندازی شد. PUB=", PUB_PORT, " REP=", REP_PORT);
|
||||
return INIT_SUCCEEDED;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| در هر تیک: انتشار داده + بررسی دستورات جدید |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnTick()
|
||||
{
|
||||
//=============================================================
|
||||
// بخش ۱: انتشار تیک روی سوکت PUB
|
||||
//=============================================================
|
||||
MqlTick tick;
|
||||
if (SymbolInfoTick(_Symbol, tick))
|
||||
{
|
||||
//--- JSON با escape صحیح کوتیشنها ---
|
||||
string json = StringFormat(
|
||||
"{\"symbol\":\"%s\",\"bid\":%.5f,\"ask\":%.5f,\"time\":%I64d}",
|
||||
_Symbol, tick.bid, tick.ask, tick.time_msc);
|
||||
|
||||
ZmqMsg msg(json);
|
||||
g_pub.send(msg, ZMQ_DONTWAIT); // غیرمسدودکننده
|
||||
}
|
||||
|
||||
//=============================================================
|
||||
// بخش ۲: دریافت دستور روی سوکت REP (حالت غیرمسدودکننده)
|
||||
//=============================================================
|
||||
ZmqMsg request;
|
||||
int rc = g_rep.recv(request, ZMQ_DONTWAIT);
|
||||
|
||||
if (rc == 0) // پیامی موجود است
|
||||
{
|
||||
string cmd = request.getData();
|
||||
string response = ProcessCommand(cmd);
|
||||
|
||||
ZmqMsg reply(response);
|
||||
g_rep.send(reply);
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| پردازش دستور معاملاتی و ساخت پاسخ JSON |
|
||||
//| فرمت ورودی: ACTION|SYMBOL|VOLUME |
|
||||
//+------------------------------------------------------------------+
|
||||
string ProcessCommand(string cmd)
|
||||
{
|
||||
//--- تفکیک اجزای دستور ---
|
||||
string parts[];
|
||||
int n = StringSplit(cmd, '|', parts);
|
||||
|
||||
if (n < 3)
|
||||
return "{\"ok\":false,\"error\":\"FORMAT\"}";
|
||||
|
||||
string action = parts[0];
|
||||
string symbol = parts[1];
|
||||
double volume = StringToDouble(parts[2]);
|
||||
|
||||
//--- اعتبارسنجی اولیه ---
|
||||
if (volume <= 0)
|
||||
return "{\"ok\":false,\"error\":\"INVALID_VOLUME\"}";
|
||||
|
||||
if (!SymbolSelect(symbol, true))
|
||||
return StringFormat("{\"ok\":false,\"error\":\"SYMBOL_NOT_FOUND\",\"symbol\":\"%s\"}", symbol);
|
||||
|
||||
//--- اجرای دستور ---
|
||||
bool ok = false;
|
||||
ulong ticket = 0;
|
||||
|
||||
if (action == "BUY")
|
||||
{
|
||||
ok = g_trade.Buy(volume, symbol);
|
||||
ticket = g_trade.ResultOrder();
|
||||
}
|
||||
else if (action == "SELL")
|
||||
{
|
||||
ok = g_trade.Sell(volume, symbol);
|
||||
ticket = g_trade.ResultOrder();
|
||||
}
|
||||
else if (action == "CLOSE_ALL")
|
||||
{
|
||||
ok = CloseAllPositions(symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
return StringFormat("{\"ok\":false,\"error\":\"UNKNOWN_ACTION\",\"action\":\"%s\"}", action);
|
||||
}
|
||||
|
||||
//--- کد بازگشتی سرور ---
|
||||
uint retcode = g_trade.ResultRetcode();
|
||||
|
||||
return StringFormat(
|
||||
"{\"ok\":%s,\"ticket\":%I64u,\"retcode\":%u,\"action\":\"%s\",\"symbol\":\"%s\"}",
|
||||
(ok ? "true" : "false"),
|
||||
ticket,
|
||||
retcode,
|
||||
action,
|
||||
symbol);
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| بستن تمام پوزیشنهای یک نماد (فقط با Magic این اکسپرت) |
|
||||
//+------------------------------------------------------------------+
|
||||
bool CloseAllPositions(string symbol)
|
||||
{
|
||||
bool allOk = true;
|
||||
|
||||
for (int i = PositionsTotal() - 1; i >= 0; i--)
|
||||
{
|
||||
ulong ticket = PositionGetTicket(i);
|
||||
if (ticket == 0)
|
||||
continue;
|
||||
|
||||
//--- فقط پوزیشنهای این نماد و این Magic ---
|
||||
if (PositionGetString(POSITION_SYMBOL) != symbol)
|
||||
continue;
|
||||
if (PositionGetInteger(POSITION_MAGIC) != EXPERT_MAGIC)
|
||||
continue;
|
||||
|
||||
if (!g_trade.PositionClose(ticket))
|
||||
allOk = false;
|
||||
}
|
||||
|
||||
return allOk;
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| آزادسازی منابع هنگام حذف اکسپرت |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnDeinit(const int reason)
|
||||
{
|
||||
g_pub.unbind(PUB_PORT);
|
||||
g_rep.unbind(REP_PORT);
|
||||
|
||||
Print("ZmqBridgeEA متوقف شد. دلیل: ", reason);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
Reference in New Issue
Block a user