Skip to content

Feature Request: Support for converting UDT .new() constructor calls to Python classes #85

@bhavishyagoyal12

Description

@bhavishyagoyal12

Summary

When using pynescript to parse Pine Script with User-Defined Types (UDT), the AST correctly captures TypeDef nodes. However, there's no built-in way to convert these to usable Python code. The unparser.py only converts back to Pine Script syntax.

Pine Script Example

//@version=5
indicator("UDT Test")

type PathTracker
    float entry
    float target
    float stop
    bool is_long
    bool is_top

type VolData
    float buy
    float sell
    float total
    float delta
    bool is_valid

// Usage
tracker = PathTracker.new(na, na, na, true, false)
vol = VolData.new(100.0, 50.0, 150.0, 50.0, true)

Current Behavior

pynescript correctly parses this and creates:

  • TypeDef nodes in the AST with field definitions
  • When using unparser.py, it correctly regenerates Pine Script

However, when trying to use the AST to generate Python code (for transpilation purposes), there's no facility to:

  1. Generate Python class definitions from TypeDef nodes
  2. Convert .new() constructor calls to Python class instantiation

Feature Request

Would it be possible to add a Python code generator (similar to unparser.py but targeting Python instead of Pine Script)?

This could generate:

class PathTracker:
    def __init__(self, entry=None, target=None, stop=None, is_long=None, is_top=None):
        self.entry = entry
        self.target = target
        self.stop = stop
        self.is_long = is_long
        self.is_top = is_top

tracker = PathTracker(float('nan'), float('nan'), float('nan'), True, False)

Environment

  • pynescript version: 0.3.0
  • Python version: 3.11
  • Platform: Windows

Additional Context

I'm working on a Pine Script to Backtrader converter and pynescript is excellent for parsing. The main gap is converting UDT constructs to Python equivalents. Currently I have to implement custom handling for TypeDef nodes in my own transformer.

Thank you for this great library!

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