以太坊MT5代码详解,用MQL5开发以太坊自动交易程序

博主:neragonerago 2026-10-04 11:16:38 5

随着加密货币市场的成熟,越来越多的交易者希望通过程序化方式交易以太坊(Ethereum),MetaTrader 5(MT5)作为全球主流的交易平台,不仅支持外汇,还支持通过差价合约(CFD)交易以太坊等加密货币,本文将介绍以太坊MT5代码的基础知识,并通过MQL5语言实例讲解如何开发简单的自动交易程序。

MT5与以太坊交易基础

在MT5上交易以太坊,需要满足以下条件:

  1. 经纪商支持:选择提供ETHUSD等加密货币CFD产品的经纪商
  2. 合约规格了解:通过SymbolInfoDouble()函数查询点差、最小手数、合约规模等
  3. MQL5开发环境:在MT5中按F4打开MetaEditor编写代码

常见的以太坊交易品种代码包括ETHUSD、ETHUSDm、ETHUSDT等,具体取决于经纪商的命名规则。

以太坊MT5代码实战:一个简单的均线EA

以下是一个基于均线策略的以太坊自动交易EA示例:

//+------------------------------------------------------------------+
//|                                          ETH_MA_EA.mq5          |
//|                     以太坊均线自动交易EA                          |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;
//--- 输入参数
input double InpLotSize    = 0.1;   // 每单手数
input int    InpMAPeriod   = 20;    // EMA周期
input int    InpStopLoss   = 3000;  // 止损(点)
input int    InpTakeProfit = 6000;  // 止盈(点)
input ulong  InpMagic      = 20240101; // 魔术号
int maHandle;
//+------------------------------------------------------------------+
//| 初始化函数                                                        |
//+------------------------------------------------------------------+
int OnInit()
{
   // 创建EMA指标句柄(以1小时图为例)
   maHandle = iMA(_Symbol, PERIOD_H1, InpMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
   if(maHandle == INVALID_HANDLE)
   {
      Print("创建均线指标失败!");
      return(INIT_FAILED);
   }
   trade.SetExpertMagicNumber(InpMagic);
   Print("以太坊EA初始化成功,当前品种:", _Symbol);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 反初始化函数                                                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   IndicatorRelease(maHandle);
}
//+------------------------------------------------------------------+
//| 主逻辑:每个Tick执行                                               |
//+------------------------------------------------------------------+
void OnTick()
{
   // 只在新K线开盘时执行,避免重复交易
   if(!IsNewBar())
      return;
   // 获取均线数据
   double maBuffer[];
   ArraySetAsSeries(maBuffer, true);
   if(CopyBuffer(maHandle, 0, 0, 3, maBuffer) < 3)
      return;
   // 获取收盘价
   double closePrices[];
   ArraySetAsSeries(closePrices, true);
   if(CopyClose(_Symbol, PERIOD_H1, 0, 3, closePrices) < 3)
      return;
   // 均线金叉逻辑:前一根K线在均线下方,当前K线穿越均线
   bool buySignal  = (closePrices[1] <= maBuffer[1]) && 
                     (closePrices[0] > maBuffer[0]);
   bool sellSignal = (closePrices[1] >= maBuffer[1]) && 
                     (closePrices[0] < maBuffer[0]);
   if(buySignal && !HasPosition(POSITION_TYPE_BUY))
   {
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      double sl  = ask - InpStopLoss   * _Point;
      double tp  = ask + InpTakeProfit * _Point;
      trade.Buy(InpLotSize, _Symbol, ask, sl, tp, "ETH均线做多");
   }
   if(sellSignal && !HasPosition(POSITION_TYPE_SELL))
   {
      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double sl  = bid + InpStopLoss   * _Point;
      double tp  = bid - InpTakeProfit * _Point;
      trade.Sell(InpLotSize, _Symbol, bid, sl, tp, "ETH均线做空");
   }
}
//+------------------------------------------------------------------+
//| 判断是否为新K线                                                    |
//+------------------------------------------------------------------+
bool IsNewBar()
{
   static datetime lastBarTime = 0;
   datetime currentBarTime = iTime(_Symbol, PERIOD_H1, 0);
   if(currentBarTime != lastBarTime)
   {
      lastBarTime = currentBarTime;
      return true;
   }
   return false;
}
//+------------------------------------------------------------------+
//| 检查是否持有指定方向的仓位                                         |
//+------------------------------------------------------------------+
bool HasPosition(ENUM_POSITION_TYPE type)
{
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionSelectByTicket(ticket))
      {
         if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
            PositionGetInteger(POSITION_MAGIC) == (long)InpMagic &&
            PositionGetInteger(POSITION_TYPE) == type)
            return true;
      }
   }
   return false;
}

代码要点解析

指标句柄机制:MQL5采用句柄方式调用指标,iMA()只需在OnInit()中调用一次,之后通过CopyBuffer()获取数据

The End

发布于:2026-10-04,除非注明,否则均为区块链社区- 欧亿APP下载原创文章,转载请注明出处。