Skip to content

Commit 3bc47d9

Browse files
committed
docs: core concepts
1 parent 5117e47 commit 3bc47d9

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

.vitepress/config.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { defineConfig } from 'vitepress'
44
export default defineConfig({
55
title: "Stochastix",
66
description: "High-Performance Quantitative Backtesting Engine",
7+
head: [['link', { rel: 'icon', href: '/favicon.ico' }]],
78
themeConfig: {
89
// https://vitepress.dev/reference/default-theme-config
910
nav: [
@@ -19,6 +20,7 @@ export default defineConfig({
1920
{ text: "What's Stochastix?", link: '/introduction' },
2021
{ text: 'Docker Tooling', link: '/docker-tooling' },
2122
{ text: 'Project Structure', link: '/project-structure' },
23+
{ text: 'Core Concepts', link: '/core-concepts' },
2224
]
2325
}
2426
],

core-concepts.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.

getting-started.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Welcome to Stochastix\! This guide will walk you through setting up the framework so you can start backtesting your first trading strategy. We offer two installation paths: a one-command Docker setup (recommended for new projects) and a manual setup for integrating Stochastix into existing Symfony applications.
44

5+
Be sure to read [What's Stochastix?](/introduction.md) page to understand the framework's purpose and capabilities before diving into the installation.
6+
57
## Docker Installation <Badge type="tip" text="Recommended" />
68

79
::: tip The Easy Way

index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ layout: home
44
hero:
55
name: "Stochastix"
66
text: "High-Performance Quantitative Backtesting Engine"
7-
tagline: "From hypothesis to statistical proof. A professional-grade PHP framework to build, test, and analyze your crypto-currencies trading strategies."
7+
tagline: "From hypothesis to statistical proof. A PHP framework to build, test, and analyze your crypto-currencies trading strategies."
88
image:
99
src: /logo.svg
1010
alt: Stochastix

public/favicon.ico

4.19 KB
Binary file not shown.

0 commit comments

Comments
 (0)