Download MetaTrader 5

Double to int

To add comments, please log in or register
m_shafiei2006
147
m_shafiei2006  

How do I write this function without "possible loss of data due to type conversion" warning ?


int S10;
int StepIndex(int PosIndex)
    {
       if(OrderSelect(PosIndex,SELECT_BY_POS,MODE_TRADES) == true)
       S10 = NormalizeDouble(((Bid-OrderOpenPrice())/10*Point),0);
       return(S10);
    }
Drazen Penic
2737
Drazen Penic  

S10 is an integer variable and you assign a double value to it and compiler warns you that double value will be truncated. 

To eliminate that warning (if you really want to truncate result), you must cast the result of a function to an integer. This will show to the compiler that you really want that to happen. 


S10 = (int) NormalizeDouble(((Bid-OrderOpenPrice())/10.0*Point),0);
m_shafiei2006
147
m_shafiei2006  

thank you very much indeed, I searched a lot.

so the parenthesis means that "hey MQL! I know it convert double to int". haha...

Fernando Carreiro
3413
Fernando Carreiro  
m_shafiei2006:

How do I write this function without "possible loss of data due to type conversion" warning ?

  1. "OrderSelect()" already returns a boolean value, so why compare it another boolean value?
  2. Use "OrderClosePrice()" and not "Bid" because you don't know the OrderType.
  3. Don't use "NormaliseDouble()". Use "round()" or "MathRound()".
  4. Be careful with the order in which operators are applied, so use parenthesis to make sure.
  5. I assume the "10" is to adjust for Pips, but remember that brokers can also be 4 digit and not all are 5 digit.
  6. Divide by a floating point number and not an Integer, otherwise you will experience truncation.
  7. Finally type cast it to an integer, but remember that if you are using pips, you will be loosing the decimal value of the pipettes
  8. S10 is already a global variable so why return that value in the function.
  9. As I pointed out in another thread, do some learning about basic programing first.
    int S10;
    void StepIndex(int PosIndex)
    {
       if( OrderSelect( PosIndex, SELECT_BY_POS, MODE_TRADES ) )
          S10 = (int) round( ( OrderClosePrice() - OrderOpenPrice() ) / ( _Point * 10.0 ) );
    }
Fernando Carreiro
3413
Fernando Carreiro  
m_shafiei2006:

thank you very much indeed, I searched a lot.

so the parenthesis means that "hey MQL! I know it convert double to int". haha...

NO! Its called typecasting. You can learn about these things by first learning how to code in "C" for example.
m_shafiei2006
147
m_shafiei2006  

1. yes you are right.

2. why OrderClosePrice, it is in TRADES not HISTORY ? besides I need to subtract last current price from OrderOpenPrice, whatever its OrderType is.

3. I want cut off everything after dot, even it would be 9.

4. OK

5. I set this as an extern to change it on start, and it is 5 digits, what if it would be 4 digits ?

6. I chnged it to 10.0. is it ok?

7.OK

8. I  just declare S10. and need the value in my EA, what did I wrong ?

9. I started learning since 2 weeks age and I know the basics and trying to make simple EAs, but the points that you mention was not in my tutorial. :))


thank you that you take time for me

Fernando Carreiro
3413
Fernando Carreiro  
m_shafiei2006:

2. why OrderClosePrice, it is in TRADES not HISTORY ? besides I need to subtract last current price from OrderOpenPrice, whatever its OrderType is.

For an Open order, "OrderClosePrice()" is the current price. "Buy" orders open at Ask and close at Bid. "Sell" orders open at Bid and close at Ask. Since your code does not test which type it is, you must use "OrderClosePrice()".
Fernando Carreiro
3413
Fernando Carreiro  
m_shafiei2006:

3. I want cut off everything after dot, even it would be 9.

Then use "round()", or "floor()" or "ceil()" depending on what you need.

Fernando Carreiro
3413
Fernando Carreiro  
m_shafiei2006:

9. I started learning since 2 weeks age and I know the basics and trying to make simple EAs, but the points that you mention was not in my tutorial. :))

thank you that you take time for me

Overall you are missing the main point. It is not a question of fixing "bugs", but that in general you don't really understand the main concepts and constructs that you are using.

So, until you do. First practice by coding small pieces of "C" code that is independent of it working in Meta-trader and as a EA. Use online resources that go though all the steps from beginner to a higher level of coding in "C".

Drazen Penic
2737
Drazen Penic  
m_shafiei2006:

thank you very much indeed, I searched a lot.

so the parenthesis means that "hey MQL! I know it convert double to int". haha...

You missed the joke here.

The hilarious thing is when you, for example, calculate stop loss price for the EUR/USD and your function instead of 1.14001 returns integer 1 and your account burns because of that.

After you suffer a huge loss, you'll learn why this warning should be carefully investigated and why is it a good thing that compiler asks you to explicitly confirm that you want to truncate double to integer. 

m_shafiei2006
147
m_shafiei2006  
drazen64:

You missed the joke here.

The hilarious thing is when you, for example, calculate stop loss price for the EUR/USD and your function instead of 1.14001 returns integer 1 and your account burns because of that.

After you suffer a huge loss, you'll learn why this warning should be carefully investigated and why is it a good thing that compiler asks you to explicitly confirm that you want to truncate double to integer. 

 

Yes you are right, but I need the cast that you mention for another purpose.
12
To add comments, please log in or register