Error 4756- Other issue than logic?

 

Hello, 

I have been struggling for the past 2 days to understand what's wrong with my code.

I am new to coding. I have done similar EA's, but this is the first time I encounter this issue.

All seems good to me, I also used chatgpt to recheck the conditions and make sure to debug the errors and type it. It added a bunch of ways to check if there is an issue to the code- Still stuck- please if anyone has an idea on what's wrong let me know, much appreciated. I will attach the image of the issue.


EA idea is to highlight the high and low of the first 5 hours of the day, and if the price touched the high/low enter a buy\sell.

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
input double  StopLoss = 10;          // Stop Loss in pips
input double  TakeProfit = 20;         // Take Profit in pips
input double  LotSize = 0.1;           // Lot size
input double  ProximityBuffer = 0.01;  // Proximity buffer in price (adjustable)
input double  MinDistance = 10;        // Minimum distance for SL/TP in points (adjust for broker)

//+------------------------------------------------------------------+
//| Global variables                                                  |
//+------------------------------------------------------------------+
double highestPrice = -DBL_MAX;
double lowestPrice  = DBL_MAX;
bool first5HoursProcessed = false;  // Flag to know if the high/low have been calculated
datetime lastDay = 0;               // To track the day
bool longTradeTaken = false;         // Flag to track if a long trade was taken
bool shortTradeTaken = false;        // Flag to track if a short trade has been taken

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Run the EA once a minute
    EventSetTimer(60);
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    EventKillTimer();  // Remove timer when the EA is deinitialized
}

