Skip to content

Commit

Permalink
feat: coinbase fee model + test
Browse files Browse the repository at this point in the history
  • Loading branch information
Romazes committed Jan 2, 2024
1 parent ef35617 commit 1d7f18a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Common/Brokerages/CoinbaseBrokerageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ public override bool CanUpdateOrder(Security security, Order order, UpdateOrderR
{
message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "NotSupported",
Messages.DefaultBrokerageModel.InvalidOrderQuantity(security, request.Quantity.Value));
return false;
}
return false;
}

message = null;
return true;
Expand Down
58 changes: 50 additions & 8 deletions Common/Orders/Fees/CoinbaseFeeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@ namespace QuantConnect.Orders.Fees
/// </summary>
public class CoinbaseFeeModel : FeeModel
{
/// <summary>
/// Level Advanced 1 maker fee
/// Tab "Fee tiers" on <see href="https://www.coinbase.com/advanced-fees"/>
/// </summary>
public const decimal MakerAdvanced1 = 0.006m;

/// <summary>
/// Level Advanced 1 taker fee
/// Tab "Fee tiers" on <see href="https://www.coinbase.com/advanced-fees"/>
/// </summary>
public const decimal TakerAdvanced1 = 0.008m;

/// <summary>
/// Stable Pairs maker fee
/// Tab "Stable pairs" on <see href="https://www.coinbase.com/advanced-fees"/>
/// </summary>
public const decimal MakerStablePairs = 0m;

/// <summary>
/// Stable Pairs taker fee
/// Tab "Stable pairs" on <see href="https://www.coinbase.com/advanced-fees"/>
/// </summary>
public const decimal TakerStableParis = 0.00001m;

private readonly decimal _makerFee;

private readonly decimal _takerFee;

/// <summary>
/// Create Coinbase Fee model setting fee values
/// </summary>
/// <param name="makerFee">Maker fee value</param>
/// <param name="takerFee">Taker fee value</param>
/// <remarks>By default: use Level Advanced 1 fees</remarks>
public CoinbaseFeeModel(decimal makerFee = MakerAdvanced1, decimal takerFee = TakerAdvanced1)
{
_makerFee = makerFee;
_takerFee = takerFee;
}

/// <summary>
/// Get the fee for this order in quote currency
/// </summary>
Expand All @@ -32,21 +72,22 @@ public class CoinbaseFeeModel : FeeModel
/// <returns>The cost of the order in quote currency</returns>
public override OrderFee GetOrderFee(OrderFeeParameters parameters)
{
if(parameters == null)
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters), "The 'parameters' argument cannot be null.");
}

var order = parameters.Order;
var security = parameters.Security;
var props = order.Properties as CoinbaseOrderProperties;

// marketable limit orders are considered takers
var isMaker = order.Type == OrderType.Limit && !order.IsMarketable;
var isMaker = order.Type == OrderType.Limit && ((props != null && props.PostOnly) || !order.IsMarketable);

// Check if the current symbol is a StableCoin
var isStableCoin = Currencies.StablePairsCoinbase.Contains(security.Symbol.Value);

var feePercentage = GetFeePercentage(order.Time, isMaker, isStableCoin);
var feePercentage = GetFeePercentage(order.Time, isMaker, isStableCoin, _makerFee, _takerFee);

// get order value in quote currency, then apply maker/taker fee factor
var unitPrice = order.Direction == OrderDirection.Buy ? security.AskPrice : security.BidPrice;
Expand All @@ -65,13 +106,14 @@ public override OrderFee GetOrderFee(OrderFeeParameters parameters)
/// <param name="utcTime">The date/time requested (UTC)</param>
/// <param name="isMaker">true if the maker percentage fee is requested, false otherwise</param>
/// <param name="isStableCoin">true if the order security symbol is a StableCoin, false otherwise</param>
/// <returns>The fee percentage effective at the requested date</returns>
public static decimal GetFeePercentage(DateTime utcTime, bool isMaker, bool isStableCoin)
/// <param name="makerFee">maker fee amount</param>
/// <param name="takerFee">taker fee amount</param>
/// <returns>The fee percentage</returns>
protected static decimal GetFeePercentage(DateTime utcTime, bool isMaker, bool isStableCoin, decimal makerFee, decimal takerFee)
{
// Advanced Trade fees: Stable pairs: 0.00% Maker | 0.001% Taker
if (isStableCoin)
{
return isMaker ? 0m : 0.001m;
return isMaker ? MakerStablePairs : TakerStableParis;
}
else if (utcTime < new DateTime(2019, 3, 23, 1, 30, 0))
{
Expand All @@ -85,7 +127,7 @@ public static decimal GetFeePercentage(DateTime utcTime, bool isMaker, bool isSt
// https://www.coinbase.com/advanced-fees
// Level | Trading amount | Spot fees (Maker | Taker)
// Advanced 1 | >= $0 | 0.60% | 0.80%
return isMaker ? 0.006m : 0.008m;
return isMaker ? makerFee : takerFee;
}
}
}
22 changes: 22 additions & 0 deletions Tests/Common/Orders/Fees/CoinbaseFeeModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void ReturnsExpectedFeeWithStableCoins()
[TestCase(2019, 3, 23, 1, 29, 59, 0.3)]
[TestCase(2019, 3, 23, 1, 30, 0, 0.25)]
[TestCase(2019, 4, 1, 0, 0, 0, 0.25)]
[TestCase(2024, 1, 2, 0, 0, 0, 0.8)]
public void FeeChangesOverTime(int year, int month, int day, int hour, int minute, int second, decimal expectedFee)
{
var time = new DateTime(year, month, day, hour, minute, second);
Expand All @@ -143,5 +144,26 @@ public void FeeChangesOverTime(int year, int month, int day, int hour, int minut
// 100 (price) * fee (taker fee)
Assert.AreEqual(expectedFee, fee.Value.Amount);
}

[TestCase(0.0035, 0.0055, false, 0.55)]
[TestCase(0.0035, 0.0055, true, 0.35)]
[TestCase(0.0025, 0.004, true, 0.25)]
public void CustomCoinbaseFeeModelPlusCoinbaseOrderProperty(decimal customMakerFee, decimal customTakerFee, bool postOnly, decimal expectedFee)
{
decimal orderAmount = -1m;
IFeeModel customFeeModel = new CoinbaseFeeModel(customMakerFee, customTakerFee);

var dateTime = new DateTime(2024, 1, 2, 0, 0, 0);
var orderProperty = new CoinbaseOrderProperties() { PostOnly = postOnly };

var fee = customFeeModel.GetOrderFee(new OrderFeeParameters(_btcusd, new LimitOrder(_btcusd.Symbol, orderAmount, 99, dateTime, "fee", orderProperty)
{
OrderSubmissionData = new OrderSubmissionData(_btcusd.BidPrice, _btcusd.AskPrice, _btcusd.Price)
}));

Assert.AreEqual(Currencies.USD, fee.Value.Currency);
// (order.Direction == Buy ? AskPrice : BidPrice) * orderAmount * (maker)fee || (taker)fee
Assert.AreEqual(expectedFee, fee.Value.Amount);
}
}
}

0 comments on commit 1d7f18a

Please sign in to comment.