Files
xSaherelmWorkspace/Documents/Resources/ZeroMQ/ZeroMQ v1.0.html
T
2026-11-19 19:45:39 +03:30

1663 lines
50 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html
lang="fa"
dir="rtl"
>
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>
اتصال ZeroMQ به MetaTrader 5 | راهنمای جامع پیاده‌سازی بریج معاملاتی
با C#
</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
--code-bg: #1e293b;
--code-text: #e2e8f0;
}
{ box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 980px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge {
display: flex;
align-items: center;
gap: 12px;
}
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta {
text-align: left;
font-size: 12px;
opacity: 0.9;
}
.doc-meta div { margin-bottom: 3px; }
.document-title {
text-align: center;
position: relative;
z-index: 1;
padding: 20px 0;
}
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 10px;
letter-spacing: -0.5px;
}
.document-title h2 {
font-size: 16px;
font-weight: 400;
opacity: 0.9;
}
.document-body {
padding: 40px;
}
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section {
margin-bottom: 45px;
page-break-inside: avoid;
}
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 {
font-size: 19px;
font-weight: 700;
color: var(--primary);
margin-bottom: 2px;
}
.section-title .subtitle {
font-size: 12px;
color: var(--text-muted);
}
.section-content { padding-right: 10px; }
.section-content p {
margin-bottom: 12px;
text-align: justify;
text-indent: 20px;
}
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong { color: var(--accent-dark); }
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.danger-box {
background: linear-gradient(to left, #fff5f5, #fed7d7);
border: 1px solid #feb2b2;
border-right: 4px solid var(--danger);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
}
.styled-table th {
padding: 14px 16px;
text-align: right;
font-weight: 600;
font-size: 13px;
}
.styled-table td {
padding: 13px 16px;
border-bottom: 1px solid var(--border);
font-size: 13px;
vertical-align: top;
}
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row {
background: #fffaf0 !important;
font-weight: 600;
}
.code-block {
background: var(--code-bg);
color: var(--code-text);
border-radius: 10px;
padding: 20px 22px;
margin: 18px 0;
overflow-x: auto;
direction: ltr;
text-align: left;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 12.5px;
line-height: 1.7;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
position: relative;
}
.code-block::before {
content: attr(data-lang);
position: absolute;
top: 8px;
right: 12px;
font-size: 10px;
color: var(--accent);
font-family: 'Vazirmatn', sans-serif;
font-weight: 600;
opacity: 0.8;
}
.code-block code { font-family: inherit; }
.code-block .comment { color: #94a3b8; }
.code-block .keyword { color: #f472b6; }
.code-block .string { color: #86efac; }
.code-block .func { color: #93c5fd; }
.steps-list {
list-style: none;
padding: 0;
counter-reset: step-counter;
margin: 20px 0;
}
.steps-list > li {
counter-increment: step-counter;
position: relative;
padding: 15px 55px 15px 0;
border-bottom: 1px solid var(--border);
margin-bottom: 5px;
}
.steps-list > li:last-child { border-bottom: none; }
.steps-list > li::before {
content: counter(step-counter);
position: absolute;
right: 0;
top: 15px;
width: 36px; height: 36px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
font-size: 14px;
}
.steps-list > li strong {
color: var(--primary);
display: block;
margin-bottom: 4px;
}
.architecture-grid {
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: 15px;
align-items: center;
margin: 25px 0;
}
.arch-card {
background: #f8fafc;
border: 2px solid var(--border);
border-radius: 10px;
padding: 18px;
text-align: center;
}
.arch-card.mt5 {
border-color: var(--primary-light);
background: linear-gradient(to bottom, #ebf8ff, #f8fafc);
}
.arch-card.csharp {
border-color: var(--success);
background: linear-gradient(to bottom, #f0fff4, #f8fafc);
}
.arch-card h5 {
font-size: 14px;
font-weight: 700;
color: var(--primary);
margin-bottom: 10px;
}
.arch-card ul {
list-style: none;
padding: 0;
font-size: 12px;
color: var(--text-muted);
}
.arch-card ul li { padding: 3px 0; }
.arch-arrow {
font-size: 24px;
color: var(--accent);
text-align: center;
}
.reference-list {
list-style: none;
padding: 0;
margin: 20px 0;
}
.reference-list li {
background: #f8fafc;
border-right: 3px solid var(--primary-light);
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 6px;
font-size: 13px;
}
.reference-list li strong {
color: var(--primary);
display: block;
margin-bottom: 5px;
}
.reference-list li .ref-source {
font-size: 11px;
color: var(--text-muted);
margin-top: 5px;
direction: ltr;
text-align: left;
}
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 {
font-size: 22px;
font-weight: 800;
margin-bottom: 15px;
color: var(--accent);
}
.conclusion-box p {
font-size: 14px;
line-height: 1.8;
text-indent: 0 !important;
margin-bottom: 15px;
}
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand {
font-weight: 700;
color: var(--accent);
margin-bottom: 5px;
font-size: 14px;
}
.footer-divider {
width: 60px;
height: 2px;
background: var(--accent);
margin: 10px auto;
}
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
.inline-code {
background: #edf2f7;
color: var(--danger);
padding: 1px 7px;
border-radius: 4px;
font-family: 'Consolas', monospace;
font-size: 12.5px;
direction: ltr;
display: inline-block;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
.code-block { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.architecture-grid { grid-template-columns: 1fr; }
.arch-arrow { transform: rotate(90deg); }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
.steps-list > li { padding-right: 45px; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: TECH-ZMQ-1405-002</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
<div>طبقه‌بندی: فنی</div>
</div>
</div>
<div class="document-title">
<span class="label">TECHNICAL IMPLEMENTATION GUIDE</span>
<h1>اتصال ZeroMQ به MetaTrader 5</h1>
<h2>
راهنمای گام‌به‌گام پیاده‌سازی بریج معاملاتی دوطرفه با C#
(NetMQ)
</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>هدف این مستند:</strong><br />
پیاده‌سازی یک پل ارتباطی (Bridge) کامل بین MetaTrader 5 و یک
سرویس C# با استفاده از ZeroMQ. در پایان این راهنما، قادر
خواهید بود داده‌های بازار و حساب را از MT5 دریافت کنید، در
سرویس C# پردازش و ذخیره نمایید، و دستورات معاملاتی (ورود،
مدیریت، توقف) را از C# به MT5 ارسال و اجرا کنید. تمام مراحل
با کدهای قابل اجرا و توضیحات فنی همراه است.
</div>
<!-- ==================== SECTION 1 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>معماری کلی و انتخاب الگوی ارتباطی</h3>
<div class="subtitle">
Architecture & Messaging Patterns
</div>
</div>
</div>
<div class="section-content">
<p>
برای یک بریج معاملاتی پایدار، هرگز نباید تمام
ارتباطات را روی یک سوکت واحد انجام دهید.
توصیه‌شده‌ترین معماری، استفاده از
<strong>دو کانال مستقل</strong> است تا داده‌های
پرحجم بازار، کانال دستورات معاملاتی را مسدود نکنند.
</p>
<div class="architecture-grid">
<div class="arch-card mt5">
<h5>🖥 MetaTrader 5 (MQL5)</h5>
<ul>
<li>PUB Socket → انتشار داده</li>
<li>REP Socket → دریافت دستور</li>
<li>Context اشتراکی</li>
</ul>
</div>
<div class="arch-arrow">⇄</div>
<div class="arch-card csharp">
<h5>💻 سرویس C# (.NET)</h5>
<ul>
<li>SUB Socket → دریافت داده</li>
<li>REQ Socket → ارسال دستور</li>
<li>پردازش و ذخیره‌سازی</li>
</ul>
</div>
</div>
<table class="styled-table">
<thead>
<tr>
<th>کانال</th>
<th>الگوی ZeroMQ</th>
<th>نقش MT5</th>
<th>نقش C#</th>
<th>پورت پیشنهادی</th>
</tr>
</thead>
<tbody>
<tr>
<td>داده (Data)</td>
<td>
<span class="inline-code"
>PUB / SUB</span
>
</td>
<td>Publisher (PUB)</td>
<td>Subscriber (SUB)</td>
<td>
<span class="inline-code">5555</span>
</td>
</tr>
<tr class="highlight-row">
<td>دستور (Command)</td>
<td>
<span class="inline-code"
>REQ / REP</span
>
</td>
<td>Reply (REP)</td>
<td>Request (REQ)</td>
<td>
<span class="inline-code">5556</span>
</td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>چرا PUB/SUB برای داده؟</strong> الگوی
PUB/SUB یک‌طرفه و غیرهمزمان است. MT5 داده را منتشر
می‌کند و حتی اگر سرویس C# قطع شود، اکسپرت MT5 دچار
توقف نمی‌شود. این ویژگی برای داده‌های پرتکرار
(تیک‌ها) حیاتی است.
</div>
<div class="warning-box">
<strong
>⚠️ هشدار حیاتی درباره
<span class="inline-code">recv</span>
مسدودکننده:</strong
>
اگر در اکسپرت MT5 از
<span class="inline-code">socket.recv()</span> بدون
گزینه
<span class="inline-code">ZMQ_DONTWAIT</span>
استفاده کنید، رشته
<span class="inline-code">OnTick</span> مسدود می‌شود
و متاتریدر ممکن است هنگ کند یا حتی کرش
نماید.[reference:0] حتماً از حالت
<strong>غیرمسدودکننده (Non-Blocking)</strong>
استفاده کنید که در بخش ۴ توضیح داده شده است.
</div>
</div>
</div>
<!-- ==================== SECTION 2 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>
نصب ZeroMQ در MetaTrader 5 (کتابخانه mql-zmq)
</h3>
<div class="subtitle">
mql-zmq Installation — Step by Step
</div>
</div>
</div>
<div class="section-content">
<p>
کتابخانه <strong>mql-zmq</strong> (مخزن
<span class="inline-code">dingmaotu/mql-zmq</span>)
یک بایندینگ کامل از ZeroMQ نسخه ۴.۲ برای MQL4/5 است
و به‌عنوان استاندارد عملی این کار شناخته می‌شود.
</p>
<ol class="steps-list">
<li>
<strong>دانلود کتابخانه</strong> مخزن
<span class="inline-code"
>dingmaotu/mql-zmq</span
>
را از GitHub دانلود و استخراج کنید.
</li>
<li>
<strong>کپی فایل‌های Include</strong> محتویات
پوشه
<span class="inline-code">Include/Zmq</span> را
در پوشه
<span class="inline-code">Include</span>
دایرکتوری داده متاتریدر ۵ قرار
دهید.[reference:2]
</li>
<li>
<strong
>کپی فایل‌های DLL (نسخه ۶۴ بیتی — پیش‌فرض
MT5)</strong
>
فایل‌های
<span class="inline-code">libzmq.dll</span> و
<span class="inline-code">libsodium.dll</span>
را از پوشه
<span class="inline-code">Library/MT5</span> به
پوشه
<span class="inline-code">Library</span>
دایرکتوری داده MT5 کپی
کنید.[reference:3][reference:4]
</li>
<li>
<strong>فعال‌سازی مجوز DLL</strong> در MT5 به
مسیر
<strong
>Tools → Options → Expert Advisors</strong
>
بروید و گزینه
<strong>“Allow DLL imports”</strong> را فعال
کنید.[reference:5] برای محیط عملیاتی، از گزینه
<strong>“Allow only listed DLLs”</strong>
استفاده کرده و
<span class="inline-code">libzmq.dll</span> و
<span class="inline-code">libsodium.dll</span>
را به لیست مجاز اضافه کنید.
</li>
<li>
<strong
>نصب Visual C++ Runtime (2015 یا
جدیدتر)</strong
>
فایل‌های DLL به آخرین نسخه Visual C++
Redistributable نیاز دارند.
</li>
<li>
<strong>فقط برای MT5 32 بیتی (اختیاری)</strong>
اگر از MT5 32 بیتی استفاده می‌کنید، فایل
<span class="inline-code"
>Include/Mql/Lang/Native.mqh</span
>
را باز کرده و ماکرو
<span class="inline-code">__X64__</span> را در
بالای فایل کامنت کنید.[reference:7] همچنین از
DLLهای ۳۲ بیتی موجود در
<span class="inline-code">Library/MT4</span>
استفاده کنید.
</li>
</ol>
<div class="success-box">
<strong>✅ تأیید نصب:</strong> یک اسکریپت ساده MQL5
با خط
<span class="inline-code"
>#include &lt;Zmq/Zmq.mqh&gt;</span
>
ایجاد کنید. اگر بدون خطا کامپایل شد، نصب به درستی
انجام شده است.
</div>
<div class="highlight-box">
<strong>💡 بهترین روش برای Context:</strong> در
ZeroMQ، تمام اسکریپت‌ها و اکسپرت‌های MT5 در یک پروسه
مشترک اجرا می‌شوند. به‌جای ساخت Context جداگانه، از
یک <strong>Context اشتراکی</strong> با یک نام یکتا
استفاده کنید تا از تداخل و ساخت نمونه‌های ایزوله
جلوگیری شود.[reference:8]
<div
class="code-block"
data-lang="MQL5"
style="margin-top: 12px; margin-bottom: 0"
>
<code
>Context context(<span class="string"
>"__3kewducdxhkd__"</span
>);
<span class="comment"
>// نام اشتراکی و یکتا</span
></code
>
</div>
</div>
</div>
</div>
<!-- ==================== SECTION 3 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>پیاده‌سازی سمت MetaTrader 5 (MQL5)</h3>
<div class="subtitle">
MQL5 Expert Advisor — Publisher + Reply Server
</div>
</div>
</div>
<div class="section-content">
<p>
اکسپرت زیر هم‌زمان داده‌های تیک را روی سوکت
<strong>PUB</strong> منتشر می‌کند و دستورات معاملاتی
را روی سوکت <strong>REP</strong> دریافت و اجرا
می‌کند. نکته کلیدی، استفاده از
<strong>polling غیرمسدودکننده</strong> برای سوکت REP
است تا <span class="inline-code">OnTick</span> هرگز
قفل نشود.
</p>
<div
class="code-block"
data-lang="MQL5"
>
<code
><span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="comment"
>//| ZmqBridgeEA.mq5 — پل ZeroMQ بین MT5 و
سرویس C# |</span
>
<span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="keyword">#include</span>
&lt;Zmq/Zmq.mqh&gt;
<span class="keyword">#include</span>
&lt;Trade\Trade.mqh&gt;
<span class="comment"
>//--- Context اشتراکی (یکتا برای این پروژه)
---</span
>
Context g_ctx(<span class="string"
>"zmq_bridge_1405"</span
>);
<span class="comment">//--- سوکت‌ها ---</span>
Socket g_pub(g_ctx, ZMQ_PUB);
<span class="comment">// انتشار داده</span>
Socket g_rep(g_ctx, ZMQ_REP);
<span class="comment">// دریافت دستور</span>
CTrade g_trade;
<span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="keyword">int</span>
<span class="func">OnInit</span>() {
g_pub.bind(<span class="string"
>"tcp://:5555"</span
>); g_rep.bind(<span class="string"
>"tcp://:5556"</span
>); g_trade.SetExpertMagicNumber(<span
class="number"
>20251119</span
>); g_trade.SetDeviationInPoints(<span
class="number"
>10</span
>); g_trade.SetTypeFillingBySymbol(_Symbol);
<span class="keyword">return</span>
INIT_SUCCEEDED; }
<span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="keyword">void</span>
<span class="func">OnTick</span>() {
<span class="comment"
>//=== ۱. انتشار تیک روی PUB ===</span
>
MqlTick tick;
<span class="keyword">if</span
>(SymbolInfoTick(_Symbol, tick)) {
<span class="keyword">string</span> json =
StringFormat(
<span class="string"
>"{"symbol":"%s","bid":%.5f,"ask":%.5f,"
""time":%I64d}"</span
>, _Symbol, tick.bid, tick.ask, tick.time_msc);
ZmqMsg msg(json); g_pub.send(msg, ZMQ_DONTWAIT);
<span class="comment">// بدون بلاک شدن</span>
}
<span class="comment"
>//=== ۲. دریافت غیرمسدودکننده دستور روی REP
===</span
>
ZmqMsg request;
<span class="keyword">int</span> rc =
g_rep.recv(request, ZMQ_DONTWAIT);
<span class="keyword">if</span>(rc ==
<span class="number">0</span>)
<span class="comment">// پیامی موجود است</span>
{
<span class="keyword">string</span> cmd =
request.getData();
<span class="keyword">string</span> response =
ProcessCommand(cmd); ZmqMsg reply(response);
g_rep.send(reply); } }
<span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="comment"
>//| پردازش دستور معاملاتی و ساخت پاسخ JSON
|</span
>
<span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="keyword">string</span>
<span class="func">ProcessCommand</span>(<span
class="keyword"
>string</span
>
cmd) {
<span class="comment"
>// فرمت دستور:
ACTION|SYMBOL|VOLUME|SL|TP</span
>
<span class="keyword">string</span> parts[];
<span class="keyword">int</span> n =
StringSplit(cmd,
<span class="string">'|'</span>, parts);
<span class="keyword">if</span>(n <
<span class="number">3</span>)
<span class="keyword">return</span>
<span class="string"
>"{"ok":false,"error":"FORMAT"}"</span
>;
<span class="keyword">string</span> action =
parts[<span class="number">0</span>];
<span class="keyword">string</span> symbol =
parts[<span class="number">1</span>];
<span class="keyword">double</span> volume =
StringToDouble(parts[<span class="number"
>2</span
>]);
<span class="keyword">bool</span> ok =
<span class="keyword">false</span>;
<span class="keyword">ulong</span> ticket =
<span class="number">0</span>;
<span class="keyword">if</span>(action ==
<span class="string">"BUY"</span>) { ok =
g_trade.Buy(volume, symbol); ticket =
g_trade.ResultOrder(); }
<span class="keyword">else if</span>(action ==
<span class="string">"SELL"</span>) { ok =
g_trade.Sell(volume, symbol); ticket =
g_trade.ResultOrder(); }
<span class="keyword">else if</span>(action ==
<span class="string">"CLOSE_ALL"</span>) { ok =
CloseAllPositions(symbol); }
<span class="keyword">uint</span> retcode =
g_trade.ResultRetcode();
<span class="keyword">return</span>
StringFormat(
<span class="string"
>"{"ok":%s,"ticket":%I64u,"retcode":%u}"</span
>, (ok ? <span class="string">"true"</span> :
<span class="string">"false"</span>), ticket,
retcode); }
<span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="keyword">bool</span>
<span class="func">CloseAllPositions</span
>(<span class="keyword">string</span> symbol) {
<span class="keyword">for</span>(<span
class="keyword"
>int</span
>
i = PositionsTotal() -
<span class="number">1</span>; i >=
<span class="number">0</span>; i--) {
<span class="keyword">ulong</span> ticket =
PositionGetTicket(i);
<span class="keyword">if</span
>(PositionGetString(POSITION_SYMBOL) == symbol)
<span class="keyword">if</span
>(!g_trade.PositionClose(ticket))
<span class="keyword">return false</span>; }
<span class="keyword">return true</span>; }
<span class="comment"
>//+------------------------------------------------------------------+</span
>
<span class="keyword">void</span>
<span class="func">OnDeinit</span>(<span
class="keyword"
>const int</span
>
reason) { g_pub.unbind(<span class="string"
>"tcp://:5555"</span
>); g_rep.unbind(<span class="string"
>"tcp://:5556"</span
>); }</code
>
</div>
<div class="info-box">
<strong
>نکته درباره
<span class="inline-code">ZMQ_DONTWAIT</span
>:</strong
>
مقدار بازگشتی
<span class="inline-code">recv</span> در حالت
غیرمسدودکننده، در صورت نبود پیام، مقدار غیرصفر
(معمولاً <span class="inline-code">-1</span> یا کد
خطای EAGAIN) است.[reference:9] بنابراین تنها زمانی
پردازش انجام می‌شود که
<span class="inline-code">rc == 0</span> باشد.
</div>
<div class="warning-box">
<strong
>⚠️ توجه به
<span class="inline-code">OrderSend</span> و
<span class="inline-code">CTrade</span>:</strong
>
مقدار بازگشتی
<span class="inline-code">true</span> از
<span class="inline-code">CTrade.Buy/Sell</span> فقط
به معنای عبور از بررسی‌های محلی است، نه تأیید نهایی
سرور معاملاتی. حتماً
<span class="inline-code">retcode</span> را در پاسخ
JSON به C# برگردانید تا سرویس شما بتواند نتیجه واقعی
را تشخیص دهد.[reference:10]
</div>
</div>
</div>
<!-- ==================== SECTION 4 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>پیاده‌سازی سرویس C# با NetMQ</h3>
<div class="subtitle">
C# Service — Subscriber + Request Socket
</div>
</div>
</div>
<div class="section-content">
<p>
در سمت C# از پکیج <strong>NetMQ</strong> استفاده
می‌کنیم. ابتدا پکیج را نصب کنید:
</p>
<div
class="code-block"
data-lang="PowerShell"
>
<code>Install-Package NetMQ</code>
</div>
<p>
سرویس زیر به‌صورت همزمان به داده‌های منتشرشده گوش
می‌دهد و یک رابط برای ارسال دستورات معاملاتی فراهم
می‌کند.
</p>
<div
class="code-block"
data-lang="C#"
>
<code
><span class="keyword">using</span> System;
<span class="keyword">using</span> System.Text;
<span class="keyword">using</span>
System.Text.Json;
<span class="keyword">using</span>
System.Threading;
<span class="keyword">using</span> NetMQ;
<span class="keyword">using</span>
NetMQ.Sockets;
<span class="keyword">public class</span>
Mt5ZmqBridge : IDisposable {
<span class="keyword">private readonly</span>
SubscriberSocket _sub;
<span class="keyword">private readonly</span>
RequestSocket _req;
<span class="keyword">private readonly</span>
Thread _subThread;
<span class="keyword"
>private volatile bool</span
>
_running = <span class="keyword">true</span>;
<span class="keyword">public</span>
Mt5ZmqBridge(
<span class="keyword">string</span> subAddress =
<span class="string"
>"tcp://127.0.0.1:5555"</span
>,
<span class="keyword">string</span> reqAddress =
<span class="string"
>"tcp://127.0.0.1:5556"</span
>) { _sub =
<span class="keyword">new</span>
SubscriberSocket(); _sub.Connect(subAddress);
_sub.Subscribe(<span class="string">""</span>);
<span class="comment"
>// اشتراک در تمام پیام‌ها</span
>
_req =
<span class="keyword">new</span>
RequestSocket(); _req.Connect(reqAddress);
_subThread =
<span class="keyword">new</span>
Thread(ReceiveLoop) { IsBackground =
<span class="keyword">true</span>, Name =
<span class="string">"ZmqSubscriber"</span>
}; _subThread.Start(); }
<span class="comment"
>//=== حلقه دریافت داده به‌صورت غیرهمزمان
===</span
>
<span class="keyword">private void</span>
ReceiveLoop() {
<span class="keyword">while</span> (_running) {
<span class="keyword">if</span>
(_sub.TryReceiveFrameString(
TimeSpan.FromMilliseconds(<span class="number"
>500</span
>), <span class="keyword">out var</span> json))
{
<span class="keyword">try</span>
{
<span class="keyword">using var</span> doc =
JsonDocument.Parse(json);
<span class="keyword">var</span> root =
doc.RootElement;
<span class="keyword">string</span> symbol =
root.GetProperty(<span class="string"
>"symbol"</span
>).GetString()!;
<span class="keyword">double</span> bid =
root.GetProperty(<span class="string"
>"bid"</span
>).GetDouble();
<span class="keyword">double</span> ask =
root.GetProperty(<span class="string"
>"ask"</span
>).GetDouble();
<span class="comment"
>// --- پردازش و ذخیره‌سازی ---</span
>
OnTickReceived(symbol, bid, ask); }
<span class="keyword">catch</span>
(JsonException ex) { Console.WriteLine($<span
class="string"
>"[JSON ERROR] {ex.Message}"</span
>); } } } }
<span class="keyword">private void</span>
OnTickReceived(<span class="keyword"
>string</span
>
symbol, <span class="keyword">double</span> bid,
<span class="keyword">double</span> ask) {
<span class="comment"
>// اینجا داده را پردازش کنید (مثلاً ذخیره
در دیتابیس)</span
>
Console.WriteLine($<span class="string"
>"[DATA] {symbol} Bid={bid} Ask={ask}"</span
>); }
<span class="comment"
>//=== ارسال دستور معاملاتی (REQ/REP)
===</span
>
<span class="keyword">public string</span>
SendCommand(<span class="keyword">string</span>
action,
<span class="keyword">string</span> symbol,
<span class="keyword">double</span> volume) {
<span class="keyword">string</span> payload =
$<span class="string"
>"{action}|{symbol}|{volume}"</span
>;
<span class="keyword">if</span>
(!_req.TrySendFrame(TimeSpan.FromSeconds(<span
class="number"
>2</span
>), payload))
<span class="keyword">return</span>
<span class="string"
>"{"ok":false,"error":"SEND_TIMEOUT"}"</span
>;
<span class="keyword">if</span>
(!_req.TryReceiveFrameString(
TimeSpan.FromSeconds(<span class="number"
>5</span
>), <span class="keyword">out string</span>?
reply)) <span class="keyword">return</span>
<span class="string"
>"{"ok":false,"error":"REPLY_TIMEOUT"}"</span
>;
<span class="keyword">return</span> reply!; }
<span class="keyword">public void</span>
Dispose() { _running =
<span class="keyword">false</span>;
_subThread.Join(TimeSpan.FromSeconds(<span
class="number"
>2</span
>)); _sub.Dispose(); _req.Dispose(); } }</code
>
</div>
<div class="success-box">
<strong
>✅ استفاده از
<span class="inline-code"
>TryReceiveFrameString</span
>
با Timeout:</strong
>
به‌جای
<span class="inline-code"
>ReceiveFrameString()</span
>
که ممکن است برای همیشه مسدود شود، از نسخه
<span class="inline-code">Try</span> با پارامتر
<span class="inline-code">TimeSpan</span> استفاده
کنید. این کار از قفل شدن سرویس C# در صورت قطع ارتباط
MT5 جلوگیری می‌کند.[reference:11]
</div>
</div>
</div>
<!-- ==================== SECTION 5 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>پروتکل دستورات معاملاتی (Command Protocol)</h3>
<div class="subtitle">
Command Specification & Error Handling
</div>
</div>
</div>
<div class="section-content">
<p>
یک پروتکل متنی ساده و قابل توسعه برای دستورات تعریف
کنید. فرمت پیشنهادی با جداکننده
<span class="inline-code">|</span> :
</p>
<table class="styled-table">
<thead>
<tr>
<th>دستور</th>
<th>فرمت ارسال از C#</th>
<th>عملکرد در MT5</th>
<th>نمونه پاسخ موفق</th>
</tr>
</thead>
<tbody>
<tr>
<td>خرید</td>
<td>
<span class="inline-code"
>BUY|EURUSD|0.10</span
>
</td>
<td>
<span class="inline-code"
>g_trade.Buy(0.1, "EURUSD")</span
>
</td>
<td>
<span class="inline-code"
>{"ok":true,"ticket":123456}</span
>
</td>
</tr>
<tr>
<td>فروش</td>
<td>
<span class="inline-code"
>SELL|EURUSD|0.10</span
>
</td>
<td>
<span class="inline-code"
>g_trade.Sell(0.1, "EURUSD")</span
>
</td>
<td>
<span class="inline-code"
>{"ok":true,"ticket":123457}</span
>
</td>
</tr>
<tr>
<td>بستن همه</td>
<td>
<span class="inline-code"
>CLOSE_ALL|EURUSD|0</span
>
</td>
<td>بستن تمام پوزیشن‌های نماد</td>
<td>
<span class="inline-code"
>{"ok":true,"ticket":0}</span
>
</td>
</tr>
<tr class="highlight-row">
<td>خطا (مثال)</td>
<td>—</td>
<td>—</td>
<td>
<span class="inline-code"
>{"ok":false,"retcode":10019}</span
>
</td>
</tr>
</tbody>
</table>
<div class="warning-box">
<strong
>مدیریت
<span class="inline-code">retcode</span
>:</strong
>
کدهای خطای MQL5 را حتماً در سرویس C# تفسیر کنید.
برای مثال:
<ul style="padding-right: 20px; margin-top: 8px">
<li>
<span class="inline-code">10019</span> — وجه
کافی نیست
</li>
<li>
<span class="inline-code">10018</span> —
بازار بسته است
</li>
<li>
<span class="inline-code">10027</span> —
معامله‌گری غیرفعال است
</li>
<li>
<span class="inline-code">10030</span> — حجم
نامعتبر
</li>
</ul>
</div>
<div class="info-box">
<strong
>پیشنهاد: استفاده از JSON کامل به‌جای متن
ساده.</strong
>
اگر پروژه بزرگ‌تر شد، پروتکل را به JSON ساختاریافته
ارتقا دهید:
<div
class="code-block"
data-lang="JSON"
style="margin-top: 10px; margin-bottom: 0"
>
<code
>{ <span class="string">"action"</span>:
<span class="string">"BUY"</span>,
<span class="string">"symbol"</span>:
<span class="string">"EURUSD"</span>,
<span class="string">"volume"</span>: 0.10,
<span class="string">"sl"</span>: 1.0800,
<span class="string">"tp"</span>: 1.0950,
<span class="string">"comment"</span>:
<span class="string">"csharp_bot_v1"</span>
}</code
>
</div>
</div>
</div>
</div>
<!-- ==================== SECTION 6 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>نکات عملیاتی، امنیت و پایداری</h3>
<div class="subtitle">
Production Hardening & Security
</div>
</div>
</div>
<div class="section-content">
<h4
style="
color: var(--primary);
font-size: 16px;
margin-bottom: 10px;
"
>
الف) جداسازی کامل کانال‌ها
</h4>
<p class="no-indent">
هرگز داده‌های بازار و دستورات معاملاتی را روی یک
سوکت واحد ارسال نکنید. PUB/SUB برای داده‌های پرتکرار
و REQ/REP برای دستورات حیاتی، مکمل یکدیگرند.
</p>
<h4
style="
color: var(--primary);
font-size: 16px;
margin-top: 20px;
margin-bottom: 10px;
"
>
ب) امنیت در محیط عملیاتی
</h4>
<p class="no-indent">
اگر MT5 و سرویس C# روی یک ماشین نیستند یا از شبکه
استفاده می‌کنید، حتماً لایه امنیتی ZeroMQ را فعال
کنید. پروژه <strong>MT5BridgeAPI</strong> نمونه‌ای
از پیاده‌سازی <strong>Curve25519</strong> و پروتکل
<strong>ZAP</strong> برای رمزنگاری نقطه‌به‌نقطه
است.[reference:12]
</p>
<div class="danger-box">
<strong
>🔒 هرگز پورت‌های ZeroMQ را بدون رمزنگاری روی
اینترنت باز نگذارید.</strong
>
در شبکه محلی (localhost) استفاده از
<span class="inline-code">tcp://127.0.0.1</span> امن
است. برای اتصال از راه دور، از VPN یا Curve25519
استفاده کنید.
</div>
<h4
style="
color: var(--primary);
font-size: 16px;
margin-top: 20px;
margin-bottom: 10px;
"
>
ج) مدیریت قطع ارتباط
</h4>
<p class="no-indent">
NetMQ به‌طور خودکار reconnect می‌کند، اما در سرویس
C# باید <strong>heartbeat</strong> و
<strong>timeout</strong> را مدیریت کنید. دستوراتی که
پاسخ دریافت نمی‌کنند را در یک صف (Queue) قرار دهید و
پس از برقراری مجدد، دوباره ارسال کنید.
</p>
<h4
style="
color: var(--primary);
font-size: 16px;
margin-top: 20px;
margin-bottom: 10px;
"
>
د) Magic Number و جداسازی معاملات
</h4>
<p class="no-indent">
حتماً از
<span class="inline-code"
>SetExpertMagicNumber</span
>
استفاده کنید تا معاملات ربات C# از معاملات دستی یا
ربات‌های دیگر قابل تفکیک باشد. این کار برای مدیریت و
بستن هدفمند پوزیشن‌ها ضروری است.
</p>
</div>
</div>
<!-- ==================== SECTION 7 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>جمع‌بندی و چک‌لیست راه‌اندازی</h3>
<div class="subtitle">
Summary & Deployment Checklist
</div>
</div>
</div>
<div class="section-content">
<table class="styled-table">
<thead>
<tr>
<th>مرحله</th>
<th>اقدام</th>
<th>وضعیت</th>
</tr>
</thead>
<tbody>
<tr>
<td>۱</td>
<td>نصب mql-zmq و کپی DLLها در MT5</td>
<td>☐</td>
</tr>
<tr>
<td>۲</td>
<td>فعال‌سازی Allow DLL imports در MT5</td>
<td>☐</td>
</tr>
<tr>
<td>۳</td>
<td>کامپایل اکسپرت MQL5 بدون خطا</td>
<td>☐</td>
</tr>
<tr>
<td>۴</td>
<td>نصب پکیج NetMQ در پروژه C#</td>
<td>☐</td>
</tr>
<tr>
<td>۵</td>
<td>
اجرای اکسپرت روی چارت و بررسی bind
پورت‌ها
</td>
<td>☐</td>
</tr>
<tr>
<td>۶</td>
<td>دریافت موفق داده در کنسول C# (SUB)</td>
<td>☐</td>
</tr>
<tr class="highlight-row">
<td>۷</td>
<td>
ارسال دستور تستی و دریافت پاسخ از MT5
(REQ/REP)
</td>
<td>☐</td>
</tr>
</tbody>
</table>
<div class="conclusion-box">
<h3>🎯 معماری نهایی</h3>
<p>
MT5 به‌عنوان <strong>Publisher</strong> داده‌های
بازار را روی پورت
<span class="inline-code">5555</span> منتشر
می‌کند. سرویس C# به‌عنوان
<strong>Subscriber</strong> این داده‌ها را
دریافت، پردازش و در صورت نیاز ذخیره می‌کند. برای
معاملات، C# یک <strong>Request</strong> روی پورت
<span class="inline-code">5556</span> ارسال
می‌کند و اکسپرت MT5 به‌عنوان
<strong>Reply</strong> دستور را اجرا و نتیجه را
بازمی‌گرداند.
</p>
<div class="highlight">
<strong>💎 مزیت کلیدی:</strong><br />
جداسازی کانال داده از کانال دستور + استفاده از
<span class="inline-code">ZMQ_DONTWAIT</span> در
MT5 +
<span class="inline-code"
>TryReceiveFrameString</span
>
با Timeout در C# = یک بریج پایدار، غیرمسدودکننده
و آماده برای محیط عملیاتی.
</div>
</div>
</div>
</div>
<!-- ==================== SECTION 8 ==================== -->
<div class="section">
<div class="section-header">
<div class="section-number">۸</div>
<div class="section-title">
<h3>منابع و مراجع فنی</h3>
<div class="subtitle">References</div>
</div>
</div>
<div class="section-content">
<ul class="reference-list">
<li>
<strong
>dingmaotu/mql-zmq — ZeroMQ Binding for
MQL4/5</strong
>
مستندات رسمی نصب، Context اشتراکی، و نمونه‌های
کد.
<div class="ref-source">
https://github.com/dingmaotu/mql-zmq
</div>
</li>
<li>
<strong
>mql-zmq Documentation — Installation & Best
Practices</strong
>
راهنمای کپی فایل‌ها، الزامات Visual C++ Runtime،
و پیکربندی MT5 32-bit.
<div class="ref-source">
https://docsearch.algolia.com/mcp/docs/repo/dingmaotu/mql-zmq
</div>
</li>
<li>
<strong
>MT5BridgeAPI — Production-Grade ZeroMQ
Bridge</strong
>
نمونه پیاده‌سازی صنعتی با Curve25519، ZAP، و
مدیریت خطا.
<div class="ref-source">
https://github.com/ding9736/MT5BridgeAPI
</div>
</li>
<li>
<strong
>NetMQ Documentation — Pub/Sub &
Request/Reply</strong
>
مستندات رسمی NetMQ برای SubscriberSocket،
RequestSocket، و TryReceiveFrameString.
<div class="ref-source">
https://netmq.readthedocs.io
</div>
</li>
<li>
<strong
>ZeroMQ Manual — zmq_msg_recv /
ZMQ_DONTWAIT</strong
>
مرجع رفتار غیرمسدودکننده و مقادیر بازگشتی.
<div class="ref-source">
https://manpages.debian.org/zmq_msg_recv(3)
</div>
</li>
</ul>
</div>
</div>
<div class="page-info">
این مستند فنی بوده و شامل کدهای نمونه قابل اجرا است. مسئولیت
تست کامل در محیط دمو و اعتبارسنجی امنیتی بر عهده پیاده‌ساز
است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">
راهنمای فنی — اتصال ZeroMQ به MetaTrader 5
</div>
<div class="footer-divider"></div>
<div>
نسخه ۱.۰ &nbsp;|&nbsp; آبان ۱۴۰۵ &nbsp;|&nbsp; مخصوص
پیاده‌سازی با C# / NetMQ
</div>
</footer>
</div>
</body>
</html>