Simple code error - *newbie*

 
Good night everyone, how are you?

I'm trying to create an EA code that does the following task:

  • When turned on, it sends a random order (buy or sell) to the market, places fixed target and stop. When the target is hit, it opens another order of the same type. If the stop is hit, it opens the same order again. After hitting the stop 3 times, it starts opening orders of the opposite type.

For example:

--The robot started and randomly selected that it would open a buy order, the market followed and it hit the take. Then he opened another buy order and the market came back and hit the stop. again he opens another buy order and the market comes back and hits the stop. again he opens another buy order and the market comes back and hits the stop. In the next order, as they completed 3 stops, he changes the order and opens a sell order.


Guys, this is an idea I had, it's nothing amazing and maybe some even think it's "idiotic". But it's an idea that I wanted to put into practice to see what happens.I asked chatGPT to create it for me, as I know absolutely nothing about mql5 just the platform itself. However, as expected, chatgpt does not make a perfect code and presents several problems. If anyone can help me solve this code and get it running, I can even buy you a beer :D

// External input parameters
input double LotSize = 0.01;
input double TakeProfit = 100.0;
input double StopLoss = 50.0;
input int MaxStopHits = 3;

// Global variables
int stopHits = 0;
ENUM_ORDER_TYPE orderType;

// Function to generate a random order type (BUY or SELL)
ENUM_ORDER_TYPE GenerateRandomOrderType()
{
    return (MathRand() % 2 == 0) ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
}

// Function to open a market order
void OpenMarketOrder(ENUM_ORDER_TYPE type)
{
    MqlTradeRequest request;
    ZeroMemory(request);

    request.action = TRADE_ACTION_DEAL;
    request.symbol = Symbol();
    request.volume = LotSize;
    request.type = type;
    request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

    MqlTradeResult result;
    ZeroMemory(result);

    if (OrderSend(request, result))
    {
        // Order opened successfully
        if (type == orderType)
            stopHits++;
        else
            stopHits = 0;

        orderType = type;
    }
    else
    {
        // Failed to open order, handle error if necessary
    }
}

// Function to handle trading logic
void TradeLogic()
{

    // Check if target or stop loss is hit
    if (orderType != ORDER_TYPE_NONE)
    {
        double targetPrice = (orderType == ORDER_TYPE_BUY) ? SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfit : SymbolInfoDouble(Symbol(), SYMBOL_ASK) - TakeProfit;
        double stopLossPrice = (orderType == ORDER_TYPE_BUY) ? SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLoss : SymbolInfoDouble(Symbol(), SYMBOL_ASK) + StopLoss;

        if (stopHits < MaxStopHits)
        {
            if (orderType == ORDER_TYPE_BUY)
            {
                if (targetPrice <= SymbolInfoDouble(Symbol(), SYMBOL_ASK))
                {
                    OpenMarketOrder(orderType);
                }
                else if (stopLossPrice >= SymbolInfoDouble(Symbol(), SYMBOL_ASK))
                {
                    OpenMarketOrder(orderType);
                }
            }
            else if (orderType == ORDER_TYPE_SELL)
            {
                if (targetPrice >= SymbolInfoDouble(Symbol(), SYMBOL_BID))
                {
                    OpenMarketOrder(orderType);
                }
                else if (stopLossPrice <= SymbolInfoDouble(Symbol(), SYMBOL_BID))
                {
                    OpenMarketOrder(orderType);
                }
            }
        }
        else
        {
            ENUM_ORDER_TYPE oppositeType = (orderType == ORDER_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;

            if ((oppositeType == ORDER_TYPE_SELL && targetPrice >= SymbolInfoDouble(Symbol(), SYMBOL_BID)) ||
                (oppositeType == ORDER_TYPE_BUY && targetPrice <= SymbolInfoDouble(Symbol(), SYMBOL_ASK)))
            {
                OpenMarketOrder(oppositeType);
                stopHits = 0;
            }
        }
    }
    else
    {
        orderType = GenerateRandomOrderType();
        OpenMarketOrder(orderType);
    }
}

// Expert Advisor start function
void OnTick()
{
    TradeLogic();
}

Practical evaluation of the adaptive market following method
Practical evaluation of the adaptive market following method
  • www.mql5.com
The main difference of the trading system proposed in the article is the use of mathematical tools for analyzing stock quotes. The system applies digital filtering and spectral estimation of discrete time series. The theoretical aspects of the strategy are described and a test Expert Advisor is created.
 
Your topic has been moved to the appropriate section: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
So what is the error or what is the thing it should do that it doesn't?

It is not idiotic. The question is if you understand what its components do. Like the OnTick event function gets called literally on every tick which calls the TradeLogic function, which calls the GenerateRandomOrderType and OpenmarketOrder.

The way it is made is quite smart and so is the naming of variables in the code. Although you may see that this is not a very effective strategy one might say. If it works as intended it is a good example of how functions can be used nonetheless. The only critique might be too many nested if statements.

Now, you talk about "solving problems", but you didn't name any...?
 
Tobias Johannes Zimmer #:
So what is the error or what is the thing it should do that it doesn't?

It is not idiotic. The question is if you understand what its components do. Like the OnTick event function gets called literally on every tick which calls the TradeLogic function, which calls the GenerateRandomOrderType and OpenmarketOrder.

The way it is made is quite smart and so is the naming of variables in the code. Although you may see that this is not a very effective strategy one might say. If it works as intended it is a good example of how functions can be used nonetheless. The only critique might be too many nested if statements.

Now, you talk about "solving problems", but you didn't name any...?
Hello Tobias how are you? Thank you very much for answering. I didn't put the errors because I thought you guys would try to compile the code and see what's wrong hahahahaha.
Just to reinforce, I'm a total beginner in programming in general, after a lot of study I can understand what each thing in the code is doing, but at first I'm lost.
Let's go to the errors (I regenerated chatgpb's response several times and finally got a code that showed the least amount of errors):
Files:
Sem_tktulo.jpg  276 kb
 
Vitor Monteiro: .I asked chatGPT to create it for me, as I know absolutely nothing about mql5 j

Stop using ChatGPT.
          Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (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. Calling MT4 functions in MT5 code.
  8. Sometimes, not using strict (MT4 code).
  9. Code that will not compile.
  10. Creating code outside of functions.*
  11. Creating incomplete code.*
  12. Initialization of Global variables with non-constants.*
  13. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call.*
  14. Useing MT4 Trade Functions without first selecting an order.*
  15. 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.
 
William Roeder #:

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

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.


Sure william! I'm going to start learning mql5, would you know how to tell me where I can start? I'm a beginner in terms of programming, I know the basics, if, else .....
 
Sorry but I stopped taking the time to check every code I see here and so did other people. 
Good thing there is only one compile error. Unfortunately there is no ORDER_TYPE_NONE.

https://www.mql5.com/en/docs/constants/tradingconstants/orderproperties#enum_order_type

Just use a boolean variable.
If you want to learn programming do a course.
 
Reason: