Lean  $LEAN_TAG$
AlpacaFeeModel.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
19 
21 {
22  /// <summary>
23  /// Represents the fee model specific to Alpaca trading platform.
24  /// </summary>
25  /// <remarks>This class inherits from <see cref="FeeModel"/> and provides the fee structure for Alpaca trades.
26  /// It implements the <see cref="IFeeModel"/> interface and should be used for calculating fees on the Alpaca platform.</remarks>
27  public class AlpacaFeeModel : FeeModel
28  {
29  /// <summary>
30  /// The fee percentage for a maker transaction in cryptocurrency.
31  /// </summary>
32  /// <see href="https://docs.alpaca.markets/docs/crypto-fees"/>
33  public const decimal MakerCryptoFee = 0.0015m;
34 
35  /// <summary>
36  /// The fee percentage for a taker transaction in cryptocurrency.
37  /// </summary>
38  public const decimal TakerCryptoFee = 0.0025m;
39 
40  /// <summary>
41  /// Gets the order fee associated with the specified order.
42  /// </summary>
43  /// <param name="parameters">A <see cref="OrderFeeParameters"/> object
44  /// containing the security and order</param>
45  /// <returns>The cost of the order in a <see cref="CashAmount"/> instance</returns>
46  public override OrderFee GetOrderFee(OrderFeeParameters parameters)
47  {
48  var security = parameters.Security;
49  if (security.Symbol.ID.SecurityType == SecurityType.Crypto)
50  {
51  var order = parameters.Order;
52  var fee = GetFee(order, MakerCryptoFee, TakerCryptoFee);
53  CashAmount cashAmount;
54  Crypto.DecomposeCurrencyPair(security.Symbol, security.SymbolProperties, out var baseCurrency, out var quoteCurrency);
55  if (order.Direction == OrderDirection.Buy)
56  {
57  // base currency, deducted from what we bought
58  cashAmount = new CashAmount(order.AbsoluteQuantity * fee, baseCurrency);
59  }
60  else
61  {
62  // quote currency
63  var positionValue = order.AbsoluteQuantity * security.Price;
64  cashAmount = new CashAmount(positionValue * fee, quoteCurrency);
65  }
66  return new OrderFee(cashAmount);
67  }
68  return new OrderFee(new CashAmount(0, Currencies.USD));
69  }
70 
71  /// <summary>
72  /// Calculates the fee for a given order based on whether it is a maker or taker order.
73  /// </summary>
74  /// <param name="order">The order for which the fee is being calculated.</param>
75  /// <param name="makerFee">The fee percentage for maker orders.</param>
76  /// <param name="takerFee">The fee percentage for taker orders.</param>
77  /// <returns>The calculated fee for the given order.</returns>
78  private static decimal GetFee(Order order, decimal makerFee, decimal takerFee)
79  {
80  if (order.Type == OrderType.Limit && !order.IsMarketable)
81  {
82  return makerFee;
83  }
84  return takerFee;
85  }
86  }
87 }