Self-learning the MQL5 language from scratch - page 66

 
Valeriy Yastremskiy:

The loop operator and operators in the loop body. Not exactly classic Russian.

Everything is good, of course, but where is the iterator i in the loop body? And in order to get the position symbol and its magic number, it has to be selected first. The position description is a structure (in MQL5 all the structure and orders and time and deals) and it is filled out through selection, by the iterator that is not a position ticket, but a position number. And we should remember that in the position description structure, it's always the last position selection.

Hello Valery! Thanks for the clarification.

That's where all my confusion came in, because everything worked without the for loop operator, while it didn't work with it for some reason. Did I get it right that I should first make all the necessary queries via request?

Regards, Vladimir.

 
MrBrooklin:

Hello Valery! Thanks for the clarification.

That's where all my confusion lay, because everything worked without the for loop operator, but with it, for some reason, it didn't. Did I get it right that I needed to first do all the necessary queries via request?

Regards, Vladimir.

No, I don't quite know MKL5 . This is a request for execution. And you need to select a position by its number, in order to work with it further.

PositionSelect

I think so.

 
MrBrooklin:

Good day and good mood everyone!

Dear professionals! Please advise me, but not in words, otherwise I'll never learn the MQL5 programming language.

Regards, Vladimir.

Hello! The matter is that you've applied the loop of open positions, and you haven't defined it in terms of "if", i.e., what exactly the for loop looks for, and you haven't completed it yet, it must return something...

The open positions are counted all the time, so it is better to create a block with a cycle, and when you open it, you can call it, for example, in this way;

input int MaxOpenedPositions=1;
.
.
.
int Total=TotalPositions(-1);
if(Total<MaxOpenedPositions && другие условия для открытия) OpenBUY();//например
if(Total<MaxOpenedPositions && другие условия для открытия) OpenSELL();//например

//+------------------------------------------------------------------+
//| Total Positions                                                  |
//+------------------------------------------------------------------+
int TotalPositions(int Type)
   {
int cnt=0;
if(PositionsTotal()>0)
for(int i=PositionsTotal()-1;i>=0;i--)
   
if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetString(POSITION_SYMBOL)==_Symbol && PositionGetInteger(POSITION_MAGIC)==MagicNumber) 
   {
   if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) {cnt++;continue;}
   if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) {cnt++;}
   }
   return(cnt);
   }
 
Valeriy Yastremskiy:

no, I don't really know MKL5 . This is a request for execution. And you need to select a position by number, in order to work with it further.

PositionSelect

I think so.

Tried everything, including PositionSelect. None of them helped.

Regards, Vladimir.

 
VVT:

Hello! With words; the point is that when you applied loop of open positions, you didn't specify it in "if" conditions, i.e. what exactly the for loop is trying, also you didn't end it in any way, it should return something...

The open positions are calculated all the time, so it's better to create a block with a loop and call it when you open it, for example;

Hello!

Thank you for the tip. I will try to analyse and apply your method of enumeration of open positions.

Regards, Vladimir.

 
VVT:

Hello! In words; the point is that when you applied the loop to search for open positions, you didn't specify it in the "if", i.e. what exactly the for loop searches for, and you didn't end it in any way, it should return something...

By the way, the MQL5 Reference says nothing about the fact that the loop must return something upon completion. Maybe, I misunderstood something? Or maybe it refers to the if statement?

Best regards, Vladimir.

 
MrBrooklin:

Tried everything, including PositionSelect. Nothing helped.

Regards, Vladimir.

Only understood, yes, position selection in netting by tool, not by number. Oh, they messed up).

It is possible through

  • PositionGetTicket returns the position ticket by position number (and we have a number) and after receiving the position ticket we can get its symbol and magik.
Or, if it is netting, we can think about it and decide why we need the total number of orders or positions, if we can have only one position on one instrument and we need to know it. That's why we may not need a cycle. On our instrument we do If the PositionSelect returns False, it opens a position and if True, it selects a position on this instrument and we can work with it and modify it for trawling.
 
Valeriy Yastremskiy:

Just realised, yes, position selection in netting by instrument, not by number. Oh, they're messed up.)

It is possible through

  • PositionGetTicket returns a position ticket by position number (and we have a number) and getting the position ticket we can get its symbol and magik.
Or, if it is netting, we can think about it and decide why we need the total number of orders or positions, if we can have only one position on one instrument and we need to know it. That's why we may not need a cycle. On our symbol, we do If the PositionSelect returns False, it opens a position and if True, it selects a position on this symbol and we can work with it and modify it for trawling.

So the code was originally written without the loop operator, although I was curious to try it later.

Regards, Vladimir.

 
MrBrooklin:

By the way, there's nothing in the MQL5 Reference about the fact that the loop should return something upon completion. Maybe, I have got it wrong again?

Sincerely, Vladimir.

The wording is not quite correct. Actions should be performed in the loop body, depending on the iterator. Depending on the iterator. And it is the result of performing these actions in the loop body that should be returned. If the loop was passed and there were no break or contingency operators, it means we will go to Expression 3 and then to Expression 2 and if it is True, we will return it with the new iterator to the loop body.

 
MrBrooklin:

So originally the code was written without the loop operator, although I was then interested to try how to apply it.

Respectfully, Vladimir.

You can apply

  • PositionGetTicket returns a position ticket by position number (and we have a number) and having received the position ticket we can get its symbol and magik.

Go through all the tickets by number. And through the tickets find out the symbols of the position and compare with ours. Get the position magik and compare with ours.

if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetString(POSITION_SYMBOL)==_Symbol && 
PositionGetInteger(POSITION_MAGIC)==MagicNumber) // вот эта строчка

But only why.