Splitting up lot position evenly

 
Another request before i go hacking away at code - hoping someone has done something similar.

I am giving the option of a "splitPosition" on X number of requested lots.

Ie, the user might have requested 1 standard lot, or a risk calculation that equates to 1 standard lot, and to split that position into 3 multiple trades.

So i just need the code to split that 1 lot into 3 even sizes, and obviously take care of the correct rounding of numbers and possibility that it will not be even numbers.

So i would assume in this case it would come back with 3 different positions of something like:

Position 1: 0.35
Position 2: 0.35
Position 3: 0.3

Oh and if it takes into account the brokers minimum position etc that would be nice.

Much appreciate your time.
 
    // Split lotsNew into three equal trades.
    double  lotStep         = MarketInfo(Symbol(), MODE_LOTSTEP),   //IBFX= 0.01
            minLot          = MarketInfo(Symbol(), MODE_MINLOT );   //IBFX= 0.01
    for(int split=3; split>0; split--) {             // 100.01=33.34+33.34+33.33
        double size=MathCeil(lotsNew/split/lotStep)*lotStep;
        if (size < minLot) continue;
        lotsNew -= size;
        OrderSend(...
   }
Reason: