@simahfud/pine-to-kline 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -0
- package/dist/index.d.ts +695 -0
- package/dist/pine-to-kline.js +1996 -0
- package/dist/pine-to-kline.js.map +1 -0
- package/dist/pine-to-kline.umd.js +111 -0
- package/dist/pine-to-kline.umd.js.map +1 -0
- package/package.json +54 -0
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# 🌲 pine-to-kline
|
|
2
|
+
|
|
3
|
+
An independent interpreter that compiles Pine Script (v5/v6) into indicators compatible with the [KlineChart](https://klinecharts.com/) charting library.
|
|
4
|
+
|
|
5
|
+
Designed as a plugin for `@simahfud/klinecharts-pro`.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
- **Clean-room implementation**: Converts Pine Script directly to KlineChart's `IndicatorTemplate` format
|
|
9
|
+
- **Series Emulation**: Bar-by-bar evaluation to support `close[1]` historical access syntax
|
|
10
|
+
- **Pine Functions**: Includes most-used `ta.*`, `math.*`, and `color.*` built-ins
|
|
11
|
+
- **Inputs & Styling**: Maps `input.int()`, `plot()`, and `hline()` to KlineChart parameters and figures
|
|
12
|
+
- **Zero Dependencies**: Independent pure-TS library
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install pine-to-kline
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
*(Note: Requires `klinecharts >= 9.0.0` as a peer dependency)*
|
|
21
|
+
|
|
22
|
+
## Basic Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { PineInterpreter } from 'pine-to-kline'
|
|
26
|
+
|
|
27
|
+
const pine = new PineInterpreter()
|
|
28
|
+
|
|
29
|
+
// Compile Pine Script
|
|
30
|
+
const result = pine.compile(`
|
|
31
|
+
//@version=5
|
|
32
|
+
indicator("My SMA", overlay=true)
|
|
33
|
+
length = input.int(14, "Length")
|
|
34
|
+
plot(ta.sma(close, length), "SMA", color=color.blue)
|
|
35
|
+
`)
|
|
36
|
+
|
|
37
|
+
if (result.success) {
|
|
38
|
+
// result.indicatorConfig is a valid KlineChart IndicatorTemplate
|
|
39
|
+
klineChart.addIndicator(result.indicatorConfig, true)
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Plugin Integration with KlineChart Pro
|
|
44
|
+
|
|
45
|
+
The library includes a dedicated plugin wrapper for `@simahfud/klinecharts-pro`.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { createPinePlugin } from 'pine-to-kline/plugin'
|
|
49
|
+
|
|
50
|
+
// Initialize the plugin
|
|
51
|
+
const pineAPI = createPinePlugin()
|
|
52
|
+
|
|
53
|
+
// 1. Compile and register the indicator
|
|
54
|
+
const indicatorName = pineAPI.run(`
|
|
55
|
+
//@version=5
|
|
56
|
+
indicator("RSI", overlay=false)
|
|
57
|
+
length = input.int(14, "Length")
|
|
58
|
+
plot(ta.rsi(close, length), "RSI", color=color.purple)
|
|
59
|
+
hline(70, "OB", color=color.red)
|
|
60
|
+
hline(30, "OS", color=color.green)
|
|
61
|
+
`)
|
|
62
|
+
|
|
63
|
+
// 2. Add the registered indicator to your chart pane
|
|
64
|
+
klineChart.createIndicator(indicatorName, true, { id: 'pane_1' })
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Supported Pine Features (Phase 1)
|
|
68
|
+
|
|
69
|
+
### Built-in TA (`ta.*`)
|
|
70
|
+
- **Moving Averages**: `sma`, `ema`, `rma`, `wma`, `hma`, `vwma`, `swma`, `dema`, `tema`
|
|
71
|
+
- **Momentum**: `rsi`, `macd`, `stoch`, `cci`, `mom`, `roc`
|
|
72
|
+
- **Volatility**: `atr`, `bb` (Bollinger Bands), `kc` (Keltner Channels), `donchian`, `tr`
|
|
73
|
+
- **Crossover**: `crossover`, `crossunder`, `cross`, `change`, `rising`, `falling`
|
|
74
|
+
- **Utility**: `highest`, `lowest`, `highestbars`, `lowestbars`, `barssince`, `valuewhen`, `pivothigh`, `pivotlow`, `cum`, `stdev`, `percentrank`
|
|
75
|
+
|
|
76
|
+
### Math (`math.*`)
|
|
77
|
+
- `abs`, `ceil`, `floor`, `round`, `log`, `log10`, `pow`, `sqrt`, `exp`, `max`, `min`, `sign`, `avg`, `sum`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `random`, `todegrees`, `toradians`
|
|
78
|
+
|
|
79
|
+
### Colors (`color.*`)
|
|
80
|
+
- All standard color constants (e.g. `color.red`, `color.blue`)
|
|
81
|
+
- Functions: `color.new()`, `color.rgb()`, `color.from_gradient()`
|
|
82
|
+
|
|
83
|
+
### Inputs
|
|
84
|
+
- `input()`, `input.int()`, `input.float()`, `input.bool()`, `input.string()`, `input.color()`, `input.source()`
|
|
85
|
+
|
|
86
|
+
## Architecture
|
|
87
|
+
|
|
88
|
+
The interpreter pipeline works in 4 phases:
|
|
89
|
+
1. **Lexer**: Tokenizes the source code, handling Pine's indentation blocks.
|
|
90
|
+
2. **Parser**: A recursive-descent parser that builds an Abstract Syntax Tree (AST).
|
|
91
|
+
3. **Runtime Engine**: Evaluates the AST using a `SeriesEmulator` to provide `var` persistence and `[]` lookbacks.
|
|
92
|
+
4. **Adapter**: Transpiles the runtime's Intermediate Representation (IR) into a KlineChart `IndicatorTemplate`.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
Apache-2.0
|