I have been using the CalculateOrderQuantity(Symbol symbol, decimal target) method to help prepare the quantity of an order that I would need to submit to get close to my desired target percent.

I noticed that within this function it attempts to find out margin remaining and make sure the quantity it returns would not use more than available margin.

"var marginRemaining = instance.Portfolio.GetMarginRemaining(symbol, direction);"

This works perfectly when I never place more than 1 order in a day.

But I noticed that when I place multiple orders in a day Portfolio.MarginRemaining does not get updated within the day.

Because it does not get updated during the day, CalculateOrderQuantity will end up calculating order quantities that will make me go over my available margin.

I attempted to use OnOrderEvent to keep track of all open orders, and then calculate the value of open orders, and use this to adjust margin remaining. But I ran into the issue where it seems OnOrderEvent gives me events up till the order is filled, but doesnt tell me whether the order is settled (already incorporated into Portfolio.MarginRemaining).

I am using daily resolution.

Anyone have any guidance on this? Maybe I am missing something totally obvious.

Author