BaseStrategy
class backtide.strategies.base.BaseStrategy()[source]
Abstract base class for all strategies.
Subclass it to create a custom strategy.
Example
from backtide.strategies import BaseStrategy
class MyStrategy(BaseStrategy):
def __init__(self, threshold=0.02):
self.threshold = threshold
def evaluate(self, data, portfolio, state, indicators):
orders = []
# Your logic here ...
return orders
Methods
| evaluate | Evaluate the strategy and return orders. |
| log | Write a message to the experiment log. |
method evaluate(data, portfolio, state, indicators)[source]
Evaluate the strategy and return orders.
| Parameters |
data : dict[str, pandas.DataFrame | polars.DataFrame]
Keys are the experiment's symbols and values are the historical
OHLCV data available up to the current bar. For example,
portfolio : backtide.backtest.Portfoliodata["AAPL"]["close"] selects AAPL's visible close-price history.
Current portfolio holdings (cash, positions and open orders). For
example,
state : backtide.backtest.Stateportfolio.positions.get("AAPL", 0.0) returns the current
signed quantity, while portfolio.orders contains pending orders.
Current simulation state. For example, use
indicators : dict[str,dict[str,pd.Series | pd.DataFrame | pl.Series | pl.DataFrame]] | Nonestate.is_warmup to
suppress orders during warmup and state.datetime to read the
current bar's timezone-aware timestamp.
The first keys are the indicator names. The second keys are the
experiment's symbols. The values are the pre-computed indicator
histories available up to the current bar. For example,
indicators["SMA_20"]["AAPL"] selects AAPL's visible 20-bar SMA
history. None is permitted when no indicators were selected.
|
| Returns |
list[Order]
Orders to place this tick.
|
method log(message, level="info")[source]
Write a message to the experiment log.
Messages appear in the live log viewer while the experiment
runs and are persisted to the experiment's logs.txt file.
Example
def evaluate(self, data, portfolio, state, indicators):
self.log(f"Bar {state.bar_index}: evaluating...")
...
| Parameters |
The message to log.
level : str | LogLevel, default="info"
Tracing log level. Choose from: "error", "warn", "info", "debug".
|