///////////////////////////////////////////////////////// double perc_lot_by_stop_f(int SL=0,double Lot_sl_risk=1) { if(SL<=0) {Alert("Need SL to be set!..min lot mode");return(MarketInfo(Symbol(),MODE_MINLOT));} double lose_on_stop_lose=SL*(MarketInfo(Symbol(),MODE_TICKVALUE)/100); double propotion=(AccountBalance()/100*Lot_sl_risk)/lose_on_stop_lose; double Loto_=MarketInfo(Symbol(),MODE_MINLOT)*propotion; if(Loto_<MarketInfo(Symbol(),MODE_MINLOT)) Loto_=MarketInfo(Symbol(),MODE_MINLOT); if(Loto_>MarketInfo(Symbol(),MODE_MAXLOT)) Loto_=MarketInfo(Symbol(),MODE_MAXLOT); if(MarketInfo(Symbol(),MODE_MARGINREQUIRED)*Loto_>AccountFreeMargin()) {Alert("Not enouth money to open order!..min lot mode");return(MarketInfo(Symbol(),MODE_MINLOT));} return(Loto_); }
////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// double GetLot_f() { double Free =AccountFreeMargin(); double One_Lot =MarketInfo(Symbol(),MODE_MARGINREQUIRED); double Step =MarketInfo(Symbol(),MODE_LOTSTEP); double Loto =MathFloor(Free*percent_of_deposit/100/One_Lot/Step)*Step; double Min_Lot =MarketInfo(Symbol(),MODE_MINLOT); double Max_Lot =MarketInfo(Symbol(),MODE_MAXLOT); if(Loto<Min_Lot) Loto=Min_Lot; if(Loto>Max_Lot) Loto=Max_Lot; return(NormalizeDouble(Loto,nor_lot)); }
//+------------------------------------------------------------------+ //| Code below deals with lot size allocation for trades. //+------------------------------------------------------------------+ double LotCalc() { // Get market info for symbol if(IsConnected()) { double One_Lot = MarketInfo(Symbol(), MODE_MARGINREQUIRED); // Get cost of 1 lot double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT); // Get minimum lot size double Step = MarketInfo(Symbol(), MODE_LOTSTEP); // Get lot stepping size double Free = AccountFreeMargin(); // Get my free margin level double Money, Giving; } else return(0.00); // If a lot size is specified, can I afford it? if(LotSize > 0) { // Calc cost of lots wanted Money = LotSize * One_Lot; // If I can afford it, accept, otherwise calculate it based on free margin available if(Money <= Free) Giving = LotSize; else Giving = MathFloor(Free/One_Lot/Step) * Step; } else { // Calc lot size as a percentage of available margin if(Percentage > 100) Percentage = 100; // If percentage is not set, take just one lot, otherwise calculate it if(Percentage <= 0) Giving = Min_Lot; else Giving = MathFloor(Free*(Percentage/100)/One_Lot/Step) * Step; } // If OptimizeTrade flag is set, increase or decrease lots size by OptimizeSize based on consecutive wins/losses if(OptimizeTrade) { if(EAWins - EALosses > 2) Giving += ((EAWins - EALosses)-2) * OptimizeSize; if(EALosses - EAWins > 2) Giving -= ((EALosses - EAWins)-2) * OptimizeSize; } // Is the lot size selected is less than the minimum lot, set to minimum if(Giving < Min_Lot) Giving = Min_Lot; // Check for maximum lots size Giving = MathMin(MarketInfo(NULL, MODE_MAXLOT), Giving); // Final check to see if I can afford this... if(Giving * One_Lot > Free) return(0.00); // Nope :( else return(Giving); // All good! }
Claude Beaudoin:
Does this work for non FX securities like Gold, and Oil? that have 0.25 steps?
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Kind of taking a survey here.
I've been reading around various area's on lot calculation. I'm wondering, how do you calculate lots? Here are some examples from a thread I posted on another forum:
Note: I put this inside of source code tags for easier reading. :)