Adding a Strategy
To add a custom trading strategy to the project, follow these guidelines. Every custom strategy must extend the BaseStrategy
class and adhere to the specified directory and file structure.
Directory Structure
Create a directory for your custom strategy inside
custom_modules/strategies/
:Implement your strategy class in a file named
strategy.py
within the directory.Assuming the strategy is named
my_strategy
:By following these guidelines, your custom strategy will integrate seamlessly with the project, ensuring compatibility and adherence to project conventions. This modular structure allows for easy addition, management, and testing of new strategies.
Implementing a Custom Strategy
Class Definition:
Your strategy class must be named
Strategy
and extend theBaseStrategy
class.Here's a basic example of a custom strategy implementation:
Requirements:
Ensure your class implements the following abstract methods from
BaseStrategy
:validate_config()
: Validates the configuration provided for the strategy.on_candle(candle: Candle)
: Defines the main logic for processing incoming candle data.
Optionally, you can override the following methods from
BaseStrategy
:on_stop()
: Called when the strategy is stopped. This method can be used to clean up resources or save the state.
States and Configuration:
Use the
state
object (SimpleState
) to manage the internal state of the strategy. This ensures that the strategy's state can be saved and restored, enabling seamless restarts.Validate all required configurations in the
validate_config()
method to ensure that the strategy has everything it needs to function correctly.
Setting the Exchange:
The
set_exchange(exchange: BaseExchange)
method will be called by the trading job to associate the strategy with an exchange. You don’t need to implement this method unless additional logic is required.
Accessing Exchange from the Strategy
Once the exchange is set using the set_exchange()
method, you can interact with the exchange through the self.exchange
object. This allows you to:
Place and manage orders
Fetch wallet balances
Access other exchange-specific functionalities
For example:
Configuration Validation
To ensure that your strategy functions correctly, all necessary configurations must be validated in the validate_config()
method. Raise an exception if any required configuration key is missing or invalid.
Notes on Strategy Logic
Event Handling: The
on_candle()
method is where the main logic of the strategy resides. It is triggered whenever a new candle is received from the exchange.State Management: Use the
self._state
object to store key-value pairs representing the strategy's state. This enables saving and reloading the state when the strategy restarts.Exchange Interaction: After the exchange is set using
set_exchange()
, you can interact with the exchange through theself.exchange
object for placing orders, retrieving balances, etc.
Last updated