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.
QuantConnect Robot
Hi Rob, the margin available updates as soon as the orders are filled -- if you're using Daily data you're placing market on open orders (market orders are converted automatically to market on open since you're placing market order after close). This means they aren't filled immediately and would result in the margin errors you're seeing.
For your case you'll need to calculate the cash allocations manually.
Rob Sauce
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!