Position Size Returning Negative Value

 

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);
 

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.

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

 

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

 
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?
 
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()?
 
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
 

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;