[Quick Start]
Parse, dump, unparse, lint, and evaluate Pine Script™ with the pynescript CLI and library in under ten minutes.
Quick Start
Abstract
This page is a linear path from a working install to the five desk operations most users need: parse, dump, unparse, lint, and evaluate. Full option trees live in CLI commands; deeper evaluation semantics live in Evaluate scripts.
Conceptual model
Interface surface
Assumes:
pip install hoox-pyne
# optional for data:
# pip install "hoox-pyne[data]"
Sample script in-tree: examples/rsi_strategy.pine.
Internals (repo paths)
| Step | Implementation |
|---|---|
| CLI parse/dump | src/pynescript/__main__.py → parse_and_dump |
| CLI unparse | parse_and_unparse |
| CLI lint | lint → pynescript.ast.linter.lint_script |
| Library helpers | src/pynescript/ast/helper.py |
| Example scripts | examples/parse_dump_unparse.py, examples/evaluate_expressions.py |
Invariants & edge cases
- Always declare
//@version=5or//@version=6at the top; the linter warns (W001) if missing. parse-and-unparseis a formatter via AST, not a semantic optimizer.literal_evalis for expressions / built-in calls with optional series context — not a full multi-bar strategy backtester. For bar loops, usebackend.runtime.Runtimeor the Pro API/run.- Filename arguments to the CLI must exist and be readable; lint may also read stdin (
-or no file).
Worked examples
1. Parse and dump the AST
pynescript parse-and-dump examples/rsi_strategy.pine
Pretty-print with indent and optional file output:
pynescript parse-and-dump examples/rsi_strategy.pine --indent 2 --output-file /tmp/rsi.ast.txt
Library equivalent:
from pynescript.ast import parse, dump
with open("examples/rsi_strategy.pine", encoding="utf-8") as f:
tree = parse(f.read(), "examples/rsi_strategy.pine")
print(dump(tree, indent=2))
2. Round-trip unparse (normalize)
pynescript parse-and-unparse examples/rsi_strategy.pine
pynescript parse-and-unparse messy.pine --output-file clean.pine
from pynescript.ast import parse, unparse
source = """
//@version=5
indicator("My RSI")
rsi(close, 14)
"""
print(unparse(parse(source)))
Canonical demo: examples/parse_dump_unparse.py.
3. Lint before upload
pynescript lint examples/rsi_strategy.pine
pynescript lint --fail-on warnings examples/rsi_strategy.pine
echo '//@version=5\nindicator("x")\nplot(close)' | pynescript lint -
Library:
from pynescript.ast.linter import lint_script
issues = lint_script(open("examples/rsi_strategy.pine").read(), "rsi_strategy.pine")
for w in issues:
print(w) # severity: [CODE] message at line N
4. Evaluate expressions
from pynescript.ast.helper import literal_eval
print(literal_eval("1 + 2 * 3")) # 7
print(literal_eval("math.sqrt(16)"))
prices = [100, 102, 101, 103, 105, 104, 106, 108, 107, 110]
print(literal_eval(f"ta.rsi({prices}, 9)"))
# Series history with context
ctx = {
"close": [100, 102, 101, 103, 105],
"open": [99, 101, 100, 102, 104],
}
print(literal_eval("close[0] - close[1]", ctx))
Broader demo: examples/evaluate_expressions.py.
5. Fetch sample market data (CLI)
# mock provider (no network)
pynescript data AAPL --provider mock
# Yahoo (network)
pynescript data AAPL --provider yahoo --period 6mo --interval 1d
# CCXT (needs pynescript[data])
pynescript data BTC/USDT --provider ccxt --exchange binance
6. Optional: local Pro API /run
# from monorepo with backend deps
make run
curl -s -X POST http://127.0.0.1:5002/run \
-H 'Content-Type: application/json' \
-d '{
"script": "//@version=5\nindicator(\"t\")\nplot(close)",
"data": [
{"time": 1, "open": 1, "high": 2, "low": 0.5, "close": 1.5, "volume": 100},
{"time": 2, "open": 1.5, "high": 2.5, "low": 1.0, "close": 2.0, "volume": 120}
]
}' | python -m json.tool
See Pro API usage for schemas and batch mode.
7. Optional: start LSP for editors
pip install "hoox-pyne[lsp]"
pynescript-lsp # stdio; normally launched by the editor
Wire Neovim / Zed / Emacs using Editors and clients/.
Failure modes
| Step fails | Check |
|---|---|
Dump raises SyntaxError | Script version / unsupported construct — try a minimal indicator + plot(close) |
| Unparse output differs cosmetically | Expected; compare semantic structure via dump |
| Lint exits non-zero | --fail-on threshold; inspect codes E001, W001, … |
literal_eval NotImplementedError | Expression not in literal/builtin subset; use full Runtime |
data provider error | Network, API key, missing ccxt extra |