Download MetaTrader 5
To add comments, please log in or register
Find out the latest MQL5.community news!
Sherif Hasan
7222
Sherif Hasan 2015.11.23 12:48 
double value=0;
int ordegap=3;
int newsgap=3
value=value+ordergap*Point*PipValue+newsgap*Point*PipValue;

is the above code i use correctly? because i dont get correct value!
Carl Schreiber
5934
Carl Schreiber 2015.11.23 12:55  

1) ordergap*Point are points but what is PipValue - probably they don't match.

2) ordergap*Point*PipValue are e.g. $ - and value is it $ as well?

otherwise you 'compare apple with oranges'.

Keith Watford
Moderator
7887
Keith Watford 2015.11.23 13:06  

We don't know the value for PipValue.

We don't know what logic you are using or what you expect to achieve.

We don't know what value you get and what value you are expecting.

You need to give more details.

Your code is the same as

  double value=0;
  int ordegap=3;
  int newsgap=3
  value=(ordergap+newsgap)*Point*PipValue;

 simplified

I can't see any problem with casting as Point is a double 

Ali Akcaagac
169
Ali Akcaagac 2015.11.23 18:50  
value=value+ordergap*Point*PipValue+newsgap*Point*PipValue;

This looks like a simple solution. The first thing you should do is, check whether the Math rules are set properly. Point calculation usually comes before adding and subtracting calculation. This is the first thing that I would look up. I would also put some parenthesis "(" and ")" around the Math, to make sure that your expected calculations are correct.

E.g.

What do you calc here:

value + ordergap * Point * PipValue + newsgap * Point * PipValue

What is being added and what multiplicated ?

value + ( ordergap * Point ) * PipValue + ( newsgap * Point ) * PipValue

might return a different value than:

value + ordergap * ( Point * PipValue )  + newsgap * ( Point * PipValue )

Make sure you read yourself about operator priorities. That should solve the problem. 

whroeder1
13596
whroeder1 2015.11.24 12:55  
aakcaagac:

value + ( ordergap * Point ) * PipValue + ( newsgap * Point ) * PipValue

might return a different value than:

value + ordergap * ( Point * PipValue )  + newsgap * ( Point * PipValue )

False. Multiplication has higher priority then addition A * B * C + D * E * F = (A * B) * C + (D * E) * F = A * (B * C) + D *( E * F) = (A * B * C) + (D * E * F)

Ali Akcaagac
169
Ali Akcaagac 2015.11.24 13:26  
WHRoeder:

False. Multiplication has higher priority then addition A * B * C + D * E * F = (A * B) * C + (D * E) * F = A * (B * C) + D *( E * F) = (A * B * C) + (D * E * F)

Not false! I exactly wanted to point out that Multiplication has a highter priority than addition Otherwise I wouldn't have referenced the link to the "operand priorities". The point is: What does the initial poster want to achieve? If he pretends that his calculations return wrong values then it's most likely that "he" calculated it wrong. What he calculates on paper by his own does not necessarily met the priority or math requirements of operand priorities.
whroeder1
13596
whroeder1 2015.11.24 17:59  
 Wrong. "might return a different value than" is false. it make no difference in the order of multiplies. (1 * 2 ) * 3 = 2 * 3 = 6 = 1 * 6 = 1 * (2 * 3)
Ali Akcaagac
169
Ali Akcaagac 2015.11.24 18:10  
WHRoeder:
 Wrong. "might return a different value than" is false. it make no difference in the order of multiplies. (1 * 2 ) * 3 = 2 * 3 = 6 = 1 * 6 = 1 * (2 * 3)

You are right with what you explain. But that's not what I wanted to express in my initial reply. Though I might have chosen a better example.

As I tried to explain in my second reply, it depends on how the initial poster has made his calculus. It could be that the initial poster wasn't aware of the calculation rules or oversight that when doing his own calculus on dry run (e.g. on a piece of paper) and then afterwards tried bringing this in a formula.

As example:

He might have calculated

30 + 3 = 33

Summed up these values and later on multiplied it with a different value.

33 * 3 = 99

Then he might have taken these values into a formula and wondered why the result differs to those he initially calculated on paper:

30 + 3 * 3 = 39

Now he is sitting at home and can't figure out why the value 39 is not the expected sum of 99 that he initially tried to calculate... To whatever reasons...

Therefore I came up with the reference to operand priorities and the hint by using parenthesis in his calculations to give the stuff some priorities).

That might look as in our example like this:

(30 + 3) * 3 = 99

whroeder1
13596
whroeder1 2015.11.24 18:42  
aakcaagac: You are right with what you explain. But that's not what I wanted to express in my initial reply.
  1. I didn't disagree with the rest so didn't comment on it.
  2. Parentheses are important.
    4 > 3 > 2
     true > 2
       1  > 2
       false

/
To add comments, please log in or register