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 () and rigorously deconstruct them into Python-native components so they can be optimized using vectorization and tools like Optuna.
.pine本技能旨在将原始TradingView PineScript文件()拆解为Python原生组件,以便通过向量化和Optuna等工具进行优化。
.pineWhen 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
工作流程
-
Deconstruction & Classification (The IR Build)
- Parse the script to identify
.pine,input(),input.int(), andinput.float().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).
- Signal: Parameters that dictate entries (e.g.,
- Parse the
-
Boundary Extraction
- For every Signal and Risk parameter, extract the ,
minval, andmaxvalif provided.step - Format these into Optuna trial suggestions (e.g., ).
trial.suggest_int('length', 10, 50)
- For every Signal and Risk parameter, extract the
-
Logic Translation
- Translate PineScript technical analysis functions (,
ta.sma,ta.ema) into theirta.rsiorpandas-taequivalents.numpy - Vectorize the entry and exit conditions. Do not use standard loops over rows unless path-dependency strictly requires it. Use
forandnp.wherewherever possible..shift()
- Translate PineScript technical analysis functions (
-
Hardening & Auditing
- Repaint Risk: Scan the translation for anything relying on the current unclosed bar data. Force the use of for signal generation.
.shift(1) - Division by Zero: Wrap all denominators in a guard to prevent NaN explosions during optimization.
np.maximum(denominator, 1e-8)
- Repaint Risk: Scan the translation for anything relying on the current unclosed bar data. Force the use of
-
拆解与分类(中间表示构建)
- 解析脚本,识别
.pine、input()、input.int()和input.float()。input.bool() - 将参数分为三类:
- 信号类:决定入场的参数(例如、
length)。crossover_threshold - 风险类:决定离场的参数(例如、
stop_ticks)。trail_offset - 显示类:仅用于绘图/UI的参数(此类将被丢弃)。
- 信号类:决定入场的参数(例如
- 解析
-
边界提取
- 针对每个信号类和风险类参数,提取、
minval(若提供则提取maxval)。step - 将这些格式化为Optuna试验建议(例如)。
trial.suggest_int('length', 10, 50)
- 针对每个信号类和风险类参数,提取
-
逻辑转换
- 将PineScript技术分析函数(、
ta.sma、ta.ema)转换为对应的ta.rsi或pandas-ta等效函数。numpy - 向量化入场和离场条件。除非路径依赖严格要求,否则不要对行使用标准循环。尽可能使用
for和np.where。.shift()
- 将PineScript技术分析函数(
-
强化与审计
- 重绘风险:扫描转换后的代码,检查是否依赖当前未闭合K线的数据。强制在信号生成时使用。
.shift(1) - 除零问题:将所有分母包裹在防护中,以防止优化过程中出现NaN激增。
np.maximum(denominator, 1e-8)
- 重绘风险:扫描转换后的代码,检查是否依赖当前未闭合K线的数据。强制在信号生成时使用
Output Format
输出格式
The output should be a single Python file containing:
- An function that builds all the indicators based on a parameter dictionary.
extract_features(df, params) - A function that creates a
generate_signals(df)column (1 for Long, -1 for Short, 0 for Flat).signal - A function that returns the hyperparameter search space dictionary.
get_optuna_space(trial)
输出应为单个Python文件,包含:
- 一个函数,基于参数字典构建所有指标。
extract_features(df, params) - 一个函数,创建
generate_signals(df)列(1代表做多,-1代表做空,0代表平仓)。signal - 一个函数,返回超参数搜索空间字典。
get_optuna_space(trial)