Hi! I have an Error: 138

ultimate3101

Trader
Mar 6, 2024
5
0
6
25
I Made an EA, but get an Error which sais Order Send Error 138
Can someone Help me?
MQL4:
input double UpZone = 1.3000; // Define your UpZone value
input double DownZone = 1.2900; // Define your DownZone value
input int ATR_Period = 14; // ATR period
input double MinATRLevel = 0.001; // Minimum ATR level to trigger a trade
input double TakeProfitPips = 20; // Take Profit in pips
input double SpreadPips = 2; // Desired spread in pips
 
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
    // Add initialization code here
    if (!CheckSettings()) {
        Print("EA initialization failed. Check settings.");
        return(INIT_FAILED);
    }
 
    return(INIT_SUCCEEDED);
}
 
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
    double closePrice = iClose(Symbol(), 0, 0);
    double openPrice = iOpen(Symbol(), 0, 0);
    double highPrice = iHigh(Symbol(), 0, 0);
    double lowPrice = iLow(Symbol(), 0, 0);
    double spread = MarketInfo(Symbol(), MODE_SPREAD);
 
    double atrValue = iATR(Symbol(), 0, ATR_Period, 0);
 
    double stopLoss, takeProfit; // Declare variables outside of if statements
 
    // Check if ATR is greater than the specified minimum level
    if (atrValue >= MinATRLevel)
    {
        // Check if candle closes below DownZone but also has its body in the Zone
        if (closePrice < DownZone && openPrice > DownZone)
        {
            stopLoss = DownZone + SpreadPips * Point;
            takeProfit = openPrice + TakeProfitPips * Point - SpreadPips * Point;
 
            // Execute sell logic
            if (ExecuteSell(stopLoss, takeProfit) < 0) {
                Print("Sell order execution failed. Check logs.");
            }
        }
 
        // Check if candle closes above UpZone but also has a wick or body in the Zone
        if (closePrice > UpZone && (highPrice > UpZone || lowPrice > UpZone))
        {
            stopLoss = UpZone - SpreadPips * Point;
            takeProfit = openPrice - TakeProfitPips * Point + SpreadPips * Point;
 
            // Execute buy logic
            if (ExecuteBuy(stopLoss, takeProfit) < 0) {
                Print("Buy order execution failed. Check logs.");
            }
        }
    }
}
 
//+------------------------------------------------------------------+
//| Check EA settings |
//+------------------------------------------------------------------+
bool CheckSettings()
{
    // Add additional checks if needed
    return (TakeProfitPips > 0 && SpreadPips >= 0);
}
 
//+------------------------------------------------------------------+
//| Execute Sell Order |
//+------------------------------------------------------------------+
int ExecuteSell(double stopLoss, double takeProfit)
{
    return (OrderSend(Symbol(), OP_SELL, 0.1, iClose(Symbol(), 0, 0), 3, stopLoss, takeProfit, "Sell order", 0, 0, Red));
}
 
//+------------------------------------------------------------------+
//| Execute Buy Order |
//+------------------------------------------------------------------+
int ExecuteBuy(double stopLoss, double takeProfit)
{
    return (OrderSend(Symbol(), OP_BUY, 0.1, iClose(Symbol(), 0, 0), 3, stopLoss, takeProfit, "Buy order", 0, 0, Blue));
}
 
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,456
1,581
144
Odesa
www.earnforex.com
OrderSend Error 138 is a Requote error, which means that the price has changed compared to the one you issued your order with. You can read more about this error in this guide: https://www.earnforex.com/guides/ordersend-error-138-requote/
However, in your case, the problem is that you are using OrderSend with the close price instead of Ask or Bid. You also need to call RefreshRates before using Ask and Bid price for orders.