Skip to content

Pine to Python #83

Open
Open

Description

@devmehta91

Would this aid in transcribing Pine to Python. I see the flow is pine -> ast -> pine, however what is the utility of converting to AST if only to convert back to Pine?

Activity

  1. elbakramer commented on Jul 3, 2024

    @elbakramer
    Owner

    Answers to questions:

    Would this aid in transcribing Pine to Python.

    Partly yes, I would say. But since Pine is not a general purpose programing language like Python, generally there won't exist an equivalent python code for a given pine script, especially for scripts that contain statements beyond the simple arithmetic expressions. Still on cases when we can transcribe a pine script to a syntactically equivalent and correct python code, that python code won't just run by itself since python doesn't know pine specific concepts like built-in functions or series types.

    I see the flow is pine -> ast -> pine, however what is the utility of converting to AST if only to convert back to Pine?

    That flow is there just for a proof of concept, demonstration that Python can understand Pine script, at least can know of how the Pine script is structured. And the utility of converting to AST, even limiting to the converting back to Pine case, can be like, formatting, static analysis on code, modification like replacing certain functions programmatically, etc.


    Thoughts:

    I assume your intention to transcribe Pine to Python is to run the same logic in python like running a python script translated from Pine to Python. But in my opinion, a more proper way to do that would be to implement a function or an engine that can understand the pine code and execute things needed to run that code. And when it comes to understanding how the pine code is structured, AST is there for. So, I started to implement that AST feature first.

    My attempts for the later ones like running the parsed AST exist partially as like the following codes, but not covers the whole functionalities yet. You can check them and develop your own from these if you would like:

    def literal_eval(node_or_string: AST | str):
    if isinstance(node_or_string, str):
    node_or_string = parse(node_or_string.lstrip(" \t"), mode="eval")
    if isinstance(node_or_string, Expression):
    node_or_string = node_or_string.body
    from pynescript.ast.evaluator import NodeLiteralEvaluator
    evaluator = NodeLiteralEvaluator()
    return evaluator.visit(node_or_string)

    executor = ExampleScriptExecutor(script_source)
    executor.execute(hist)

  2. wallneradam commented on Jun 26, 2025

    @wallneradam

    Really interesting thread — we’ve been working on a full Pine Script (v6) to Python pipeline over at PyneSys, and wanted to share our approach since it's closely related to this topic.

    We took a slightly different path: instead of focusing only on AST parsing, we designed a system where Pine and Python meet halfway. On one side, there's PyneCore, a Python framework that brings Python much closer to Pine — syntactically and semantically — using AST transformations and some clever runtime patterns. On the other side, the compiler at pynesys.io translates Pine scripts directly into that format, preserving Pine semantics like time-indexed series, persistent vars, scoped function state, and even bit-level RNG behavior.

    So the result is: Pine code gets compiled into real Python code, and then runs natively using PyneCore — no VM, no interpreter. It’s readable, debuggable Python that behaves like Pine.

    The public beta just launched, so if anyone’s working on similar tools or curious about executable Pine-to-Python workflows, happy to share more.

  3. elbakramer commented on Jul 22, 2025

    @elbakramer
    Owner

    Hi @wallneradam, really impressed by your work on PyneSys and PyneCore. I gave them a try recently.
    Some rough edges (few runtime failures), but the foundation looks great, especially for an early-stage project.

    I had once thought about building an engine like this by myself, but never got around to it.

    Things held me back:

    • Mapping all of PineScript's API surface felt like a huge task
    • I wasn’t sure how to design a proper semantic translation from Pine to Python.
    • Guaranteeing the same evaluation results as PineScript didn’t seem easy

    Your approach looks quite well thought out.

    As an experiment, I tried building a small Pine -> Python compiler using pynescript.
    It’s still rough and not yet fully functional, but I thought it might be worth sharing:

    Actually, I hesitated a bit to share since it might look like a competing approach to pynesys.io
    Still, I believe in sharing and open collaboration. And I hope you'll understand that.

    While testing, I also noticed one thing worth flagging:
    The compiler output from pynesys.io might not be preserving operator precedence in some cases.
    For example, a Pine expression like a and (b or c) gets translated to a and b or c, dropping the parentheses, which Python would interpret as (a and b) or c. (My implementation preserved them, so I was able to notice the difference.)

    FYI, I tested with this script:

    Also FYI, related to the precedence issue, note that Pine has a different precedence level for the not operator compared to Python.
    I haven't checked how pynesys.io handles this, but thought it was worth mentioning.

    Just sharing in case it's helpful.

    Keep up the great work.
    This project is really pushing the ecosystem forward!

  4. wallneradam commented on Jul 23, 2025

    @wallneradam

    Hi @elbakramer,

    I really appreciated your message — especially because I also wrote to you with some hesitation. I didn’t want to take attention away from your project, which actually seems very thoughtful and well-designed. I had assumed it was primarily intended to allow Pine Script manipulation via code, but I could also see its potential.

    My project started over two and a half years ago. Initially, I built an interpreter, then a VM-based system — but it was slow and became really hard to maintain. That’s when I realized that Python itself could serve as the best VM, and from that point, I started designing a framework (PyneCore) that behaves like Pine, but is fully Python — simple, readable, and relatively fast.

    Then I built a compiler (PyneComp) that converts Pine Script to Python code compatible with PyneCore. Unlike your approach, which uses Python AST directly (very cool, by the way!), mine generates clean Python source code as text directly. The idea was to keep the output human-readable while preserving Pine semantics.

    About your compiler — I really think it's awesome that you're generating AST directly. It opens up a lot of interesting use cases, especially for scenarios outside PyneCore. It’s open-source, and I'm sure it could be very useful to others who want a lightweight or custom solution.

    Like you, I believe in sharing — that’s why PyneCore is fully open-source. I think it has standalone value. The compiler isn’t open yet, but if other parts of the system start generating revenue, I may open it too.

    And yes — you were totally right about the precedence! Thank you for pointing it out, I fixed it right away.
    It’s kind of surprising that it didn’t show up earlier — I’ve run over 100 PineScript test cases and processed 150+ user-submitted scripts without hitting it.
    The fact that you not only spotted the bug but also identified the cause — that really shows deep insight. Great catch!

    Wishing you lots of inspiration and good flow with your projects. Hope our paths cross again in the future!

  5. bhavishyagoyal12 commented on Jan 26, 2026

    @bhavishyagoyal12

    @elbakramer Do you have something which can convert pinescript into backtrader code using this library . This would be super helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions