# silasspencer1/lean architecture

> Architecture diagram of the GitHub repository silasspencer1/lean, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/silasspencer1/lean
- Repository: https://github.com/silasspencer1/lean

## Overview

LEAN is an event-driven algorithmic trading engine for backtesting and live trading. The principal workflow is to load and initialize a user algorithm, provide market data, let it make trading decisions, process orders through a brokerage, and report results. Its modular handlers support different execution modes and pluggable integrations. The graph combines explicit initialization and transaction-handler evidence with the README’s product description; several runtime subsystems are represented at their enclosing directory because their implementation was not sampled.

Main entry points: [`Engine/Engine.cs`](https://github.com/silasspencer1/lean/blob/master/Engine/Engine.cs) (Engine coordinator), [`Algorithm.CSharp/BasicTemplateAlgorithm.cs`](https://github.com/silasspencer1/lean/blob/master/Algorithm.CSharp/BasicTemplateAlgorithm.cs) (Trading algorithm), [`Engine/TransactionHandlers/BrokerageTransactionHandler.cs`](https://github.com/silasspencer1/lean/blob/master/Engine/TransactionHandlers/BrokerageTransactionHandler.cs) (Order processing), [`Brokerages`](https://github.com/silasspencer1/lean/tree/master/Brokerages) (Brokerage)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_execution["Engine execution"]
  node_algorithm["Trading algorithm"]
  node_engine["Engine coordinator<br/>[Engine.cs]"]
  node_setup["Algorithm setup"]
  node_handlers["Pluggable handlers"]
  node_synchronizer["Event synchronizer"]
  node_results["Results reporting"]
end

subgraph group_market["Market data"]
  node_datafeed["Data feed"]
  node_marketdata[("Market data")]
  node_universe["Universe selection<br/>[Algorithm.CSharp]"]
end

subgraph group_trading["Trading state"]
  node_portfolio["Portfolio state"]
  node_transactions["Order processing"]
end

subgraph group_integration["Brokerage integration"]
  node_brokerage["Brokerage"]
  node_messagehandler["Brokerage messages"]
  node_websockets["WebSocket subscriptions"]
end

node_author(("Algorithm author"))

node_author -->|"defines strategy"| node_algorithm
node_engine -->|"uses handlers"| node_handlers
node_engine -->|"invokes setup"| node_setup
node_setup -->|"creates algorithm"| node_algorithm
node_engine -->|"initializes feed"| node_datafeed
node_engine -->|"initializes sync"| node_synchronizer
node_engine -->|"initializes orders"| node_transactions
node_engine -->|"sets algorithm"| node_results
node_marketdata -->|"supplies data"| node_datafeed
node_datafeed -->|"streams events"| node_algorithm
node_algorithm -->|"selects assets"| node_universe
node_algorithm -->|"submits orders"| node_transactions
node_transactions -->|"sends orders"| node_brokerage
node_brokerage -->|"reports events"| node_transactions
node_transactions -->|"updates holdings"| node_portfolio
node_results -->|"reports outcomes"| node_author
node_brokerage -->|"emits messages"| node_messagehandler
node_brokerage -.->|"streams quotes"| node_websockets

click node_algorithm "https://github.com/silasspencer1/lean/blob/master/Algorithm.CSharp/BasicTemplateAlgorithm.cs"
click node_engine "https://github.com/silasspencer1/lean/blob/master/Engine/Engine.cs"
click node_setup "https://github.com/silasspencer1/lean/blob/master/Engine/Setup/BrokerageSetupHandler.cs"
click node_handlers "https://github.com/silasspencer1/lean/blob/master/Engine/LeanEngineAlgorithmHandlers.cs"
click node_synchronizer "https://github.com/silasspencer1/lean/tree/master/Engine"
click node_results "https://github.com/silasspencer1/lean/tree/master/Engine"
click node_datafeed "https://github.com/silasspencer1/lean/tree/master/Engine"
click node_universe "https://github.com/silasspencer1/lean/tree/master/Algorithm.CSharp"
click node_portfolio "https://github.com/silasspencer1/lean/blob/master/Common/Securities/SecurityPortfolioManager.cs"
click node_transactions "https://github.com/silasspencer1/lean/blob/master/Engine/TransactionHandlers/BrokerageTransactionHandler.cs"
click node_brokerage "https://github.com/silasspencer1/lean/tree/master/Brokerages"
click node_messagehandler "https://github.com/silasspencer1/lean/blob/master/Common/Brokerages/DefaultBrokerageMessageHandler.cs"
click node_websockets "https://github.com/silasspencer1/lean/blob/master/Brokerages/BrokerageMultiWebSocketSubscriptionManager.cs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_algorithm,node_engine,node_setup,node_handlers,node_synchronizer,node_results toneBlue
class node_datafeed,node_marketdata,node_universe toneAmber
class node_portfolio,node_transactions toneMint
class node_brokerage,node_messagehandler,node_websockets toneRose
class node_author toneIndigo
```

## Components

### Engine execution

- [Trading algorithm](https://github.com/silasspencer1/lean/blob/master/Algorithm.CSharp/BasicTemplateAlgorithm.cs): `Algorithm.CSharp/BasicTemplateAlgorithm.cs`
- [Engine coordinator](https://github.com/silasspencer1/lean/blob/master/Engine/Engine.cs): `Engine/Engine.cs`
- [Algorithm setup](https://github.com/silasspencer1/lean/blob/master/Engine/Setup/BrokerageSetupHandler.cs): `Engine/Setup/BrokerageSetupHandler.cs`
- [Pluggable handlers](https://github.com/silasspencer1/lean/blob/master/Engine/LeanEngineAlgorithmHandlers.cs): `Engine/LeanEngineAlgorithmHandlers.cs`
- [Event synchronizer](https://github.com/silasspencer1/lean/tree/master/Engine): `Engine`
- [Results reporting](https://github.com/silasspencer1/lean/tree/master/Engine): `Engine`

### Market data

- [Data feed](https://github.com/silasspencer1/lean/tree/master/Engine): `Engine`
- **Market data**
- [Universe selection](https://github.com/silasspencer1/lean/tree/master/Algorithm.CSharp): `Algorithm.CSharp`

### Trading state

- [Portfolio state](https://github.com/silasspencer1/lean/blob/master/Common/Securities/SecurityPortfolioManager.cs): `Common/Securities/SecurityPortfolioManager.cs`
- [Order processing](https://github.com/silasspencer1/lean/blob/master/Engine/TransactionHandlers/BrokerageTransactionHandler.cs): `Engine/TransactionHandlers/BrokerageTransactionHandler.cs`

### Brokerage integration

- [Brokerage](https://github.com/silasspencer1/lean/tree/master/Brokerages): `Brokerages`
- [Brokerage messages](https://github.com/silasspencer1/lean/blob/master/Common/Brokerages/DefaultBrokerageMessageHandler.cs): `Common/Brokerages/DefaultBrokerageMessageHandler.cs`
- [WebSocket subscriptions](https://github.com/silasspencer1/lean/blob/master/Brokerages/BrokerageMultiWebSocketSubscriptionManager.cs): `Brokerages/BrokerageMultiWebSocketSubscriptionManager.cs`

### Other components

- **Algorithm author**

## Connections

- Algorithm author → Trading algorithm: defines strategy
- Engine coordinator → Pluggable handlers: uses handlers
- Engine coordinator → Algorithm setup: invokes setup
- Algorithm setup → Trading algorithm: creates algorithm
- Engine coordinator → Data feed: initializes feed
- Engine coordinator → Event synchronizer: initializes sync
- Engine coordinator → Order processing: initializes orders
- Engine coordinator → Results reporting: sets algorithm
- Market data → Data feed: supplies data
- Data feed → Trading algorithm: streams events
- Trading algorithm → Universe selection: selects assets
- Trading algorithm → Order processing: submits orders
- Order processing → Brokerage: sends orders
- Brokerage → Order processing: reports events
- Order processing → Portfolio state: updates holdings
- Results reporting → Algorithm author: reports outcomes
- Brokerage → Brokerage messages: emits messages
- Brokerage → WebSocket subscriptions: streams quotes
