Meta Trader 4 和 5 代码无法正常工作并且无法打开 Buy Sel Trade

问题描述 投票:0回答:1

我有这个 MQL4 代码:

我开发了它,然后在线阅读资源和帮助小组并修复了错误。

现在它给出了 0 个错误和 0 个警告,但仍然没有按预期工作:

    //+------------------------------------------------------------------+
//|                                          BollingerBandsBot.mq4    |
//|                        Copyright 2024, YourName                  |
//+------------------------------------------------------------------+
input double LotSize = 0.1; // Trade Size
input int BBPeriod = 20; // Bollinger Band Period
input double BBDeviation = 2.0; // Bollinger Band Deviation
input int StopLoss = 100; // Stop Loss (in pips)
input int TakeProfit = 200; // Take Profit (in pips)

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    double upperBand = iBands(NULL,0,20,2,0,PRICE_LOW,MODE_LOWER,0);
    double lowerBand = iBands(NULL,0,20,2,0,PRICE_LOW,MODE_LOWER,0);
    double currentPrice = Close[0];

// Open a buy trade when the lower limit is broken
    if (currentPrice < lowerBand)
    {
        double stopLoss = currentPrice - StopLoss * Point;
        double takeProfit = currentPrice + TakeProfit * Point;

        int buyTicket = OrderSend(Symbol(), OP_BUY, LotSize, currentPrice, 3, stopLoss, takeProfit, "Buy Order", 0, 0, clrGreen);
        if (buyTicket < 0) {
            Print("Error opening buy order: ", GetLastError());
        }
    }

// Open a sell trade when the upper limit is broken
    if (currentPrice > upperBand)
    {
         stopLoss = currentPrice + StopLoss * Point;
         takeProfit = currentPrice - TakeProfit * Point;

        int sellTicket = OrderSend(Symbol(), OP_SELL, LotSize, currentPrice, 3, stopLoss, takeProfit, "Sell Order", 0, 0, clrRed);
        if (sellTicket < 0) {
            Print("Error opening sell order: ", GetLastError());
        }
    }
}

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

这是同一个机器人的 MQL5 代码:

//+------------------------------------------------------------------+
//|                                          BollingerBandsBot.mq5    |
//|                        Copyright 2024, YourName                  |
//+------------------------------------------------------------------+
input double LotSize = 0.1; // Trade Size
input int BBPeriod = 20; // Bollinger Band Period
input double BBDeviation = 2.0; // Bollinger Band Deviation
input int StopLoss = 100; // Stop Loss (in pips)
input int TakeProfit = 200; // Take Profit (in pips)

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    double upperBand =iBands(_Symbol, PERIOD_CURRENT, 20, 2, 0.0, PRICE_HIGH);

    double lowerBand = iBands(_Symbol, PERIOD_CURRENT, 20, 2, 0.0, PRICE_LOW);
    double currentPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID); // Get current bid price

// Open a buy trade when the lower limit is broken
    if (currentPrice < lowerBand)
    {
        double stopLoss = currentPrice - StopLoss * _Point;
        double takeProfit = currentPrice + TakeProfit * _Point;

        // Create a buy order
        MqlTradeRequest request;
        MqlTradeResult result;
        
        request.action = TRADE_ACTION_DEAL; // Change this line
        request.symbol = Symbol();
        request.volume = LotSize;
        request.type     =ORDER_TYPE_BUY; // Use ORDER_BUY
        request.price = currentPrice;
        request.sl = stopLoss;
        request.tp = takeProfit;
        request.magic = 0;
        request.deviation = 0;

        if (!OrderSend(request, result))
        {
            Print("Error opening buy order: ", GetLastError());
        }
    }

// Open a sell trade when the upper limit is broken
    if (currentPrice > upperBand)
    {
        double stopLoss = currentPrice + StopLoss * _Point;
        double takeProfit = currentPrice - TakeProfit * _Point;

        // Create a sell order
        MqlTradeRequest request;
        MqlTradeResult result;
        
        request.action = TRADE_ACTION_DEAL; // Change this line
        request.symbol = Symbol();
        request.volume = LotSize;
        request.type     =ORDER_TYPE_SELL; // Use ORDER_SELL
        request.price = currentPrice;
        request.sl = stopLoss;
        request.tp = takeProfit;
        request.magic = 0;
        request.deviation = 0;

        if (!OrderSend(request, result))
        {
            Print("Error opening sell order: ", GetLastError());
        }
    }
}

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

根据 MQL4 机器人,其工作并打开买入/卖出,但始终为 0 利润。

根据 MQL5 机器人,它不会开启任何买入/卖出交易。

我的代码有什么问题吗?

mql4 metatrader4 mql5 metatrader5
1个回答
0
投票

尝试在 Buy 和 Sell 的 OrderSend 函数中使用要价和出价,而不是“currentPrice”,如果您没有通过使用 OrdersTotal 函数给出 if 条件来限制订单数量,则此代码将继续发送订单。 例如: if(OrdersTotal() == 0) { 您的 OrderSend 函数在这里。 }

© www.soinside.com 2019 - 2024. All rights reserved.