Download MetaTrader 5
To add comments, please log in or register
Having troubles understanding the code? Perhaps, you can find an article on that topic!
jurcekmpt
486
jurcekmpt 2015.08.26 07:34 

Hello!

Is there any better/smarter way to increase/decrease lot size every 100$ by 0.05lot?
For now I have this, but I don't like it. Any better way?

Thank you!

if(AccountBalance()<300)
      Lots=0.1;
  else if ( (AccountBalance()>=300) && (AccountBalance()<400) )
      Lots=0.15;
  else if ( (AccountBalance()>=400) && (AccountBalance()<500) )
      Lots=0.20;
  else if ( (AccountBalance()>=500) && (AccountBalance()<600) )
      Lots=0.25;
  else if ( (AccountBalance()>=600) && (AccountBalance()<700) )
      Lots=0.3;
  else if ( (AccountBalance()>=700) && (AccountBalance()<800) )
      Lots=0.35;
  else if ( (AccountBalance()>=800) && (AccountBalance()<900) )
      Lots=0.4;
                             .
                             .
                             .
Ali Akcaagac
169
Ali Akcaagac 2015.08.26 11:17  

Even if I don't share your approach, I think this might make more sense ?

double step = 100;    // the 100 is the step you want to go

double mulu = AccountBalance() / step;
double muls = AccountBalance() % step;

if (mulu >= 1 && muls == 0) {
    double lots = mulu * 0.05;
}

This is not tested yet:

100 / 100 = 1

100 % 100 = 0

cash = 100 ; lots = 1 * 0.05 = 0.05;

101 / 100 =  1.01

101 % 100 =  1

cash = 101 ; lot's = 1 * 0.05 = 0.05; because muls != 0;

...

200 / 100 = 2

200 % 100 = 0

cash = 200 ; lots = 2 * 0.05 = 0.10;

201 / 100 = 2.01

201 % 100 = 1

cash = 201 ; lot's = 2 * 0.05 = 0.10; because muls != 0;

... and so on. Everything below 100 will be ignored. There you can set a default lot's size if you wish.

Edit: There's still a problem in case you earn more money e.g. from 102 USD -> 322 USD in one step. In this case no lot's being added because the muls doesn't reach the clear 0. But at least this mechanism can give you a hint in howto calculate this stuff on the go, rather than if/else'ing the hell out of it.

Keith Watford
Moderator
7869
Keith Watford 2015.08.26 11:24  
  double lots=MathFloor(AccountBalance()/100)*0.05;
You may also need to make provision for minimum and maximum lot size
jurcekmpt
486
jurcekmpt 2015.08.26 11:28  
Thank you!

I made it this way, with MM.


extern double Risk_P=5;
extern double Max_Lots=7;
double Lots;
extern int StopLoss = 20;

double GetLots() 
{
  Lots=NormalizeDouble( (AccountBalance()*(Risk_P/(1000*StopLoss))),2 );
  if(Lots<0.01) Lots=0.01;
  if(Lots>Max_Lots) Lots=Max_Lots;
  return (Lots);
}



/
To add comments, please log in or register