Download MetaTrader 5

Incrementing Lot Size per AccountBalance()

To add comments, please log in or register
Do you need a programmer? Make an order to let the developer find you!
DeanDeV
330
DeanDeV 2015.06.28 08:51 

Hey guys,

Does anybody have a function that increases the lot size by a certain amount when the account balance increases by a certain amount. Eg. If the account is initially on $2000 I want it to trade on 0.2 and when the account balance reaches $3000 it must start trading at 0.3 etc.

Thanks! 

whroeder1
14527
whroeder1 2015.06.28 11:47  
  1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  3. Do NOT use TickValue by itself - DeltaPerlot
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out
JD4
1101
JD4 2015.06.28 17:27  
While the stop is a good idea, don't remember them asking about calculating stops here.  The rest of WH's post is also a good practice to get into.
whroeder1
14527
whroeder1 2015.06.28 17:30  
If you don't have a stop, you can not define risk.
JD4
1101
JD4 2015.06.28 17:31  
True, but the original question was not about risk, but about calculating lot size based on acct balance.
Donald Gibson
498
Donald Gibson 2015.06.28 17:47  
DeanDeV:

Hey guys,

Does anybody have a function that increases the lot size by a certain amount when the account balance increases by a certain amount. Eg. If the account is initially on $2000 I want it to trade on 0.2 and when the account balance reaches $3000 it must start trading at 0.3 etc.

Thanks! 

This is what I use on a Daily basis in my real account. It works for me, I am not sure if it is what you are looking for.

I manage risk through LotSize not Stops,I use ATR for StopLevels.

Below is just a function to be added to a EA.

//+------------------------------------------------------------------+
//|                                        RiskManagementLotSize.mq4 |
//|                                                            Gypsy |
//|                                                             none |
//+------------------------------------------------------------------+
#property copyright "Gypsy"
#property link      "none"
#property version   "1.00"
#property strict
//-------------------------------------------------------------------------------------------------
//Risk Management
//-------------------------------------------------------------------------------------------------
extern string risk="Use Risk Management to set LotSize - True or False";//Alert
extern bool   Use_RiskManagement_LotSize=false;
extern double Risk_Percent=2;//Percentage of Account Balance to Risk
extern string Risk_break="";//=======================================
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double RMLotSize()
  {
/*
//-------------------------------------------------------------------------------------------------
//Risk Management
//-------------------------------------------------------------------------------------------------
extern string risk="Use Risk Management to set LotSize - True or False";//Alert
extern bool   Use_RiskManagement_LotSize=false;
extern double Risk_Percent=2;//Percentage of Account Balance to Risk
extern string Risk_break="";//=======================================
*/
//-------------------------------------------------------------------------------------------------
//current account balance
   double acctbal=0.00;
   if(AccountBalance()>0){acctbal=AccountBalance();}
//-------------------------------------------------------------------------------------------------
//risk percentage
   double riskpercentage=0.00;
   if(Risk_Percent>0){riskpercentage=Risk_Percent/100;}
//-------------------------------------------------------------------------------------------------
//amount of capitol to risk based on percentage
   double riskcapitol=0.00;
   if(acctbal>0){riskcapitol=acctbal*riskpercentage;}
//-------------------------------------------------------------------------------------------------
//lotsize based on risk capitol
   double lotsize=0.00;
   if(riskcapitol>0){lotsize=riskcapitol*0.01;}
//-------------------------------------------------------------------------------------------------
//if lotsize is more than is permitted change the lotsize to the maximum  permitted
   if(lotsize>MarketInfo(Symbol(),MODE_MAXLOT)) {lotsize=MarketInfo(Symbol(),MODE_MAXLOT);}
//-------------------------------------------------------------------------------------------------
//if lotsize is less than is permitted change the lotsize to the minimum  permitted  
   if(lotsize<MarketInfo(Symbol(),MODE_MINLOT)) {lotsize=MarketInfo(Symbol(),MODE_MINLOT);}
//-------------------------------------------------------------------------------------------------
//if there is no orders available print Account Information and LotSize available   
   if(OrdersTotal()==0)
     {
      Print("-------------------------------------------------------------------------------------------------");
      Print("LotSize based on the Capitol available...",DoubleToStr(lotsize,2));
      Print("Amount of Capitol to Risk...",DoubleToStr(riskcapitol,2));
      Print("Risk Percentage...",DoubleToStr(Risk_Percent,2));
      Print("Account Balance...",DoubleToStr(acctbal,2));
      Print("-------------------------------------------------------------------------------------------------");
     }
   return(lotsize);
  }
DeanDeV
330
DeanDeV 2015.06.29 15:59  
WHRoeder:
  1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. Account Balance * percent = RISK = (OrderOpenPrice - OrderStopLoss)*DIR * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  3. Do NOT use TickValue by itself - DeltaPerlot
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out

Hey WHRoeder,

I simply would like something like this...

if(AccountBalance()>=1000 && AccountBalance()<2000) Lots=0.1;
if(AccountBalance()>=2000 && AccountBalance()<3000) Lots=0.2;

 etc...

Any way to do this instead of typing them all out??

JD4
1101
JD4 2015.06.29 17:54  
DeanDeV:

Hey WHRoeder,

I simply would like something like this...

 etc...

Any way to do this instead of typing them all out??

Not sure it works in MQL4 or not, but in Java they have a switch/case setup, where you set the switch to check for the enclosed (in brackets) cases, and depending on what each case test comes out as, your program will do the line(s) for that particular case.  It is functionally similar to an if/elseif/else type of operation.  It might be a little easier to code than your if... for every single one but past that, no other way around doing at least one for each situation you want to cover that I can see.
DeanDeV
330
DeanDeV 2015.06.29 18:25  
JD4:
Not sure it works in MQL4 or not, but in Java they have a switch/case setup, where you set the switch to check for the enclosed (in brackets) cases, and depending on what each case test comes out as, your program will do the line(s) for that particular case.  It is functionally similar to an if/elseif/else type of operation.  It might be a little easier to code than your if... for every single one but past that, no other way around doing at least one for each situation you want to cover that I can see.
Okay cool. Thanks for the response and the advice. :) 
DeanDeV
330
DeanDeV 2015.06.29 18:28  
JD4:
Not sure it works in MQL4 or not, but in Java they have a switch/case setup, where you set the switch to check for the enclosed (in brackets) cases, and depending on what each case test comes out as, your program will do the line(s) for that particular case.  It is functionally similar to an if/elseif/else type of operation.  It might be a little easier to code than your if... for every single one but past that, no other way around doing at least one for each situation you want to cover that I can see.
Please excuse this if this is completely incorrect, however, is it possible to do something like this:
for(int inc=0; inc<=50; inc++)
     {
      if(AccountBalance()>=1000+(1000*inc) && AccountBalance()<2000+(1000*inc))
      double lots=NormalizeDouble(0.01+(0.01*inc),Digits);
DeanDeV
330
DeanDeV 2015.06.29 18:34  
DeanDeV:
Please excuse this if this is completely incorrect, however, is it possible to do something like this:

Okay I can confirm this works: 

for(int inc=0; inc<=1000; inc++)
     {
      if(AccountBalance()>=1000+(1000*inc) && AccountBalance()<2000+(1000*inc))
       {
        double lots=NormalizeDouble(0.01+(0.01*inc),Digits);
       }
     }

 For anyone who reads this and needs something similar. :) 

12
To add comments, please log in or register