Basic questions from a newcomer

 

Hi all!

I should point out right away that I tried to ask these questions in the relevant thread here, but somehow the question didn't get the attention it deserved. Although, it seems to me, the topics touched at least once touch/emerge in many people's minds. Sort of like questions from the FAQ (although I could be wrong). I'll duplicate the questions themselves here directly:

1. There is a differencebetween Mql4 and Mql5 (site and programming language). There is Metatrade 4 and Metatrader 5 with Mql4 and Mql5 respectively. There are also two different corresponding websites. At the same time, all those Expert Advisors I looked at in MT4 lead to that site. Which one is more relevant? Which forum is "livelier"? Where is the best place to post questions? Is there a backward compatibility between different versions of MT and Mql? I personally am currently writing my MT4 Expert Advisor on Mql4 respectively. This is due to the fact that the broker I'm using for testing the demo with is using MT4. How can I identify the language in which my Expert Advisor is written? I mean by extension only (.mq4 and .mq5). If I rename a file, will it compile (again, about compatibility)?

2. Dynamic External Expert Advisor Parameters. Is it possible to dynamically change adjustable parameters of an Expert Advisor? Let me explain... For example, there is an external parameter extern bool a. If it is true, an additional parameter extern int b shall be set. If a=false, the parameter b is not needed. Is there any way to display/not display it depending on the current value of a selected? Taking into account that I could not find any external parameter change handlers and there is no #if ... #endif, I suspect it cannot be done... If so, but could you suggest the best way to proceed in this situation, so as not to overload the process of setting external parameters? For example, I may put a=false, forget that the parameter b is not used, but still include it into optimization (although there would be no point, it would only waste extra time). And it's one thing when there is only one such parameter b. But if, for example, I make an enum external parameter and there are several dropdown variants of TC. They have a common part (therefore it is logical to implement them inside one EA, rather than write several), but there are also different ones with many different parameters. Then it's very easy to confuse which parameters are relevant to the selected TS.

Automatic optimization of Expert Advisor. IMHO, it's very useful and useful. I found here an article onAutomated optimization of trading robot in real trading. However, as far as I understand, a second instance of MT is used that is run for optimization from outside (from a running instance of MT) and the optimization results are read from a report in the form of html. This is not very convenient and crooked. Logically I should write my own optimization function/dll and run it directly from the Expert Advisor. So here is the question. As far as I understood, a genetic optimization algorithm was programmed into the strategy tester. I know about these algorithms very briefly. But they have been known for a long time, hence the question - why reinvent the wheel?) Is there a ready-made algorithm that is used directly in MT? Maybe it already exists somewhere in the libraries of the terminal itself... Maybe there is a separate source code or ready dll. In general, please share your experience in this matter.

Thanks in advance for answers to at least part of these questions.

 
gammaray:

Hi all!

I should point out right away that I tried to ask these questions in the relevant thread here, but somehow the question didn't get the attention it deserved. Although, it seems to me, the topics touched at least once touch/emerge in many people's minds. Sort of like questions from the FAQ (although I could be wrong). I'll duplicate the questions themselves here directly:

1. There is a differencebetween Mql4 and Mql5 (site and programming language). There is Metatrade 4 and Metatrader 5 with Mql4 and Mql5 respectively. There are also two different corresponding websites. At the same time, all those Expert Advisors I looked at in MT4 lead to that site. Which one is more relevant? Which forum is "livelier"? Where is the best place to post questions? Is there a backward compatibility between different versions of MT and Mql? I personally am currently writing my MT4 Expert Advisor on Mql4 respectively. This is because the broker I use for testing the demo with is using MT4. ...

...

It would be logical to put my MQL4 related questions in the "fourth" forum. Although the difference between MQL5 and MQL4 is not very significant. We have moved from MQL4. Therefore, if you want to ask a question on the "4" in this forum, you had better specify in the thread title that it is related to MT4. It will be clearer that way.


... How do you determine the language in which the Expert Advisor is written? I mean, only by the extension (.mq4 and .mq5). And if the file is renamed, will it compile (again, about compatibility)?

...

As for the extension. There are files that after renaming will compile well and work in both MT4 and MT5 - no general recipes here.
 
Karputov Vladimir:

It makes sense to address MQL4 questions to the "fourth" forum. Although differences between MQL5 and MQL4 are not significant. It's a transition from MQL4. Therefore, if you want to ask a question on the "4" in this forum, you had better specify in the topic heading that it is related to MT4. It will be clearer that way.


Regarding extension. If you do not have an MT4 or MT5 account with an MT4 or MT5 file, you can use it as a pre-requisite for a trading robot and you don't need to create a trading robot or an MT4 file.
It is only the first question concerns mql4, the rest are IMHO common for mql4 and mql5. Thank you for the answer to the first question;) Most of all I am interested in the second question. And IMHO the third question may be interesting for all EA developers)
 
For the second question, see input, sinput, ParametrGetRange(...), ParametrSetRange(...).
 
Vladimir Kazakov:
For the second question, see input, sinput, ParametrGetRange(...), ParametrSetRange(...).

I did not ask how to set external parameters of the Expert Advisor. I learned to do this long ago (input, extern). I asked how and whether it is possible to dynamically change the set of parameters displayed in the settings. ParametrGetRange and ParametrSetRange cannot be found in mql. These are not built-in functions? Where to look for them?

P.S. The use of sinput may be used only as a hint that the parameter cannot be optimized by itself without being bound with other parameters. But I can't even dynamically change input into sinput depending on the parameters I've already chosen to suggest what parameters to fetch in the optimization (what parameters are significant given the chosen parameters) so I don't have to waste time on running through the parameters that currently make no sense

 

