gold ea

 

Hello everyone, I'm trying to write a simple EA for MT4, but during backtesting I encounter this error for all trades:

2024.05.14 12:29:52.244 2024.05.13 23:50:02 test XAUUSD,M5: Failed to open sell position.
Error code: 131 2024.05.14 12:29:52.244 2024.05.13 23:50:02 test XAUUSD,M5: OrderSend error 131

What is causing this? Is there any logical error in the code?


// Calcola l'EMA a 12 periodi del prezzo high
double ema_high() {
    return iMA(NULL, 0, 12, 0, MODE_EMA, PRICE_HIGH, 0);
}

// Calcola l'EMA a 12 periodi del prezzo low
double ema_low() {
    return iMA(NULL, 0, 12, 0, MODE_EMA, PRICE_LOW, 0);
}

// Calcola la media mobile a 50 periodi
double sma_50() {
    return iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
}

// Calcola l'Awesome Oscillator
double awesome_oscillator() {
    return iAO(NULL, 0, 5);
}

// Verifica se il prezzo di chiusura è al di sopra dell'EMA High
bool above_high() {
    return (Close[0] > ema_high());
}

// Verifica se il prezzo di chiusura è al di sotto dell'EMA Low
bool below_low() {
    return (Close[0] < ema_low());
}

// Dichiarazioni globali per tenere traccia dello stato delle posizioni
bool isPositionOpen = false;
bool closeOrderSent = false;
int lastOrderTicket = -1;

// Avvio delle operazioni di acquisto e vendita
void OnTick() {
    if (is_5min()) {
        double ao_value = awesome_oscillator();
        
        if (ao_value > 0 && ema_low() > sma_50()) {
            if (!isPositionOpen && !closeOrderSent) {
                double stop_loss = 400 * Point;
                double take_profit = 800 * Point;
                double open_price = Bid;
                double sl = open_price - stop_loss;
                double tp = open_price + take_profit;
                lastOrderTicket = OrderSend(_Symbol, OP_BUY, 0.01, open_price, 3, sl, tp, "Buy", 0, 0, Green);
                if(lastOrderTicket >= 0) {
                    isPositionOpen = true;
                    closeOrderSent = false; // Reset close order flag
                } else {
                    Print("Failed to open buy position. Error code: ", GetLastError());
                }
            }
        }

        if (ao_value < 0 && ema_high() < sma_50()) {
            if (!isPositionOpen && !closeOrderSent) {
                double stop_loss_sell = 400 * Point;
                double take_profit_sell = 800 * Point;
                double open_price_sell = Ask;
                double sl_sell = open_price_sell + stop_loss_sell;
                double tp_sell = open_price_sell - take_profit_sell;
                lastOrderTicket = OrderSend(_Symbol, OP_SELL, 0.01, open_price_sell, 3, sl_sell, tp_sell, "Sell", 0, 0, Red);
                if(lastOrderTicket >= 0) {
                    isPositionOpen = true;
                    closeOrderSent = false; // Reset close order flag
                } else {
                    Print("Failed to open sell position. Error code: ", GetLastError());
                }
            }
        }
    }

    // Controlla se l'ordine aperto è stato chiuso
    if (isPositionOpen && closeOrderSent) {
        isPositionOpen = false; // Reset position open flag
        closeOrderSent = false; // Reset close order flag
        lastOrderTicket = -1; // Reset last order ticket
        Print("Order closed successfully.");
    }
}

// Funzione chiamata quando viene chiuso l'ordine
void OnTrade() {
    if (OrderCloseTime() != 0 && OrderTicket() == lastOrderTicket) {
        closeOrderSent = true;
    }
}