Close half position

 
With help of firends it is made one simple script for closing half of the open position. (Scailing out). It is working beautiful with 5-digit brokers who have micro lots available. Problem is with brokers such as FxPro who uses mini lots. If position size is 0,80 lots, script is working normal (you can scail out 0,80-->0,40-->0,20-->0,10), but if position size is 0,70 the script is quiet. What should we do with the code, so the script would scail by half a positions which require micro lots but are not available with broker. For example 1,40 lot -->0,70-->0,30 or 0,40 --.....

Appreciate any your help. Thank you.

Attached script is free to use.
Files:
 
tradeways wrote >>
With help of firends it is made one simple script for closing half of the open position. (Scailing out). It is working beautiful with 5-digit brokers who have micro lots available. Problem is with brokers such as FxPro who uses mini lots. If position size is 0,80 lots, script is working normal (you can scail out 0,80-->0,40-->0,20-->0,10), but if position size is 0,70 the script is quiet. What should we do with the code, so the script would scail by half a positions which require micro lots but are not available with broker. For example 1,40 lot -->0,70-->0,30 or 0,40 --.....

Appreciate any your help. Thank you.

Attached script is free to use.


https://docs.mql4.com/common/MarketInfo

https://docs.mql4.com/constants/marketinfo


MODE_MINLOT and MODE_LOTSTEP will be interesting for you, then you can round with NormalizeDouble.

If (MODE_LOTSTEP ==0.01) NewLot=NormalizeDouble(OldLot/2,2); else NewLot=NormalizeDouble(OldLot/2,1);

somthing like this (not tested)...
 
Thank you EADeveloper.

We are trying, for now without success. (Beginners in coding).
 
int LotSizeDigits=-MathRound(MathLog(MarketInfo(Symbol(),MODE_LOTSTEP))/MathLog(10.)); // Number of digits after decimal point for the Lot for the current broker, like Digits for symbol prices
double NewLot=0;

if(OldLot/2. >= MarketInfo(Symbol(),MODE_MINLOT))
   {
   //Calculate the new lot size and normalize to the brokers accepted lotsize increments
   NewLot=NormalizeDouble(MathFloor((OldLot/2.-MarketInfo(Symbol(),MODE_MINLOT))/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)+MarketInfo(Symbol(),MODE_MINLOT),LotSizeDigits);
   }
else Print("NewLot will be too small for broker to accept...");
You will have to determine what you want done when oldlot/2 is smaller than the minimum accepted lot for the broker. Also this equation assumes you want the newlot to be equal to or smaller than 1/2 the oldlot (i.e. it rounds down to the nearest acceptable lotsize). If it you want to roundup to the nearest acceptable lotsize just replace "MathFloor" with "MathCeil".
 
int LotSizeDigits=-MathRound(MathLog(MarketInfo(Symbol(),MODE_LOTSTEP))/MathLog(10.)); // Number of digits... 
                                                                                       // after decimal point for the Lot 
                                                                                       // for the current broker, 
                                                                                       // like Digits for symbol prices
double NewLot=0;

if(OldLot/2. >= MarketInfo(Symbol(),MODE_MINLOT))
   {
   //Calculate the new lot size and normalize to the brokers accepted lotsize increments
   NewLot = NormalizeDouble(MathFloor((OldLot/2.-MarketInfo(Symbol(),MODE_MINLOT))
            / MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)
            + MarketInfo(Symbol(),MODE_MINLOT),LotSizeDigits);
   }
else Print("NewLot will be too small for broker to accept...");
Phillip, this doesn't run off screen... :)
( nothing wrong with your style, it's just I have the attention span of a goldfish... when I scroll to the right I forgot what I was reading :))
 
cameofx wrote >>
Phillip, this doesn't run off screen... :)
( nothing wrong with your style, it's just I have the attention span of a goldfish... when I scroll to the right I forgot what I was reading :))


double cL=OldLot/2;
if (MarketInfo(Symbol(),MODE_MINLOT)==0.1)
cL=NormalizeDouble(cL,1);
else
cL=NormalizeDouble(cL,2);

but this will check only mini (0,1) and microlots (0,01).
Maybe you need to implement more detailed if you need ... Normallot (1,0), Supermicrolot (0,0001) ... ?!
 
tradeways:
With help of firends it is made one simple script for closing half of the open position. (Scailing out). It is working beautiful with 5-digit brokers who have micro lots available. Problem is with brokers such as FxPro who uses mini lots. If position size is 0,80 lots, script is working normal (you can scail out 0,80-->0,40-->0,20-->0,10), but if position size is 0,70 the script is quiet. What should we do with the code, so the script would scail by half a positions which require micro lots but are not available with broker. For example 1,40 lot -->0,70-->0,30 or 0,40 --.....

Appreciate any your help. Thank you.

Attached script is free to use.

I assume your situation requires that you can only close in the increments of 0.1 lotstep. Check if your LotSize is 'even'; If it's not, subtract it by 0.1 then you can scale by half; later close the remaining 0.1.
Or simply don't open a position in 'odd' lots (so you can scale it by half later) ...

 
EADeveloper wrote >>


double cL=OldLot/2;
if (MarketInfo(Symbol(),MODE_MINLOT)==0.1)
cL=NormalizeDouble(cL,1);
else
cL=NormalizeDouble(cL,2);

but this will check only mini (0,1) and microlots (0,01).
Maybe you need to implement more detailed if you need ... Normallot (1,0), Supermicrolot (0,0001) ... ?!


This takes care of the question of how many digits for normalizedouble a lotsize programatically:
int LotSizeDigits=-MathRound(MathLog(MarketInfo(Symbol(),MODE_LOTSTEP))/MathLog(10.)); // Number of digits... 
                                                                                       // after decimal point for the Lot 
                                                                                       // for the current broker, 
                                                                                       // like Digits for symbol prices
 
Thank you all for your help!

I am curious is script working normal for you with this codes. I am getting error 'OldLot'- variable not defined. If I make some changes I don't have errors, but script is working as it was working before or it is not working at all. Although it is a big likelihood that I am not doing right. Do we need code in 'NewLots' 'OldLots'? Script is allready closing positions by half, with sizes such as 0.20, 0.40, 0.60, 0.80, 1.00, 1.20, ..... Problems are with 0.30, 0.50, 0.70, 0.90, 1.10, 1.30, ......
Where can I check if my LotSize is 'even'?
Script is for manual trading(drag and drop) so the Terminal window can be closed.

Thank you.