MQL5 : Writing a function to get Position Netting by Magic and Symbol

 

Hi all,

I am writing a function to get the netting for a particular magic and symbol. 

But somehow I get  a very minute amout (0.00000000000000004) at the end of the amount even though the answer should just be 0.3.

Can anyone explain why's that happening?

Here's the Position Data :


Symbol Position Time Type Volume Price S / L T / P Market Price Swap Profit Comment
EURUSD 2012969250 2022.11.03 17:09:12 buy 0.1 0.97472 0.97865 0.00 40.16
EURUSD 2012969252 2022.11.03 17:12:38 sell 0.1 0.97533 0.97867 0.00 -34.13
EURUSD 2012969258 2022.11.03 17:08:14 sell 0.1 0.97510 0.97867 0.00 -36.48
EURUSD 2012969259 2022.11.03 17:08:10 buy 0.1 0.97501 0.97865 0.00 37.19
EURUSD 2012969260 2022.11.03 17:08:15 buy 0.1 0.97520 0.97865 0.00 35.25
EURUSD 2012969261 2022.11.03 17:08:07 sell 0.1 0.97494 0.97867 0.00 -38.11
EURUSD 2012969262 2022.11.03 17:08:31 sell 0.1 0.97487 0.97867 0.00 -38.83
0.00 -34.95

Here's the print result :

2022.11.03 21:44:20.910 Test (EURUSD,D1) Cycle Data is 0.1

2022.11.03 21:44:20.910 Test (EURUSD,D1) Cycle Data is 0.1

2022.11.03 21:44:20.910 Test (EURUSD,D1) Cycle Data is 0.1

2022.11.03 21:44:20.910 Test (EURUSD,D1) volLong vShort 0.30000000000000004 0.4

2022.11.03 21:44:20.910 Test (EURUSD,D1) Data is -0.09999999999999998



Here's the code :


 

// If negative means net short
double GetPositionNettingByMagicAndSymbol(ulong magicNumber, string symbol){
   
   double result = 0.0;
   double volLong =0;
   double volShort =0 ;
   
   
   double totalPositions = PositionsTotal();
   
   int symbolDigits = (int) SymbolInfoInteger(symbol,SYMBOL_DIGITS);
   
   for(int i=0;i<totalPositions;i++)
     {
         if (PositionGetTicket(i))
           if( (PositionGetInteger(POSITION_MAGIC) == magicNumber) && (PositionGetString(POSITION_SYMBOL) == symbol)){
               if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY )
               {
                     volLong += NormalizeDouble( PositionGetDouble(POSITION_VOLUME), symbolDigits);
                     Print("Cycle Data is ",PositionGetDouble(POSITION_VOLUME));
               }
               else
                     volShort += NormalizeDouble( PositionGetDouble(POSITION_VOLUME), symbolDigits);
           }
      
     }
   
   Print("volLong vShort ", volLong, " ", volShort  );
   
   result = volLong - volShort;
   return result;
   
 
dhermanus: But somehow I get  a very minute amout (0.00000000000000004) at the end of the amount even though the answer should just be 0.3.

Floating-point has an infinite number of decimals, it's you, not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.)
          Double-precision floating-point format - Wikipedia

See also The == operand. - MQL4 programming forum (2013)

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum (2016)

 

Hi  William thanks for the reply.   I  just  need to  instantiate the my double variable to 0.0 instead of just 0 because then there's a conversion from int to double which may produce inaccuracies.

 
dhermanus #:Hi  William thanks for the reply.   I  just  need to  instantiate the my double variable to 0.0 instead of just 0 because then there's a conversion from int to double which may produce inaccuracies.

No, that has nothing to do with the issue. You need to take some time to understand how binary floating point numbers work and that they as "binary" cannot completely represent normal "decimal" number very easily. There will always be slight misrepresentations and rounding errors.

To better work with these numbers, in the case of volume, you should align them to the SYMBOL_VOLUME_STEP value.

Forum on trading, automated trading systems and testing trading strategies

Volume Limit Reached - Validation for new Expert Advisor error

Fernando Carreiro, 2022.07.22 18:22

Your EA must be coded to read the broker's contract specifications, such volume limitations, and prevent that from happening.

SYMBOL_VOLUME_MIN

Minimal volume for a deal

double

SYMBOL_VOLUME_MAX

Maximal volume for a deal

double

SYMBOL_VOLUME_STEP

Minimal volume change step for deal execution

double

SYMBOL_VOLUME_LIMIT

Maximum allowed aggregate volume of an open position and pending orders in one direction (buy or sell) for the symbol. For example, with the limitation of 5 lots, you can have an open buy position with the volume of 5 lots and place a pending order Sell Limit with the volume of 5 lots. But in this case you cannot place a Buy Limit pending order (since the total volume in one direction will exceed the limitation) or place Sell Limit with the volume more than 5 lots.

double

Forum on trading, automated trading systems and testing trading strategies

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

// Variables for Symbol Volume Conditions
double
   dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
   
// Variables for Geometric Progression
double
   dblGeoRatio = 2.8,
   dblGeoInit  = dblLotsMinimum;
   
// Calculate Next Geometric Element
double
   dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );
   
// Adjust Volume for allowable conditions
double
   dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
                   fmax( dblLotsMinimum,                                   // Prevent too smaller volume
                     round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value

Forum on trading, automated trading systems and testing trading strategies

Looking for indicator that gives the info tp of the globality of open trades please

Fernando Carreiro, 2022.10.12 16:05

No such indicator exists as far as I known. It will have to be coded (use the Freelance section), but it will be easier to just have your existing EA be modified to show that target on the chart using the following maths:

How to Calculate the Net Resulting Equivalent Order
    • vi = volume of individual position
    • oi = open price of individual position
    • ci = close price of individual position
    • Vn = total volume for a basket of positions
    • On = net mean open price for a basket of positions
    • Cn = net mean close price for a basket of positions
    • PLn = profit/loss for a basket of positions

    Reason: