Lot size calculation ... but it doesn't add up.

 

Hello all,

I attached my code below which is a mixture of codes found on this forum and an adaptation of the indicator from this page http://www.earnforex.com/position-size-calculator.

I'm using a 5 digits broker with 30 pips of Stoploss hence the Stoploss * pips2points. The account currency is USD.


Can anyone point me in the right direction?

Many thanks.

 double FXLots()
{
        double balance   = AccountFreeMargin();
    double tickvalue = MarketInfo(Symbol(), MODE_TICKVALUE);
    double spread    = MarketInfo(Symbol(), MODE_SPREAD);
    double point     = MarketInfo(Symbol(), MODE_POINT);
    double ticksize  = MarketInfo(Symbol(), MODE_TICKSIZE);
    double Max_Lot = MarketInfo(Symbol(), MODE_MAXLOT);
        double lotstep = MarketInfo(Symbol(), MODE_LOTSTEP);
        double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT);
        
        
                
        double CBtickvalue = tickvalue / ticksize;           
    
        double percent = NormalizeDouble((Risk * balance) / 100,2);     // OK
        
        Lots = percent / ((StopLoss * pips2points) * CBtickvalue);
        
        
        Lots = NormalizeDouble(Lots, 2);
        
        
        Lots = MathFloor(Lots/lotstep + 0.5) * lotstep;

        if(Lots < Min_Lot) Lots = Min_Lot;
        if(Lots > Max_Lot) Lots = Max_Lot;
}
 

As we don't know how values for StopLoss and pips2points are represented, it's difficult to say, but possibly you are working with values based on points instead of a decimal

Lots = percent / ((StopLoss * pips2points) * CBtickvalue); 
//(StopLoss * pips2points) needs to be a decimal
//ie if SL is 30 pips on EURUSD it should calculate to 0.00300
 
It is a change in price * tickvalue, not number of points * tickvalue. See my code on how to handle margin, stop out, and non-multiple of ten lotsteps
 

GumRai and WHRoeder thanks for your help.

WHRoeder, i believe part of my code was using some of the solutions you've kindly shared in this forum.

Reason: