MQL 5 . bear engulfing EA built on ChatGpt shows no error on compile but sell trade not executing at market price

 

This is the code , there is no error on compile but its not executing order on market price : 

// Input parameters
input double RiskPercentage = 0.4;  // Risk percentage per trade
input double TakeProfitPercentage = 0.7;  // Take profit percentage per trade

// Global variables
int MagicNumber = 12345;  // Unique identifier for the EA

// Calculate position size based on risk percentage
double CalculatePositionSize(double price, double riskPercentage)
{
    double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
    double positionSize = (accountBalance * riskPercentage) / price;
    return positionSize;
}

// Calculate take profit based on account balance and take profit percentage
double CalculateTakeProfit(double accountBalance, double takeProfitPercentage)
{
    double takeProfit = (accountBalance * takeProfitPercentage) / 100.0;
    return takeProfit;
}

// Check for bearish engulfing pattern
bool BearishEngulfingPattern()
{
    double prevClose = iClose(_Symbol, PERIOD_M30, 1);
    double prevOpen = iOpen(_Symbol, PERIOD_M30, 1);
    double prevLow = iLow(_Symbol, PERIOD_M30, 1);
    double currClose = iClose(_Symbol, PERIOD_M30, 0);
    double currOpen = iOpen(_Symbol, PERIOD_M30, 0);
    double currLow = iLow(_Symbol, PERIOD_M30, 0);

    bool isPrevBullish = prevClose > prevOpen;
    bool isCurrBearish = currClose < currOpen;
    bool isEngulfing = currOpen > prevClose && currClose < prevOpen;
    bool isCurrLowLower = currLow < prevLow;
    bool isCurrOpenGreaterOrEqual = currOpen >= prevClose;

    if (isPrevBullish && isCurrBearish && isEngulfing && isCurrLowLower && isCurrOpenGreaterOrEqual)
    {
        return true;
    }

    return false;
}

// Entry function
void OnTick()
{
    // Check for bearish engulfing pattern
    if (BearishEngulfingPattern())
    {
        double entryPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

        // Calculate position size based on risk percentage
        double positionSize = CalculatePositionSize(entryPrice, RiskPercentage);

        // Calculate take profit based on account balance and take profit percentage
        double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
        double takeProfit = CalculateTakeProfit(accountBalance, TakeProfitPercentage);

        // Calculate stop loss at the high of the engulfing candle
        double stopLoss = iHigh(_Symbol,PERIOD_M30,0);

        // Open the sell order at market price
        MqlTradeRequest request;
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol;
        request.volume = positionSize;
        request.type = ORDER_TYPE_SELL;
        request.price = 0.0;  // Set price to 0 for market execution
        request.sl = stopLoss;
        request.tp = entryPrice - takeProfit;
        request.magic = MagicNumber;
        request.deviation = 3;
        request.type_filling = ORDER_FILLING_RETURN;

        MqlTradeResult result;

        if (OrderSend(request, result))
        {
            Print("Sell order opened successfully. Ticket: ", result.order);
        }
        else
        {
            Print("Error opening sell order: ", GetLastError());
        }
    }
}

If someone can find that little mistake please tell me , Thank you

 

If your code compiles with no error but it does not do what what it should learn to use the debugger:

Code debugging:  https://www.metatrader5.com/en/metaeditor/help/development/debug
Error Handling and Logging in MQL5:  https://www.mql5.com/en/articles/2041
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin": https://www.mql5.com/en/articles/272

Tips from a professional programmer (Part  I): Code storing, debugging and compiling. Working with projects and logs https://www.mql5.com/en/articles/9266
Tips from a professional programmer (Part II): Storing and exchanging parameters between an Expert Advisor, scripts and external programs https://www.mql5.com/en/articles/9327
Tips from a professional programmer (Part III): Logging. Connecting to the Seq log collection and analysis system https://www.mql5.com/en/articles/10475

Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator https://www.mql5.com/en/articles/35

Beside that:

Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5 and is ready for you - so searching gives better results than AI or ChatGPT!

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 

i just changed the order send function but it says there is an error in ordersned parameters : 

ulong ticket = OrderSend(_Symbol, ORDER_TYPE_SELL, positionSize, entryPrice, 3, stopLoss, entryPrice - takeProfit, "Bearish Engulfing", MagicNumber, 0);
would you please find out anything wrong data type or parameters enterd
 

This is good post about :

ChatGPT (the worst), “Bots Builder”, “EA builder”, more .., etc., are all the same. Y
ou will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

read this post

Simple code error - *newbie*
Simple code error - *newbie*
  • 2023.06.05
  • www.mql5.com
Good night everyone, how are you...
 

i know its chatgpt generated, but i want to know whats wrong in this just order sending block code , there is some little common mistake i think

 
DEEPAK SONI #:

i know its chatgpt generated, but i want to know whats wrong in this just order sending block code , there is some little common mistake i think

Use the debugger!
Reason: