elbakramer / pynescript

Handle Pinescript using Python
GNU Lesser General Public License v3.0
45 stars 13 forks source link

Pine to Python #83

Open devmehta91 opened 3 months ago

devmehta91 commented 3 months ago

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?

elbakramer commented 3 months ago

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: https://github.com/elbakramer/pynescript/blob/0d01bfe12c3d2a54cda51efc54209e1726d77a80/src/pynescript/ast/helper.py#L167-L176 https://github.com/elbakramer/pynescript/blob/0d01bfe12c3d2a54cda51efc54209e1726d77a80/examples/execute_script.py#L436-L437