Skip to content

Commit

Permalink
Introduce new OrderPosition enum (#7730)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul authored Jan 29, 2024
1 parent 3a10afe commit ec530d8
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Brokerages/Brokerage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,24 @@ public virtual IEnumerable<BaseData> GetHistory(HistoryRequest request)
return Enumerable.Empty<BaseData>();
}

/// <summary>
/// Gets the position that might result given the specified order direction and the current holdings quantity.
/// This is useful for brokerages that require more specific direction information than provided by the OrderDirection enum
/// (e.g. Tradier differentiates Buy/Sell and BuyToOpen/BuyToCover/SellShort/SellToClose)
/// </summary>
/// <param name="orderDirection">The order direction</param>
/// <param name="holdingsQuantity">The current holdings quantity</param>
/// <returns>The order position</returns>
protected static OrderPosition GetOrderPosition(OrderDirection orderDirection, decimal holdingsQuantity)
{
return orderDirection switch
{
OrderDirection.Buy => holdingsQuantity >= 0 ? OrderPosition.BuyToOpen : OrderPosition.BuyToClose,
OrderDirection.Sell => holdingsQuantity <= 0 ? OrderPosition.SellToOpen : OrderPosition.SellToClose,
_ => throw new ArgumentOutOfRangeException(nameof(orderDirection), orderDirection, "Invalid order direction")
};
}

#region IBrokerageCashSynchronizer implementation

/// <summary>
Expand Down
26 changes: 26 additions & 0 deletions Common/Orders/OrderTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ public enum OrderDirection
Hold
}

/// <summary>
/// Position of the order
/// </summary>
public enum OrderPosition
{
/// <summary>
/// Indicates the buy order will result in a long position, starting either from zero or an existing long position (0)
/// </summary>
BuyToOpen,

/// <summary>
/// Indicates the buy order is starting from an existing short position, resulting in a closed or long position (1)
/// </summary>
BuyToClose,

/// <summary>
/// Indicates the sell order will result in a short position, starting either from zero or an existing short position (2)
/// </summary>
SellToOpen,

/// <summary>
/// Indicates the sell order is starting from an existing long position, resulting in a closed or short position (3)
/// </summary>
SellToClose,
}

/// <summary>
/// Fill status of the order class.
/// </summary>
Expand Down
96 changes: 96 additions & 0 deletions Tests/Brokerages/DefaultBrokerageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using NUnit.Framework;
using QuantConnect.Brokerages;
using QuantConnect.Orders;
using QuantConnect.Securities;

namespace QuantConnect.Tests.Brokerages
{
/// <summary>
/// Additional tests for the base <see cref="Brokerage"/> class
/// </summary>
public class DefaultBrokerageTests
{
[TestCase(OrderDirection.Buy, 0, ExpectedResult = OrderPosition.BuyToOpen)]
[TestCase(OrderDirection.Buy, 100, ExpectedResult = OrderPosition.BuyToOpen)]
[TestCase(OrderDirection.Buy, -100, ExpectedResult = OrderPosition.BuyToClose)]
[TestCase(OrderDirection.Sell, 0, ExpectedResult = OrderPosition.SellToOpen)]
[TestCase(OrderDirection.Sell, 100, ExpectedResult = OrderPosition.SellToClose)]
[TestCase(OrderDirection.Sell, -100, ExpectedResult = OrderPosition.SellToOpen)]
public OrderPosition GetsOrderPosition(OrderDirection direction, decimal holdingsQuantity)
{
return TestableBrokerage.GetOrderPositionPublic(direction, holdingsQuantity);
}

private class TestableBrokerage : Brokerage
{
public TestableBrokerage(string name) : base(name)
{
}

public override bool IsConnected => throw new NotImplementedException();

public override bool CancelOrder(Order order)
{
throw new NotImplementedException();
}

public override void Connect()
{
throw new NotImplementedException();
}

public override void Disconnect()
{
throw new NotImplementedException();
}

public override List<Holding> GetAccountHoldings()
{
throw new NotImplementedException();
}

public override List<CashAmount> GetCashBalance()
{
throw new NotImplementedException();
}

public override List<Order> GetOpenOrders()
{
throw new NotImplementedException();
}

public override bool PlaceOrder(Order order)
{
throw new NotImplementedException();
}

public override bool UpdateOrder(Order order)
{
throw new NotImplementedException();
}

public static OrderPosition GetOrderPositionPublic(OrderDirection direction, decimal holdingsQuantity)
{
return GetOrderPosition(direction, holdingsQuantity);
}
}

}
}

0 comments on commit ec530d8

Please sign in to comment.