Skip to content

Commit

Permalink
Minor improvements (QuantConnect#8223)
Browse files Browse the repository at this point in the history
- Liquidate takes into account invested flag
- Minor tweaks for symbol representation parsing
  • Loading branch information
Martin-Molinero authored Jul 23, 2024
1 parent 69fa2fd commit a46acef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 9 additions & 1 deletion Algorithm/QCAlgorithm.Trading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,11 +1244,19 @@ public List<OrderTicket> Liquidate(IEnumerable<Symbol> symbols, bool asynchronou
var orders = Transactions.GetOpenOrders(symbolToLiquidate);

// get quantity in portfolio
var quantity = Portfolio[symbolToLiquidate].Quantity;
var quantity = 0m;
var holdings = Portfolio[symbolToLiquidate];
if (holdings.Invested)
{
// invested flag might filter some quantity that's less than lot size
quantity = holdings.Quantity;
}

// if there is only one open market order that would close the position, do nothing
if (orders.Count == 1 && quantity != 0 && orders[0].Quantity == -quantity && orders[0].Type == OrderType.Market)
{
continue;
}

// cancel all open orders
var marketOrdersQuantity = 0m;
Expand Down
5 changes: 2 additions & 3 deletions Common/SymbolRepresentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,14 @@ public static Symbol ParseOptionTickerOSI(string ticker, SecurityType securityTy
/// <returns>Symbol object for the specified OSI option ticker string</returns>
public static Symbol ParseOptionTickerOSI(string ticker, SecurityType securityType, OptionStyle optionStyle, string market)
{

var match = _optionTickerRegex.Match(ticker);
if (!match.Success)
{
throw new FormatException("Invalid ticker format.");
throw new FormatException($"Invalid ticker format {ticker}");
}

var optionTicker = match.Groups[1].Value;
var expiration = DateTime.ParseExact(match.Groups[2].Value, "yyMMdd", null);
var expiration = DateTime.ParseExact(match.Groups[2].Value, DateFormat.SixCharacter, null);
OptionRight right = match.Groups[3].Value.ToUpper() == "C" ? OptionRight.Call : OptionRight.Put;
var strike = decimal.Parse(match.Groups[4].Value) / 1000m;

Expand Down

0 comments on commit a46acef

Please sign in to comment.