# alaa-eddine/pinets architecture

> Architecture diagram of the GitHub repository alaa-eddine/pinets, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alaa-eddine/pinets
- Repository: https://github.com/alaa-eddine/pinets

## Overview

##### Project Type and Purpose
- This is an open-source JavaScript/TypeScript library (“PineTS”) that ports TradingView’s Pine Script v5+ indicators into JS/TS.
- It offers a runtime transpiler, a set of namespaces (TA functions, math, input, etc.), market-data providers, and returns indicator results that can be consumed by any JS environment (for charting, backtesting, etc.).

##### File Structure
- src/index.ts: The public entry point exporting PineTS, Providers, types.
- src/transpiler: The runtime transpiler that rewrites PineTS code for series management, scoping, indexing.
- src/namespaces: Modular collections of functions & classes grouped by responsibility (Core, TechnicalAnalysis, PineMath, Input, PineArray, PineColor, PineRequest, Syminfo).
- src/marketData: Connectors/Provider interfaces (e.g. Binance) to fetch OHLCV data.
- src/types: Shared TS types (PineTypes).
- rollup.config.js: Bundling into UMD/ESM for npm.
- docs/*: Static site with usage guides, API coverage, live demo indicators.
- tests/*: Unit tests for transpiler, namespaces, indicators, providers.

##### README for Additional Insights
- Core Components: PineTS class (manages market data, series, built-in variables, runtime execution context).
- Namespaces: Each implements a slice of Pine Script behavior (TA functions, math ops, input handling, symbol info).
- Workflow: User code → PineTS.run() → transpiler rewrites context → namespaces perform calculations over time-series → results returned.

##### Main Components & Relationships
a. Client Application
- Your script or app that imports PineTS and Providers, invokes PineTS.run(…) with a callback implementing indicator logic.
b. Market-Data Provider Layer
- Providers.Binance (and future providers) fetch raw OHLCV series.
- Implements IProvider interface for pluggability.
c. PineTS Core (Runtime Execution Context)
- Initializes context.data (series), built-ins (open, high, low, close, volume), context.ta, context.math, etc.
- Handles scoping rules, maintains historical series buffers.
d. Transpiler
- Parses & transforms the user’s PineTS code at runtime to insert series-aware operations, index adjustments, scoping wrappers.
e. Namespaces
- Modular functions referenced by transpiled code:
- Core (base operations)
- TechnicalAnalysis (ta.ema, ta.sma, etc.)
- PineMath (math.abs, math.round, etc.)
- Input (user-defined parameters)
- Syminfo (symbol metadata)
- PineArray, PineColor, PineRequest for advanced features
f. Result Emitter
- After loop/series processing, PineTS returns a unified result object (all plots, values).
g. Optional Visualization Layer
- Charts (eg. TradingView Lightweight Charts) consume the returned series for plotting.

##### Architectural Patterns & Design Principles
- Modular namespaces encourage Single Responsibility & easy tree-shaking.
- Provider interface enables Open/Closed principle—add new market data sources without touching core.
- Runtime code transformation (transpiler) implements Aspect-Oriented concerns (series management, scoping) without polluting user logic.
- Clear separation of concerns between data acquisition, code transpilation, execution context, calculation libraries, and result outputs.

##### Detail & Coverage
- Don’t leave out the transpiler or namespaces—they are central to Pine Script compatibility.
- Buffer/series management inside the runtime context.
- Provider interface boundary, so it’s clear how to add new data sources.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Application
    ClientApp["Client Application"]:::external
    click ClientApp "https://github.com/alaa-eddine/pinets/blob/main/src/index.ts"

    %% Market-Data Provider Layer
    subgraph "Market-Data Provider Layer"
        ProviderInterface["IProvider Interface"]:::provider
        click ProviderInterface "https://github.com/alaa-eddine/pinets/blob/main/src/marketData/IProvider.ts"
        BinanceProvider["Binance Provider"]:::provider
        click BinanceProvider "https://github.com/alaa-eddine/pinets/tree/main/src/marketData/Binance"
    end

    %% Core Runtime
    PineTSCore["PineTS Core\n(Runtime Execution Context)"]:::core
    click PineTSCore "https://github.com/alaa-eddine/pinets/blob/main/src/index.ts"

    %% Transpiler
    Transpiler["Transpiler\n(AOP/Transformer)"]:::transpiler
    click Transpiler "https://github.com/alaa-eddine/pinets/blob/main/src/transpiler/index.ts"

    %% Namespaces Function Library
    subgraph "Namespaces\n(Function Libraries)" 
        nsCore["Core Utilities"]:::namespace
        click nsCore "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/Core.ts"
        nsTA["TechnicalAnalysis (ta)"]:::namespace
        click nsTA "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/TechnicalAnalysis.ts"
        nsMath["PineMath"]:::namespace
        click nsMath "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/PineMath.ts"
        nsInput["Input Handling"]:::namespace
        click nsInput "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/Input.ts"
        nsArray["PineArray"]:::namespace
        click nsArray "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/PineArray.ts"
        nsColor["PineColor"]:::namespace
        click nsColor "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/PineColor.ts"
        nsRequest["PineRequest"]:::namespace
        click nsRequest "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/PineRequest.ts"
        nsSyminfo["Syminfo"]:::namespace
        click nsSyminfo "https://github.com/alaa-eddine/pinets/blob/main/src/namespaces/Syminfo.ts"
    end

    %% Result Emitter
    ResultEmitter["Result Emitter\n(Unified Result Object)"]:::emitter

    %% Optional Visualization
    Visualization["Visualization Layer\n(Charts)"]:::optional

    %% Infrastructure & Config
    subgraph "Infrastructure & Config"
        Types["Shared Types"]:::infra
        click Types "https://github.com/alaa-eddine/pinets/blob/main/src/types/PineTypes.ts"
        Bundler["Rollup Config"]:::infra
        click Bundler "https://github.com/alaa-eddine/pinets/blob/main/rollup.config.js"
        Docs["Documentation Site"]:::infra
        click Docs "https://github.com/alaa-eddine/pinets/tree/main/docs/"
        DocsInd["Live Demo Indicators"]:::infra
        click DocsInd "https://github.com/alaa-eddine/pinets/tree/main/docs/indicators/"
        TestsT["Transpiler Tests"]:::infra
        click TestsT "https://github.com/alaa-eddine/pinets/blob/main/tests/core/transpiler.test.ts"
        TestsN["Namespace Tests"]:::infra
        click TestsN "https://github.com/alaa-eddine/pinets/tree/main/tests/namespaces/"
        TestsI["Indicator Tests"]:::infra
        click TestsI "https://github.com/alaa-eddine/pinets/tree/main/tests/indicators/"
        CI["CI/CD Workflow"]:::infra
        click CI "https://github.com/alaa-eddine/pinets/blob/main/.github/workflows/release.yaml"
        Package["package.json"]:::infra
        TSConfig["tsconfig.json"]:::infra
        Vitest["vitest.config.ts"]:::infra
    end

    %% Relationships
    ClientApp -->|imports & calls PineTS.run| PineTSCore
    ClientApp -->|implements provider| ProviderInterface
    ProviderInterface -->|fetchOHLCV| BinanceProvider
    BinanceProvider -->|provides series data| PineTSCore
    PineTSCore <-->|"transpile code"| Transpiler
    Transpiler -->|invokes functions| nsCore
    Transpiler -->|invokes functions| nsTA
    Transpiler -->|invokes functions| nsMath
    Transpiler -->|invokes functions| nsInput
    Transpiler -->|invokes functions| nsArray
    Transpiler -->|invokes functions| nsColor
    Transpiler -->|invokes functions| nsRequest
    Transpiler -->|invokes functions| nsSyminfo
    nsCore -->|access series buffers| PineTSCore
    nsTA -->|access series buffers| PineTSCore
    nsMath -->|access series buffers| PineTSCore
    nsInput -->|access series buffers| PineTSCore
    nsArray -->|access series buffers| PineTSCore
    nsColor -->|access series buffers| PineTSCore
    nsRequest -->|access series buffers| PineTSCore
    nsSyminfo -->|access series buffers| PineTSCore
    PineTSCore -->|emit results| ResultEmitter
    ResultEmitter -->|returned series| ClientApp
    ResultEmitter -->|returned series| Visualization

    %% Infrastructure dependencies
    PineTSCore --> Types
    PineTSCore --> Bundler
    ClientApp --> Docs
    ClientApp --> DocsInd
    PineTSCore --> TestsT
    PineTSCore --> TestsN
    PineTSCore --> TestsI
    PineTSCore --> CI
    Bundler --> Package
    Bundler --> TSConfig
    Bundler --> Vitest

    %% Styles
    classDef external fill:#f0f0f0,stroke:#333,stroke-width:1px;
    classDef provider fill:#fdebd3,stroke:#b55400,stroke-width:1px;
    classDef core fill:#d5f5e3,stroke:#1e8449,stroke-width:1px;
    classDef transpiler fill:#fadbd8,stroke:#c0392b,stroke-width:1px;
    classDef namespace fill:#d6eaf8,stroke:#2471a3,stroke-width:1px;
    classDef emitter fill:#d1f2eb,stroke:#148f77,stroke-width:1px;
    classDef optional fill:#fcf3cf,stroke:#b7950b,stroke-width:1px;
    classDef infra fill:#ebedef,stroke:#566573,stroke-width:1px;
```
