Error in compiler

 
Hi there, I am currently trying to get my first EA to be compiled, but I got some errors, any idea how to fix them?
//--- input parameters
input int MA_Length = 50;             // Moving Average length
input ENUM_APPLIED_PRICE MA_Source = PRICE_CLOSE; // Moving Average source
input int MA_Offset = 0;              // Moving Average offset

//--- variables
double ma_value;                      // Moving Average value

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    //--- initialize Moving Average
    ma_value = iMA(NULL, 0, MA_Length, MA_Offset, MA_Method, MA_Source, 0);
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    //--- get current candle values
    double open = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    double high = SymbolInfoDouble(_Symbol, SYMBOL_HIGH);
    double low = SymbolInfoDouble(_Symbol, SYMBOL_LOW);
    double close = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

    //--- check if we have any open trades
    if (PositionsTotal() > 0)
    {
        //--- get the current trade
        for (int i = 0; i < PositionsTotal(); i++)
        {
            ulong ticket = PositionGetTicket(i);

            //--- check if the trade is long
            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
            {
                //--- check if the current candle has crossed below the Moving Average
                if (close < ma_value && open > ma_value)
                {
                    //--- close the trade
                    bool result = PositionClose(ticket);
                    if (result)
                    {
                        Print("Closed trade ", ticket);
                    }
                    else
                    {
                        Print("Failed to close trade ", ticket);
                    }
                }
            }

            //--- check if the trade is short
            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
            {
                //--- check if the current candle has crossed above the Moving Average
                if (close > ma_value && open < ma_value)
                {
                    //--- close the trade
                    bool result = PositionClose(ticket);
                    if (result)
                    {
                        Print("Closed trade ", ticket);
                    }
                    else
                    {
                        Print("Failed to close trade ", ticket);
                    }
                }
            }
        }
    }
}
Thanks in advance,
 

Please post your code properly useing the CODE button when you post code. Don't use external links.

Code button in editor

I will edit it for you this time, but please do it properly next time.

Also, inform us of what compile errors you get. Don't make us have to compile it to find out.

 
  1. Don't post external links of your code, just attach the file or use the code button.

    Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. double ma_value;                      // Moving Average value
    int OnInit(){
        ma_value = iMA(NULL, 0, MA_Length, MA_Offset, MA_Method, MA_Source, 0);

    Why did you post your MT4 code in the MT5 General section instead of the MQL4 section?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    Where do you ever update the value?

  4.     if (PositionsTotal() > 0)
        {
            //--- get the current trade
            for (int i = 0; i < PositionsTotal(); i++)
            {
                ulong ticket = PositionGetTicket(i);

    MT5 code. Stop using ChatGPT.
              Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum (2023)

    ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, Molanis, Octa-FX Meta Editor, Strategy Builder FX, Strategy Quant, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You 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.

    Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

    We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

    ChatGPT
    1. Even it says do not use it for coding.*
    2. Mixing MT4 and MT5 code together.
    3. Creating multiple OnCalculate/OnTick functions.
    4. OnCalculate returning a double.
    5. Filling buffers with zero in OnInit (they have no size yet). Setting buffer elements to zero but not setting Empty Value to correspond.
    6. Calling undefined functions.
    7. Sometimes, not using strict (MT4 code).
    8. Code that will not compile.
    9. Creating code outside of functions.*
    10. Creating incomplete code.*
    11. Initialization of Global variables with non-constants.*
    12. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call.*
    13. Useing MT4 Trade Functions without first selecting an order.*
    14. Uses NULL in OrderSend.*
    bot builder Creating two OnInit() functions.*
    EA builder
    1. Counting up while closing multiple orders.
    2. Not useing time in new bar detection.
    3. Not adjusting for 4/5 digit brokers, TP/SL and slippage.*
    4. Not checking return codes.
    EATree Uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
    ForexEAdvisor
    1. Non-updateing global variables.
    2. Compilation errors.
    3. Not checking return codes.
    4. Not reporting errors.
    FX EA Builder
    1. Not checking return codes.
    2. Loosing open tickets on terminal restart. No recovery (crash/power failure.)
    3. Not adjusting stops for the spread .*
    4. Using OrdersTotal directly.
    5. Using the old event handlers.

  5. Drake.mac: I am currently trying to get my first EA to be compiled, but I got some errors, any idea how to fix them?
    1. learn to code.
    2. You don't even state what the problem is.
                Be precise and informative about your problem
Reason: