EA Code for closing buy orders when overbought

 

I'm trying to make a MetaEditor 5/MQL5  EA code to be used in MT5.

The command is to close buy orders when the stochastic (5,3,3) reaches or increased greater than 80.

I am using the help of Chat GPT but I keep on getting the same errors:

'SELECT_BY_POS' - undeclared identifier

'OrderSelect' - wrong parameters count

'OrderType' - undeclared identifier

'OrderType' - cannot convert enum

'OrderClose' - undeclared identifier

'OrderTicket' - undeclared identifier

And the code provided was this: 


//+------------------------------------------------------------------+

//|                                                     CloseBuys.mq5|

//|                        Copyright 2024, MetaQuotes Software Corp. |

//|                                       http://www.metaquotes.net/ |

//+------------------------------------------------------------------+

#property strict



// Include the necessary library

#include <Trade\Trade.mqh>



// Stochastic settings

input int Stochastic_Period = 5;

input int Stochastic_Slowing = 3;

input int Stochastic_Method = MODE_SMA;

input double Stochastic_Overbought_Level = 80.0;



//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

{

    // Initialization code here

    return INIT_SUCCEEDED;

}



//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{

    // Get Stochastic value

    double stochasticValue = iStochastic(_Symbol, PERIOD_CURRENT, Stochastic_Period, Stochastic_Slowing, Stochastic_Method, 0, 0);



    // Check if Stochastic value is greater than or equal to the overbought level

    if (stochasticValue >= Stochastic_Overbought_Level)

    {

        // Close all buy orders

        CloseBuyOrders();

    }

}



//+------------------------------------------------------------------+

//| Function to close all open buy orders                            |

//+------------------------------------------------------------------+

void CloseBuyOrders()

{

    // Loop through all orders

    for (int i = OrdersTotal() - 1; i >= 0; i--)

    {

        // Select order by index

        if (OrderSelect(i, SELECT_BY_POS))

        {

            // Check if order is a buy order

            if (OrderType() == ORDER_TYPE_BUY)

            {

                // Close the buy order

                double closePrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

                OrderClose(OrderTicket(), OrderLots(), closePrice, 3, clrRed);

            }

        }

    }

}

//+------------------------------------------------------------------+


MetaQuotes — the developer of trading platforms for brokers, banks, exchanges and hedge funds
MetaQuotes — the developer of trading platforms for brokers, banks, exchanges and hedge funds
  • www.metaquotes.net
MetaTrader 5 trading platform is a free Forex and stock trading tool
 
When you post code please use the Code button (Alt+S) !

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Sergey Golubev #:
When you post code please use the Code button (Alt+S) !

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Hover your mouse over your post and select "edit" ... 

No edit button. I also posted the wrong code. I'll just create a new post.
 
jjsantos88 #:
No edit button. I also posted the wrong code. I'll just create a new post.

There is an edit option at the bottom of your first post, so do not create a new post, just edit the previous one above!

 
jjsantos88: I am using the help of Chat GPT but I keep on getting the same errors:

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, “LP-MOBI”, 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.