Download MetaTrader 5

Help getting position volume

To add comments, please log in or register
Eric Duran
160
Eric Duran  

Okay I know I can use PositionGetDouble(POSITION_VOLUME) to get the current order's position, and that works fine, but when I close the order it still returns the volume of the last position? Shouldn't it return 0?

Documentation on MQL5: Trade Functions / PositionGetDouble
  • www.mql5.com
Trade Functions / PositionGetDouble - Documentation on MQL5
Andriy Moraru
1259
Andriy Moraru  
cuallito:

Okay I know I can use PositionGetDouble(POSITION_VOLUME) to get the current order's position, and that works fine, but when I close the order it still returns the volume of the last position? Shouldn't it return 0?

 

 

If you do it immediately after sending close order then the position probably didn't close yet. You should wait for the position to really close before requesting a new volume.
Eric Duran
160
Eric Duran  
Hi, thanks for the reply. I have the command in the OnTick function of an EA and it still gives me the last volume of the position well after it's closed every tick.
Andriy Moraru
1259
Andriy Moraru  
cuallito:
Hi, thanks for the reply. I have the command in the OnTick function of an EA and it still gives me the last volume of the position well after it's closed every tick.
Wait, if the position is closed, how was you able to select it at all?
Eric Duran
160
Eric Duran  

I have something like 

 

int OnInit() {
    
    PositionSelect(_Symbol);

}

int OnTick() {

    Print(PositionGetDouble(POSITION_VOLUME));

}


The output I get is something like (time goes from bottom to top):

......end 

0.1

0.1 

0.1

0.1 <-----close position 

0.1 

0.1

0.1

0.1  <----buy here 

0

start.....

 

Shouldn't it return zero after I closed the position? 

Andriy Moraru
1259
Andriy Moraru  

Sorry, but that's a wrong way to do it. You should Select the position each time in the OnTick() function:


int OnTick() {

if (PositionSelect(_Symbol)) Print(PositionGetDouble(POSITION_VOLUME));
else Print("No position is currently open for " + _Symbol + ".");

}
Eric Duran
160
Eric Duran  
Thanks again!
To add comments, please log in or register