Skip to content

Commit

Permalink
python version
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisSzeto committed Dec 9, 2024
1 parent 84e4be8 commit d26361c
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.

from AlgorithmImports import *
from Orders.Fees.InteractiveBrokersTieredFeeModel import InteractiveBrokersTieredFeeModel

### <summary>
### Test algorithm using "InteractiveBrokersTieredFeeModel"
Expand Down
21 changes: 7 additions & 14 deletions Common/Orders/Fees/InteractiveBrokersFeeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal static void ProcessFutureRateSchedule(decimal monthlyFutureTradeVolume,
}

/// <summary>
/// Determines which tier an account falls into based on the monthly trading volume of forex
/// Determines which tier an account falls into based on the monthly trading volume of Forex
/// </summary>
/// <remarks>https://www.interactivebrokers.com/en/pricing/commissions-spot-currencies.php?re=amer</remarks>
internal static void ProcessForexRateSchedule(decimal monthlyForexTradeAmountInUSDollars, out decimal commissionRate, out decimal minimumOrderFee)
Expand Down Expand Up @@ -100,7 +100,7 @@ internal static void ProcessForexRateSchedule(decimal monthlyForexTradeAmountInU
}

/// <summary>
/// Determines which tier an account falls into based on the monthly trading volume of options
/// Determines which tier an account falls into based on the monthly trading volume of Options
/// </summary>
/// <remarks>https://www.interactivebrokers.com/en/pricing/commissions-options.php?re=amer</remarks>
internal static void ProcessOptionsRateSchedule(decimal monthlyOptionsTradeAmountInContracts, out Func<decimal, decimal, CashAmount> optionsCommissionFunc)
Expand Down Expand Up @@ -140,7 +140,7 @@ internal static void ProcessOptionsRateSchedule(decimal monthlyOptionsTradeAmoun
}

