|
| 1 | +# Core Concepts |
| 2 | + |
| 3 | +To effectively use Stochastix, it's important to understand its main architectural components. These components are designed to be modular and intuitive, allowing you to focus on logic rather than boilerplate. |
| 4 | + |
| 5 | +## The Strategy |
| 6 | + |
| 7 | +The **Strategy** is the brain of your trading system. It's a PHP class where you define your logic for analyzing market data and making trading decisions. Stochastix automatically discovers any strategy class that implements the `StrategyInterface` and is decorated with the `#[AsStrategy]` attribute. |
| 8 | + |
| 9 | +The easiest way to create a strategy is by extending `AbstractStrategy`, which provides a rich set of helper methods. |
| 10 | + |
| 11 | +A strategy has a simple lifecycle: |
| 12 | +1. **`afterConfigured()`**: This method is called once after the strategy configuration, and before the backtest run, this is where you prepare extra logic or resources needed for the backtest. |
| 13 | +1. **`defineIndicators()`**: Called after configuration, this is where you set up your indicators for the backtest run. |
| 14 | +2. **`onBar()`**: This is the core method, executed for every single bar (e.g., every hour for a '1h' timeframe) of data in your backtest. All of your primary trading logic, like checking indicator values and placing orders, will live here. |
| 15 | + |
| 16 | +```php |
| 17 | +#[AsStrategy(alias: 'my_strategy', name: 'My Awesome Strategy')] |
| 18 | +class MyStrategy extends AbstractStrategy |
| 19 | +{ |
| 20 | + #[Input(description: 'The period for my indicator.')] |
| 21 | + private int $myPeriod = 20; |
| 22 | + |
| 23 | + protected function defineIndicators(): void |
| 24 | + { |
| 25 | + // Setup indicators here |
| 26 | + } |
| 27 | + |
| 28 | + public function onBar(MultiTimeframeOhlcvSeries $bars): void |
| 29 | + { |
| 30 | + // Your trading logic here |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## The Series |
| 36 | + |
| 37 | +A **Series** is an object representing a time-ordered sequence of data points, like the closing price of a stock or the value of an indicator. The most important feature of a Series is its chronological array-like access: |
| 38 | +* `$series[0]` always returns the **current** value (for the bar being processed in `onBar`). |
| 39 | +* `$series[1]` returns the **previous** value. |
| 40 | +* `$series[2]` returns the value before that, and so on. |
| 41 | + |
| 42 | +This makes it easy to compare current and historical data, for example: `if ($rsi[0] > 70 && $rsi[1] <= 70) { ... }`. |
| 43 | + |
| 44 | +The `onBar` method of your strategy receives a `MultiTimeframeOhlcvSeries` object, which contains multiple `Series` for the primary timeframe (e.g., `$bars->close`, `$bars->high`) and can also hold `OhlcvSeries` for other timeframes. |
| 45 | + |
| 46 | +## The Indicator |
| 47 | + |
| 48 | +An **Indicator** is a component that calculates a new `Series` of data from one or more existing `Series`. For example, a Simple Moving Average (SMA) indicator takes the `close` price series and a time period as input and produces a new `Series` of SMA values. |
| 49 | + |
| 50 | +Stochastix provides a powerful `TALibIndicator` that acts as a wrapper for the vast majority of indicators available in the `trader` PHP extension. You will typically define all your indicators within the `defineIndicators()` method of your strategy. |
| 51 | + |
| 52 | +```php |
| 53 | +// In your strategy's defineIndicators() method: |
| 54 | +$this->addIndicator( |
| 55 | + 'ema_fast', |
| 56 | + new TALibIndicator(TALibFunctionEnum::Ema, ['timePeriod' => 12]) |
| 57 | +); |
| 58 | + |
| 59 | +// Then, in onBar(), you can access its calculated values: |
| 60 | +$fastEmaSeries = $this->getIndicatorSeries('ema_fast'); |
| 61 | +$currentEmaValue = $fastEmaSeries[0]; |
| 62 | +``` |
| 63 | + |
| 64 | +## The Context |
| 65 | + |
| 66 | +The **Strategy Context** is a central object that acts as the bridge between your strategy and the backtesting engine's other systems. It is passed to your strategy during initialization and is accessible within `AbstractStrategy` via `$this->context`. |
| 67 | + |
| 68 | +The context provides access to the core managers: |
| 69 | + |
| 70 | +### The Order Manager |
| 71 | + |
| 72 | +Accessed via `$this->orderManager` in an `AbstractStrategy`, the **Order Manager** is your interface for all trade actions. Its primary role is to queue and process order "signals". When you call `$this->entry(...)` or `$this->exit(...)`, you are not executing a trade directly; you are creating an `OrderSignal` and sending it to the Order Manager to be executed on the next bar's open price. |
| 73 | + |
| 74 | +It is also responsible for managing a book of pending orders, such as Limit and Stop orders, checking their trigger conditions on every bar. |
| 75 | + |
| 76 | +### The Portfolio Manager |
| 77 | + |
| 78 | +Accessed via `$this->orderManager->getPortfolioManager()`, the **Portfolio Manager** is responsible for tracking the financial state of your backtest. It knows about your initial capital, available cash, currently open positions, and the log of all closed trades. When the Order Manager executes a trade, the result is passed to the Portfolio Manager, which then updates your cash and position status accordingly. |
0 commit comments