Skip to main content

Handle Pinescript using Python

Project description

Pynescript

PyPI Status Python Version License

Read the documentation at https://pynescript.readthedocs.io/

Features

Handle Pinescript using Python

  • Parse Pinescript code into AST
  • Dump parsed AST
  • Unparse parsed AST back to Pinescript code

Given an example pinescript with name of rsi_strategy.pine:

//@version=5
strategy("RSI Strategy", overlay=true)
length = input( 14 )
overSold = input( 30 )
overBought = input( 70 )
price = close
vrsi = ta.rsi(price, length)
co = ta.crossover(vrsi, overSold)
cu = ta.crossunder(vrsi, overBought)
if (not na(vrsi))
	if (co)
		strategy.entry("RsiLE", strategy.long, comment="RsiLE")
	if (cu)
		strategy.entry("RsiSE", strategy.short, comment="RsiSE")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)

Parsing script into AST and dumping it:

$ pynescript parse-and-dump rsi_strategy.pine

Gives like:

Script(
  body=[
    Expr(
      value=Call(
        func=Name(id='strategy', ctx=Load()),
        args=[
          Arg(
            value=Constant(value='RSI Strategy')),
          Arg(
            value=Constant(value=True),
            name='overlay')])),
    Assign(
      target=Name(id='length', ctx=Store()),
      value=Call(
        func=Name(id='input', ctx=Load()),
        args=[
          Arg(
            value=Constant(value=14))]),
      annotations=[]),
    ...
Full AST dump that is quite long...
Script(
  body=[
    Expr(
      value=Call(
        func=Name(id='strategy', ctx=Load()),
        args=[
          Arg(
            value=Constant(value='RSI Strategy')),
          Arg(
            value=Constant(value=True),
            name='overlay')])),
    Assign(
      target=Name(id='length', ctx=Store()),
      value=Call(
        func=Name(id='input', ctx=Load()),
        args=[
          Arg(
            value=Constant(value=14))]),
      annotations=[]),
    Assign(
      target=Name(id='overSold', ctx=Store()),
      value=Call(
        func=Name(id='input', ctx=Load()),
        args=[
          Arg(
            value=Constant(value=30))]),
      annotations=[]),
    Assign(
      target=Name(id='overBought', ctx=Store()),
      value=Call(
        func=Name(id='input', ctx=Load()),
        args=[
          Arg(
            value=Constant(value=70))]),
      annotations=[]),
    Assign(
      target=Name(id='price', ctx=Store()),
      value=Name(id='close', ctx=Load()),
      annotations=[]),
    Assign(
      target=Name(id='vrsi', ctx=Store()),
      value=Call(
        func=Attribute(
          value=Name(id='ta', ctx=Load()),
          attr='rsi',
          ctx=Load()),
        args=[
          Arg(
            value=Name(id='price', ctx=Load())),
          Arg(
            value=Name(id='length', ctx=Load()))]),
      annotations=[]),
    Assign(
      target=Name(id='co', ctx=Store()),
      value=Call(
        func=Attribute(
          value=Name(id='ta', ctx=Load()),
          attr='crossover',
          ctx=Load()),
        args=[
          Arg(
            value=Name(id='vrsi', ctx=Load())),
          Arg(
            value=Name(id='overSold', ctx=Load()))]),
      annotations=[]),
    Assign(
      target=Name(id='cu', ctx=Store()),
      value=Call(
        func=Attribute(
          value=Name(id='ta', ctx=Load()),
          attr='crossunder',
          ctx=Load()),
        args=[
          Arg(
            value=Name(id='vrsi', ctx=Load())),
          Arg(
            value=Name(id='overBought', ctx=Load()))]),
      annotations=[]),
    Expr(
      value=If(
        test=UnaryOp(
          op=Not(),
          operand=Call(
            func=Name(id='na', ctx=Load()),
            args=[
              Arg(
                value=Name(id='vrsi', ctx=Load()))])),
        body=[
          Expr(
            value=If(
              test=Name(id='co', ctx=Load()),
              body=[
                Expr(
                  value=Call(
                    func=Attribute(
                      value=Name(id='strategy', ctx=Load()),
                      attr='entry',
                      ctx=Load()),
                    args=[
                      Arg(
                        value=Constant(value='RsiLE')),
                      Arg(
                        value=Attribute(
                          value=Name(id='strategy', ctx=Load()),
                          attr='long',
                          ctx=Load())),
                      Arg(
                        value=Constant(value='RsiLE'),
                        name='comment')]))],
              orelse=[])),
          Expr(
            value=If(
              test=Name(id='cu', ctx=Load()),
              body=[
                Expr(
                  value=Call(
                    func=Attribute(
                      value=Name(id='strategy', ctx=Load()),
                      attr='entry',
                      ctx=Load()),
                    args=[
                      Arg(
                        value=Constant(value='RsiSE')),
                      Arg(
                        value=Attribute(
                          value=Name(id='strategy', ctx=Load()),
                          attr='short',
                          ctx=Load())),
                      Arg(
                        value=Constant(value='RsiSE'),
                        name='comment')]))],
              orelse=[]))],
        orelse=[]))],
  annotations=[
    '//@version=5'])

Parsing into AST and unparsing it back:

$ pynescript parse-and-unparse rsi_strategy.pine

Gives (with some difference in syntax including spacing):

//@version=5
strategy("RSI Strategy", overlay=true)
length = input(14)
overSold = input(30)
overBought = input(70)
price = close
vrsi = ta.rsi(price, length)
co = ta.crossover(vrsi, overSold)
cu = ta.crossunder(vrsi, overBought)
if not na(vrsi)
    if co
        strategy.entry("RsiLE", strategy.long, comment="RsiLE")
    if cu
        strategy.entry("RsiSE", strategy.short, comment="RsiSE")

Requirements

  • Python 3.10 or higher

Installation

You can install Pynescript via pip from PyPI:

$ pip install pynescript

Usage

Please see the Usage for details.

License

Distributed under the terms of the LGPL 3.0 license, Pynescript is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynescript-0.3.0.tar.gz (246.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynescript-0.3.0-py3-none-any.whl (118.8 kB view details)

Uploaded Python 3

File details

Details for the file pynescript-0.3.0.tar.gz.

File metadata

  • Download URL: pynescript-0.3.0.tar.gz
  • Upload date:
  • Size: 246.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for pynescript-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f4a32821a6b04e3236f6998627667f72ebfaa22e87c2c544e3481de61806ac96
MD5 6c1b9070159341eb04629672e1f98a5e
BLAKE2b-256 2da3d5cd57c733f97c34ad6cb394edc9c674e4b506afeb27c6b5973767b9b5ca

See more details on using hashes here.

File details

Details for the file pynescript-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pynescript-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 118.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for pynescript-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a24fac07afad68bb9e3150fa3686f7c9b6d9316c971e91f4799abd4b921c6a1
MD5 5fe59411f76ae51026aa885619eb381a
BLAKE2b-256 d3190e412839e6c3f42c80d53bb21f28651149ab1a8468b7a29e665df68b8b74

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page