Download MetaTrader 5

How to change a code to set up a start lots?

To add comments, please log in or register
MQL5 language supports OpenCL. Try it!
Slawomir Dziegielewski
246
Slawomir Dziegielewski 2016.08.10 17:02 
double getLots(double lt) 
  (
   double marginrequired = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double freemargin = AccountFreeMargin();
   if(freemargin > (marginrequired * lt)) 
     {
    
       return(lt);
     } 
   double result = freemargin / marginrequired;
   result = MathFloor(result * 10) / 10;
   return(result);
  }

How to change this code to make a possibility to set up in parameters to change a starting lots size?
Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.08.10 17:25  
Slawomir Dziegielewski:
double getLots(double lt) 
  (
   double marginrequired = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double freemargin = AccountFreeMargin();
   if(freemargin > (marginrequired * lt)) 
     {
    
       return(lt);
     } 
   double result = freemargin / marginrequired;
   result = MathFloor(result * 10) / 10;
   return(result);
  }

How to change this code to make a possibility to set up in parameters to change a starting lots size?
bool runonce=0;// Flag
sinput double startlots=0.01;// Starting Lotsize
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
double getLots(double lt)
  {
   if(runonce==0)
     {
      result = startlots;
      runonce=1;
      return(result);
     }
   else if(runonce==1)
     {
      double marginrequired=MarketInfo(Symbol(),MODE_MARGINREQUIRED);
      double freemargin=AccountFreeMargin();
      if(freemargin>(marginrequired*lt))
        {

         return(lt);
        }
      double result=freemargin/marginrequired;
      result=MathFloor(result*10)/10;
      return(result);
     }
  }
//+------------------------------------------------------------------+

I take it literally.

Slawomir Dziegielewski
246
Slawomir Dziegielewski 2016.08.10 17:34  
Marco vd Heijden:
I take it literally.

Thank you at start,

 

I get a error: 'startlots' - declaration without type

Slawomir Dziegielewski
246
Slawomir Dziegielewski 2016.08.10 17:37  
Slawomir Dziegielewski:

Thank you at start,

 

I get a error: 'startlots' - declaration without type

but when put double I get a error:

'result' - undeclared identifier

Marco vd Heijden
Moderator
4685
Marco vd Heijden 2016.08.10 18:00  
Slawomir Dziegielewski:

but when put double I get a error:

'result' - undeclared identifier

bool runonce=0;// Flag
sinput double startlots=0.01;// Starting Lotsize
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
double getLots(double lt)
  {
   if(runonce==0)
     {
      double result = startlots;
      runonce=1;
      return(result);
     }
   else if(runonce==1)
     {
      double marginrequired=MarketInfo(Symbol(),MODE_MARGINREQUIRED);
      double freemargin=AccountFreeMargin();
      if(freemargin>(marginrequired*lt))
        {

         return(lt);
        }
      double result=freemargin/marginrequired;
      result=MathFloor(result*10)/10;
      return(result);
     }
  }
//+------------------------------------------------------------------+
Osama Shaban
1447
Osama Shaban 2016.08.10 23:16  

If you always want to use same lot size then do the following ...

input StartLots = 0.01;

 then, 

double getLots(double lt) 
  (
   double marginrequired = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double freemargin = AccountFreeMargin();
   if(freemargin > (marginrequired * lt)) 
     {
    
       return(StartLots);
     } 
   double result = freemargin / marginrequired;
   result = MathFloor(result * 10) / 10;
   return(StartLots);
  }

 In this code, you always enforce the getLots( ) function to return same StartLots value !!!.

I believe it will work !. 

Slawomir Dziegielewski
246
Slawomir Dziegielewski 2016.08.17 09:41  
Osama Shaban:

If you always want to use same lot size then do the following ...

 then, 

 In this code, you always enforce the getLots( ) function to return same StartLots value !!!.

I believe it will work !. 

Yes it work fine! 

 

Great thank you guys!

Good Luck Guys! 

To add comments, please log in or register