Skip to main content
Home
This package has been archived, and as such it is read-only.

Built and signed on GitHub Actions

yanked

OakScriptJS is a TypeScript/JavaScript library that mirrors PineScript's calculation and indicator API, maintaining exact function signatures and behavior. This library focuses on the computational core of PineScript

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
Published
a month ago (0.1.2)

OakScriptJS

JavaScript mirror of the PineScript API - Calculation & Indicator Functions

OakScriptJS is a TypeScript/JavaScript library that mirrors PineScript's calculation and indicator API, maintaining exact function signatures and behavior. This library focuses on the computational core of PineScript - technical analysis, mathematics, and data manipulation - making it perfect for building custom trading engines, backtesting systems, or analysis tools in JavaScript/TypeScript.

Scope

This library includes:

  • ✅ Technical Analysis (ta.*) - All indicators and calculations
  • ✅ Mathematics (math.*) - All mathematical operations
  • ✅ Arrays (array.*) - Array manipulation and operations
  • ✅ Matrices (matrix.*) - Matrix operations
  • ✅ Strings (str.*) - String manipulation
  • ✅ Time (time.*) - Time calculations and conversions
  • ✅ Color (color.*) - Color data structures and manipulation
  • Drawing Objects (line.*, box.*, label.*, linefill.*) - Computational features only

This library does NOT include:

  • ❌ Rendering functions (plot.*, table.*)
  • ❌ UI/Input functions (input.*)
  • ❌ Strategy execution (strategy.*)
  • ❌ Data fetching (request.*)
  • ❌ Alert systems (alert.*, alertcondition.*)

Why Include Drawing Objects?

While drawing objects (line, box, label, linefill) are primarily visual in TradingView, they have genuine computational value:

  • line.get_price() - Calculate trend line prices using linear interpolation for breakout detection
  • box getters - Detect gap fills, range breakouts, and pattern recognition
  • label & linefill - Primarily annotations, but useful for algorithmic context

These objects are implemented without rendering - focusing purely on their computational aspects.

Why These Limitations?

The excluded namespaces require external infrastructure (rendering engines, UI frameworks, data feeds, backtesting systems) that are specific to TradingView's platform. OakScriptJS focuses on what can be accurately replicated in pure JavaScript: calculations and data transformations.

Features

  • Exact API Match: Function signatures match PineScript exactly
  • Type Safety: Full TypeScript support with type definitions
  • Performance Optimized: Efficient implementations for technical analysis
  • Calculation-Focused: All computational functions from PineScript
  • Well Tested: Extensive test coverage ensuring accuracy
  • Zero Dependencies: Lightweight with no external runtime dependencies

Priorities

  1. Exact same signature as PineScript API - Maintains 100% compatibility
  2. Accuracy - Produces results matching PineScript calculations
  3. Performance - Optimized for speed and efficiency

Installation

JSR (Recommended)

# npm
npx jsr add @deepentropy/oakscriptjs

# pnpm (10.9+)
pnpm add jsr:@deepentropy/oakscriptjs

# yarn (4.9+)
yarn add jsr:@deepentropy/oakscriptjs

# Deno
deno add jsr:@deepentropy/oakscriptjs

# Bun
bunx jsr add @deepentropy/oakscriptjs

npm (Coming Soon)

npm install @deepentropy/oakscriptjs

Quick Start

import { ta, math, line, box, createContext } from '@deepentropy/oakscriptjs';

// Calculate Simple Moving Average
const prices = [10, 12, 11, 13, 15, 14, 16, 18, 17, 19];
const sma20 = ta.sma(prices, 5);

// Calculate RSI
const rsi = ta.rsi(prices, 14);

// Calculate MACD
const [macdLine, signalLine, histogram] = ta.macd(prices, 12, 26, 9);

// Use math functions
const max = math.max(10, 20, 30); // 30
const avg = math.avg(10, 20, 30); // 20

// NEW: Use drawing objects for computational analysis
const trendLine = line.new(0, 100, 50, 150);
const priceAt25 = line.get_price(trendLine, 25); // 125 (linear interpolation)

