Questions from Beginners MQL5 MT5 MetaTrader 5 - page 611

 
Vladimir Zubov:

I don't know about that, I usually don't close the browser until I get to the "ready to check" stage. Try looking in your tab for "my products", the draft is usually there, if it's not there, you might need to start from the beginning. If the name of the product you wanted to publish isn't taken, you probably haven't saved the draft.

Thank you! Found it!
 
Greetings all, dear colleagues!
Once upon a time I asked a question about the possibility to save an array of class objects to a file, and I got an affirmative answer from the community. Finally I got a chance to check it all. And yes, indeed THAT-that saves. And then it even reads THAT. Anyway, both FileWriteArray and FileReadArray return correct values by the number of written and read items. However, when trying to access the first element of the array through a class method, the tester generates the invalid pointer access error followed by OnTick critical error and terminates. Note, though, that if you're working not with a written and then read array, but just with something that's created in memory, everything works. What could be the problem? What is the correct way to restore a saved object array?
 
BlackTomcat:
Greetings all, dear colleagues!
Once upon a time I asked a question about the possibility to save an array of class objects to a file, and I got an affirmative answer from the community. Finally I got a chance to check it all. And yes, indeed THAT-that saves. And then it even reads THAT. Anyway, both FileWriteArray and FileReadArray return correct values by the number of written and read items. However, when trying to access the first element of the array through a class method, the tester generates the invalid pointer access error followed by OnTick critical error and terminates. Note, though, that if you're working not with a written and then read array, but just with something that's created in memory, everything works. What could be the problem? How to correctly restore a saved array of objects?
Please send me the code!
 
Hello! I know MT4 pretty well, but MT5 is a beginner. Please advise which command to close an order! I couldn't find it in the list of operation types here https://www.mql5.com/ru/docs/constants/tradingconstants/enum_trade_request_actions. OnlyCLOSE_BY and I just need to close it.
Документация по MQL5: Стандартные константы, перечисления и структуры / Торговые константы / Типы торговых операций
Документация по MQL5: Стандартные константы, перечисления и структуры / Торговые константы / Типы торговых операций
  • www.mql5.com
Стандартные константы, перечисления и структуры / Торговые константы / Типы торговых операций - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Alexander Kharlamov:
Hello, I know MT4 pretty well, but MT5 is a newbie. Please advise which command to close the order! I couldn't find any command in the list of operation types here https://www.mql5.com/ru/docs/constants/tradingconstants/enum_trade_request_actions. The only command I have isCLOSE_BY, I just need to close the order.

in mt5 it's not that simple

to close means to open an opposite trade with the same lot

i.e.TRADE_ACTION_DEAL operation

 
Vladislav Andruschenko:

in mt5 it's not that simple

to close means to open an opposite trade with the same lot

i.e.TRADE_ACTION_DEAL operation

Thanks for the reply, but I do not quite understand... I opened an opposite trade and then I need to do aTRADE_ACTION_CLOSE_BY between them?
 
Alexander Kharlamov:
Thanks for the reply, but I do not quite understand it yet. Do I open an opposite position and thenTRADE_ACTION_CLOSE_BY between them?

In MT5 there is the concept of a POSITION. You can close a position. If you want to close a position completely, use the standard library. For a netting account an example of closing a position:

//+------------------------------------------------------------------+
//|                                                 PositionClos.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//--- подключение стандартной библиотеки
#include <Trade\Trade.mqh>
//--- объект класса CTrade
CTrade trade;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- закрытие позицию по текущему символу
   trade.PositionClose(Symbol());
  }
//+------------------------------------------------------------------+
Files:
 
Karputov Vladimir:

In MT5 there is a concept of a POSITION. You can close a position. If you want to close a position completely, use the standard library. For a netting account an example of how to close a position:

I think I'm starting to get it! ) The position will change with every order execution for that instrument, right. The question now is"The position identifier is a unique number that is assigned to each newly opened position and does not change throughout its life. It corresponds to the ticket of the order with which the position was opened".

And if subsequent orders change the position, does its number remain equal to the first order which created it?
 
Alexander Kharlamov:

1. I think I'm beginning to understand! ) The position will change with every order execution for this symbol, right?
2. Theposition identifier is a unique number, which is assigned to each newly opened position and does not change throughout its lifetime. It corresponds to the ticket of the order with which the position was opened".

And if subsequent orders change the position, does its number remain equal to the first order which created it?

1. Yes, that's right. Execution of an order generates a transaction. A transaction can:

  • create a position
  • change the size of a position
  • close a position
2. What could be better than practice? :) Open a position (manually) on any chart. Run the script

//+------------------------------------------------------------------+
//|                                           PositionGetInteger.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   for(int i=0;i<PositionsTotal();i++)
     {
      //--- проверим наличие позиции и выведем время её изменения 
      string pos_symbol=PositionGetSymbol((uint)i);
      if(pos_symbol!=NULL)
        {
         //--- получим идентификатор позиции для дальнейшей работы с ней 
         ulong position_ID=PositionGetInteger(POSITION_IDENTIFIER);
         Print(pos_symbol," postion #",position_ID);
         //--- получим время образования позиции в миллисекундах с 01.01.1970 
         long create_time_msc=PositionGetInteger(POSITION_TIME_MSC);
         PrintFormat("Position #%d  POSITION_TIME_MSC = %i64 milliseconds => %s",position_ID,
                     create_time_msc,TimeToString(create_time_msc/1000));
         //--- получим время последнего изменения позиции в секундах с 01.01.1970 
         long update_time_sec=PositionGetInteger(POSITION_TIME_UPDATE);
         PrintFormat("Position #%d  POSITION_TIME_UPDATE = %i64 seconds => %s",
                     position_ID,update_time_sec,TimeToString(update_time_sec));
         //--- получим время последнего изменения позиции в миллисекундах с 01.01.1970 
         long update_time_msc=PositionGetInteger(POSITION_TIME_UPDATE_MSC);
         PrintFormat("Position #%d  POSITION_TIME_UPDATE_MSC = %i64 milliseconds => %s",
                     position_ID,update_time_msc,TimeToString(update_time_msc/1000));
        }
     }
//--- 
  }
//+------------------------------------------------------------------+

- Look at the line (look at the "Experts" tab of the terminal):

2016.08.29 14:51:24.435 PositionGetInteger (EURUSD,M1)  EURUSD postion #95873997

Increase volume, run the script again - compare the line again

2016.08.29 14:51:32.872 PositionGetInteger (EURUSD,M1)  EURUSD postion #95873997

As you can see, the position property (POSITION_IDENTIFIER) does not change if the position is not reversed.

Files:
 
POSIITION_IDENTIFIER changes when the position is reversed.