Skip to content

Commit

Permalink
Rename CommissionModel to FeeModel
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 10, 2024
1 parent 563427e commit 3348b56
Show file tree
Hide file tree
Showing 22 changed files with 81 additions and 80 deletions.
4 changes: 2 additions & 2 deletions nautilus_trader/adapters/sandbox/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from nautilus_trader.backtest.exchange import SimulatedExchange
from nautilus_trader.backtest.execution_client import BacktestExecClient
from nautilus_trader.backtest.models import FillModel
from nautilus_trader.backtest.models import InstrumentSpecificPercentCommissionModel
from nautilus_trader.backtest.models import LatencyModel
from nautilus_trader.backtest.models import MakerTakerFeeModel
from nautilus_trader.cache.cache import Cache
from nautilus_trader.common.component import LiveClock
from nautilus_trader.common.component import MessageBus
Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(
msgbus=self._msgbus,
cache=cache,
fill_model=FillModel(),
commission_model=InstrumentSpecificPercentCommissionModel(),
fee_model=MakerTakerFeeModel(),
latency_model=LatencyModel(0),
clock=self.test_clock,
frozen_account=True, # <-- Freezing account
Expand Down
17 changes: 10 additions & 7 deletions nautilus_trader/backtest/engine.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ from nautilus_trader.backtest.data_client cimport BacktestDataClient
from nautilus_trader.backtest.data_client cimport BacktestMarketDataClient
from nautilus_trader.backtest.exchange cimport SimulatedExchange
from nautilus_trader.backtest.execution_client cimport BacktestExecClient
from nautilus_trader.backtest.models cimport CommissionModel
from nautilus_trader.backtest.models cimport FeeModel
from nautilus_trader.backtest.models cimport FillModel
from nautilus_trader.backtest.models cimport InstrumentSpecificPercentCommissionModel
from nautilus_trader.backtest.models cimport LatencyModel
from nautilus_trader.backtest.models cimport MakerTakerFeeModel
from nautilus_trader.backtest.modules cimport SimulationModule
from nautilus_trader.cache.base cimport CacheFacade
from nautilus_trader.common.actor cimport Actor
Expand Down Expand Up @@ -368,7 +368,7 @@ cdef class BacktestEngine:
leverages: dict[InstrumentId, Decimal] | None = None,
modules: list[SimulationModule] | None = None,
fill_model: FillModel | None = None,
commission_model: CommissionModel = InstrumentSpecificPercentCommissionModel(),
fee_model: FeeModel | None = None,
latency_model: LatencyModel | None = None,
book_type: BookType = BookType.L1_MBP,
routing: bool = False,
Expand Down Expand Up @@ -405,8 +405,8 @@ cdef class BacktestEngine:
The simulation modules to load into the exchange.
fill_model : FillModel, optional
The fill model for the exchange.
commission_model : CommissionModel, optional
The commission model for the exchange.
fee_model : FeeModel, optional
The fee model for the venue.
latency_model : LatencyModel, optional
The latency model for the exchange.
book_type : BookType, default ``BookType.L1_MBP``
Expand Down Expand Up @@ -441,11 +441,14 @@ cdef class BacktestEngine:
modules = []
if fill_model is None:
fill_model = FillModel()
if fee_model is None:
fee_model = MakerTakerFeeModel()
Condition.not_none(venue, "venue")
Condition.not_in(venue, self._venues, "venue", "_venues")
Condition.not_empty(starting_balances, "starting_balances")
Condition.list_type(modules, SimulationModule, "modules")
Condition.type_or_none(fill_model, FillModel, "fill_model")
Condition.type(fill_model, FillModel, "fill_model")
Condition.type(fee_model, FeeModel, "fee_model")

if default_leverage is None:
if account_type == AccountType.MARGIN:
Expand All @@ -468,7 +471,7 @@ cdef class BacktestEngine:
msgbus=self.kernel.msgbus,
cache=self.kernel.cache,
fill_model=fill_model,
commission_model=commission_model,
fee_model=fee_model,
latency_model=latency_model,
book_type=book_type,
clock=self.kernel.clock,
Expand Down
6 changes: 3 additions & 3 deletions nautilus_trader/backtest/exchange.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from libc.stdint cimport uint64_t
from nautilus_trader.accounting.accounts.base cimport Account
from nautilus_trader.backtest.execution_client cimport BacktestExecClient
from nautilus_trader.backtest.matching_engine cimport OrderMatchingEngine
from nautilus_trader.backtest.models cimport CommissionModel
from nautilus_trader.backtest.models cimport FeeModel
from nautilus_trader.backtest.models cimport FillModel
from nautilus_trader.backtest.models cimport LatencyModel
from nautilus_trader.cache.cache cimport Cache
Expand Down Expand Up @@ -79,8 +79,8 @@ cdef class SimulatedExchange:
"""The latency model for the exchange.\n\n:returns: `LatencyModel`"""
cdef readonly FillModel fill_model
"""The fill model for the exchange.\n\n:returns: `FillModel`"""
cdef readonly CommissionModel commission_model
"""The commission model for the exchange.\n\n:returns: `CommissionModel`"""
cdef readonly FeeModel fee_model
"""The fee model for the exchange.\n\n:returns: `FeeModel`"""
cdef readonly bint bar_execution
"""If bars should be processed by the matching engine(s) (and move the market).\n\n:returns: `bool`"""
cdef readonly bint reject_stop_orders
Expand Down
13 changes: 7 additions & 6 deletions nautilus_trader/backtest/exchange.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ from libc.stdint cimport uint64_t
from nautilus_trader.accounting.accounts.base cimport Account
from nautilus_trader.backtest.execution_client cimport BacktestExecClient
from nautilus_trader.backtest.matching_engine cimport OrderMatchingEngine
from nautilus_trader.backtest.models cimport CommissionModel
from nautilus_trader.backtest.models cimport FeeModel
from nautilus_trader.backtest.models cimport FillModel
from nautilus_trader.backtest.models cimport LatencyModel
from nautilus_trader.backtest.models cimport MakerTakerFeeModel
from nautilus_trader.backtest.modules cimport SimulationModule
from nautilus_trader.cache.base cimport CacheFacade
from nautilus_trader.common.component cimport Logger
Expand Down Expand Up @@ -89,8 +90,8 @@ cdef class SimulatedExchange:
The read-only cache for the exchange.
fill_model : FillModel
The fill model for the exchange.
commission_model : CommissionModel
The commission model for the matching engine.
fee_model : FeeModel
The fee model for the exchange.
latency_model : LatencyModel, optional
The latency model for the exchange.
clock : TestClock
Expand Down Expand Up @@ -147,7 +148,7 @@ cdef class SimulatedExchange:
CacheFacade cache not None,
TestClock clock not None,
FillModel fill_model not None,
CommissionModel commission_model not None,
FeeModel fee_model not None,
LatencyModel latency_model = None,
BookType book_type = BookType.L1_MBP,
bint frozen_account = False,
Expand Down Expand Up @@ -197,7 +198,7 @@ cdef class SimulatedExchange:
self.use_random_ids = use_random_ids
self.use_reduce_only = use_reduce_only
self.fill_model = fill_model
self.commission_model = commission_model
self.fee_model = fee_model
self.latency_model = latency_model

# Load modules
Expand Down Expand Up @@ -333,7 +334,7 @@ cdef class SimulatedExchange:
instrument=instrument,
raw_id=len(self.instruments),
fill_model=self.fill_model,
commission_model=self.commission_model,
fee_model=self.fee_model,
book_type=self.book_type,
oms_type=self.oms_type,
account_type=self.account_type,
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/backtest/matching_engine.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ from libc.stdint cimport int64_t
from libc.stdint cimport uint32_t
from libc.stdint cimport uint64_t

from nautilus_trader.backtest.models cimport CommissionModel
from nautilus_trader.backtest.models cimport FeeModel
from nautilus_trader.backtest.models cimport FillModel
from nautilus_trader.cache.base cimport CacheFacade
from nautilus_trader.common.component cimport Clock
Expand Down Expand Up @@ -77,7 +77,7 @@ cdef class OrderMatchingEngine:
cdef OrderBook _opening_auction_book
cdef OrderBook _closing_auction_book
cdef FillModel _fill_model
cdef CommissionModel _commission_model
cdef FeeModel _fee_model
# cdef object _auction_match_algo
cdef bint _bar_execution
cdef bint _reject_stop_orders
Expand Down
13 changes: 6 additions & 7 deletions nautilus_trader/backtest/matching_engine.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import uuid
from cpython.datetime cimport timedelta
from libc.stdint cimport uint64_t

from nautilus_trader.backtest.models cimport CommissionModel
from nautilus_trader.backtest.models cimport FeeModel
from nautilus_trader.backtest.models cimport FillModel
from nautilus_trader.cache.base cimport CacheFacade
from nautilus_trader.common.component cimport LogColor
Expand Down Expand Up @@ -111,8 +111,8 @@ cdef class OrderMatchingEngine:
The raw integer ID for the instrument.
fill_model : FillModel
The fill model for the matching engine.
commission_model : CommissionModel
The commission model for the matching engine.
fee_model : FeeModel
The fee model for the matching engine.
book_type : BookType
The order book type for the engine.
oms_type : OmsType
Expand Down Expand Up @@ -153,7 +153,7 @@ cdef class OrderMatchingEngine:
Instrument instrument not None,
uint32_t raw_id,
FillModel fill_model not None,
CommissionModel commission_model not None,
FeeModel fee_model not None,
BookType book_type,
OmsType oms_type,
AccountType account_type,
Expand Down Expand Up @@ -191,7 +191,7 @@ cdef class OrderMatchingEngine:
self._use_reduce_only = use_reduce_only
# self._auction_match_algo = auction_match_algo
self._fill_model = fill_model
self._commission_model = commission_model
self._fee_model = fee_model
self._book = OrderBook(
instrument_id=instrument.id,
book_type=book_type,
Expand Down Expand Up @@ -1770,8 +1770,7 @@ cdef class OrderMatchingEngine:
order.liquidity_side = liquidity_side

# Calculate commission
cdef Money commission
commission = self._commission_model.get_commission(
cdef Money commission = self._fee_model.get_commission(
order=order,
fill_qty=last_qty,
fill_px=last_px,
Expand Down
15 changes: 5 additions & 10 deletions nautilus_trader/backtest/models.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,12 @@ cdef class LatencyModel:
"""The latency (nanoseconds) for order cancel messages to reach the exchange.\n\n:returns: `int`"""


cdef class CommissionModel:
cdef class FeeModel:
cpdef Money get_commission(self, Order order, Quantity fill_qty, Price fill_px, Instrument instrument)


cdef class InstrumentSpecificPercentCommissionModel(CommissionModel):
"""
Provide a commission model for trades based on a percentage of the notional value
of the trade.
cdef class MakerTakerFeeModel(FeeModel):
pass

"""

cdef class FixedCommissionModel(CommissionModel):
cdef Money commission
"""The constant commission."""
cdef class FixedCommissionModel(FeeModel):
cdef Money _commission
19 changes: 11 additions & 8 deletions nautilus_trader/backtest/models.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ cdef class LatencyModel:
self.cancel_latency_nanos = base_latency_nanos + cancel_latency_nanos


cdef class CommissionModel:
cdef class FeeModel:
"""
Provide an abstract commission model for trades.
Provides an abstract fee model for trades.
"""

cpdef Money get_commission(
self,
Order order,
Expand Down Expand Up @@ -196,10 +197,10 @@ cdef class CommissionModel:
raise NotImplementedError("Method 'get_commission' must be implemented in a subclass.")


cdef class InstrumentSpecificPercentCommissionModel(CommissionModel):
cdef class MakerTakerFeeModel(FeeModel):
"""
Provide a commission model for trades based on a percentage of the notional value
of the trade.
Provide a fee model for trades based on a maker/taker fee schedule
and notional value of the trade.
"""

Expand Down Expand Up @@ -235,7 +236,7 @@ cdef class InstrumentSpecificPercentCommissionModel(CommissionModel):
return commission


cdef class FixedCommissionModel(CommissionModel):
cdef class FixedCommissionModel(FeeModel):
"""
Provides a fixed commission model for trades.
Expand All @@ -253,7 +254,9 @@ cdef class FixedCommissionModel(CommissionModel):

def __init__(self, Money commission):
Condition.type(commission, Money, "commission")
self.commission = commission
Condition.positive(commission, "commission")

self._commission = commission

cpdef Money get_commission(
self,
Expand All @@ -262,4 +265,4 @@ cdef class FixedCommissionModel(CommissionModel):
Price fill_px,
Instrument instrument,
):
return self.commission
return self._commission
14 changes: 7 additions & 7 deletions tests/unit_tests/backtest/test_commission_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# fmt: off
from nautilus_trader.backtest.models import FixedCommissionModel
from nautilus_trader.backtest.models import InstrumentSpecificPercentCommissionModel
from nautilus_trader.backtest.models import MakerTakerFeeModel
from nautilus_trader.model.currencies import USD
from nautilus_trader.model.enums import OrderSide
from nautilus_trader.model.instruments.base import Instrument
Expand Down Expand Up @@ -55,10 +55,10 @@ def sell_order(instrument: Instrument) -> Order:
def test_fixed_commission(buy_order, instrument):
# Arrange
expected = Money(1, USD)
commission_model = FixedCommissionModel(expected)
fee_model = FixedCommissionModel(expected)

# Act
commission = commission_model.get_commission(
commission = fee_model.get_commission(
buy_order,
buy_order.quantity,
Price.from_str("1.1234"),
Expand All @@ -71,11 +71,11 @@ def test_fixed_commission(buy_order, instrument):

def test_instrument_percent_commission_maker(instrument, buy_order):
# Arrange
commission_model = InstrumentSpecificPercentCommissionModel()
fee_model = MakerTakerFeeModel()
expected = buy_order.quantity * buy_order.price * instrument.maker_fee

# Act
commission = commission_model.get_commission(
commission = fee_model.get_commission(
buy_order,
buy_order.quantity,
buy_order.price,
Expand All @@ -89,11 +89,11 @@ def test_instrument_percent_commission_maker(instrument, buy_order):

def test_instrument_percent_commission_taker(instrument, sell_order):
# Arrange
commission_model = InstrumentSpecificPercentCommissionModel()
fee_model = MakerTakerFeeModel()
expected = sell_order.quantity * sell_order.price * instrument.taker_fee

# Act
commission = commission_model.get_commission(
commission = fee_model.get_commission(
sell_order,
sell_order.quantity,
sell_order.price,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/backtest/test_exchange_bitmex.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from nautilus_trader.backtest.exchange import SimulatedExchange
from nautilus_trader.backtest.execution_client import BacktestExecClient
from nautilus_trader.backtest.models import FillModel
from nautilus_trader.backtest.models import InstrumentSpecificPercentCommissionModel
from nautilus_trader.backtest.models import LatencyModel
from nautilus_trader.backtest.models import MakerTakerFeeModel
from nautilus_trader.common.component import MessageBus
from nautilus_trader.common.component import TestClock
from nautilus_trader.data.engine import DataEngine
Expand Down Expand Up @@ -104,7 +104,7 @@ def setup(self):
instruments=[XBTUSD_BITMEX],
modules=[],
fill_model=FillModel(),
commission_model=InstrumentSpecificPercentCommissionModel(),
fee_model=MakerTakerFeeModel(),
clock=self.clock,
latency_model=LatencyModel(0),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from nautilus_trader.backtest.exchange import SimulatedExchange
from nautilus_trader.backtest.execution_client import BacktestExecClient
from nautilus_trader.backtest.models import FillModel
from nautilus_trader.backtest.models import InstrumentSpecificPercentCommissionModel
from nautilus_trader.backtest.models import LatencyModel
from nautilus_trader.backtest.models import MakerTakerFeeModel
from nautilus_trader.common.component import MessageBus
from nautilus_trader.common.component import TestClock
from nautilus_trader.data.engine import DataEngine
Expand Down Expand Up @@ -108,7 +108,7 @@ def setup(self):
instruments=[ETHUSDT_PERP_BINANCE],
modules=[],
fill_model=FillModel(),
commission_model=InstrumentSpecificPercentCommissionModel(),
fee_model=MakerTakerFeeModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/backtest/test_exchange_cash.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from nautilus_trader.backtest.exchange import SimulatedExchange
from nautilus_trader.backtest.execution_client import BacktestExecClient
from nautilus_trader.backtest.models import FillModel
from nautilus_trader.backtest.models import InstrumentSpecificPercentCommissionModel
from nautilus_trader.backtest.models import LatencyModel
from nautilus_trader.backtest.models import MakerTakerFeeModel
from nautilus_trader.common.component import MessageBus
from nautilus_trader.common.component import TestClock
from nautilus_trader.config import ExecEngineConfig
Expand Down Expand Up @@ -100,7 +100,7 @@ def setup(self) -> None:
instruments=[_AAPL_XNAS],
modules=[],
fill_model=FillModel(),
commission_model=InstrumentSpecificPercentCommissionModel(),
fee_model=MakerTakerFeeModel(),
portfolio=self.portfolio,
msgbus=self.msgbus,
cache=self.cache,
Expand Down
Loading

0 comments on commit 3348b56

Please sign in to comment.