Why is my PC crashing running this simple EA ?

 

Hello, 


Please could you tell me why my PC is crashing running this particular EA from which you have the code inserted below ? All others EA I tried run fine, there is a particular problem with this one, which has been generated using ChatGPT.


Be careful if you run it in your computer as it surely will crash too, maybe due to ressource consumption ?


Thanks in advance :)


//+------------------------------------------------------------------+
//|                                                      MyExpert.mq5|
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;

input int MA1Period = 10;
input bool useMA1 = true;
input int MA2Period = 20;
input bool useMA2 = true;
input int MA3Period = 50;
input bool useMA3 = true;
// MACD settings
input int FastEMA = 12;
input int SlowEMA = 26;
input int SignalSMA = 9;
input bool useMACD = true;

// Generalized MA condition function
bool MACondition(int period, bool useCondition, bool forBuy) {
    if (!useCondition) return true; // Condition neutralized if not used
    double currentPrice = iClose(_Symbol, _Period, 0);
    double ma = iMA(_Symbol, _Period, period, 0, MODE_SMA, PRICE_CLOSE);
    return forBuy ? (currentPrice > ma) : (currentPrice < ma);
}

// MACD condition function, corrected for MQL5
bool MACDCondition(bool forBuy) {
    if (!useMACD) return true; // Condition neutralized if not used
    int macdHandle = iMACD(_Symbol, _Period, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE);
    double macdMain[1], macdSignal[1];
    CopyBuffer(macdHandle, 0, 0, 1, macdMain);  // Copy Main line
    CopyBuffer(macdHandle, 1, 0, 1, macdSignal);  // Copy Signal line
    IndicatorRelease(macdHandle); // Release the handle after use
    return forBuy ? (macdMain[0] > macdSignal[0]) : (macdMain[0] < macdSignal[0]);
}

// Unified buy decision based on MA conditions and MACD
bool ShouldBuy() {
    return MACondition(MA1Period, useMA1, true) &&
           MACondition(MA2Period, useMA2, true) &&
           MACondition(MA3Period, useMA3, true) &&
           MACDCondition(true);
}

// Unified sell decision based on MA conditions and MACD
bool ShouldSell() {
    return MACondition(MA1Period, useMA1, false) &&
           MACondition(MA2Period, useMA2, false) &&
           MACondition(MA3Period, useMA3, false) &&
           MACDCondition(false);
}

// Main trading logic
void OnTick() {
    // Buy or sell based on conditions, ensuring no existing position
    if (ShouldBuy() && !PositionSelect(_Symbol)) {
        trade.Buy(0.1, _Symbol); // Adjust lot size as needed
    } else if (ShouldSell() && !PositionSelect(_Symbol)) {
        trade.Sell(0.1, _Symbol); // Adjust lot size as needed
    }
}
//+------------------------------------------------------------------+
 
Every tick you call iMA and iMACD. These functions return a handle to the indicadotr, not the actual indicator data. Everytime you call them you create a new handle, but these handles are never closed, thus causing the crashes. To fix this, call these functions on the OnInit function, store their handles on a global variable and copy their actual data using CopyBuffer.
 
Thank you very much for your answer, as a non developer I struggle with the code logic. Copy pasting your answer allowed chatgpt to solve the problem, really powerful tool. Thanks a lot sir