Metatrader 5 - Function (PositionSelectByIndex) Error/Missing

 

Hello,

I just download the official Metatrader 5.

When I coded, I received the error: 'PositionSelectByIndex' - undeclared identifier

In the file Trade.mqh, I don't see any reference to the  PositionSelectByIndex.

I'm using a pure MQL5 environment, in windows software. And I don't install MT4 in my PC.

Someone could help me?

Attached the Trade.mqh installed by the MT5 download.

I am using the version 5.00 build 4755 from 13 Dec 2024

Thank you!
Pedro

Files:
Trade.mqh  68 kb
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

The file you provided is not your code. It is part of the MQL5 Standard Library and we all have it on our systems.

If you need help with your code, then please show your code, and use the CODE button (Alt-S) when inserting code.

Code button in editor


 
Pedro Matos: When I coded, I received the error: 'PositionSelectByIndex' - undeclared identifier

There is no such MQL5 function. Please reference the documentation ...

PositionGetSymbol

Returns the symbol corresponding to the open position

PositionSelect

Chooses an open position for further working with it

PositionSelectByTicket

Selects a position to work with by the ticket number specified in it


For selection by an index, use PositionGetSymbol, which selects by the index and returns the symbol of the selected position.

Documentation on MQL5: Trade Functions / PositionGetSymbol
Documentation on MQL5: Trade Functions / PositionGetSymbol
  • www.mql5.com
Returns the symbol corresponding to the open position and automatically selects the position for further working with it using functions...
 

I suggest you read the online book ...

As well as the following article ...

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
MQL5 programming for traders - Book on MQL5.com
MQL5 programming for traders - Book on MQL5.com
  • www.mql5.com
Modern trading relies heavily on computer technology. Automation now extends beyond the boundaries of exchanges and brokerage offices, becoming...
 
Fernando Carreiro #:

There is no such MQL5 function. Please reference the documentation ...

PositionGetSymbol

Returns the symbol corresponding to the open position

PositionSelect

Chooses an open position for further working with it

PositionSelectByTicket

Selects a position to work with by the ticket number specified in it


For selection by an index, use PositionGetSymbol, which selects by the index and returns the symbol of the selected position.

Thank you, Fernando for your help.

Have a nice year!!

Pedro


 

Sounds like you're using Gemini to create MQL5, it thinks that is a real function in MQL5, it basically hallunicates it.

When it generates the code with that non-existent function tell it:

There is no such function as PositionSelectByIndex in MQL5. Recreate the code without using this


Which will get a response like:

You are absolutely correct! My apologies for that oversight. PositionSelectByIndex is indeed not a valid function in MQL5.

I have updated the code in the mql5_trading_automation document to correctly select the open position using PositionGetTicket and PositionSelectByTicket , which is the proper way to handle position selection in MQL5. Since the logic specifies only one trade is open at a time, we can reliably get the first (and only) position's ticket.

Here is the updated code:


You still need to learn MQL5 though otherwise you'll never be able to find out why the AI version doesn't work in the event that it fails to compile or produce the results you want.