Errors, bugs, questions - page 8

 
FEDOR_MQL:

Can I see the code, if you don't mind? I've tried it all ways, but it doesn't work.

I don't know whether it's easier to add the risk in percents of securities. I would like to use the code as an example and show the profitability of my trading robot.

I gave you the code that works without errors
 
sergey1294:

Each casevariant can be marked with an integer constant, a character constant or a constant expression. A constant expression may not include variables or function calls . Aswitch statement must be of integer type.
 
Valmars:
Each case variant can be marked with an integer constant, a character constant or a constant expression. A constant expression may not include variables or function calls. A switch statement must be of integer type.
This has already been dealt with.
 
sergey1294:
I gave you code that works without errors
You're right, the rest of the code should have been thrown out instead of being saved...
 

I'm wondering why a construction like this doesn't work, it gives an error - '}' - not all control paths return a value

 //+------------------------------------------------------------------+
#property copyright "2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//--- input parameters

input double   Lots=0.1;
input bool     MM = true;
input double   Risk = 10.0;

double lots;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(MM) lots = LotSon();      //вызов функции LotSon 
   else lots = Lots;
  }
//+------------------------------------------------------------------+
double LotSon()
{
  double LotMin     = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
  double LotMax     = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
  double LotStep    = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
  double Lot        = AccountInfoDouble(ACCOUNT_FREEMARGIN)/100000.0 * Risk;
  Lot               = MathMin(LotMax,MathMax(LotMin,Lot));
  
  if (Lot < LotMin) Lot = LotMin;
  if (Lot > LotMax) Lot = LotMax;

  if (LotStep == 0.01)return(NormalizeDouble(Lot,2));
  if (LotStep == 0.1) return(NormalizeDouble(Lot,1));
  if (LotStep == 1.0) return(NormalizeDouble(Lot,0));
}
 
sergey1294:
I gave you the code that works without errors.
Sorry Sergei, I somehow missed it. I found it now. Thank you all so much I didn't think it would take so long.
 
sergey1294:

I'm wondering why a construction like this doesn't work, it gives an error - '}' - not all control paths return a value


What do you think this function should return? The way I see it
return(Lot);
 
Interesting:
What do you think this function should return? The way I see it -

It doesn't work like this.


  if (LotStep == 0.01)return(NormalizeDouble(Lot,2));
  if (LotStep == 0.1) return(NormalizeDouble(Lot,1));
  if (LotStep == 1.0) return(NormalizeDouble(Lot,0));

This is how it works.

  if (LotStep == 0.01)Lot=NormalizeDouble(Lot,2);
  if (LotStep == 0.1) Lot=NormalizeDouble(Lot,1);
  if (LotStep == 1.0) Lot=NormalizeDouble(Lot,0);

  return(Lot); 

	          
 
sergey1294:

It doesn't work like this

Right and it doesn't work. It's up to everyone, but I personally have always believed that return() should be declared at the end of any function, and the compiler supports me in this.


PS

Personally, back in MQL4 I started using Result variable to calculate the return value. I should note that calling return() at the end of function code is mandatory and its presence in the beginning/middle of code will sometimes be perceived as some analogue of break (the only difference is that break itself isn't perceived by the compiler as a command to return the value of function's result).

 

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

double LotSon()
{
  double LotMin     = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
  double LotMax     = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
  double LotStep    = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
  double Lot        = AccountInfoDouble(ACCOUNT_FREEMARGIN)/100000.0 * Risk;
  Lot               = MathMin(LotMax,MathMax(LotMin,Lot));
  
  if (Lot < LotMin) Lot = LotMin;
  if (Lot > LotMax) Lot = LotMax;

  if (MathAbs(LotStep-0.01)<0.001)return(NormalizeDouble(Lot,2));
  if (MathAbs(LotStep-0.1)<0.001) return(NormalizeDouble(Lot,1));
  if (MathAbs(LotStep-1.0)<0.001) return(NormalizeDouble(Lot,0));
  return(-1);
}
Документация по MQL5: Основы языка / Типы данных / Вещественные типы (double, float)
Документация по MQL5: Основы языка / Типы данных / Вещественные типы (double, float)
  • www.mql5.com
Основы языка / Типы данных / Вещественные типы (double, float) - Документация по MQL5
Reason: