I believe my question addressed more to developers of Lean engine.

I have the following concern. My algo is working on multiple instruments - 25 instances - and each produces signals based on same logic, same indicators. As there many signals from various instruments at various time I thought about how to allocate the available portfolio funds effectively among the securities. And wrote a simple method to manage the available funds.

1) Every time there is a new signal initiated by one of 25 securities I am trying to SetHolding for 0.3m percent of total portofolio value for that ticker. So I first check if there is enough money to do so by calling:

if( algorithm.Portfolio.MarginRemaining/algorithm.Portfolio.TotalPortfolioValue ) > 0.3 * _initialMarginRequirement

If there is enough maring I buy as much as I intended to:

SetHoldings(symbol, 0.3m).

2) if there is not enough maring available, I am re-setting the non-zero holdings to release the funds: so I go through all such holdings :

foreach (var s in symbols)

and call SetHoldings(s, someCalculatedPercentage) inside the loop, where someCalculatedPercentage is some value that will help to free part of funds.

I also count number of shares that have been sold by SetHoldings operation by calling additionally in foreach loop: algorithm.CalculateOrderQuantity(s, percentage); and write these values in a log.

3) So here is the problem. The normal behavior is after calling SetHoldings for the symbols in a loop. I can see the amounts of shares being reduced, they do always have a non zero value:

2017-01-17 12:30:00 Qnt: INCY -39 |PTLA -123 |TSRO -48 |SAGE -121 |CLVS -211 |UTHR -14 |

The same way the value of GetMaintenanceMargin (security) , which I also calculate in a loop and write to the log afterwards, where security is the one corresponding to the symbol, alters accordingly, the way like this (was -> became) :

2017-01-17 12:30:00 BP: INCY : 39194 -> 36898 | PTLA : 39607 -> 37990 | TSRO : 37490 -> 33948 | SAGE : 37821 -> 34598 | CLVS : 37473 -> 32281 | UTHR : 37715 -> 36651 |

Except for two cases that make me stupor:

2017-09-12 16:00:00 Qnt: SRPT -515 |FOLD -1033 |SUPN -336 |ILMN -98 |GILD -224 |BIIB -63 |IMMU -1254 |ARRY -413 |

2017-09-12 16:00:00 BP: SRPT : 58835 -> 58835 | FOLD : 58904 -> 58904 | SUPN : 54723 -> 54723 | ILMN : 55530 -> 55530 | GILD : 56853 -> 56853 | BIIB : 58149 -> 58149 | IMMU : 55808 -> 55808 | ARRY : 54432 -> 54432 |

2018-01-02 16:00:00 Qnt: SAGE -560 |GWPH -128 |ASND 480 |CRSP -1893 |NKTR -399 |IMMU -2596 |UTHR -18 |

2018-01-02 16:00:00 BP: SAGE : 71417 -> 71417 | GWPH : 90895 -> 90895 | ASND : 66972 -> 66972 | CRSP : 99666 -> 99666 | NKTR : 65055 -> 65055 | IMMU : 88890 -> 88890 | UTHR : 73511 -> 73511 |

These two log records made me dig into the source code of the Lean engine for last three days, which I find infinitely useful pursuit, but leaving without a hint on such an unexpected behavior. So I wondering if guys developers could help me to find the roots of my issue, please?

Thanks in advance!