Nothing can be changed programmatically in the "Parameters" tab. You can skip the disabled parameter set by returning INIT_PARAMETERS_INCORRECT from OnInit().

You can change start, step, stop and optimisation permission programmatically: https://www.mql5.com/ru/docs/optimization_frames

Документация по MQL5: Работа с результатами оптимизации
Документация по MQL5: Работа с результатами оптимизации
  • www.mql5.com
Работа с результатами оптимизации - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Vladimir Kazakov:

Nothing can be changed programmatically in the parameters tab. A disabled parameter set cannot be skipped - a stub can be switched on so as not to make unnecessary calculations.

You can change the start, step, stop and optimisation resolution programmatically: https://www.mql5.com/ru/docs/optimization_frames

These functions have apparently appeared in mql5 because I haven't found them in mql4 help. Then again, there is the question of compatibility. If I write an EA in Metatrader 5, may I use it in Metatrader 4? It seems unlikely. If especially I use the new functions introduced in mql5. Does mql5 have functions to run optimization (it is just the matter of my question 3)?
 

The topic is called "basic questions from a beginner", and as a beginner I don't understand anything. Well, this is where I will get an answer to my elementary question (or rather hopefully). I tried to follow a step by step guide to write my Expert Advisor. I got stuck on the aspect of how to forcefully close an open position in MKL5 (without waiting for the SL or TP), when a certain indicator signal appears, such as at the intersection of two MAs, or when the narrowing of the Bollinger lines or other signals? There must be a function like ClosePos(), but where is it and how do I get it? I ask for a detailed piece of code, because I myself do not understand the nuances - I am still a baby in MKL5.

 
doublezero:

The topic is called "basic questions from a beginner", and as a beginner I don't understand anything. Well, this is where I will get an answer to my elementary question (or rather hopefully). I tried to follow a step by step guide to write my Expert Advisor. I got stuck on the aspect of how to forcefully close an open position in MKL5 (without waiting for the SL or TP), when a certain indicator signal appears, such as at the intersection of two MAs, or when the narrowing of the Bollinger lines or other signals? There must be a function like ClosePos(), but where is it and how do I get it? I request a detailed snippet of code, as I myself do not understand the nuances - in MKL5 is still a baby.

I am dealing with this myself for less than a month. The question is whether you want to do it yourself or ask on the forum. Simple questions usually do not ask, because it's not very good (you can look in the Help, look for ready answers on the Internet). That's why I asked those pretty extensive questions that came up during this month. As for your question... OrderClose function is called. There are a lot of examples of its use. It's in the built-in MACD Sample. Here is the code used to close orders by MACD signals (here is a code sample for implementation of Trailing Stop)


//--- it is important to enter the market correctly, but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderType()<=OP_SELL &&   // check for opened position
         OrderSymbol()==Symbol())  // check for symbol
        {
         //--- long position is opened
         if(OrderType()==OP_BUY)
           {
            //--- should it be closed?
            if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
               MacdCurrent>(MACDCloseLevel*Point))
              {
               //--- close order and exit
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
         else // go to short position
           {
            //--- should it be closed?
            if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
               MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))
              {
               //--- close order and exit
               if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
                  Print("OrderClose error ",GetLastError());
               return;
              }
            //--- check for trailing stop
            if(TrailingStop>0)
              {
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     //--- modify order and exit
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red))
                        Print("OrderModify error ",GetLastError());
                     return;
                    }
                 }
              }
           }
        }
     }


And, in general, just type the word "Order" in the "Index" and all functions concerning order management will be shown there
 
gammaray:

I've been doing this myself for less than a month. The question is whether you want to do it yourself, or whether you want to ask questions on the forum. I don't usually ask simple questions, because it's not really appropriate (you can do some digging in the Help or search for answers on the Internet). That's why I asked those pretty extensive questions that came up during this month. As for your question... OrderClose function is called. There are a lot of examples of its use. It's in the built-in MACD Sample. Here is the code used to close orders by MACD signals (here is a sample code of how to implement the Trailing Stop mechanism)



Thanks for such a quick reply! I thought that it would be probably given by someone who has been dealing with this kind of thing for a long time.

Except that OrderClose is in MKL4, but it's not in MKL5. There's only thePositionClose function in the CTrade class in the standard library. And it's not at all clear to me how to get it out of there. I think, for this you have to somehow call from a code to library, then to class CTrade..... All in all, it's a dark forest for me. I was not going to study MKL5, I just decided to automate my quite successful (for me) strategy, so I just started to follow step by step instruction. And now I'm stuck...

 
doublezero:

Thanks for such a quick reply! I thought it would probably come from someone who's new to this.

Except that OrderClose is in MKL4, but MKL5 doesn't have it. There's only thePositionClose function in the CTrade class in the standard library. And it's not at all clear to me how to get it out of there. I think, for this you have to somehow call from a code to library, then to class CTrade.... All in all, it's a dark forest for me. I was not going to study MKL5, I just decided to automate my quite successful (for me) strategy, so I just started to follow step-by-step instructions. And now I'm stuck...

I had messed up something in mql 5, I don't want to use OrderSend, everything is done there (including closing). Again, MT5 has the same MACD Sample. But it takes 10 times more lines...
Ошибка закрытия ордера
Ошибка закрытия ордера
  • www.mql5.com
Скажите а есть отдельная команда по закрытию позиции или только встречная позиция? - - Категория: эксперты форекс, торговые роботы и советники
Reason: