Developing Trading Algorithms
Relevant source files
The following files were used as context for generating this wiki page:
Developing trading algorithms within this project involves leveraging the Tinkoff API to test strategies against real-time and historical market data. The system provides a structured environment where developers can define logic, apply technical indicators, and monitor trade executions via a GraphQL-based terminal.
Sources: readme.md:1-5
Core Architecture
The algorithm testing system is built on a NestJS backend that communicates with the Tinkoff Invest API. It utilizes a resolver-service pattern to manage candle data streams and trade execution logic.
Data Flow and Signal Processing
The system maintains a candlesStorage to track price movement for instruments in the user's watchlist. When a new candle is received via the Tinkoff streaming service, the system triggers the algorithm logic to evaluate potential entry or exit points.
The diagram shows the lifecycle of a price update from the external API through the internal logic to the user interface.
Sources: backend/src/modules/algorithmTesting/algorithmTesting.resolver.ts:25-70, readme.md:10-15
Algorithm Implementation
The entry point for custom strategy logic is located at backend/src/modules/algorithmTesting/algorithms/index.ts. This function is invoked for every candle update and must return directional signals.
Logic Interface
The algorithm function receives the following context:
isNewCandle: Boolean indicating if the current update represents a new time interval.instrument: Metadata about the asset (ticker, FIGI).candle: The current price data (OHLCV).candles: The historical collection of candles for the instrument.
Sources: backend/src/modules/algorithmTesting/algorithmTesting.resolver.ts:60-65
Trade Execution Logic
When the algorithm returns isShort or isLong, the system automatically manages trade positions:
- Closing existing positions: If an active trade exists for the instrument, it is closed at the current candle's price.
- Opening new positions: A new
AlgorithmTradeentity is created, storing the price, type (Short/Long), and interval.
Sources: backend/src/modules/algorithmTesting/algorithmTesting.resolver.ts:68-85
Technical Indicators Package
The project includes a dedicated package @aduryagin/technical-indicators that provides a wide range of mathematical tools for strategy development. These indicators are designed to support real-time updates by allowing the modification of the last item in a result set without recalculating the entire history.
Available Indicators
| Indicator | Category | Source File |
|---|---|---|
| ATR / True Range | Volatility | packages/technical-indicators/src/index.ts |
| Bollinger Bands / FBB | Volatility | packages/technical-indicators/src/index.ts |
| EMA / SMA / RMA / VWMA | Moving Averages | packages/technical-indicators/src/index.ts |
| RSI / Stochastic | Oscillators | packages/technical-indicators/src/index.ts |
| MACD | Momentum | packages/technical-indicators/src/index.ts |
| PMax / OTT / VAR | Trend Following | packages/technical-indicators/src/index.ts |
| VWAP | Volume Weighted | packages/technical-indicators/src/index.ts |
Sources: packages/technical-indicators/src/index.ts:1-25, packages/technical-indicators/readme.md:10-35
Indicator Usage Pattern
Indicators typically return a result set and an update function for efficient real-time data processing.
Sources: packages/technical-indicators/readme.md:37-60
Data Models
The system relies on specific entities to track performance and state during testing.
AlgorithmTrade Entity
| Field | Type | Description |
|---|---|---|
id | Float | Unique identifier for the trade. |
price | Float | Entry price of the trade. |
closePrice | Float | Exit price (if closed). |
type | String | Trade direction: "Long" or "Short". |
closed | Boolean | Status of the position. |
pricePercentChange | Float | Percentage gain or loss based on current/close price. |
Sources: backend/schema.gql:5-16, frontend/src/graphql.ts:25-35
Monitoring and Frontend Integration
The frontend provides an AlgorithmTesting component that subscribes to real-time trade updates via GraphQL.
The sequence shows how the frontend maintains a live view of algorithm performance using subscriptions.
Sources: frontend/src/pages/chart/components/AlgorithmTesing.tsx:10-30, backend/src/modules/algorithmTesting/algorithmTesting.resolver.ts:90-110
Interaction Features
- Filter: Users can toggle between "Only Active" trades or the full history.
- Navigation: Clicking a trade selects the instrument (ticker) on the main chart.
- Real-time Performance: The UI displays
pricePercentChangewhich reflects live gains/losses against current market prices.
Sources: frontend/src/pages/chart/components/AlgorithmTesing.tsx:35-65, backend/src/modules/algorithmTesting/algorithmTesting.resolver.ts:95-105
Conclusion
Developing trading algorithms in this ecosystem centers on customizing the core algorithm function and utilizing the optimized technical indicators package. By integrating with the Tinkoff API's streaming capabilities, the project allows for high-fidelity strategy testing and real-time performance monitoring through a unified terminal interface.