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
Place your custom fee model file in the following directory:
Implement your fee model as a Python class in the specified file.
Implementing a Custom Fee Model
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:
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
andquote_fee
.
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:
Example Directory Structure
Assuming the fee model is named custom_fee_model
:
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