PositionSelect() function is returning false when I create 2 open positions(1 Buy and 1 Sell) for the same SYMBOL.

 

Hi All,

Can anyone here please tell me why the PositionSelect() function is returning false when I create 2 open positions(1 Buy and 1 Sell) for the same SYMBOL.. My objective is to manage each position's profit individually.

I am unable to do so as the function PositionSelect() is returning 'false'. When I create my first open position(Buy) the function PositionsTotal()=1. After creating a second position (Sell), PositionsTotal()=0 and eventually PositionSelect() returns false.


Does anyone have a solution for this situation?


Note: Both positions are for the same 'SYMBOL'.


Thank you,

Raj

 
raj2301 :

Hi All,

Can anyone here please tell me why the  PositionSelect() function is returning false when I create 2 open positions(1 Buy and 1 Sell) for the same SYMBOL.. My objective is to manage each position's profit individually.

I am unable to do so as the function PositionSelect() is returning 'false'. When I create my first open position(Buy) the function PositionsTotal()=1. After creating a second position (Sell), PositionsTotal()=0 and eventually PositionSelect() returns false.


Does anyone have a solution for this situation?


Note: Both positions are for the same 'SYMBOL'.


Thank you,

Raj

Show your code. Describe the situation at the time the code was run (how many positions are open, which positions are open).

 

I wrote a short example:

//+------------------------------------------------------------------+
//|                                PositionsTotal PositionSelect.mq5 | |
//+------------------------------------------------------------------+
#property version   "1.00"
//--- input parameters
input int      Input1=9;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ResetLastError();
   bool position_select=PositionSelect(Symbol());
   string text="";
   if(position_select)
      text="PositionSelect \"true\", selected position (ticket) "+IntegerToString(PositionGetInteger(POSITION_TICKET));
   else
      text="PositionSelect \"false\", ERROR: "+IntegerToString(GetLastError());
   Comment("Number of open positions: ",PositionsTotal(),"\n",text);
//---
  }
//+------------------------------------------------------------------+


Trading environment: BUY 0.03 and SELL 0.01 are open on the USDPLN symbol. The EA is launched on USDJPY. Result - everything works correctly 


 

Raj:

Hi Vladimir Karputov,thank you to get back but the PositionSelect is false is in your screenshot.

 
raj2301 :

Raj:

Hi  Vladimir Karputov ,thank you to get back but the PositionSelect is false is in your screenshot.

Everything is correct. I showed you that everything works absolutely correctly.

We carefully read:

Trading environment: BUY 0.03 and SELL 0.01 are open on the USDPLN symbol. The EA is launched on USDJPY

void OnTick()
  {
//---
   ResetLastError();
   bool position_select=PositionSelect(Symbol())

 Result - everything works correctly 


 
Vladimir Karputov:

Everything is correct. I showed you that everything works absolutely correctly.

We carefully read:

Trading environment: BUY 0.03 and SELL 0.01 are open on the USDPLN symbol. The EA is launched on USDJPY

 Result - everything works correctly 


The PositionSelect() function must return true. It's not the case bros. See the definition, it returns false only when it fails.


PositionSelect

Chooses an open position for further working with it. Returns true if the function is successfully completed. Returns false in case of failure. 

 
raj2301:

The PositionSelect() function must return true. It's not the case bros. See the definition, it returns false only when it fails.


PositionSelect

Chooses an open position for further working with it. Returns true if the function is successfully completed. Returns false in case of failure. 

I’m not your brother. Do you even read the help?

PositionSelect()

PositionSelect

Chooses an open position for further working with it. Returns true if the function is successfully completed. Returns false in case of failure. To obtain information about the error, call GetLastError().

bool  PositionSelect(
   string  symbol      // Symbol name
   );

Parameters

symbol

[in]  Name of the financial security.


Pay attention to the parameter

   string  symbol      // Symbol name


Now read ATTENTIVELY my example:

Trading environment: BUY 0.03 and SELL 0.01 are open on the USDPLN symbol. The EA is launched on USDJPY

void OnTick()
  {
//---
   ResetLastError();
   bool position_select=PositionSelect(Symbol())

 Result - everything works correctly 

Documentation on MQL5: Trade Functions / PositionSelect
Documentation on MQL5: Trade Functions / PositionSelect
  • www.mql5.com
Chooses an open position for further working with it. Returns true if the function is successfully completed. Returns false in case of failure. To obtain information about the error, call GetLastError(). If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case...
 
Vladimir Karputov:

I’m not your brother. Do you even read the help?

PositionSelect()

PositionSelect

Chooses an open position for further working with it. Returns true if the function is successfully completed. Returns false in case of failure. To obtain information about the error, call GetLastError().

Parameters

symbol

[in]  Name of the financial security.


Pay attention to the parameter


Now read ATTENTIVELY my example:

Trading environment: BUY 0.03 and SELL 0.01 are open on the USDPLN symbol. The EA is launched on USDJPY

 Result - everything works correctly 

Thank you Mr  Vladimir Karputov for your kind help.