Self-learning the MQL5 language from scratch - page 74

 
VVT:

Wait, you have a time limit on trading there... Doesn't the time of trawl fall under those restrictions? though no, wrong stop, strange

Your Expert Advisor is set to run from 09:00:00 to 09:01:00 Moscow time only to open a position. No problems with position opening. The Expert Advisor was installed on a demo account in the PSB-Forex trading platform.

I decided to put the Expert Advisor on my demo account in MetaQuote MT5 trading terminal. Everything has worked as it should but the 10016 trade server return code still remains. Very interesting! I will try to analyze the matter.

Yours sincerely, Vladimir.

 
MrBrooklin:

Valery, what is the freeze level? Maybe it needs to be prescribed in some way in the code?

Regards, Vladimir.

Freezing distance is more correctly, it is the distance from a market order/position in which orders are not allowed to be executed. Stops cannot be placed in this area. Use your estimated stop as the Parameter and you will get the required one in the output. The code is for 4k. The logic is the same in 5. The MarketInfo parameter seems to be the same.

int New_Stop(int Parametr)                      // Проверка стоп-прик.
  {
   int Min_Dist=MarketInfo(Symb,MODE_STOPLEVEL);// Миним. дистанция
   if (Parametr < Min_Dist)                     // Если меньше допуст.
     {
      Parametr=Min_Dist;                        // Установим допуст.
      Alert("Увеличена дистанция стоп-приказа.");
     }
   return(Parametr);                            // Возврат значения
  }
//-------------------------------------------------------------- 12 -- 

For 5

SYMBOL_TRADE_FREEZE_LEVEL

Trading operations freezing distance (in points)

int



Документация по MQL5: Константы, перечисления и структуры / Состояние окружения / Информация об инструменте
Документация по MQL5: Константы, перечисления и структуры / Состояние окружения / Информация об инструменте
  • www.mql5.com
Для получения текущей рыночной информации служат функции SymbolInfoInteger(), SymbolInfoDouble() и SymbolInfoString(). В качестве второго параметра этих функций допустимо передавать один из идентификаторов из перечислений ENUM_SYMBOL_INFO_INTEGER, ENUM_SYMBOL_INFO_DOUBLE и ENUM_SYMBOL_INFO_STRING соответственно. Некоторые символы (как правило...
 
Valeriy Yastremskiy:

The freeze distance is correct, the distance from the market order/position in which orders are not allowed to be executed. Stops cannot be placed in this area. Use your estimated stop as the Parameter, and you will get the desired one in the output. The code is for 4k. The logic is the same in 5. The MarketInfo parameters seem to be the same.

Thank you, Valeriy! I will continue to improve the code of the Expert Advisor.

Regards Vladimir.

 
MrBrooklin:

Thank you, Valery! I will continue to understand and improve the Expert Advisor code.

Sincerely, Vladimir.

Please send me function of stop calculation and position modification. Code is long, not much time)))))

 
Valeriy Yastremskiy:

Please upload the stop calculation and position modification function here. The code is long, not much time)))))

Valery, there is no function to calculate the stop because it is set in the input parameters like this:

input ushort SL=200; //Стоп-лосс

and in the code for the Buy position the following line is prescribed:

request.sl=NormalizeDouble(price-SL*point,digits);

Thank you, Valery, but I want to deal with modification of stop loss myself. I already have enough information to improve the Expert Advisor myself.

Regards, Vladimir.

 
MrBrooklin:

Valery, there is no function to calculate the stop, because it is set in the input parameters like this:

And in the code for Buy position the following line is prescribed:

Thank you, Valery, but I want to deal with modification of stop loss myself. I already have enough information to improve the Expert Advisor myself.

Sincerely, Vladimir.

request.sl=NormalizeDouble(price-SL*point,digits);
Alert("request.sl= ",request.sl);

And also price, SL and point to be sure)

 
Valeriy Yastremskiy:

And also for sure price, SL and point)

Thank you, Valery!

Sincerely, Vladimir.

 
Valeriy Yastremskiy:

The freeze distance is correct, the distance from the market order/position in which orders are not allowed to be executed. Stops cannot be placed in this area. Use your estimated stop as the Parameter, and you will get the desired one in the output. The code is for 4k. The logic is the same in 5. The MarketInfo parameter seems to be the same.

For 5

SYMBOL_TRADE_FREEZE_LEVEL

Trading operations freezing distance (in points)

int



Thank you, Valery for the clarification.

Regards, Vladimir.

 

Good day and good mood everyone!

I continue studying the MQL5 programming language. Today is the time when I can put the code of the Expert Advisor with a trailing stop to the public view. Since the code of the Expert Advisor is quite cumbersome, I had to cut the comments as much as possible and publish it as a file called Trailing_Stop (v.1).mq5. I will publish the version of the Expert Advisor with detailed comments on each code line, in a form easy to understand for a 1st year student of a programming school, a little later.

Before publishing the code, we checked the Expert Advisor in the strategy tester. No problems were detected. The netting system of position accounting is used in the Expert Advisor. This system of accounting means that at one point in time on the account, there can be only one open position for the same symbol (security).

At this point in time, the Expert Advisor has implemented the ability to:

  1. Check the trading account, on which you plan to install it. If the Expert Advisor will be installed on a demo account, then in the message box will appear permission to continue working. If an attempt is made to install the Expert Advisor to areal account, the message window will show a warning that it cannot continue working, and then it will be automatically deleted from the work window of the trading terminal .
  2. Once a trading day, at 9 am Moscow time sharp, automatically open one position in the direction in which the first tick is directed. For example, if the first tick is directed upwards at 9 a.m. Moscow time, then BUY position will be opened; if the first tick is directed downwards, then SELL position will be opened.
  3. Set the lot size.
  4. Set stop loss size.
  5. Specify the level relative to the opening price, at which the Stop Loss is moved to Breakeven.
  6. Set the trailing step of the stop loss.

This version of the Expert Advisor partially implements the hints earlier suggested by Vassily Sokolov. All additions and detailed comments will be included in the final version of the EA.

WARNING!!!

This Expert Advisor is developed according to the self-study plan for demo accounts, and it is intended for training purposes only! It is not intended for trading on a real account and making profit!

Regards, Vladimir.

Files:
 

Dear participants and readers of this thread!

I was sitting up all night trying to figure out why the previously published file with the Expert Advisor code, which had a defect, could not be replaced with the corrected version. By morning I finally figured it out!

Frankly speaking, I did not think that if one of the forum participants had already downloaded the file Trailing_Stop.mq5 with a defect, it would not be possible to simply download and replace the file with a corrected one with the same name. It turned out that you should also change the name of the EA in order to publish the corrected variant.

Due to this fact, I apologize and propose to download the corrected version of the Expert Advisor named Trailing_Stop (v.1).mq5 that is located in the previous message.

Regards, Vladimir.