MQL5 type conversion

 

Hi

I think i get type conversion warning because ulong has no negative value possible, but the calculation could finish negative...
(For understanding the fSpot is a double with the actual price witch is turned in his one magicnumber by multiplying it with 1000000)

...i know it will not, but how to code this right to avoid unexpected roundingproblems and to ensure the number is right.


//fSpot is actual Ask Price

//_____________________________________________________________________________________________________________________________

bool NewTrendTrade(double fSpot){//Place new trendtrade LONG
//=============================================================================================================================
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);
ZeroMemory(result);

ulong fMagicNumber = NormalizeDouble(fSpot * 1000000, 0) + 1; // <== HERE I GET CONVERSION PROBLEMS ??

bool  fOrderSendFAIL = false;

request.action    = TRADE_ACTION_DEAL;
request.type      = ORDER_TYPE_BUY;
request.symbol    = Symbol();
request.volume    = StartLot;
request.price     = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
request.sl        = 0;
request.tp        = 0;      
request.comment   = "LONG  " + IntegerToString(fMagicNumber) + "  ○  ";
request.deviation = Deviation;
request.magic     = fMagicNumber;  

fOrderSendFAIL = OrderSend(request,result);

//... ..  .




thanx ;-)

 
ulong fMagicNumber = (ulong)NormalizeDouble(fSpot * 1000000, 0) + 1;
 
Hi

Returns: ')' - unexpected token

Can you explain me your thinking maybe then i understand more. thanx
 

It is called typecasting.

That error isn't caused by the (ulong)... remove it and you will most likely see that the "unexpected token" is still there (else you didn't copy the line correctly?)

Typecasting - Data Types - Language Basics - MQL4 Reference
Typecasting - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Typecasting - Data Types - Language Basics - MQL4 Reference
 
I already understand typecasting so far.

I know that ulong is positiv and has no comma. I also know that when i multiply
the double fSpot (5 digits) with 1000000 ( fSpot is for example 0.12345 * 1000000 = 123450)
i will end wit a ulong - the question is how must i code it ...

When i look for magicnumber variable they suggest ulong.

is there something like "DoubleToString" for ulong like DoubleToUlong or so ... i did not find
anything in the dokumentation thats why i ask in forum.
 

By the way ...

 

i have the same typcasting problem with this function

 

//_____________________________________________________________________________________________________________________________
int MagicXX(ulong fMagic){ // Ich gebe dir die letzten 2 Digits einer int zurück
//=============================================================================================================================
int ret = fMagic - (100 * NormalizeDouble((fMagic /100),0));

return(ret);
}//MagicXX()~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Both are the same a double to ulong/int problem.

So how to do  :D

 

I'm not sure I understand the problem.

You want a magic number which is based on fSpot.

You say fSpot is price. Price is never negative. So no problem with unsigned.

You want to push a double value into a ulong. No problem there - you're not going to get into the range limits of either ulong or double with what you are proposing.

Is there any need for NormalizeDouble at all? Let the typecast trim off the decimal places.

ulong fMagicNumber = ulong(fSpot * 1000000)+1;

They are only warnings in the compiler, to make sure you are aware that the types are different. To stop the warning, you tell the compiler you are aware. You do this by typecasting.

 
Now the MAGIC happend yea!!

So just to round it up this is wath i understand: int(operation) ; ulong(operation) ;... and so on will tell the compiler that i am certain of the inQuote operation, an my compilerInfo's stay clean. 

COOl it works - Thank you
Reason: