Help me with a code

 

I am finally trying to take the first steps with programming and I am inserting a lot calculator system on one of my EAs that does not have one. The code is this.

extern double  LotSize = 0.01;
extern   bool    UseMM = false;
extern    int     Risk = 1;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
   void OnTick()
   {
   if(UseMM==true) LotSize=Lots();
   }
//+------------------------------------------------------------------+
//| Money Management function                                        |
//+------------------------------------------------------------------+
double Lots()
  {
   double lot=MathCeil(AccountFreeMargin()*Risk/1000)/100;
   if(lot<MarketInfo(Symbol(),MODE_MINLOT))
      lot=MarketInfo(Symbol(),MODE_MINLOT);
   if(lot>MarketInfo(Symbol(),MODE_MAXLOT))
      lot=MarketInfo(Symbol(),MODE_MAXLOT);

   return(lot);
  }
//+------------------------------------------------------------------+


Unfortunately, while changing the various parameters and the balance, it continues to open positions from 1 lot. This is unthinkable in reality if the account has an available margin of 500 USD.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Please edit your post and use the code button (Alt+S) to paste code.
 
Keith Watford:
Please edit your post and use the code button (Alt+S) to paste code.


I didn't know that. done

 
Giovanni Salatto:

I am finally trying to take the first steps with programming and I am inserting a lot calculator system on one of my EAs that does not have one. The code is this.


Unfortunately, while changing the various parameters and the balance, it continues to open positions from 1 lot. This is unthinkable in reality if the account has an available margin of 500 USD.

Don't think you can write an EA to change the operation of another EA ?
 
andrew:
Don't think you can write an EA to change the operation of another EA ?

He said that he is writing code to insert into another EA

 
Giovanni Salatto:

I am finally trying to take the first steps with programming and I am inserting a lot calculator system on one of my EAs that does not have one. The code is this.


Unfortunately, while changing the various parameters and the balance, it continues to open positions from 1 lot. This is unthinkable in reality if the account has an available margin of 500 USD.

double lot=MathCeil(AccountFreeMargin()*Risk/1000)/100;

I don't know what you are trying to achieve here

MathCeil(AccountFreeMargin()*Risk/1000)

If Risk==1

any free margin below 1000, 1 will be returned

between 1000 and 2000, 2 will be returned

etc

Then you divide that by 100

Is that what you want?

 
Giovanni Salatto:

I am finally trying to take the first steps with programming and I am inserting a lot calculator system on one of my EAs that does not have one. The code is this.


Unfortunately, while changing the various parameters and the balance, it continues to open positions from 1 lot. This is unthinkable in reality if the account has an available margin of 500 USD.

Try like something that...

extern double  LotSize = 0.01;
extern   bool    UseMM = false;
extern    int     Risk = 1;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
   void OnTick()
   {
   if(UseMM==true) LotSize=NormalizeLot(((AccountBalance()*AccountLeverage())/100000000)*Risk); else LotSize=NormalizeLot(LotSize);
   }



double NormalizeLot(double _LotsSize)
{
  //---------------------------------------------------------------------
  if(IsConnected())
  {
  return(MathMin(MathMax((MathRound(_LotsSize/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT)));
  }
  else
  {
  return(NormalizeDouble(_LotsSize,2));
  }
  //---------------------------------------------------------------------
}
 
'NormalizeLot' - function not defined   Blue&Jellow_EA 4.0_2.mq4        164     28
'NormalizeLot' - function not defined   Blue&Jellow_EA 4.0_2.mq4        164     110
'NormalizeLot' - function declarations are allowed on global, namespace or class scope only     Blue&Jellow_EA 4.0_2.mq4        169     8

Thanks everyone for the tips. I get these errors on compilation. I don't understand, the function seems defined to me.

 
Giovanni Salatto:

Thanks everyone for the tips. I get these errors on compilation. I don't understand, the function seems defined to me.

You probably have mismatched { }

 
Keith Watford:

I don't know what you are trying to achieve here

If Risk==1

any free margin below 1000, 1 will be returned

between 1000 and 2000, 2 will be returned

etc

Then you divide that by 100

Is that what you want?


I also tried with a margin of 5000 USD, setting the risk at 0.5, but unfortunately the result is always 1 lot.

 
Keith Watford:

You probably have mismatched { }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

   {
   if(UseMM==true) LotSize=NormalizeLot(((AccountBalance()*AccountLeverage())/100000000)*Risk); else LotSize=NormalizeLot(LotSize);
   }

//+-----------------------------------------------------------

double NormalizeLot(double LotsSize)
{
  //---------------------------------------------------------------------
  if(IsConnected())
  {
  return(MathMin(MathMax((MathRound(LotsSize/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)),MarketInfo(Symbol(),MODE_MINLOT)),MarketInfo(Symbol(),MODE_MAXLOT)));
  }
  else
  {
  return(NormalizeDouble(LotsSize,2));
  }
  //---------------------------------------------------------------------

This is the code that gives the three errors

Reason: