Skip to content

Utility Scripts

The scripts/ directory contains standalone Node.js utilities for common Pine Script development tasks. These scripts connect directly to TradingView Desktop via CDP (port 9222) and do not require the MCP server to be running.


pine_pull.js -- Extract Pine Script Source

File: scripts/pine_pull.js

Reads the current Pine Script source from TradingView's Pine Editor and saves it to scripts/current.pine.

How It Works

  1. Fetches the CDP target list from http://localhost:9222/json/list
  2. Finds the first target with tradingview.com in its URL
  3. Connects via CDP and enables the Runtime domain
  4. Locates the Monaco editor instance inside the .monaco-editor.pine-editor-monaco element
  5. Traverses React fiber nodes (up to 20 levels up, then 15 levels of return pointers) to find the monacoEnv object
  6. Calls getEditors()[0].getValue() to extract the source text
  7. Writes the source to scripts/current.pine

Usage

node scripts/pine_pull.js

Output:

Pulled 142 lines -> scripts/current.pine

Prerequisites

  • TradingView Desktop running with --remote-debugging-port=9222
  • Pine Editor panel must be open with a script loaded

Error Conditions

  • "No TradingView target" -- TradingView Desktop is not running or CDP port is wrong
  • "Could not read Pine editor" -- Pine Editor panel is not open, or the Monaco editor element was not found

pine_push.js -- Inject Pine Script Source

File: scripts/pine_push.js

Reads Pine Script source from scripts/current.pine, injects it into TradingView's Pine Editor, clicks the compile button, waits 3 seconds, then checks for compilation errors.

How It Works

  1. Reads source from scripts/current.pine
  2. Connects to TradingView via CDP (same target discovery as pine_pull.js)
  3. Locates the Monaco editor via React fiber traversal
  4. Calls editors[0].setValue(source) to replace the editor contents
  5. Finds and clicks the compile button by scanning all <button> elements for text matching:
    • "Save and add to chart"
    • "Add to chart"
    • "Update on chart"
    • Falls back to the .saveButton class
    • Final fallback: sends Ctrl+Enter keyboard shortcut
  6. Waits 3 seconds for compilation to complete
  7. Reads compilation errors from Monaco's model markers via editor.getModelMarkers()

Usage

node scripts/pine_push.js

Output (success):

Pushed 142 lines -> Pine editor
Compile: Add to chart
Compiled clean -- 0 errors

Output (errors):

Pushed 142 lines -> Pine editor
Compile: Add to chart
3 errors:
  Line 12: Undeclared identifier 'ema_length'
  Line 25: Cannot call 'plot' with argument 'color'=series[float]
  Line 40: Mismatched input 'if' expecting 'end of line without continuation'

Error Conditions

  • "No TradingView target" -- TradingView not running
  • "Could not inject into Pine editor" -- Pine Editor not open or Monaco not found

Development Workflow

These scripts are designed to work together in a rapid development loop, as used by the pine-develop skill:

# 1. Pull current source to edit locally
node scripts/pine_pull.js

# 2. Edit scripts/current.pine in your editor

# 3. Push changes and compile
node scripts/pine_push.js

# 4. Fix any errors, then push again
node scripts/pine_push.js

# 5. Repeat until clean

This workflow keeps the source of truth in a local file (scripts/current.pine), which enables version control, diff review, and AI-assisted editing -- while using TradingView's built-in compiler for validation.


Launch Scripts

For TradingView Desktop launch scripts and platform-specific startup configuration, see the ictedge-mcp documentation. Launch scripts handle starting TradingView with the required --remote-debugging-port=9222 flag on macOS, Windows, and Linux.