Errors, bugs, questions - page 9

 
DC2008:

It is not correct to use the == condition for variables of type double. It is recommended to compare like this:


I can add to that, that although with some delay, we still wrote an article on this topic - Features of working with numbers of type double in MQL4. Everything mentioned there will still be relevant for any programming language when it comes to operations with real numbers (double and float in MQL5).
Особенности работы с числами типа double в MQL4 - Статьи по MQL4
  • www.mql5.com
Особенности работы с числами типа double в MQL4 - Статьи по MQL4: примеры использования экспертов, тестирования и оптимизации
 
DC2008:

It is not correct to use the == condition for variables of type double. It is recommended to compare like this:


Quite reasonable, and most importantly a sensible approach. But it seems to me that at least the first line should have looked like this:

if (MathAbs(LotStep-0.01)<0.01)return(NormalizeDouble(Lot,2));
 

I get these messages in the log from time to time

2010.06.15 14:48:09 MemoryException 4915200 bytes not available

I don't know what to do ?

 
Prival:

I get these messages in the log from time to time

2010.06.15 14:48:09 MemoryException 4915200 bytes not available

I don't know what to do ?

Not enough RAM. Check size of dynamic arrays, most likely an attempt to allocate more memory than available.
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
Документация по MQL5: Основы языка / Типы данных / Объект динамического массива
  • www.mql5.com
Основы языка / Типы данных / Объект динамического массива - Документация по MQL5
 
DC2008:

It is not correct to use the == condition for variables of type double. It is recommended to compare like this:


That's not the point of the question, if I understood it correctly, you should always put the return operator at the end of a user function
 
joo:
Not enough op. memory. Check the size of the dynamic arrays, most likely an attempt to allocate more memory than is available.
No, that's not it. It's probably something with the servers. There's probably some work going on. A hole has appeared in the data. The connection to the server is often lost. The history is not being downloaded.
 
sergey1294:
That's not the point of the question. If I understood it correctly, the operator return must always be placed at the end of the user function

I think the answer about == was essentially. There really should be a return(...) somewhere in a function of type double. Not necessarily at the end. Using == operator in if() return(); may cause false output if you have the LotStep type 0.1000000000000000001. In this case none of the returns will work and the function returns nothing. Although in your case the return probably does not like NormalizeDouble inside. In your version that works, fix the end to return(NormilizeDouble(lot,2)) and see if it works. I'm curious about it myself.

 
gpwr:

I think the answer about == was essentially. There really should be a return(...) somewhere in a function of type double. Not necessarily at the end. Using == operator in if() return(); may cause false output if you have the LotStep type 0.1000000000000000001. In this case none of the returns will work and the function returns nothing. Although in your case the return probably does not like NormalizeDouble inside. In your version that works, fix the end to return(NormilizeDouble(lot,2)) and see if it works. I'm curious about it myself.

There must be return(...) at the end, but it may never get there (if one of the If conditions works)...
 
Interesting:
There must be return(...) at the end, but it may not come to it (if one of the If conditions works)...
That's what I was interested in, in 4 it was not necessary to put it at the end.
 

double volMin     =SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
double LotSize    =SymbolInfoDouble(_Symbol,SYMBOL_TRADE_CONTRACT_SIZE);
long   Leverage   =AccountInfoInteger(ACCOUNT_LEVERAGE);
double FreeMargin =AccountInfoDouble(ACCOUNT_FREEMARGIN);  
double LotRqdMgn  =LotSize/Leverage;
double vol=NormalizeDouble(MathFloor(FreeMargin*MaxRisk/LotRqdMgn/Step)*Step,2);
if(vol<volMin) vol=volMin;
if(vol*LotRqdMgn>FreeMargin) vol=0.0;
Print(LotRqdMgn," ",FreeMargin);
return(vol);
Renat
:

The mistake is that you forgot about the margin currency in your calculations. Balance = 100 USD and margin requirement = 100 EUR (119 USD).

That's why the operation cannot be performed - everything is correct.

I want to get back to my question about correct margin calculation. As Renat noticed in my calculations, LotRqdMgn (margin required to buy 1 lot) does not take into account the price of the currency. I remember having seen this table

Identifier

Description

Formula

SYMBOL_CALC_MODE_FOREX

Forex mode - calculation of profit and margin for Forex

Margin: Lots*Contract_Size/Leverage

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_FUTURES

Futuresmode - calculation of margin and profit for futures

Margin: Lots *InitialMargin*Percentage/100

Profit: (close_price-open_price)*TickPrice/TickSize*Lots

SYMBOL_CALC_MODE_CFD

CFD mode - calculation of margin and profit for CFD

Margin: Lots *ContractSize*MarketPrice*Percentage/100

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_CFDINDEX

CFD index mode - calculation of margin and profit for CFD by indexes

Margin: (Lots*ContractSize*MarketPrice)*TickPrice/TickSize

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_CFDLEVERAGE

CFD Leverage mode - calculation of margin and profit for CFD at leverage trading

Margin: (Lots*ContractSize*MarketPrice*Percentage)/Leverage

Profit: (close_price-open_price)*Contract_Size*Lots

So it turns out that there is an error in the table: instead of Lots*Contract_Size/Leverage, it must be Price*Lots*Contract_Size/Leverage.

Reason: