An open-source Python library that translates TradingView Pine Script (v5/v6) strategies and indicators into executable Python code that runs on pandas, NumPy, and TA-Lib.
- Parse Pine v5/v6 (e.g.,
//@version=6
) and common constructs - Map built-ins (
close
,open
,high
,low
,volume
,time
,bar_index
,na
) - Map indicators (
ta.sma
,ta.ema
,ta.rsi
,ta.macd
, etc.) to TA-Lib - Translate
plot()
to matplotlib stubs or console logs - Translate
strategy.entry/exit/close
to a PythonStrategy
class with simple order/position simulation - Emit a Python class you can execute on a pandas DataFrame with OHLCV columns
- Install dependencies (ensure TA-Lib is installed in your system):
pip install -r requirements.txt
- Translate a Pine Script string to Python code:
from pine2py.translator import translate
pine_code = """
//@version=5
indicator(title="SMA Demo", overlay=true)
len = input.int(14)
s = ta.sma(close, len)
plot(s)
"""
code_str = translate(pine_code)
print(code_str)
- Execute the translated strategy against a DataFrame:
import pandas as pd
from pine2py.executor import execute_translated_code
df = pd.DataFrame({...}) # must include 'open','high','low','close','volume' indexed by datetime
result = execute_translated_code(code_str, df)
In your trading app editor:
from pine2py.translator import translate
from pine2py.executor import execute_translated_code
def on_run_button(pine_script_text: str, df):
py_code = translate(pine_script_text)
result = execute_translated_code(py_code, df)
return result
pine2py/parser.py
: Pine tokenizer/lightweight parserpine2py/mapper.py
: Mapping dictionaries and helperspine2py/translator.py
:translate(pine_code: str) -> str
pine2py/executor.py
: BaseStrategy
and execution helperspine2py/plotting.py
: Plot stubstests/
: Unit/integration tests
This is a pragmatic MVP that supports a large subset of Pine commonly used in indicators/strategies. Unknown or unmapped functions raise descriptive errors.
MIT