/*
* 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 QuantConnect.Brokerages;
using QuantConnect.Securities;
using System;
namespace QuantConnect.Orders.Fees
{
///
/// Represents a fee model specific to Tastytrade.
///
///
public class TastytradeFeeModel : FeeModel
{
///
/// Represents the fee associated with equity options transactions (per contract).
///
private const decimal _optionFeeOpen = 1m;
///
/// The fee associated with futures transactions (per contract).
///
private const decimal _futureFee = 1.25m;
///
/// The fee associated with futures options transactions (per contract).
///
private const decimal _futureOptionFeeOpen = 2.5m;
///
/// Gets the order fee for a given security and order.
///
/// The parameters including the security and order details.
///
/// A instance representing the total fee for the order,
/// or if no fee is applicable.
///
public override OrderFee GetOrderFee(OrderFeeParameters parameters)
{
var feeRate = default(decimal);
switch (parameters.Security.Type)
{
case SecurityType.Option:
case SecurityType.IndexOption:
feeRate = IsOpenPosition(parameters.Order.Direction, parameters.Security.Holdings.Quantity) ? _optionFeeOpen : 0m;
break;
case SecurityType.Future:
feeRate = _futureFee;
break;
case SecurityType.FutureOption:
feeRate = IsOpenPosition(parameters.Order.Direction, parameters.Security.Holdings.Quantity) ? _futureOptionFeeOpen : 0m;
break;
default:
break;
}
return new OrderFee(new CashAmount(parameters.Order.AbsoluteQuantity * feeRate, Currencies.USD));
}
///
/// Determines whether the specified order represents the opening of a new position.
///
/// The direction of the order (buy/sell).
/// The current holdings quantity for the security.
///
/// true if the order is intended to open a new position; otherwise, false.
///
///
/// Thrown when the resolved is not recognized.
///
private static bool IsOpenPosition(OrderDirection orderDirection, decimal holdingsQuantity)
{
var orderPosition = BrokerageExtensions.GetOrderPosition(orderDirection, holdingsQuantity);
return orderPosition switch
{
OrderPosition.BuyToClose or OrderPosition.SellToClose => false,
OrderPosition.BuyToOpen or OrderPosition.SellToOpen => true,
_ => throw new NotSupportedException($"{nameof(TastytradeFeeModel)}.{nameof(IsOpenPosition)}: Unsupported order position: {orderPosition}")
};
}
}
}