Package: pynescript
PynescriptFeaturesHandlePinescriptusingPythonParse Pinescript code into ASTDump parsed ASTUnparse parsed AST back to Pinescript codeGiven an example pinescript with name ofrsi_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:$pynescriptparse-and-dumprsi_strategy.pineGives 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:$pynescriptparse-and-unparsersi_strategy.pineGives (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")RequirementsPython 3.10 or higherInstallationYou can installPynescriptviapipfromPyPI:$pipinstallpynescriptUsagePlease see theUsagefor details.LicenseDistributed under the terms of theLGPL 3.0 license,Pynescriptis free and open source software.IssuesIf you encounter any problems,
pleasefile an issuealong with a detailed description.