Close Percentage of Trade Calculation

 

I'm trying to write a subroutine that accepts 2 parameters(Percent, Lots) and return the number of lots to close based on a percentage of the current trades lots size (CloseLots).

The issues are as I see it, the simple (Percent x Lots ) calculation can return values that are not valid when divided by Lot Steps. Also, the remaining Lots could be invalid as a minimum lot size.

Anyone have a routine that will calculate the CloseLots close to the desired Percentage without OrderClose errors?


Thanks!


double CalcCloseLots(double Percent, double Lots)

{

....blah blah magic code and wizardy blah....


return(CloseLots);

}

 
hkjonus:

I'm trying to write a subroutine that accepts 2 parameters(Percent, Lots) and return the number of lots to close based on a percentage of the current trades lots size (CloseLots).

The issues are as I see it, the simple (Percent x Lots ) calculation can return values that are not valid when divided by Lot Steps. Also, the remaining Lots could be invalid as a minimum lot size.

Anyone have a routine that will calculate the CloseLots close to the desired Percentage without OrderClose errors?


Thanks!


double CalcCloseLots(double Percent, double Lots)

{

....blah blah magic code and wizardy blah....


return(CloseLots);

}


double CalcCloseLots(double Percent, double Lots, string symbol)
{
    double step = MarketInfo(symbol, MODE_LOTSTEP); 
    int l = Lots * Percent / step + 0.5;
    
    return (l * step);
}
 
Irtron:

double step = MarketInfo(symbol, MODE_LOTSTEP); [...]

That looks as though it assumes that MODE_MINLOT equals MODE_LOTSTEP. Probably safe in practice, though not in theory. In principle, the settings allow a broker to have MODE_MINLOT of e.g. 0.10 and MODE_LOTSTEP of 0.03 - i.e. permitted lot sizes which increase by 0.03 but don't start at 0.03 and aren't a multiple of 0.03.

 
jjc:

That looks as though it assumes that MODE_MINLOT equals MODE_LOTSTEP. Probably safe in practice, though not in theory. In principle, the settings allow a broker to have MODE_MINLOT of e.g. 0.10 and MODE_LOTSTEP of 0.03 - i.e. permitted lot sizes which increase by 0.03 but don't start at 0.03 and aren't a multiple of 0.03.

Brilliant example. Have you tested your theory?

minlot + 3 * lotstep = 0.1 + 3 * 0.03 = 0.19

0.19 doesn't start at 0.03 and isn't a multiple of 0.03. Try to round it with minlot 0.1


I've demonstrated an example of how fixed point value can be rounded to given precision regardless to the specific of certain broker setup.

 
Irtron:

Brilliant example. Have you tested your theory?


Yes.

  • Let's say that MODE_MINLOT is 0.10 and MODE_LOTSTEP is 0.01. (This may well not happen in real life with any brokers, but something like this isn't wildly implausible.)
  • The Lots parameter to the function is 1 and percent is 5 (specified as 0.05)
  • The code emits a lot size which will be disallowed by the broker (0.05).

Sorry, but I can't see anything other than an assumption that MODE_MINLOT = MODE_LOTSTEP.

 
jjc:

Yes.

  • Let's say that MODE_MINLOT is 0.10 and MODE_LOTSTEP is 0.01. (This may well not happen in real life with any brokers, but something like this isn't wildly implausible.)
  • The Lots parameter to the function is 1 and percent is 5 (specified as 0.05)
  • The code emits a lot size which will be disallowed by the broker (0.05).

Sorry, but I can't see anything other than an assumption that MODE_MINLOT = MODE_LOTSTEP.

Why do you think that the size of partial closure can't be less than minlot if lotstep is less than minlot? Which broker would disallow to decrease the position size from 1 to 0.95 in your example?

Have you tested your theory really?

 
Irtron:

Why do you think that the size of partial closure can't be less than minlot if lotstep is less than minlot? Which broker would disallow to decrease the position size from 1 to 0.95 in your example?

