Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 565

 
PolarSeaman:

You substituted in the code

to

but it didn't help.)

I'm sorry that's all you noticed)

 
Alekseu Fedotov:

FileSeek() with SEEK_END flag, will help you.

<

Thank you! I'll give it a try!

 
YanSay:

Good evening!

I am puzzling over how to add a universal (for different instruments) code to calculate the lot for a deal based on % of deposit.

I made it like this:

Price ( opening price) and SL (Stop Loss) are calculated separately.

For pairs where quote currency is in dollars (eg EURUSD), for index SPX500 and for gold - all correctly calculated, but for pairs where the dollar is the first in a quote (eg USDJPY) does not work.

Please advise what I missed?

Pay attention to normalization

The _Digits variable stores the number of decimals after the decimal point which determines price precision of the current chart symbol.

 
Alekseu Fedotov:

Note the normalisation

The _Digits variable stores the number of decimal places after the decimal point which determines the accuracy of the current chart symbol price.

Thank you! Here is how it has been re-done:

input double MaximumRisk=0.02;                  //Риск в сделке от депозита

//1ый вариант
{Lots = NormalizeDouble(((AccountBalance()*MaximumRisk)/((MathAbs(Price-SL))/Point)/((MarketInfo(Symbol(),MODE_LOTSIZE)*(MarketInfo(Symbol(),MODE_ASK)+Point))
-(MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_ASK)))),
int(MathAbs(log(MarketInfo(Symbol(), MODE_LOTSTEP)))));}

//2ой вариант
{Lots = NormalizeDouble((AccountBalance()*MaximumRisk)/((MathAbs(Price-SL))/Point)/((MarketInfo(Symbol(),MODE_TICKVALUE))),
int(MathAbs(log(MarketInfo(Symbol(), MODE_LOTSTEP)))));}

In the 1st variant: EURUSD and XAUUSD - everything is correct; USDJPY - instead of 2% of deposit it risks 0.2% of the deposit; in case of SPX500 and BRN it generates OrderSend error 131 (error in lot calculation).

In the 2nd variant: EURUSD, XAUUSD and USDJPY - everything is correct; SPX500 risks 20% of the deposit instead of 2%; for BRN, it generates OrderSend error 131 (an error with lot calculation).

There is an error somewhere else, I can not understand in what place, please advise.

 
YanSay:

Thank you! Here's how it's re-done:

The first version: EURUSD and XAUUSD - everything is correct; USDJPY - it risks 0,2% of deposit instead of 2% of deposit; also in case of SPX500 and BRN it generates OrderSend error 131 (lot calculation error).

In the second case: EURUSD, XAUUSD and USDJPY - everything is correct; SPX500 risks 20% of the deposit instead of 2%; for BRN, it generates OrderSend error 131 (an error with lot calculation).

Somewhere else there is an error, I can not understand in what place, please advise.

ReplacePoint, withMarketInfo(Symbol(),MODE_POINT)

 
Vitaly Muzichenko:

ReplacePoint, withMarketInfo(Symbol(),MODE_POINT)

What is the point? If everything is calculated by chart symbol. It makes sense if there is a multivariable and Symbol different from the current chart is calculated.

 
Vitaly Muzichenko:

ReplacePoint, withMarketInfo(Symbol(),MODE_POINT)

Unfortunately, it didn't help(

MODE_POINT information says "Point size in the quote currency. It is stored in the predefined variable Point for the current symbol".

Judging by the description, it doesn't matter which variant to use. But I tried it and it didn't help.


 

Can you tell me whether it is possible to put a "selection" of a graphical tool on the button in the Expert Advisor, so that by clicking on the button the tool icon would appear under the cursor and it would be possible to stretch it?

And the channel should already have specified properties

 
Roman Sharanov:

Can you tell me whether it is possible to put a "selection" of a graphical tool on the button in the Expert Advisor, so that by clicking on the button the tool icon would appear under the cursor and it would be possible to stretch it?

Besides, the channel can already have specified properties.

Perhaps

 
YanSay:

Unfortunately it didn't help(

And in the MODE_POINT information it says "Point size in quote currency. For the current symbol is stored in the predefined variable Point".

Judging by the description, it doesn't matter which variant to use. But I tried it and it didn't help.

Try the function:

//===============================================================================================
//------------------------------ Расчет лота по риску на StopLoss -------------------------------+
//===============================================================================================
double sLot(double Percent=1, double Stloss=100, string symb="0") {
 if(symb=="0") symb=Symbol();
 double TickValue   =SymbolInfoDouble(symb,SYMBOL_TRADE_TICK_VALUE),
        TickSize    =SymbolInfoDouble(symb,SYMBOL_TRADE_TICK_SIZE),
        ContractSize=SymbolInfoDouble(symb,SYMBOL_TRADE_CONTRACT_SIZE),
        Min_Lot     =SymbolInfoDouble(symb,SYMBOL_VOLUME_MIN),
        Max_Lot     =SymbolInfoDouble(symb,SYMBOL_VOLUME_MAX),
        Step        =SymbolInfoDouble(symb,SYMBOL_VOLUME_STEP),
        Free        =AccountInfoDouble(ACCOUNT_FREEMARGIN),
        Lots_New    =0.0;
 int CalcMode=(int)SymbolInfoInteger(symb,SYMBOL_TRADE_CALC_MODE);

  if(Percent > 100) Percent = 100;
  if(Stloss <=0) return(0);
  if(Percent == 0) Lots_New = Min_Lot;
   else {
    if(CalcMode==0 || CalcMode==4)
      Lots_New = MathFloor((((Free*Percent/100)/Stloss)/TickValue)/Step)*Step;
    if(CalcMode==1||CalcMode==2||CalcMode==3)
      Lots_New = MathFloor(((((Free*Percent)/100)/Stloss)/((TickSize*TickValue)*ContractSize/TickValue))/Step)*Step;
   }
   if(Lots_New > Max_Lot) Lots_New = Max_Lot;
   if(Lots_New < Min_Lot) return(0);
  return(NormalizeDouble(Lots_New,/*LotDigit()*/ 2));
 }
Reason: