Discussion of article "Creating an EA that works automatically (Part 15): Automation (VII)"

 

New article Creating an EA that works automatically (Part 15): Automation (VII) has been published:

To complete this series of articles on automation, we will continue discussing the topic of the previous article. We will see how everything will fit together, making the EA run like clockwork.

I showed the main failures, problems and difficulties involved in what governs the work of a programmer while creating an EA that works automatically. But I also showed you that this can bring a lot of knowledge and change the way you actually observe the market.

I tried to present things in such a way that you can actually create a system that is safe, reliable and robust. At the same time, it should be modular, compact and very light. And you should be able to use it in conjunction with many other things. It's no use having a system that doesn't allow you to operate a variety of things at the same time. Because you won't always be able to actually make a good profit if trading only one asset.

Perhaps most of the readers will be interested in this last article in the sequence, where I explain the idea using 3 practical examples. However, please note that knowledge of the entire sequence of articles is necessary in order to take advantage of this article. But in a very simple way, I believe I have managed to convey the idea that it is not necessary to be a genius in programming or to have several courses and graduations. You just need to really understand how the MetaTrader 5 platform and the MQL5 language work.

I also presented how you can create specific circumstances for an efficient working system, even when MQL5 or MetaTrader 5 do not provide the indicator that you want to use. This was done in example 3, where I showed how to create the HILO indicator internally. But regardless of this, the system should always be correctly implemented and tested. Because there is no point in creating a wonderful system, which in the end will not give you any profit.

Author: Daniel Jose

 

Hi Daniel, first of all I'd like to congratulate you on your performance in collaborating with the community of developers and enthusiasts.

I have a question about the execution of the EA in this sequence of articles.

I have tested it in Netting and Hedging accounts by changing the OCO Order to Pending, according to your documentation. But the orders are not executed.

And there are no errors in the log either.

What could it be?

Note: I didn't change anything in the code, just the buy and sell logic... I tested it with my logic (my strategy) and it didn't give any entries, I thought: I've done something wrong, kkkk.... then I took your code without any changes, compiled it and the same thing happens.

It runs manually (when you set the batch quantity), but not automatically.

 
César Augusto #:

Hi Daniel, first of all I'd like to congratulate you on your performance in collaborating with the community of developers and enthusiasts.

I have a question about running the EA in this sequence of articles.

I have tested it in Netting and Hedging accounts by changing the OCO Order to Pending, according to your documentation. But the orders are not executed.

Nor does it give any error in the log.

What could it be?

Note: I didn't change anything in the code, just the buy and sell logic... I tested it with my logic (my strategy) and it didn't give any entries, I thought: I've done something wrong, kkkk.... then I took your code without any changes, compiled it and the same thing happens.

It runs manually (when you set the batch quantity), but not automatically.

Automatic entries only happen when the input parameters you set allow the order to be triggered. In other words, if your logic says that the indicator, whatever it is, needs to have a certain value, the automatic entry or exit will only happen when that value is reached. Otherwise, it will never happen. You may have seen that there are 3 examples of setups in the sequence. Try and study each one to understand how the trigger logic works. Without understanding this trigger logic, you won't be able to make the automatic mechanism work. It's like a mousetrap, if you set it up to catch an elephant, it won't be triggered when the mouse touches it 😂 ... Anyway, study the mechanisms in the examples and good luck ... 😁👍

 

Daniel, thank you for your dedication to teaching.

Can you please check the functioning of the EA of this part 15 - version v3 - which uses the C_Automaton_v3.mqh?

The definition of the eSelectMedia list is clear. However, I think there's a problem with using it when it comes to manipulating the elements of m_Op arrays in the FOR loop, specifically with "sizeof(eSelectMedia)".

According to the documentation(https://www.mql5.com/en/docs/basis/types/integer/enumeration), sizeof always returns 4, even for enumerations with more elements.

Documentation on MQL5: Language Basics / Data Types / Integer Types / Enumerations
Documentation on MQL5: Language Basics / Data Types / Integer Types / Enumerations
  • www.mql5.com
Enumerations - Integer Types - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
FernandoN23 #:

Daniel, thank you for your dedication to teaching.

Can you please check the functioning of the EA of this part 15 - version v3 - which uses the C_Automaton_v3.mqh?

The definition of the eSelectMedia list is clear. However, I think its use has some problem, regarding the manipulation of the elements of the m_Op arrays in the FOR loop, specifically with "sizeof(eSelectMedia)".

According to the documentation(https://www.mql5.com/en/docs/basis/types/integer/enumeration), sizeof always returns 4, even for enumerations with more elements.

You're confusing things. The 4 that the documentation says is the size, in terms of bytes, used to return sizeof and not the number of maximum elements that will be returned.

 
Daniel Jose #:

You're confusing things. The 4 that the documentation says is the size, in terms of bytes, used in the return of sizeof and not the number of maximum elements that will be returned.

Daniel, thanks for the quick reply.

Still on the subject of FOR with sizeof(enum), I'll add a test script, the result obtained and one more question below.

Thank you for your guidance.

test.mq5 script

//+------------------------------------------------------------------+
void OnStart()
  {
      enum eSelectMedia {MEDIA_FAST, MEDIA_SLOW};
      enum eSelectMeses {JANEIRO, FEVEREIRO, MARCO, ABRIL, MAIO, JUNHO, JULHO, AGOSTO, SETEMBRO, OUTUBRO, NOVEMBRO, DEZEMBRO};
   
      struct st01
         {
               double  Buff[];
               int     Handle;
         }m_Op[sizeof(eSelectMedia) + 1];
                   
      Print("Tamanho do eSelectMedia = ", sizeof(eSelectMedia));
      Print("Tamanho do eSelectMeses = ", sizeof(eSelectMeses));
      Print("Tamanho do m_Op = ", ArraySize(m_Op));
      
      Print("========= Mostrar enum eSelectMedia =========");
      for (int i = sizeof(eSelectMedia); i >= 0; i--)
        {
            Print("eSelectMedia - idx = ", i);
        }
   
      Print("========= Mostrar enum eSelectMeses =========");
      for (int i = sizeof(eSelectMeses); i >= 0; i--)
        {
            Print("eSelectMeses - idx = ", i);
        }
   
      Print("========= Mostrar enum m_Op =========");
      for (int i = ArraySize(m_Op); i >= 0; i--)
        {
            Print("m_Op - idx = ", i);
        }     
  }
// End OnStart() 
//+------------------------------------------------------------------+


Result:

2023.06.07 09:09:10.415    teste (EURUSD,M1)    Tamanho do eSelectMedia = 4
2023.06.07 09:09:10.415    teste (EURUSD,M1)    Tamanho do eSelectMeses = 4
2023.06.07 09:09:10.415    teste (EURUSD,M1)    Tamanho do m_Op = 5
2023.06.07 09:09:10.415    teste (EURUSD,M1)    ========= Mostrar enum eSelectMedia =========
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMedia - idx = 4
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMedia - idx = 3
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMedia - idx = 2
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMedia - idx = 1
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMedia - idx = 0
2023.06.07 09:09:10.415    teste (EURUSD,M1)    ========= Mostrar enum eSelectMeses =========
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMeses - idx = 4
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMeses - idx = 3
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMeses - idx = 2
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMeses - idx = 1
2023.06.07 09:09:10.415    teste (EURUSD,M1)    eSelectMeses - idx = 0
2023.06.07 09:09:10.415    teste (EURUSD,M1)    ========= Mostrar enum m_Op =========
2023.06.07 09:09:10.415    teste (EURUSD,M1)    m_Op - idx = 5
2023.06.07 09:09:10.415    teste (EURUSD,M1)    m_Op - idx = 4
2023.06.07 09:09:10.415    teste (EURUSD,M1)    m_Op - idx = 3
2023.06.07 09:09:10.415    teste (EURUSD,M1)    m_Op - idx = 2
2023.06.07 09:09:10.415    teste (EURUSD,M1)    m_Op - idx = 1
2023.06.07 09:09:10.415    teste (EURUSD,M1)    m_Op - idx = 0


The eSelectMedia enum has 2 elements.
The eSelectMonths enum has 12 elements.
The return of sizeof() is 4, for any of them, according to the documentation.

The m_Op has ArraySize() = 5, because it was defined based on sizeof(eSelectMedia) + 1, according to the C_Automaton_v3.mqh file.

When I used sizeof(enum) in the FOR loop, the number of elements in the corresponding enum was not taken into account. The interaction considered 4, which is the return of sizeof(enum), both for the enum with 2 elements and for the enum with 12 elements.

With this, how should I create a loop that considers the exact number of elements in an enumeration?

Incorrectly formatted code edited by the moderator.

 
@FernandoN23 #: script test.mq5

Please use the use the CODE button (Alt -S) when entering your code.

Code button in the editor

Nota de usuário - MQL5.community
Nota de usuário - MQL5.community
  • www.mql5.com
Você acabou de registrar e, provavelmente, você tem perguntas como: "Como faço para inserir uma imagem na minha mensagem?", "Como faço para formatar meu código fonte MQL5?", "Onde são mantidas minhas mensagens pessoais?". Você pode ter muitas outras perguntas. Neste artigo, nós preparamos algumas dicas práticas que ajudarão você a se acostumar com o MQL5.community e tirar o máximo proveito de seus recursos disponíveis.
 
MetaQuotes:

New article Creating an EA that works automatically (Part 15): Automation (VII) has been published:

Author: Daniel Jose

Good day Mr. Daniel please can you share the
 EA_v1.mq5, EA_v2.mq5 and EA_v3.mq5 file to me. Or I wonder where I can see the attached file.
 

Hi, I'm using the EA you provided on the EURUSD 1M chart and during the process, I'm experiencing that the ClosePosition function is not closing the position successfully.

I'm guessing it's because the account type is Hedging and the position must be closed by setting the action to TRADE_ACTION_CLOSE_BY instead of TRADE_ACTION_DEAL.

 
Dear Daniel. Congratulations on your articles. Please help me with a question: when I replace the code block of the "inline virtual void Triggers(void) final" method to send a pending order at the high or low of the bar that triggered the moving average signal, the EA already buys 2 contract volumes, even though it is set to buy 1 volume. If I go back to the original code, it works perfectly again. If you can help me, thank you!!!