A broker might potentially enforce MINLOT on closing orders for the same - hypothetical - reasons that they might have a restriction on opening orders. There's not much difference from the average broker's point of view. They could potentially enforce a relatively large MINLOT on both opening and closing orders if they wanted to avoid having to make tediously small alterations to the hedging of customer positions.


Have you tested your theory really?

Like I'm saying, it's hypothetical. (When you said "tested" earlier, I assumed you meant "tested" using your code, not in real life). I'm not aware of a broker where MODE_MINLOT != MODE_LOTSTEP. To repeat what I said right at outset: your code is almost certainly certainly safe in practice. I just wouldn't go as far as saying that it definitely works "regardless to the specific of certain broker setup".

 
jjc:

A broker might potentially enforce MINLOT on closing orders for the same - hypothetical - reasons that they might have a restriction on opening orders. There's not much difference from the average broker's point of view. They could potentially enforce a relatively large MINLOT on both opening and closing orders if they wanted to avoid having to make tediously small alterations to the hedging of customer positions.


Like I'm saying, it's hypothetical. (When you said "tested" earlier, I assumed you meant "tested" using your code, not in real life). I'm not aware of a broker where MODE_MINLOT != MODE_LOTSTEP. To repeat what I said right at outset: your code is almost certainly certainly safe in practice. I just wouldn't go as far as saying that it definitely works "regardless to the specific of certain broker setup".

Yes, I am using a broker now whos Minlot is .20 an the Lotstep is .10.

However, in manual trading they Dont allow closing anything less than .20. So Im not even sure if an EA close will work using .10. Guess I need to try it.

Anyway, my stated goal is still the same. If someone has a fool proof routine, please post it here!

thanks

 
Irtron:



Im curious as to why you chose .5 to add to the Step. Where did you get that number?

 
hkjonus:

Yes, I am using a broker now whos Minlot is .20 an the Lotstep is .10.

However, in manual trading they Dont allow closing anything less than .20. So Im not even sure if an EA close will work using .10. Guess I need to try it.

Anyway, my stated goal is still the same. If someone has a fool proof routine, please post it here!

thanks


Well, that proves jjc's right. Lets try full scale approach:

double CalcCloseLots(double Percent, double Lots, string symbol)
{
    double lotstep, minlot;
    int l;
    
    lotstep = MarketInfo(symbol, MODE_LOTSTEP); 
    minlot = MarketInfo(symbol, MODE_MINLOT); 
    l = (Lots * Percent - minlot) / lotstep + 0.5;
    if (l < 0)
    {
        Print("Lot fracture is too small, MinLot = ", minlot);
        return (0.);
    }
    
    return (l * lotstep + minlot);
}

<div class=" fquote"="" color:="" rgb(66,="" 99,="" 156);="" font-family:="" tahoma,="" verdana,="" arial,="" helvetica,="" sans-serif;="" font-size:="" 1em;=""><p><span style=" color:="" rgb(0,="" 0,="" 0);="" font-size:="" 16px;=""><div style=" font-family:="" tahoma,="" verdana,="" arial,="" helvetica,="" sans-serif;="" background-color:="" rgb(251,="" 251,="" 252);="" color:="" rgb(98,="" 99,="" 99);="" font-size:="" 0.8em;="" margin-top:="" 4px;="" margin-right:="" margin-bottom:="" margin-left:="" padding-top:="" padding-right:="" padding-bottom:="" padding-left:="" h=""><p style=" font-family:="" tahoma,="" verdana,="" arial,="" helvetica,="" sans-serif;="" font-size:="" 1em;="" margin-top:="" 0px;="" margin-right:="" margin-left:="" padding-top:="" padding-right:="" padding-bottom:="" padding-left:="" margin-bottom:="" 4px="" !important;=""><span style=" color:="" rgb(0,="" 0,="" 0);="" font-size:="" 16px;="">

 
hkjonus:

Im curious as to why you chose .5 to add to the Step. Where did you get that number?


This is not an addition to the Step. 0.5 is added to the result of expression.


This is for rounding the double result to the nearest integer.