//+------------------------------------------------------------------+
//| Expert timer function                                            |
//+------------------------------------------------------------------+
void OnTimer()
{
    datetime currentTime = TimeCurrent();
    datetime dayStart = iTime(NULL, PERIOD_D1, 0);  // Start of the current day

    // Reset variables for a new day
    if (lastDay != dayStart)
    {
        lastDay = dayStart;
        first5HoursProcessed = false;  // Reset flag for the new day
        highestPrice = -DBL_MAX;
        lowestPrice = DBL_MAX;
        longTradeTaken = false;  // Reset trade flags for the new day
        shortTradeTaken = false;

        // Delete old lines to prepare for the new day
        ObjectDelete(0, "HighestPriceLine");
        ObjectDelete(0, "LowestPriceLine");
    }

    // First 5 hours after the start of the day
    datetime endTime = dayStart + 5 * 3600;  // 5 hours after day start

    // Only run the high/low calculation once after the first 5 hours
    if (!first5HoursProcessed && currentTime > endTime)
    {
        // Loop through the bars to get the high and low for the first 5 hours
        int bars = iBars(NULL, PERIOD_M1);
        for (int i = 0; i < bars; i++)
        {
            datetime barTime = iTime(NULL, PERIOD_M1, i);
            if (barTime >= dayStart && barTime <= endTime)  // Only consider bars within the first 5 hours
            {
                double barHigh = iHigh(NULL, PERIOD_M1, i);
                double barLow  = iLow(NULL, PERIOD_M1, i);

                // Update highest and lowest prices
                if (barHigh > highestPrice) highestPrice = barHigh;
                if (barLow < lowestPrice) lowestPrice = barLow;
            }
        }

        // Draw the high and low lines
        ObjectCreate(0, "HighestPriceLine", OBJ_HLINE, 0, 0, highestPrice);
        ObjectCreate(0, "LowestPriceLine", OBJ_HLINE, 0, 0, lowestPrice);

        // Set line properties
        ObjectSetInteger(0, "HighestPriceLine", OBJPROP_COLOR, clrGreen);
        ObjectSetInteger(0, "LowestPriceLine", OBJPROP_COLOR, clrRed);
        ObjectSetInteger(0, "HighestPriceLine", OBJPROP_WIDTH, 2);
        ObjectSetInteger(0, "LowestPriceLine", OBJPROP_WIDTH, 2);
        ObjectSetInteger(0, "HighestPriceLine", OBJPROP_RAY_RIGHT, true);
        ObjectSetInteger(0, "LowestPriceLine", OBJPROP_RAY_RIGHT, true);

        // Mark the first 5 hours as processed
        first5HoursProcessed = true;

        // Log the high and low prices found
        Print("High Price: ", highestPrice, " Low Price: ", lowestPrice);
    }

    // Once the high and low are fixed, check for trades
    if (first5HoursProcessed)
    {
        // Get current bid and ask prices
        double bidPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID);
        double askPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

        // Log current prices
        Print("Current Bid Price: ", bidPrice, " Current Ask Price: ", askPrice);

        // Check if there are no open trades and no trade of that type has been taken for the day
        if (PositionsTotal() == 0)
        {
            // Check if price is within the proximity buffer of the highest price (go long) and no long trade has been taken
            if (askPrice >= highestPrice - ProximityBuffer && !longTradeTaken)
            {
                double sl = NormalizeDouble(askPrice - StopLoss * _Point, _Digits);
                double tp = NormalizeDouble(askPrice + TakeProfit * _Point, _Digits);

                // Ensure SL and TP respect the minimum distance rule
                if ((askPrice - sl) < MinDistance * _Point || (tp - askPrice) < MinDistance * _Point)
                {
                    Print("Invalid SL/TP distance for BUY order. SL: ", sl, " TP: ", tp);
                    return;  // Exit if the distances are invalid
                }

                Print("Attempting to place BUY order at price: ", askPrice, " StopLoss: ", sl, " TakeProfit: ", tp);
                
                // Place the buy order
                MqlTradeRequest request;
                MqlTradeResult result;
                request.action = TRADE_ACTION_DEAL;
                request.symbol = _Symbol;
                request.volume = LotSize;
                request.type = ORDER_TYPE_BUY;
                request.price = askPrice;  // Use ask price for placing the buy order
                request.sl = sl;
                request.tp = tp;
                request.deviation = 10;
                request.type_filling = ORDER_FILLING_IOC;
                request.type_time = ORDER_TIME_GTC;

                if (!OrderSend(request, result))
                {
                    Print("Error opening BUY trade: ", GetLastError());
                }
                else
                {
                    Print("Long trade opened: ", result.order);
                    longTradeTaken = true;  // Mark that a long trade has been taken
                }
            }
            else
            {
                Print("Long trade not triggered. Ask Price: ", askPrice, " Highest Price: ", highestPrice);
            }

            // Check if price is within the proximity buffer of the lowest price (go short) and no short trade has been taken
            if (bidPrice <= lowestPrice + ProximityBuffer && !shortTradeTaken)
            {
                double sl = NormalizeDouble(bidPrice + StopLoss * _Point, _Digits);
                double tp = NormalizeDouble(bidPrice - TakeProfit * _Point, _Digits);

                // Ensure SL and TP respect the minimum distance rule
                if ((sl - bidPrice) < MinDistance * _Point || (bidPrice - tp) < MinDistance * _Point)
                {
                    Print("Invalid SL/TP distance for SELL order. SL: ", sl, " TP: ", tp);
                    return;  // Exit if the distances are invalid
                }

                Print("Attempting to place SELL order at price: ", bidPrice, " StopLoss: ", sl, " TakeProfit: ", tp);
                
                // Place the sell order
                MqlTradeRequest request;
                MqlTradeResult result;
                request.action = TRADE_ACTION_DEAL;
                request.symbol = _Symbol;
                request.volume = LotSize;
                request.type = ORDER_TYPE_SELL;
                request.price = bidPrice;  // Use bid price for placing the sell order
                request.sl = sl;
                request.tp = tp;
                request.deviation = 10;
                request.type_filling = ORDER_FILLING_IOC;
                request.type_time = ORDER_TIME_GTC;

                if (!OrderSend(request, result))
                {
                    Print("Error opening SELL trade: ", GetLastError());
                    }
                    }
               }
}
}
Files:
WHY.JPG  25 kb
 

Hi

You need to reset the request before creating a new one like this

MqlTradeRequest request;

 MqlTradeResult result;

ZeroMemory(request);

Have a nice weekend👍📊

 
Marzena Maria Szmit #:

Hi

You need to reset the request before creating a new one like this

Have a nice weekend👍📊

That worked!- Thank you very much Marzena, very much appreciated.