Custom Fee Structures

To implement a custom fee structure for your trading system, follow the guidelines below. Custom fees are crucial for accurately simulating trading costs during backtesting and ensuring the same logic can be applied to live trading. Every custom fee model must extend the BaseFees class and adhere to the specified directory and file structure.

Fee models are only used by the back testing exchange objects. Never use fee model in the strategy since ATS assumes that all the fee modelling happens within the exchange. The default BackTrading exchange class uses Generic fee class and your custom fee models cannot be loaded to the BackTrading exchange class.

Directory Structure

  1. Place your custom fee model file in the following directory:

    custom_modules/fees/<your_fee_model>.py
  2. Implement your fee model as a Python class in the specified file.

Implementing a Custom Fee Model

  1. Class Definition:

    • Your fee model class must extend the BaseFees class and implement the _calculate() method, which contains the fee calculation logic.

    • Here's an example of a custom fee model:

    from ats.exchanges.fees.base_fees import BaseFees
    from typing import Dict, Union, Literal
    
    class CustomFeeModel(BaseFees):
        def _calculate(self, size: Union[int, float], price: Union[int, float],
                       order_type: Literal['LIMIT', 'MARKET'], order_side: Literal['BUY', 'SELL']) -> Dict:
            """
            Custom fee calculation logic.
            Args:
                size: The size of the order.
                price: The price of the base asset.
                order_type: The type of the order ('LIMIT' or 'MARKET').
                order_side: The side of the order ('BUY' or 'SELL').
    
            Returns:
                A dictionary containing the calculated fees in terms of the base and quote assets.
            """
            fee_rate = 0.001 if order_type == 'LIMIT' else 0.002  # Example: different fees for LIMIT vs MARKET orders
            base_fee = size * fee_rate
            quote_fee = size * price * fee_rate
            return {
                'base_fee': base_fee,
                'quote_fee': quote_fee
            }
  2. Key Methods to Implement:

    • _calculate():

      • This is the abstract method that must be implemented in your custom fee model.

      • It should compute the fees based on the provided size, price, order type, and order side.

      • Returns a dictionary with two keys: base_fee and quote_fee.

  3. Setting and Calculating Fees:

    • The BaseFees class provides utility methods for setting and retrieving fees:

      • Use the set(base_fee, quote_fee) method to manually set fees.

      • Use the calculate(size, price, order_type, order_side) method to calculate fees dynamically based on the custom _calculate() logic.

Using a Custom Fee Model

To use your custom fee model, import and instantiate it with the required configuration:

from src.exchanges.fees.custom_fee_model import CustomFeeModel

# Initialize the fee model with configuration
config = {"example_param": "value"}
fee_model = CustomFeeModel(config)

# Calculate fees for an order
fee_model.calculate(size=1.0, price=1000, order_type="MARKET", order_side="BUY")

# Retrieve the calculated fees
fees = fee_model.get()
print(f"Base Fee: {fees['base_fee']}, Quote Fee: {fees['quote_fee']}")

Example Directory Structure

Assuming the fee model is named custom_fee_model:

custom_modules/
├── exchanges/
│   ├── fees/
│   │   └── custom_fee_model.py

Notes on Fee Models

  • Backtesting and Live Trading:

    • In a backtesting environment (in your back testing exchange), fees are calculated dynamically using the calculate() method.

    • In live trading, fees are directly calculated directly by the live exchange.

  • Dynamic Fee Logic:

    • Your fee model can adapt to different trading scenarios, such as varying fee rates for different asset pairs, order types, or trading volumes.

  • Validation:

    • Ensure your fee model is thoroughly tested to reflect accurate trading costs. Misrepresenting fees can lead to significant discrepancies between backtesting results and live trading performance.

By following these guidelines, your custom fee model will integrate seamlessly with the system, ensuring accurate and flexible fee handling for both backtesting and live environments.

Last updated