pinescript-to-python-translator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

🌲 PineScript to Python Translator

🌲 PineScript 转 Python 转换器

This skill is designed to take raw TradingView PineScript files (
.pine
) and rigorously deconstruct them into Python-native components so they can be optimized using vectorization and tools like Optuna.
本技能旨在将原始TradingView PineScript文件(
.pine
)拆解为Python原生组件,以便通过向量化和Optuna等工具进行优化。

When to use this skill

何时使用本技能

Use this skill when you want to migrate a backtest from the TradingView ecosystem into a headless Python environment. This is critical for running multi-fold Walk-Forward Analysis (WFA) and Monte Carlo simulations that TradingView cannot handle.
当你想要将回测从TradingView生态系统迁移到无界面Python环境时,可以使用本技能。这对于运行TradingView无法处理的多轮滚动向前分析(WFA)和蒙特卡洛模拟至关重要。

Workflow

工作流程

  1. Deconstruction & Classification (The IR Build)
    • Parse the
      .pine
      script to identify
      input()
      ,
      input.int()
      ,
      input.float()
      , and
      input.bool()
      .
    • Classify parameters into three buckets:
      • Signal: Parameters that dictate entries (e.g.,
        length
        ,
        crossover_threshold
        ).
      • Risk: Parameters that dictate exits (e.g.,
        stop_ticks
        ,
        trail_offset
        ).
      • Display: Parameters used only for plotting/UI (discard these).
  2. Boundary Extraction
    • For every Signal and Risk parameter, extract the
      minval
      ,
      maxval
      , and
      step
      if provided.
    • Format these into Optuna trial suggestions (e.g.,
      trial.suggest_int('length', 10, 50)
      ).
  3. Logic Translation
    • Translate PineScript technical analysis functions (
      ta.sma
      ,
      ta.ema
      ,
      ta.rsi
      ) into their
      pandas-ta
      or
      numpy
      equivalents.
    • Vectorize the entry and exit conditions. Do not use standard
      for
      loops over rows unless path-dependency strictly requires it. Use
      np.where
      and
      .shift()
      wherever possible.
  4. Hardening & Auditing
    • Repaint Risk: Scan the translation for anything relying on the current unclosed bar data. Force the use of
      .shift(1)
      for signal generation.
    • Division by Zero: Wrap all denominators in a
      np.maximum(denominator, 1e-8)
      guard to prevent NaN explosions during optimization.
  1. 拆解与分类(中间表示构建)
    • 解析
      .pine
      脚本,识别
      input()
      input.int()
      input.float()
      input.bool()
    • 将参数分为三类:
      • 信号类:决定入场的参数(例如
        length
        crossover_threshold
        )。
      • 风险类:决定离场的参数(例如
        stop_ticks
        trail_offset
        )。
      • 显示类:仅用于绘图/UI的参数(此类将被丢弃)。
  2. 边界提取
    • 针对每个信号类和风险类参数,提取
      minval
      maxval
      (若提供则提取
      step
      )。
    • 将这些格式化为Optuna试验建议(例如
      trial.suggest_int('length', 10, 50)
      )。
  3. 逻辑转换
    • 将PineScript技术分析函数(
      ta.sma
      ta.ema
      ta.rsi
      )转换为对应的
      pandas-ta
      numpy
      等效函数。
    • 向量化入场和离场条件。除非路径依赖严格要求,否则不要对行使用标准
      for
      循环。尽可能使用
      np.where
      .shift()
  4. 强化与审计
    • 重绘风险:扫描转换后的代码,检查是否依赖当前未闭合K线的数据。强制在信号生成时使用
      .shift(1)
    • 除零问题:将所有分母包裹在
      np.maximum(denominator, 1e-8)
      防护中,以防止优化过程中出现NaN激增。

Output Format

输出格式

The output should be a single Python file containing:
  1. An
    extract_features(df, params)
    function that builds all the indicators based on a parameter dictionary.
  2. A
    generate_signals(df)
    function that creates a
    signal
    column (1 for Long, -1 for Short, 0 for Flat).
  3. A
    get_optuna_space(trial)
    function that returns the hyperparameter search space dictionary.
输出应为单个Python文件,包含:
  1. 一个
    extract_features(df, params)
    函数,基于参数字典构建所有指标。
  2. 一个
    generate_signals(df)
    函数,创建
    signal
    列(1代表做多,-1代表做空,0代表平仓)。
  3. 一个
    get_optuna_space(trial)
    函数,返回超参数搜索空间字典。