Lean  $LEAN_TAG$
BitfinexFeeModel.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 using QuantConnect.Util;
18 
20 {
21  /// <summary>
22  /// Provides an implementation of <see cref="FeeModel"/> that models Bitfinex order fees
23  /// </summary>
24  public class BitfinexFeeModel : FeeModel
25  {
26  /// <summary>
27  /// Tier 1 maker fees
28  /// Maker fees are paid when you add liquidity to our order book by placing a limit order under the ticker price for buy and above the ticker price for sell.
29  /// https://www.bitfinex.com/fees
30  /// </summary>
31  public const decimal MakerFee = 0.001m;
32  /// <summary>
33  /// Tier 1 taker fees
34  /// Taker fees are paid when you remove liquidity from our order book by placing any order that is executed against an order of the order book.
35  /// Note: If you place a hidden order, you will always pay the taker fee. If you place a limit order that hits a hidden order, you will always pay the maker fee.
36  /// https://www.bitfinex.com/fees
37  /// </summary>
38  public const decimal TakerFee = 0.002m;
39 
40  /// <summary>
41  /// Get the fee for this order in quote currency
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 quote currency</returns>
46  public override OrderFee GetOrderFee(OrderFeeParameters parameters)
47  {
48  var order = parameters.Order;
49  var security = parameters.Security;
50  // apply fee factor, currently we do not model 30-day volume, so we use the first tier
51  var fee = TakerFee;
52  var props = order.Properties as BitfinexOrderProperties;
53 
54  if (order.Type == OrderType.Limit &&
55  props?.Hidden != true &&
56  (props?.PostOnly == true || !order.IsMarketable))
57  {
58  // limit order posted to the order book
59  fee = MakerFee;
60  }
61 
62  if (order.Direction == OrderDirection.Buy)
63  {
64  // fees taken in the received currency
65  CurrencyPairUtil.DecomposeCurrencyPair(order.Symbol, out var baseCurrency, out _);
66  return new OrderFee(new CashAmount(order.AbsoluteQuantity * fee, baseCurrency));
67  }
68 
69  // get order value in quote currency
70  var unitPrice = order.Direction == OrderDirection.Buy ? security.AskPrice : security.BidPrice;
71  if (order.Type == OrderType.Limit)
72  {
73  // limit order posted to the order book
74  unitPrice = ((LimitOrder)order).LimitPrice;
75  }
76 
77  unitPrice *= security.SymbolProperties.ContractMultiplier;
78 
79  return new OrderFee(new CashAmount(
80  unitPrice * order.AbsoluteQuantity * fee,
81  security.QuoteCurrency.Symbol));
82  }
83  }
84 }