Position volume and PositionsTotal swapped

 

It seems that position volume and PositionsTotal are switched or something. Running this code, which just buys twice then sells once leaves POSITION_VOLUME at 1 after the first order is filled. However PositionsTotal() increases after each order (including the final sell order) to 3. I'm a newbie, so I'm sure I'm missing something, but can't see what it is. 


  #include <Trade\Trade.mqh>
   CTrade Trade;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

Trade.SetExpertMagicNumber(51482);

double TolPoss = PositionsTotal(); // TolPoss is 0

Trade.PositionOpen(Symbol(), ORDER_TYPE_BUY, 1, SymbolInfoDouble(Symbol(),SYMBOL_ASK), 0, 0);

PositionSelect(Symbol());
double vol = PositionGetDouble(POSITION_VOLUME);  // vol is 1.0
TolPoss = PositionsTotal();                       // TolPoss is 1.0

Trade.PositionOpen(Symbol(), ORDER_TYPE_BUY, 1, SymbolInfoDouble(Symbol(),SYMBOL_ASK), 0, 0);

PositionSelect(Symbol());
vol = PositionGetDouble(POSITION_VOLUME);   // vol is 1.0
TolPoss = PositionsTotal();                 // TolPoss is 2

Trade.PositionOpen(Symbol(), ORDER_TYPE_SELL, 1, SymbolInfoDouble(Symbol(),SYMBOL_BID), 0, 0);

PositionSelect(Symbol());
vol = PositionGetDouble(POSITION_VOLUME);    // vol is 1.0
TolPoss = PositionsTotal();                  // TolPoss is 3
   
   
//---
   return(INIT_SUCCEEDED);
  }
 

You must understand the difference between 'heading' and 'netting' a merchant account.

More details in the help: Basic Principles - Trading Operations

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
 
Thanks. That helps a lot. A bit surprised because this documentation makes it clear that there's only one position per symbol.