/// <summary>
/// Determines which tier an account falls into based on the monthly trading volume of cryptos
/// Determines which tier an account falls into based on the monthly trading volume of Crypto
/// </summary>
/// <remarks>https://www.interactivebrokers.com/en/pricing/commissions-cryptocurrencies.php?re=amer</remarks>
internal static void ProcessCryptoRateSchedule(decimal monthlyCryptoTradeAmountInUSDollars, out decimal commissionRate)
Expand Down Expand Up @@ -202,7 +202,7 @@ internal static decimal CalculateOptionFee(Security security, Order order, decim
/// <summary>
/// Calculate the transaction fee of a Future or FOP order
/// </summary>
internal static void CalculateFutureFopFee(Security security, Order order, decimal quantity, string market,
internal static void CalculateFutureFopFee(Security security, decimal quantity, string market,
Dictionary<string, Func<Security, CashAmount>> feeRef, out decimal fee, out string currency)
{
// The futures options fee model is exactly the same as futures' fees on IB.
Expand All @@ -229,7 +229,7 @@ internal static void CalculateFutureFopFee(Security security, Order order, decim
/// Calculate the transaction fee of an Equity order
/// </summary>
/// <returns>Commission part of the transaction cost</returns>
internal static decimal CalculateEquityFee(Security security, Order order, decimal quantity, decimal tradeValue, string market,
internal static void CalculateEquityFee(decimal quantity, decimal tradeValue, string market,
decimal usFeeRate, decimal usMinimumFee, out decimal fee, out string currency)
{
EquityFee equityFee;
Expand Down Expand Up @@ -263,8 +263,6 @@ internal static decimal CalculateEquityFee(Security security, Order order, decim
currency = equityFee.Currency;
//Always return a positive fee.
fee = Math.Abs(tradeFee);

return tradeFee;
}

/// <summary>
Expand Down Expand Up @@ -293,8 +291,8 @@ internal static decimal CalculateCryptoFee(Security security, Order order, decim
decimal cryptoMinimumOrderFee, out decimal fee, out string currency)
{
// get the total trade value in the USD
var totalTradeValue = order.GetValue(security);
var cryptoFee = Math.Abs(cryptoCommissionRate*totalTradeValue);
var totalTradeValue = Math.Abs(order.GetValue(security));
var cryptoFee = cryptoCommissionRate*totalTradeValue;
// 1% maximum fee
fee = Math.Max(Math.Min(totalTradeValue * 0.01m, cryptoMinimumOrderFee), cryptoFee);
// IB Crypto fees are all in USD
Expand Down Expand Up @@ -399,10 +397,6 @@ internal static decimal GetEquityExchangeFee(Order order, Exchange exchange, dec
{
return order.AbsoluteQuantity * 0.0005m;
}
else if (exchange == Exchange.ARCA)
{
return order.AbsoluteQuantity * 0.0015m;
}
else if (exchange == Exchange.BATS)
{
return order.AbsoluteQuantity * 0.00075m;
Expand Down Expand Up @@ -494,7 +488,6 @@ internal static decimal GetPotentialOrderPrice(Order order, Security security)
case OrderType.MarketOnOpen:
case OrderType.MarketOnClose:
case OrderType.Market:
decimal securityPrice;
if (order.Direction == OrderDirection.Buy)
{
price = security.BidPrice;
Expand Down
4 changes: 2 additions & 2 deletions Common/Orders/Fees/InteractiveBrokersFeeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ public override OrderFee GetOrderFee(OrderFeeParameters parameters)

case SecurityType.Future:
case SecurityType.FutureOption:
InteractiveBrokersFeeHelper.CalculateFutureFopFee(security, order, quantity, market, _futureFee, out feeResult, out feeCurrency);
InteractiveBrokersFeeHelper.CalculateFutureFopFee(security, quantity, market, _futureFee, out feeResult, out feeCurrency);
break;

case SecurityType.Equity:
var tradeValue = Math.Abs(order.GetValue(security));
InteractiveBrokersFeeHelper.CalculateEquityFee(security, order, quantity, tradeValue, market, 0.005m, 1m, out feeResult, out feeCurrency);
InteractiveBrokersFeeHelper.CalculateEquityFee(quantity, tradeValue, market, 0.005m, 1m, out feeResult, out feeCurrency);
break;

case SecurityType.Cfd:
Expand Down
14 changes: 8 additions & 6 deletions Common/Orders/Fees/InteractiveBrokersTieredFeeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ public override OrderFee GetOrderFee(OrderFeeParameters parameters)
(0.01915m + 0.0048m) * quantity :
0.0048m * quantity;
// Transaction Fees: SEC Transaction Fee + FINRA Trading Activity Fee (only charge on sell)
var transaction = order.Quantity < 0 ? 0.0000278m * Math.Abs(order.GetValue(security)) + 0.00279m * quantity : 0m;
var transaction = order.Direction == OrderDirection.Sell ?
0.0000278m * Math.Abs(order.GetValue(security)) + 0.00279m * quantity :
0m;
// Clearing Fee
var clearing = Math.Min(0.02m * quantity, 55m);

Expand All @@ -162,14 +164,14 @@ public override OrderFee GetOrderFee(OrderFeeParameters parameters)

case SecurityType.Future:
case SecurityType.FutureOption:
InteractiveBrokersFeeHelper.CalculateFutureFopFee(security, order, quantity, market, _futureFee, out feeResult, out feeCurrency);
InteractiveBrokersFeeHelper.CalculateFutureFopFee(security, quantity, market, _futureFee, out feeResult, out feeCurrency);
// Update the monthly contracts traded
_monthlyTradeVolume[SecurityType.Future] += quantity;
break;

case SecurityType.Equity:
var tradeValue = Math.Abs(order.GetValue(security));
var tradeFee = InteractiveBrokersFeeHelper.CalculateEquityFee(security, order, quantity, tradeValue, market, _equityCommissionRate, EquityMinimumOrderFee, out feeResult, out feeCurrency);
InteractiveBrokersFeeHelper.CalculateEquityFee(quantity, tradeValue, market, _equityCommissionRate, EquityMinimumOrderFee, out feeResult, out feeCurrency);

// Tiered fee model has the below extra cost.
// FINRA Trading Activity Fee only applies to sale of security.
Expand All @@ -181,11 +183,11 @@ public override OrderFee GetOrderFee(OrderFeeParameters parameters)
// Clearing Fee: NSCC, DTC Fees.
var clearingFee = Math.Min(quantity * 0.0002m, tradeValue * 0.005m);
// Exchange related handling fees.
var exchangeFee = InteractiveBrokersFeeHelper.GetEquityExchangeFee(order, (security as Equity).PrimaryExchange, tradeValue, tradeFee);
var exchangeFee = InteractiveBrokersFeeHelper.GetEquityExchangeFee(order, (security as Equity).PrimaryExchange, tradeValue, feeResult);
// FINRA Pass Through Fees.
var passThroughFee = Math.Min(8.3m, tradeFee * 0.00056m);
var passThroughFee = Math.Min(8.3m, feeResult * 0.00056m);

feeResult = feeResult + regulatoryFee + clearingFee + exchangeFee + passThroughFee;
feeResult += regulatoryFee + clearingFee + exchangeFee + passThroughFee;

// Update the monthly volume shares traded
_monthlyTradeVolume[SecurityType.Equity] += quantity;
Expand Down
Loading

0 comments on commit d26361c

Please sign in to comment.