Download MetaTrader 5

Position Size Returning Negative Value

To add comments, please log in or register
Use MQL5 Cloud Network. Test robots at a super speed!
Robert Martin
61
Robert Martin 2015.07.09 04:45 

Within my strategy, I use a function GetPosSize() to calculate position sizes. I'm only using the strategy with EURUSD at the time being and for the life of me I can't figure out why posSize is returning a negative value, resulting in an OrderSend error 4051. Any help would be greatly appreciated. I'll post all relevant code here. No other calculations towards posSize are done throughout the code, only the function is called. All three separate pieces of code are within the function.

double GetPosSize(string symbol, double pipsToSL)
{
   double marginReq;
   double marginAllot = AccountBalance()/Number_of_Pairs;
   double posSize;
   int pairsOpen = OpenOrderCount();
   int pairsNotOpen = Number_of_Pairs - pairsOpen;
   if(symbol == "EURUSD")
   {
      marginReq = 2600;
   }
if(pairsOpen == 0)
   {
      posSize = (marginAllot * (PercentRisk/100)/pipsToSL) / 10;
   }
NormalizeDouble(posSize,2);
   Print(posSize);
   return(posSize);
honest_knave
Moderator
2159
honest_knave 2015.07.09 06:04  

Try printing the values of marginAllot, PercentRisk and pipsToSL to narrow down where the negative number is coming from. My speculative guess is pipsToSL is being passed as a negative number under certain circumstances.

whroeder1
14470
whroeder1 2015.07.09 14:11  
  1. NormalizeDouble(posSize,2);
    This line does nothing.
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
Robert Martin
61
Robert Martin 2015.07.09 22:54  
honest_knave:

Try printing the values of marginAllot, PercentRisk and pipsToSL to narrow down where the negative number is coming from. My speculative guess is pipsToSL is being passed as a negative number under certain circumstances.

 

I was actually able to find some places in the rest of the code where pipsToSL was incorrectly calculated as negative. Thanks.

 

WHRoeder:
  1. This line does nothing.
  2. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong

 I've read of you posting about this before, but I didn't understand your point until your very last link. Thanks.

Robert Martin
61
Robert Martin 2015.07.09 23:00  

Why would I still be getting answers to multiple decimal places if I'm using MathRound()? I've tried using DoubleToString but that didn't help at all. Is that due entirely to the limitations of Print() or is there something else wrong?

 

posSize = posSize * 10;
MathRound(posSize);
posSize = posSize / 10;
whroeder1
14470
whroeder1 2015.07.10 01:03  
NoLimitations: Why would I still be getting answers to multiple decimal places if I'm using MathRound()
MathRound returns a double; infinite number of decimal places.
JD4
1101
JD4 2015.07.10 01:09  
WHRoeder:
NoLimitations: Why would I still be getting answers to multiple decimal places if I'm using MathRound()
MathRound returns a double; infinite number of decimal places.

Not according to the doc page for it. https://docs.mql4.com/math/mathround

"Return Value

Value rounded till to the nearest integer."

If it is supposed to but doesn't, then MQ needs to be notified, or they need to put a warning label on the function.

Robert Martin
61
Robert Martin 2015.07.10 01:26  
JD4 is right, the doc page says it rounds to the nearest integer. I'm not sure of another way to accomplish what I was trying to above without rounding or cutting off a certain number of decimal places. What would be the point of MathRound() or round() if they don't return a whole number?
JD4
1101
JD4 2015.07.10 01:38  
NoLimitations:
JD4 is right, the doc page says it rounds to the nearest integer. I'm not sure of another way to accomplish what I was trying to above without rounding or cutting off a certain number of decimal places. What would be the point of MathRound() or round() if they don't return a whole number?
You said MathRound was giving you fits.  Did you try the suggested alternate of just round()?
Robert Martin
61
Robert Martin 2015.07.10 01:42  
Yes I've used round(); it resulted in the exact same values. I tried to use MathFloor() or MathCeil() as a way to calculate the value less accurately and get around the problem for the time being, but both of those are seemingly doing nothing as well. I just want to turn a damn double into an int hahaha
Robert Martin
61
Robert Martin 2015.07.10 01:49  

I also just tried using this to no avail. Possibly DTS simply changes the accuracy of a value for appearance and not the actual value? I don't see any other options.

posSize = posSize * 100;
DoubleToString(posSize,0);
StringToDouble(posSize);
posSize = posSize / 100;
1234
To add comments, please log in or register