Hi,
First you should check this condition with lot step,not min lot,
if(MarketInfo(Symbol(),MODE_LOTSTEP)==1)LotDigits =0; if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1)LotDigits =1; if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01)LotDigits =2;
The correct version of the function should be such :
double subLotSize() { int LotDigits=0; double lots = 0; int TotalClosedOrder = 0; static int WonOrderCount; datetime OrderTime; double lot_max = MarketInfo(Symbol(), MODE_MAXLOT); double lot_min = MarketInfo(Symbol(), MODE_MINLOT); double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double contract = MarketInfo(Symbol(), MODE_LOTSIZE); if(lotStep==0.01) LotDigits=2; if(lotStep==0.1) LotDigits=1; if(lotStep==1) LotDigits=0; if(UseLotMultiplier) { for(int h=OrdersHistoryTotal()-1; h>=0; h--) { if(OrderSelect(h,SELECT_BY_POS,MODE_HISTORY)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderType()==OP_BUY || OrderType()==OP_SELL) TotalClosedOrder++; if(TotalClosedOrder>=1) break; } } } if(TotalClosedOrder==0) lots=NormalizeDouble(AccountBalance()*Risk/100*AccountLeverage()/contract,LotDigits); else if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderCloseTime()>OrderTime) { OrderTime=OrderCloseTime(); if(WonOrderCount==StartOver) { lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); WonOrderCount = 0; } else if(OrderProfit()+OrderCommission()+OrderSwap()<=0) { lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); WonOrderCount = 0; } else if(OrderProfit()+OrderCommission()+OrderSwap()>0) { lots=NormalizeDouble(OrderLots()*LotMultiplier,LotDigits); WonOrderCount++; } } } } if(!UseLotMultiplier) lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract,LotDigits); if(lots>= lot_max) lots = lot_max; if(lots<= lot_min) lots = lot_min; return(lots); } //+------------------------------------------------------------------+
you should remove the static literal for the "OrderTime" variable,because it will store last value,next time it will have the previous value,then the "if else" condition
wont be called.
So you can call this function anywhere in code.either first of the code in OnTick() function,or inside OrderSend() function.
Regards.
Hi,
First you should check this condition with lot step,not min lot,
The correct version of the function should be such :
you should remove the static literal for the "OrderTime" variable,because it will store last value,next time it will have the previous value,then the "if else" condition
wont be called.
So you can call this function anywhere in code.either first of the code in OnTick() function,or inside OrderSend() function.
Regards.
Dear @Mehrdad Jeddi
Thanks so much for your reply, But still doesn't work, corrected code is :
double subLotSize() { double lots = 0; int TotalClosedOrder = 0; static int WonOrderCount; static datetime OrderTime; double lot_max = MarketInfo(Symbol(), MODE_MAXLOT); double lot_min = MarketInfo(Symbol(), MODE_MINLOT); double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double contract = MarketInfo(Symbol(), MODE_LOTSIZE); if (lotStep == 0.01) LotDigits = 2; if (lotStep == 0.1) LotDigits = 1; if (lotStep == 1) LotDigits = 0; if (!UseLotMultiplier) lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); else { for(int h=OrdersHistoryTotal()-1; h>=0; h--) { if(OrderSelect(h,SELECT_BY_POS,MODE_HISTORY)) { if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderType()==OP_BUY || OrderType()==OP_SELL) TotalClosedOrder++; if(TotalClosedOrder>=1) break; } } } if (TotalClosedOrder == 0) lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); else if (OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_TICKET, MODE_HISTORY)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if (WonOrderCount >= StartOver) { lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); WonOrderCount = 0; } else if (OrderProfit() + OrderCommission() + OrderSwap() <= 0) { lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); WonOrderCount = 0; } else if (OrderProfit() + OrderCommission() + OrderSwap() > 0) { lots = NormalizeDouble(OrderLots() * LotMultiplier, LotDigits); WonOrderCount++; } } } else Print("OrderSelect failed error code is", GetLastError()); } return(NormalizeLots(lots)); } //+------------------------------------------------------------------+ double NormalizeLots(double lots) { double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double minLot = MarketInfo(Symbol(), MODE_MINLOT); double maxLot = MarketInfo(Symbol(), MODE_MAXLOT); lots = MathRound(lots/lotStep) * lotStep; if (lots < minLot) lots = minLot; if (lots > maxLot) lots = maxLot; return(lots); }
So my previous code is working on my side:
Mehrdad JAAN,
I figured it out as you told me above, thanks bro, NOKARAM!
double AutoLotSize() { double lots = 0; int TotalClosedOrder = 0; static int WonOrderCount; double lot_max = MarketInfo(Symbol(), MODE_MAXLOT); double lot_min = MarketInfo(Symbol(), MODE_MINLOT); double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double contract = MarketInfo(Symbol(), MODE_LOTSIZE); if (lotStep == 0.01) LotDigits = 2; if (lotStep == 0.1) LotDigits = 1; if (lotStep == 1) LotDigits = 0; if (!UseLotMultiplier) lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); else { for (int h = OrdersHistoryTotal() - 1; h >= 0; h--) { if (OrderSelect(h, SELECT_BY_POS, MODE_HISTORY)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if (OrderType() == OP_BUY || OrderType() == OP_SELL) TotalClosedOrder++; if (TotalClosedOrder >= 1) break; } } } if (TotalClosedOrder == 0) lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); else if (OrderSelect(OrdersHistoryTotal() - 1, SELECT_BY_POS, MODE_HISTORY)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if (WonOrderCount == StartOver) { lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); WonOrderCount = 0; } else if (OrderProfit() + OrderCommission() + OrderSwap() <= 0) { lots = NormalizeDouble(AccountBalance() * Risk / 100 * AccountLeverage() / contract, LotDigits); WonOrderCount = 0; } else if (OrderProfit() + OrderCommission() + OrderSwap() > 0) { lots = NormalizeDouble(OrderLots() * LotMultiplier, LotDigits); WonOrderCount++; } } } else Print("OrderSelect failed error code is", GetLastError()); } return(NormalizeLots(lots)); } double NormalizeLots(double lots) { double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double minLot = MarketInfo(Symbol(), MODE_MINLOT); double maxLot = MarketInfo(Symbol(), MODE_MAXLOT); lots = MathRound(lots / lotStep) * lotStep; if (lots < minLot) lots = minLot; if (lots > maxLot) lots = maxLot; return(lots); }
Good,You're welcome,
Have a nice weekend.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi guys,
I have problem in my code and I can't find the solution. description of my code is:
1 - use auto lot size:
2 - use fixed manual lots
The problem is I got Just 0.01 lot that is minimum lot size, appreciated if anyone can help me to solved
Auto lot function is :