Insufficient buying power to complete order quantconnect

Hi All,

I have created a simple algorithm to sell bear call spreads using the OptionStrategies.BearCallSpread method.

I'm just trying to get my head around the system, so this is very simple.

The issue I have is that I am getting unexpected errors of insufficient buying power. I'd expect this code to place a bunch of combo orders pretty quickly, and indeed run out of buying power. However, it reports lack of buying power almost immediately. It's trying to sell a five dollar wide SPY call spread, so the maximum risk should be $500. 

A typical error is:

Backtest Handled Error: Order Error: id: 3113, Insufficient buying power to complete order (Value:371), Reason: Id: 3113, Initial Margin: 372, Free Margin: 0
Order Error: id: 3114, Insufficient buying power to complete order (Value:-313), Reason: Id: 3114, Initial Margin: -314, Free Margin: 0

Weirdly this happens when the simulation is at 9645.00 equity and only 210.00 holdings. 

Am I horribly confused about this works??

0

Hi All,

I have attached a backtest that shows an example situation where my order is not filling due to insufficient buying power. I have a suspicion it is due to my one cancels the other order function (Taken from: https://www.quantconnect.com/forum/discussion/1700/support-for-one-cancels-the-other-order-oco/p1, thank you JayJayD)

The console shows an error, and the logs show the buying power is reduced, even though there are no open holdings/cash.

Could someone please help me submit the order of NVEE in the attached backtest with OCO orders. The goal is to create an algo that OCO orders can be created, but the entire portfolio (and pending orders) can also be liquidated/cancelled separately

Hi Lucas,

On line 58, the limitorder is on the same direction as the original order. Since 98% of buying power long is already used, the "insufficient buying power" error is thrown and all the following strategies break. I believe both "take profit" and "stop loss" order should be in the opposite direction to the original order.

Besides, for short, the "take profit" should be StopMarketOrder and "stop loss" should be LimitOrder. You could find more information about order type here.

In your algorithm, the range between take profit and stop loss is a little wide and neither of those two are triggered. To demonstrate the OCO strategy, I've modified those numbers in my backtest. Hope it helps!

0


Hi Link Liang, thanks for taking the time to reply back to me and for picking up in the line 58 error.

In the example the SL & TP are deliberately unrealistically high. What I am trying like to do is simulate a scenario where they arent reached but a better opportunity presents. In the backtest on line 63 I deliberately use Liquidate() in attempt to get back to 100% margin.

With your attached backtest, if I increase the SL/TP size (so that either arent triggered), I run into the same issue of insufficient buying power. Could you please share any insight on this?

0


Hi Link Liang, I realised this may not have anything to do with the OCO function. Because of this, please disregard my previous comment as I have created a new post.
 

https://www.quantconnect.com/forum/discussion/5792/does-ib-brokerage-model-include-immediate-settlement/p1

0


Loading...

I keep getting margin called in my algorithm because of insufficient buying power. I've printed the TotalMarginUsed, MarginRemaining, Cash, and TotalPortfolioValue of self.Portfolio prior:

2019-09-04 12:00:00 2019-09-04 12:00:00 TMU:163635.91, MR:1394.14, C:492301.87, TPV:165030.05
2019-09-04 12:00:00 Order Error: id: 92, Insufficient buying power to complete order (Value:-15913.48), Reason: Id: 92, Initial Margin: -7969.72, Free Margin: 1394.14
2019-09-05 00:00:00 Order Error: id: 3, Insufficient buying power to complete order (Value:-4050), Reason: Id: 3, Initial Margin: -325, Free Margin: 0.
2019-09-05 00:00:00 Order Error: id: 44, Insufficient buying power to complete order (Value:-943.36), Reason: Id: 44, Initial Margin: -219.24, Free Margin: 0.
2019-09-05 00:00:00 Order Error: id: 58, Insufficient buying power to complete order (Value:-20020), Reason: Id: 58, Initial Margin: -274, Free Margin: 0.
2019-09-05 00:00:00 Order Error: id: 64, Insufficient buying power to complete order (Value:-13833.5), Reason: Id: 64, Initial Margin: -384.25, Free Margin: 0.
2019-09-05 00:00:00 09/05/2019 00:00:00 - Executed MarginCallOrder: <XYZ security> - Quantity: <Q> @ <P>

What I'm pretty sure is happening is, since I am doing Universe selection Resolution.Daily, at midnight my Short Sell orders get converted to Market On Open orders. But if I have a bunch of orders queued, I quickly eat up my margin as these orders get executed. Some fail because of insufficient margin. Regardless, I end up with only a small amount of margin left. Any price movement against me results in a margin call. Also, note that while in the example I had $1394.14 of margin left, I've also been called when I have $80000 in margin because of all the orders quicky eat up my buying power when they go through on open.

I've tried to do a couple things to fix this issue but none of these have worked.

1) I've tried to determine the impact on margin my orders will have. I don't execute the trade if it forces the margin remaining below 20% of TotalPortfolioValue. But this only checks if a single order disrupts the buying power and fails when multiple orders are queued.

cost = abs(q) * price
r = (self.Portfolio.MarginRemaining - cost)/self.Portfolio.TotalPortfolioValue
if r > 0.2:
self.LimitOrder(symbol, -q, price) # -q because shorting

2) I've tried to use the

OnMarginCallWarning()

method to handle low margin, but this method fires too late after I've already recieved my insufficient buying power warnings.

3) I tried handling this in the OnOrderEvent() method but this also doesn't work.

First, I tried to cancel all my outstanding orders with self.Transactions.CancelOpenOrders(), but this doesn't stop my orders from executing on market open. Then, I tried liquidating my entire portfolio:

def OnOrderEvent(self, orderEvent):
if orderEvent.Status == OrderStatus.Filled:
order = self.Transactions.GetOrderById(orderEvent.OrderId)
#self.Debug("{} Filled {} of {} at {}".format(self.Time, order.Quantity, order.Symbol, order.Price))

# invalid due to buying power issues most likely
if orderEvent.Status == OrderStatus.Invalid:
order = self.Transactions.GetOrderById(orderEvent.OrderId)
self.Liquidate()
self.Debug("{} Order Error: {} {} {}".format(self.Time, order.Symbol, order.Status, orderEvent.OrderId))

But this results in an unknown error:

[ERROR] FATAL UNHANDLED EXCEPTION: at (wrapper managed-to-native) Python.Runtime.Runtime.PyObject_Call (intptr, intptr, intptr) [0x00002] in :0, at Python.Runtime.PyObject.Invoke (Python.Runtime.PyTuple, Python.Runtime.PyDict) [0x00000] in :0, at Python.Runtime.PyObject.TryInvoke (System.Dynamic.InvokeBinder, object[],object&) [0x0001d] in :0, at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, object, QuantConnect.Orders.OrderEvent) [0x00056] in :0, at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnOrderEvent (QuantConnect.Orders.OrderEvent) [0x00049] in <5dc47b3335e14c7781b5cd695e57e0f1>:0, at QuantConnect.Lean.Engine.TransactionHandlers.BrokerageTransactionHandler.HandleOrderEvent (QuantConnect.Orders.OrderEvent) [0x003ba] in <5954f57d4b2f4fb2a5

So now I'm completely unsure of how to handle my margin calls.