// Detect gap with box
const gapBox = box.new(10, 120, 15, 110);
const gapTop = box.get_top(gapBox);
const gapBottom = box.get_bottom(gapBox);
const gapFilled = prices[20] > gapBottom && prices[20] < gapTop;

Use Cases

OakScriptJS is perfect for:

  • Custom Trading Engines - Build your own backtesting or execution system
  • Analysis Tools - Create technical analysis applications
  • Data Processing - Calculate indicators on market data
  • Algorithm Development - Develop and test trading algorithms
  • Educational Projects - Learn about technical indicators

Supported Namespaces

Technical Analysis (ta) ✅

Complete implementation of PineScript's technical analysis functions:

  • Moving Averages: sma(), ema(), wma(), vwma(), swma(), etc.
  • Oscillators: rsi(), stoch(), cci(), macd(), mfi(), etc.
  • Volatility: bb(), atr(), stdev(), variance(), etc.
  • Momentum: mom(), roc(), percentrank(), etc.
  • Regression: linreg(), correlation(), etc.
  • Crossovers: crossover(), crossunder(), cross()
  • Other: change(), tr(), supertrend(), and many more

Math (math) ✅

Mathematical functions and operations:

  • Basic: abs(), ceil(), floor(), round()
  • Min/Max: min(), max(), avg()
  • Powers: sqrt(), pow(), exp(), log(), log10()
  • Trigonometry: sin(), cos(), tan(), asin(), acos(), atan(), atan2()
  • Utilities: sum(), sign(), random(), todegrees(), toradians()

Array (array) ✅

Array manipulation functions:

  • Creation: new_array(), from()
  • Access: get(), set(), size()
  • Modification: push(), pop(), shift(), unshift(), insert(), remove()
  • Analysis: sum(), avg(), min(), max(), median(), stdev(), variance()
  • Operations: sort(), reverse(), slice(), concat(), includes(), indexof()

Matrix (matrix) ✅

Matrix operations for advanced calculations:

  • Creation: new(), copy()
  • Operations: mult(), add(), transpose()
  • Access: get(), set(), row(), col()

String (str) ✅

String manipulation functions:

  • Conversion: tostring(), tonumber()
  • Manipulation: substring(), split(), concat(), replace()
  • Case: upper(), lower()
  • Search: contains(), pos(), startswith(), endswith()
  • Formatting: format(), trim()

Time (time) ✅

Time calculations and conversions:

  • Conversions: Convert between timestamps and time components
  • Calculations: Work with timeframes and time-based logic

Color (color) ✅

Color creation and manipulation:

  • Creation: rgb(), from_hex(), new_color()
  • Components: r(), g(), b(), t()
  • Predefined Colors: red, green, blue, yellow, etc.

Drawing Objects ✅

NEW: Drawing objects with computational features (no rendering):

Line (line) - High Computational Value

  • Creation: new() - Create trend lines with coordinates
  • Computation: get_price() - Linear interpolation for breakout detection
  • Getters: get_x1(), get_y1(), get_x2(), get_y2()
  • Setters: set_x1(), set_y1(), set_xy1(), set_color(), set_style(), etc.
  • Operations: copy(), delete()

Box (box) - High Computational Value

  • Creation: new() - Create rectangles for ranges
  • Computation: get_top(), get_bottom(), get_left(), get_right() - Gap detection & range analysis
  • Setters: set_top(), set_bottom(), set_bgcolor(), set_border_color(), etc.
  • Operations: copy(), delete()

Label (label) - Annotation

  • Creation: new() - Create labels at coordinates
  • Getters: get_x(), get_y(), get_text()
  • Setters: set_xy(), set_text(), set_color(), set_style(), etc.
  • Operations: copy(), delete()

Linefill (linefill) - Annotation

  • Creation: new() - Fill between two lines
  • Getters: get_line1(), get_line2()
  • Setters: set_color()
  • Operations: delete()

API Documentation

ta.sma(source, length)

Simple Moving Average

ta.sma(source: series_float, length: simple_int): series_float

Parameters:

  • source - Source series (e.g., close prices)
  • length - Number of bars to average

Returns: Series of SMA values

ta.ema(source, length)

Exponential Moving Average

ta.ema(source: series_float, length: simple_int): series_float

ta.rsi(source, length)

Relative Strength Index

ta.rsi(source: series_float, length: simple_int): series_float

ta.macd(source, fastLength, slowLength, signalLength)

Moving Average Convergence Divergence

ta.macd(
  source: series_float,
  fastLength: simple_int,
  slowLength: simple_int,
  signalLength: simple_int
): [series_float, series_float, series_float]

Returns: [macdLine, signalLine, histogram]

ta.bb(source, length, mult)

Bollinger Bands

ta.bb(
  source: series_float,
  length: simple_int,
  mult: simple_float
): [series_float, series_float, series_float]

Returns: [basis, upper, lower]

Examples

See the /examples directory for complete examples:

  • basic-indicators.ts - Basic indicator calculations
  • strategy-example.ts - Trading strategy implementation
  • custom-indicator.ts - Building custom indicators

Type System

OakScriptJS uses TypeScript types that mirror PineScript's type system:

type int = number;
type float = number;
type bool = boolean;
type series<T> = T[];
type series_float = series<float>;
type series_bool = series<bool>;

Development

Setup

npm install

Build

npm run build

Test

npm test
npm run test:watch
npm run test:coverage

Lint

npm run lint
npm run lint:fix

Format

npm run format
npm run format:check

Project Structure

oakscriptjs/
├── src/
│   ├── ta/          # Technical analysis functions
│   ├── math/        # Mathematical functions
│   ├── array/       # Array operations
│   ├── str/         # String operations
│   ├── color/       # Color functions
│   ├── types/       # Type definitions
│   ├── utils/       # Internal utilities
│   └── index.ts     # Main entry point
├── tests/           # Test files
├── examples/        # Usage examples
└── dist/           # Built output

Roadmap

Included Namespaces:

  • Complete matrix namespace implementation
  • Complete time namespace implementation
  • ta namespace - Core indicators implemented
  • math namespace - Complete
  • array namespace - Complete
  • str namespace - Complete
  • color namespace - Complete
  • line, box, label, linefill - Drawing objects (computational features only)

Improvements:

  • Performance benchmarks
  • Comprehensive documentation site
  • Additional technical indicators
  • More test coverage

Explicitly Excluded (require external infrastructure):

  • plot, table - Rendering functions
  • input - UI controls
  • strategy - Strategy execution engine
  • request - Data fetching
  • alert, alertcondition - Alert system

Contributing

Contributions are welcome! Please ensure:

  1. Maintain exact PineScript API signatures
  2. Add tests for new functionality
  3. Follow the existing code style
  4. Update documentation

License

MIT

Acknowledgments

This library is inspired by TradingView's PineScript language. It is not affiliated with or endorsed by TradingView.

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@deepentropy/oakscriptjs

Import symbol

import * as oakscriptjs from "@deepentropy/oakscriptjs";
or

Import directly with a jsr specifier

import * as oakscriptjs from "jsr:@deepentropy/oakscriptjs";

Add Package

pnpm i jsr:@deepentropy/oakscriptjs
or (using pnpm 10.8 or older)
pnpm dlx jsr add @deepentropy/oakscriptjs

Import symbol

import * as oakscriptjs from "@deepentropy/oakscriptjs";

Add Package

yarn add jsr:@deepentropy/oakscriptjs
or (using Yarn 4.8 or older)
yarn dlx jsr add @deepentropy/oakscriptjs

Import symbol

import * as oakscriptjs from "@deepentropy/oakscriptjs";

Add Package

vlt install jsr:@deepentropy/oakscriptjs

Import symbol

import * as oakscriptjs from "@deepentropy/oakscriptjs";

Add Package

npx jsr add @deepentropy/oakscriptjs

Import symbol

import * as oakscriptjs from "@deepentropy/oakscriptjs";

Add Package

bunx jsr add @deepentropy/oakscriptjs

Import symbol

import * as oakscriptjs from "@deepentropy/oakscriptjs";