I am working with an options spread strategy that sometimes sees short legs getting assigned before expiration. My code looks to see if I'm holding any position in an underlying and if so liquidates both the underlying and any options still held for that underlying.
I've attached a demo project in which this works the way I expect. (The closing of assigned positions is handled in the Update of the alpha model.) The backtester shows a market order for 2100 shares of TRN right after assignment happens.
But when I try this in a "real" project, it's not working at all; I never see that order. In the Alpha Model's LiquidateEquity I added some details to try to see what's going on:
private void LiquidateEquity(Symbol symbol, QCAlgorithmFramework algorithm)
{
var current_quantity = algorithm.Securities[symbol].Holdings.Quantity;
var ordered_quantity = algorithm.Transactions.GetOpenOrders(symbol).Sum(o => o.Quantity);
var order_direction = (current_quantity + ordered_quantity) > 0 ? OrderDirection.Sell : OrderDirection.Buy;
var buying_power = algorithm.Portfolio.GetBuyingPower(symbol, order_direction);
var target_quantity = 0 - (current_quantity + ordered_quantity);
// algorithm.MarketOrder(symbol, target_quantity);
// algorithm.RemoveSecurity(symbol);
algorithm.SetHoldings(symbol, 0);
algorithm.Log($"LIQUIDATING EQUITY {symbol} current qty {current_quantity} ordered qty {ordered_quantity} buying power {buying_power} target qty {target_quantity}");
}
I see the log line "LIQUIDATING EQUITY TRN R735QTJ8XC9X current qty -2100 ordered qty 0 buying power 108984.05000000000000000000000 target qty 2100" so I know this code is getting hit. However, the backtester shows no order has been put in to close the position -- regardless of whether I use algorithm.SetHoldings, algorithm.MarketOrder, or algorithm.RemoveSecurity. It looks like I have plenty of buying power to close the position.
I also have logging in the execution model that should alert me if there are any open orders for TRN, and I never see that line. So for some reason it doesn't look like the order has been submitted.
I know that the position hasn't been closed because further along I get margin calls on the open equity position. But there doesn't seem to be anything I can do to close it.
Any ideas?
Paul Blair
One more thing -- in my "real" project the other legs of the option spread are closed successfully. It's just the equity position resulting from assignment that I can't close.
Paul Blair
I was able to get the order status back from the order to close the position; it's telling me "The security with symbol 'TRN R735QTJ8XC9X' is marked as non-tradable."
How can I make this tradable?
Paul Blair
Changing from Resolution.Hour to Resolution.Minute solved the problem. I guess that's fine.
Jared Broad
Thanks for posting your solution back Paul!
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.
Paul Blair
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!