下面是一个简单的MQL4代码示例,用于创建一个一键平仓按钮:

//+------------------------------------------------------------------+
//|                                                       CloseAll.mq4 |
//|                      Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
    // 创建一个一键平仓按钮
    if(!ButtonCreate(0, "Close All", 0, 10, 10, 100, 30))
    {
        Print("Failed to create button!");
    }

    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void OnCalculate(const int rates_total,
                 const int prev_calculated,
                 const datetime &time[],
                 const double &open[],
                 const double &high[],
                 const double &low[],
                 const double &close[],
                 const long &tick_volume[],
                 const long &volume[],
                 const int &spread[])
{
    // 检查是否点击了一键平仓按钮
    if(ButtonClicked(0))
    {
        // 平掉所有已开仓的交易
        for(int i = OrdersTotal() - 1; i >= 0; i--)
        {
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                {
                    OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0);
                }
            }
        }
    }
}

//+------------------------------------------------------------------+

这个代码是一个自定义指标(indicator)的示例,它在图表上创建了一个按钮,并在点击按钮时平掉所有已开仓的交易。

请将此代码保存为.mq4文件并将其添加到MetaTrader 4的图表中运行。然后,您将在图表左上角看到一个按钮,点击该按钮即可执行一键平仓操作

mql4写一个一键平仓按钮

原文地址: http://www.cveoy.top/t/topic/iJIR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录