adding data-services
This commit is contained in:
parent
e3bfd05b90
commit
405b818c86
139 changed files with 55943 additions and 416 deletions
87
docs/enhanced-architecture.md
Normal file
87
docs/enhanced-architecture.md
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
# Enhanced Separation of Concerns
|
||||
|
||||
This guide describes how the project architecture has been improved to better separate concerns through a modular libs structure.
|
||||
|
||||
## New Library Structure
|
||||
|
||||
We've reorganized the project's shared libraries for improved maintainability:
|
||||
|
||||
### 1. Shared Types (`@stock-bot/shared-types`)
|
||||
|
||||
Types are now organized by domain:
|
||||
|
||||
```
|
||||
libs/shared-types/
|
||||
├── src/
|
||||
│ ├── market/ # Market data types (OHLCV, OrderBook)
|
||||
│ ├── trading/ # Trading types (Orders, Positions)
|
||||
│ ├── strategy/ # Strategy and signal types
|
||||
│ ├── events/ # Event definitions
|
||||
│ ├── api/ # API response/request types
|
||||
│ └── config/ # Configuration types
|
||||
```
|
||||
|
||||
### 2. Event Bus (`@stock-bot/event-bus`)
|
||||
|
||||
A consistent event publishing system:
|
||||
|
||||
```
|
||||
libs/event-bus/
|
||||
├── src/
|
||||
│ ├── EventBus.ts # Core event bus implementation
|
||||
│ └── index.ts # Public API
|
||||
```
|
||||
|
||||
### 3. Utils (`@stock-bot/utils`)
|
||||
|
||||
Shared utility functions:
|
||||
|
||||
```
|
||||
libs/utils/
|
||||
├── src/
|
||||
│ ├── dateUtils.ts # Date manipulation helpers
|
||||
│ ├── financialUtils.ts # Financial calculations
|
||||
│ ├── logger.ts # Standardized logging
|
||||
│ └── index.ts # Public API
|
||||
```
|
||||
|
||||
### 4. API Client (`@stock-bot/api-client`)
|
||||
|
||||
Type-safe service clients:
|
||||
|
||||
```
|
||||
libs/api-client/
|
||||
├── src/
|
||||
│ ├── BaseApiClient.ts # Common HTTP client logic
|
||||
│ ├── BacktestClient.ts # Backtest service client
|
||||
│ ├── StrategyClient.ts # Strategy service client
|
||||
│ └── index.ts # Public API
|
||||
```
|
||||
|
||||
## Service Architecture Improvements
|
||||
|
||||
The intelligence services have been split into focused services:
|
||||
|
||||
1. `strategy-orchestrator`: Core strategy management
|
||||
2. `backtest-engine`: Dedicated historical testing
|
||||
3. `signal-engine`: Signal generation
|
||||
|
||||
This provides:
|
||||
- Better scaling for resource-intensive operations
|
||||
- Focused codebases for each concern
|
||||
- Independent deployment cycles
|
||||
- Clear service boundaries
|
||||
|
||||
## Usage Guidelines
|
||||
|
||||
- Use the shared types library for all data models
|
||||
- Use the event bus for inter-service communication
|
||||
- Use the API clients for direct service calls
|
||||
- Use utility functions instead of duplicating common code
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. Continue migrating services to use the new libraries
|
||||
2. Add comprehensive tests for each library
|
||||
3. Create a complete API gateway for external access
|
||||
4. Document service boundaries with OpenAPI schemas
|
||||
98
docs/migration-guide.md
Normal file
98
docs/migration-guide.md
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
# Migration Guide: From packages to libs
|
||||
|
||||
This guide will help you migrate your service to use the new library structure for better separation of concerns.
|
||||
|
||||
## Steps for each service
|
||||
|
||||
1. Update your `package.json` dependencies to use the new libraries:
|
||||
|
||||
```diff
|
||||
"dependencies": {
|
||||
- "@stock-bot/shared-types": "workspace:*",
|
||||
+ "@stock-bot/shared-types": "workspace:*",
|
||||
+ "@stock-bot/utils": "workspace:*",
|
||||
+ "@stock-bot/event-bus": "workspace:*",
|
||||
+ "@stock-bot/api-client": "workspace:*",
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
2. Update your imports to use the domain-specific modules:
|
||||
|
||||
```diff
|
||||
- import { OHLCV, Strategy, Order } from '@stock-bot/shared-types';
|
||||
+ import { OHLCV } from '@stock-bot/shared-types';
|
||||
+ import { Strategy } from '@stock-bot/shared-types';
|
||||
+ import { Order } from '@stock-bot/shared-types';
|
||||
```
|
||||
|
||||
For logging:
|
||||
```diff
|
||||
- // Custom logging or console.log usage
|
||||
+ import { createLogger, LogLevel } from '@stock-bot/utils';
|
||||
+
|
||||
+ const logger = createLogger('your-service-name');
|
||||
+ logger.info('Message');
|
||||
```
|
||||
|
||||
For API client usage:
|
||||
```diff
|
||||
- // Manual axios calls
|
||||
+ import { createBacktestClient, createStrategyClient } from '@stock-bot/api-client';
|
||||
+
|
||||
+ const backtestClient = createBacktestClient();
|
||||
+ const result = await backtestClient.runBacktest(config);
|
||||
```
|
||||
|
||||
For event-based communication:
|
||||
```diff
|
||||
- // Manual Redis/Dragonfly usage
|
||||
+ import { createEventBus } from '@stock-bot/event-bus';
|
||||
+ import { MarketDataEvent } from '@stock-bot/shared-types';
|
||||
+
|
||||
+ const eventBus = createEventBus({
|
||||
+ redisHost: process.env.REDIS_HOST || 'localhost',
|
||||
+ redisPort: parseInt(process.env.REDIS_PORT || '6379')
|
||||
+ });
|
||||
+
|
||||
+ eventBus.subscribe('market.data', async (event) => {
|
||||
+ // Handle event
|
||||
+ });
|
||||
```
|
||||
|
||||
## Example: Updating BacktestEngine
|
||||
|
||||
```typescript
|
||||
// Before
|
||||
import { Strategy, BacktestConfig } from '@stock-bot/shared-types';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
// After
|
||||
import { Strategy } from '@stock-bot/shared-types';
|
||||
import { BacktestConfig } from '@stock-bot/shared-types';
|
||||
import { createLogger } from '@stock-bot/utils';
|
||||
import { createEventBus } from '@stock-bot/event-bus';
|
||||
|
||||
const logger = createLogger('backtest-engine');
|
||||
const eventBus = createEventBus({
|
||||
redisHost: process.env.REDIS_HOST || 'localhost',
|
||||
redisPort: parseInt(process.env.REDIS_PORT || '6379')
|
||||
});
|
||||
```
|
||||
|
||||
## Updating build scripts
|
||||
|
||||
If your turbo.json configuration references specific packages, update the dependencies:
|
||||
|
||||
```diff
|
||||
"backtest": {
|
||||
"dependsOn": [
|
||||
"^build",
|
||||
- "packages/shared-types#build"
|
||||
+ "libs/shared-types#build",
|
||||
+ "libs/utils#build",
|
||||
+ "libs/event-bus#build",
|
||||
+ "libs/api-client#build"
|
||||
],
|
||||
}
|
||||
```
|
||||
247
docs/websocket-api.md
Normal file
247
docs/websocket-api.md
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
# WebSocket API Documentation - Strategy Orchestrator
|
||||
|
||||
## Overview
|
||||
|
||||
This document outlines the WebSocket API provided by the Strategy Orchestrator service for real-time communication between the backend and frontend.
|
||||
|
||||
## Connection Endpoints
|
||||
|
||||
- **Strategy Orchestrator WebSocket:** `ws://localhost:8082`
|
||||
- **Market Data Gateway WebSocket:** `ws://localhost:3001/ws`
|
||||
- **Risk Guardian WebSocket:** `ws://localhost:3002/ws`
|
||||
|
||||
## Message Format
|
||||
|
||||
All WebSocket messages follow this standard format:
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "message_type",
|
||||
"timestamp": "ISO-8601 timestamp",
|
||||
"data": {
|
||||
// Message-specific data
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Client to Server Messages
|
||||
|
||||
### Get Active Strategies
|
||||
|
||||
Request information about all active strategies.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "get_active_strategies"
|
||||
}
|
||||
```
|
||||
|
||||
### Start Strategy
|
||||
|
||||
Start a specific strategy.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "start_strategy",
|
||||
"id": "strategy-id",
|
||||
"config": {
|
||||
"symbols": ["AAPL", "MSFT"],
|
||||
"dataResolution": "1m",
|
||||
"realTrading": false,
|
||||
"maxPositionValue": 10000,
|
||||
"maxOrdersPerMinute": 5,
|
||||
"stopLossPercentage": 0.02
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Stop Strategy
|
||||
|
||||
Stop a running strategy.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "stop_strategy",
|
||||
"id": "strategy-id"
|
||||
}
|
||||
```
|
||||
|
||||
### Pause Strategy
|
||||
|
||||
Pause a running strategy.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "pause_strategy",
|
||||
"id": "strategy-id"
|
||||
}
|
||||
```
|
||||
|
||||
## Server to Client Messages
|
||||
|
||||
### Strategy Status List
|
||||
|
||||
Response to `get_active_strategies` request.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "strategy_status_list",
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"data": [
|
||||
{
|
||||
"id": "strategy-123",
|
||||
"name": "Moving Average Crossover",
|
||||
"status": "ACTIVE",
|
||||
"symbols": ["AAPL", "MSFT"],
|
||||
"positions": [
|
||||
{
|
||||
"symbol": "AAPL",
|
||||
"quantity": 10,
|
||||
"entryPrice": 150.25,
|
||||
"currentValue": 1550.50
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Strategy Started
|
||||
|
||||
Notification that a strategy has been started.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "strategy_started",
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"data": {
|
||||
"strategyId": "strategy-123",
|
||||
"name": "Moving Average Crossover",
|
||||
"symbols": ["AAPL", "MSFT"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Strategy Stopped
|
||||
|
||||
Notification that a strategy has been stopped.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "strategy_stopped",
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"data": {
|
||||
"strategyId": "strategy-123",
|
||||
"name": "Moving Average Crossover"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Strategy Paused
|
||||
|
||||
Notification that a strategy has been paused.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "strategy_paused",
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"data": {
|
||||
"strategyId": "strategy-123",
|
||||
"name": "Moving Average Crossover"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Strategy Signal
|
||||
|
||||
Trading signal generated by a strategy.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "strategy_signal",
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"data": {
|
||||
"id": "sig_123456789",
|
||||
"strategyId": "strategy-123",
|
||||
"name": "Moving Average Crossover",
|
||||
"symbol": "AAPL",
|
||||
"price": 152.75,
|
||||
"action": "BUY",
|
||||
"quantity": 10,
|
||||
"metadata": { "orderType": "MARKET" },
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"confidence": 0.9
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Strategy Trade
|
||||
|
||||
Notification of a trade executed by a strategy.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "strategy_trade",
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"data": {
|
||||
"id": "trade_123456789",
|
||||
"strategyId": "strategy-123",
|
||||
"orderId": "order-123",
|
||||
"symbol": "AAPL",
|
||||
"side": "BUY",
|
||||
"quantity": 10,
|
||||
"entryPrice": 152.75,
|
||||
"entryTime": "2023-07-10T12:34:56Z",
|
||||
"status": "FILLED",
|
||||
"timestamp": "2023-07-10T12:34:56Z"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Execution Service Status
|
||||
|
||||
Notification about the overall execution service status.
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "execution_service_status",
|
||||
"timestamp": "2023-07-10T12:34:56Z",
|
||||
"data": {
|
||||
"status": "RUNNING" // or "STOPPED"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Frontend Integration
|
||||
|
||||
The frontend can use the `WebSocketService` to interact with the WebSocket API:
|
||||
|
||||
```typescript
|
||||
// Subscribe to strategy signals
|
||||
webSocketService.getStrategySignals(strategyId)
|
||||
.subscribe(signal => {
|
||||
// Handle the signal
|
||||
});
|
||||
|
||||
// Subscribe to strategy trades
|
||||
webSocketService.getStrategyTrades(strategyId)
|
||||
.subscribe(trade => {
|
||||
// Handle the trade
|
||||
});
|
||||
|
||||
// Subscribe to strategy status updates
|
||||
webSocketService.getStrategyUpdates()
|
||||
.subscribe(update => {
|
||||
// Handle the update
|
||||
});
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
WebSocket connections will automatically attempt to reconnect if disconnected. The frontend can monitor connection status using:
|
||||
|
||||
```typescript
|
||||
webSocketService.isConnected() // Returns a boolean signal indicating connection status
|
||||
```
|
||||
|
||||
If a WebSocket message fails to process, the error will be logged, but the connection will be maintained.
|
||||
Loading…
Add table
Add a link
Reference in a new issue