Jump to content

[req] Help calculating lot size


Recommended Posts

I am demo testing and I want to code my EA in a manner that would result in trade sizes of 0.2 lots for every 1000 of Account Balance.

 

The closest I could get to that goal is the following, assuming a balance of 5000

 

double lot=1;

lot=NormalizeDouble(MathFloor(AccountFreeMargin()*200)/1,1);

Link to comment
Share on other sites

  • 2 weeks later...

Re: [req] Help calculating lot size

 

//external variables

extern bool UseMoneyManagement = false;

extern double DecreaseFactor = 5;

extern int Risk = 10;

extern double InitialLot=0.1;

extern bool AccountIsMicro=false;

 

 

double Lots()

{

if(UseMoneyManagement==false) return(InitialLot);

double lots;

int orders=HistoryTotal();

int losses=0;

int decimalPlaces=1;

if(AccountIsMicro==true) decimalPlaces=2;

 

lots=NormalizeDouble((AccountFreeMargin()*Risk/1000.0)/100,decimalPlaces);

StoppedOut = LastTradeStoppedOut();

if (StoppedOut==true)

{

if(AccountIsMicro==false) lots=0.1;

if(AccountIsMicro==true) lots=0.01;

}

if(lots<0.1 && AccountIsMicro==false) lots=0.1;

if(lots<0.01 && AccountIsMicro==true) lots=0.01;

double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);

if (MyMaxLot>0)

{

if (lots>MyMaxLot)

return (MyMaxLot);

}

if(lots>maxlot) lots=maxlot-1;

return(lots);

}

 

bool LastTradeStoppedOut()

{

int cnt, total;

datetime NextTime;

bool Stopped=false;

 

NextTime = 0;

 

total = HistoryTotal();

for (cnt = total - 1; cnt >= 0; cnt--)

{

OrderSelect (cnt, SELECT_BY_POS, MODE_HISTORY);

 

if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic)

{

Stopped = false;

if (OrderType() == OP_BUY)

{

if (OrderClosePrice() - OrderOpenPrice() < 0)

{

Stopped = true;

}

cnt = 0;

}

if (OrderType() == OP_SELL)

{

if (OrderOpenPrice() - OrderClosePrice() < 0)

{

Stopped = true;

}

cnt = 0;

}

}

}

 

if (Stopped)

{

StopTime = OrderCloseTime() + MinutesToDelay*60;

}

 

return (Stopped);

}

Link to comment
Share on other sites

Re: [req] Help calculating lot size

 

Ok here you go.. put it in strategy tester and play with your variables to test..

 

http://www.mediafire.com/?sharekey=2f912bd94f9b924a9bf8d6369220dcabe04e75f6e8ebb871

 

 

//+------------------------------------------------------------------+
//|                                                    LotTester.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

extern double Lots = 0.2;
extern double MinBalance = 1000;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
 {
//----
  
//----
  return(0);
 }
 
 
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
 {
//----
  
//----
  return(0);
 }
 
 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
 {
//----
  Comment("AccountBalance: " +DoubleToStr(AccountBalance(),2)+"  Lots: "+ DoubleToStr(CalcLots(),2)); 
  return(0);
//----
  return(0);
 }
 
 
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| expert CalcLots function                                            |
//+------------------------------------------------------------------+
double CalcLots()
{
//----
  double iLots;
  if (AccountBalance()>MinBalance)
  {
   iLots= NormalizeDouble((AccountFreeMargin()*(Lots*100)/1000)/100,2);

  } 
   else iLots = Lots;
//----    
  return (iLots);
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...