Help getting position volume

 

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
 
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.
 
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.
 
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?
 

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? 

 

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 + ".");

}
 
Thanks again!