EA is not an expert and cannot be executed?

 

When I try to add this EA to the chart I get a message same "is not an expert and cannot be executed".

Can someone shed some light on what is wrong here?


// Constants (Input Parameters)
input string EA_Name = "MR EA";
input double LotSize = 0.01; // Starting lot size
input double PipAmount = 10; // Minimum pip amount for trade entry
input int BuySellOption = 1; // 1 for buy, -1 for sell
input int StartHour = 8;     // Trading start hour
input int EndHour = 20;      // Trading end hour
input double TakeProfit = 50; // Take profit in pips
input double StopLoss = 30;   // Stop loss in pips
input double LotIncreasePercent = 0.25; // Lot size increase percent

// Global variables
int magicNumber = 12345; // Magic number for identification
int totalTradesThisMonth = 0;

void OnStart()
{
    // Check if it's within the specified trading hours
    int currentHour = Hour();
    if (currentHour < StartHour || currentHour >= EndHour)
        return;

    // Check for new closed candles
    double lastClosePrice = iClose(Symbol(), PERIOD_CURRENT, 1);
    double previousClosePrice = iClose(Symbol(), PERIOD_CURRENT, 2);
    double pipDifference = MathAbs(lastClosePrice - previousClosePrice) / Point;

    // Check if the closed candle has achieved the minimum pip amount
    if (pipDifference >= PipAmount)
    {
        // Determine the direction of the trade based on BuySellOption
        int tradeDirection = (BuySellOption == 1) ? OP_SELL : OP_BUY;
        double lotSize = LotSize + (AccountEquity() * LotIncreasePercent / 100.0);

        // Open the trade
        int ticket = OrderSend(Symbol(), tradeDirection, lotSize, Bid, 2, 0, 0, EA_Name, magicNumber, 0, clrNONE);
        if (ticket > 0)
        {
            // Order was placed successfully
            totalTradesThisMonth++;
            Print("Trade opened. Ticket: ", ticket);
        }
        else
        {
            Print("Failed to open trade. Error code: ", GetLastError());
        }
    }
}

int TradesThisMonth(int type)
{
    datetime firstDayOfMonth = iTime(Symbol(), PERIOD_MN1, 0);
    datetime trades[]; // Array to store trade times

    int total = 0;
    int tradeCount = OrdersHistoryTotal();
    for (int i = 0; i < tradeCount; i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
            if (OrderType() == type && OrderOpenTime() >= firstDayOfMonth)
            {
                trades[total++] = OrderOpenTime();
            }
        }
    }
    return total;
}

bool OrderCloseAll()
{
    int totalOrders = OrdersTotal();
    bool closed = false;
    for (int i = totalOrders - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == magicNumber)
            {
                if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), clrNONE))
                {
                    closed = true;
                    Print("Trade closed. Ticket: ", OrderTicket());
                }
                else
                {
                    Print("Failed to close trade. Error code: ", GetLastError());
                }
            }
        }
    }
    return closed;
Extract profit down to the last pip
Extract profit down to the last pip
  • www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
 

The function

void OnStart()

features either a script or a service. EA must have OnTick.

Maybe you should start with this:

    If you place the cursor on an MQL function and press F1, you will see the reference directly, many with examples to copy and paste - the fastest form to code.
    https://www.mql5.com/en/articles/496
    https://www.mql5.com/en/articles/100
    and for debugging: https://www.metatrader5.com/en/metaeditor/help/development/debug
    https://www.mql5.com/en/search#!keyword=cookbook
    "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!" (Carl Schreiber):
    => Search in the articles: https://www.mql5.com/en/articles
    => Search in the codebase: https://www.mql5.com/en/code
    => Search in general: https://www.mql5.com/en/search or via Google with: "site:mql5.com .." (forgives misspelling)

Quick Start: Short Guide for Beginners
Quick Start: Short Guide for Beginners
  • www.mql5.com
Hello dear reader! In this article, I will try to explain and show you how you can easily and quickly get the hang of the principles of creating Expert Advisors, working with indicators, etc. It is beginner-oriented and will not feature any difficult or abstruse examples.