pine2pyne 0.2.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pine2pyne/__init__.py +84 -0
- pine2pyne/__main__.py +12 -0
- pine2pyne/ast_nodes.py +405 -0
- pine2pyne/ast_walk.py +93 -0
- pine2pyne/block_value_lowering.py +694 -0
- pine2pyne/bool_builtins.py +42 -0
- pine2pyne/bool_value_typing.py +580 -0
- pine2pyne/cli.py +247 -0
- pine2pyne/codegen.py +886 -0
- pine2pyne/compat/__init__.py +19 -0
- pine2pyne/compat/__main__.py +6 -0
- pine2pyne/compat/cli.py +114 -0
- pine2pyne/compat/comparison.py +105 -0
- pine2pyne/compat/corpus.py +73 -0
- pine2pyne/compat/report.py +95 -0
- pine2pyne/compat/runner.py +63 -0
- pine2pyne/condition_typing.py +260 -0
- pine2pyne/conditional_history_calls.py +96 -0
- pine2pyne/core/__init__.py +26 -0
- pine2pyne/core/contracts.py +125 -0
- pine2pyne/di.py +173 -0
- pine2pyne/emit/__init__.py +5 -0
- pine2pyne/emit/legacy.py +14 -0
- pine2pyne/emit/pynecomp/__init__.py +20 -0
- pine2pyne/emit/pynecomp/banner.py +30 -0
- pine2pyne/emit/pynecomp/blank_lines.py +69 -0
- pine2pyne/emit/pynecomp/emitter.py +61 -0
- pine2pyne/emit/pynecomp/imports.py +65 -0
- pine2pyne/emit/pynecomp/passes/__init__.py +84 -0
- pine2pyne/emit/pynecomp/passes/annotations.py +422 -0
- pine2pyne/emit/pynecomp/passes/base.py +69 -0
- pine2pyne/emit/pynecomp/passes/builtins.py +169 -0
- pine2pyne/emit/pynecomp/passes/hoisting.py +205 -0
- pine2pyne/emit/pynecomp/passes/imports.py +112 -0
- pine2pyne/emit/pynecomp/passes/legacy_colors.py +101 -0
- pine2pyne/emit/pynecomp/passes/library.py +205 -0
- pine2pyne/emit/pynecomp/passes/rewrites.py +215 -0
- pine2pyne/emit/pynecomp/printer.py +288 -0
- pine2pyne/emit/pynecomp/unparser.py +186 -0
- pine2pyne/emitted_names.py +152 -0
- pine2pyne/errors.py +69 -0
- pine2pyne/for_loop_end_bound.py +172 -0
- pine2pyne/grouped_history_lowering.py +310 -0
- pine2pyne/import_resolver.py +469 -0
- pine2pyne/input_hoisting.py +195 -0
- pine2pyne/legacy_function_names.py +74 -0
- pine2pyne/legacy_sessions.py +68 -0
- pine2pyne/lexer.py +630 -0
- pine2pyne/library_collision.py +434 -0
- pine2pyne/namespace_members.py +247 -0
- pine2pyne/node_ids.py +235 -0
- pine2pyne/once_lowering.py +75 -0
- pine2pyne/overload_resolution.py +475 -0
- pine2pyne/parse_repair.py +398 -0
- pine2pyne/parser.py +1873 -0
- pine2pyne/pine_builtins.py +394 -0
- pine2pyne/pine_semantics.py +604 -0
- pine2pyne/pipeline/__init__.py +18 -0
- pine2pyne/pipeline/service.py +70 -0
- pine2pyne/pipeline/stages.py +73 -0
- pine2pyne/scope_renamer.py +965 -0
- pine2pyne/strategy_when_lowering.py +101 -0
- pine2pyne/strict_boolean.py +101 -0
- pine2pyne/symbol_table.py +166 -0
- pine2pyne/tokens.py +143 -0
- pine2pyne/transformer.py +2510 -0
- pine2pyne/type_inference.py +422 -0
- pine2pyne/udt_field_defaults.py +149 -0
- pine2pyne/value_blocks.py +357 -0
- pine2pyne-0.2.4.dist-info/METADATA +99 -0
- pine2pyne-0.2.4.dist-info/RECORD +75 -0
- pine2pyne-0.2.4.dist-info/WHEEL +5 -0
- pine2pyne-0.2.4.dist-info/entry_points.txt +3 -0
- pine2pyne-0.2.4.dist-info/licenses/LICENSE +201 -0
- pine2pyne-0.2.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Pine v1-v4 call names, spelled the way Pine v5 spells them (card #116).
|
|
2
|
+
|
|
3
|
+
Before Pine v5 the technical-analysis, math, request and ticker functions were global: `sma(close, 5)`,
|
|
4
|
+
`mom(close, 2)`, `security(...)`, `heikenashi(...)`. v5 moved them into the `ta.`, `math.`, `str.`, `request.`
|
|
5
|
+
and `ticker.` namespaces. The transformer and the runtime know only the namespaced names, so a bare `sma(...)`
|
|
6
|
+
reached the generated code as a bare `sma(...)` that nothing imports: `NameError` at run.
|
|
7
|
+
|
|
8
|
+
`qualify_legacy_function_names` renames those calls in a script that declares //@version 1-4. Only a call of a
|
|
9
|
+
name from the table is renamed, and only when the script declares no FUNCTION of that name itself: a user's own
|
|
10
|
+
`sma(x, n) => ...` stays. A variable, a parameter or a loop counter of that name does not stop the rename: a call
|
|
11
|
+
`sma(...)` looks up a function, a variable lives apart from it (`rsi = rsi(close, 14)` is a common idiom) or is out of
|
|
12
|
+
scope at the call, so the builtin is the callee. A source with no //@version line, or one at v5 or later, is not
|
|
13
|
+
touched.
|
|
14
|
+
|
|
15
|
+
Left out on purpose (card #116 follow-ups): names a Python builtin of the same spelling already answers
|
|
16
|
+
(`abs`, `max`, `min`, `round`, `pow`, `range`), which run today with Python semantics; and the legacy names
|
|
17
|
+
that are series, not calls (a bare `tr`, `vwap`, `obv`, `tickerid`, ...).
|
|
18
|
+
"""
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from typing import Dict, Set
|
|
22
|
+
|
|
23
|
+
from .ast_nodes import FuncDecl, FunctionCall, Script
|
|
24
|
+
from .ast_walk import walk
|
|
25
|
+
|
|
26
|
+
TECHNICAL_ANALYSIS_NAMES = (
|
|
27
|
+
'alma atr barssince bb bbw cci change cmo cog correlation cross crossover crossunder cum dev dmi ema falling '
|
|
28
|
+
'hma highest highestbars kc kcw linreg lowest lowestbars macd mfi mom percentile_linear_interpolation '
|
|
29
|
+
'percentile_nearest_rank percentrank pivothigh pivotlow rising rma roc rsi sar sma stdev stoch supertrend '
|
|
30
|
+
'swma tr tsi valuewhen variance vwma wma wpr'
|
|
31
|
+
).split()
|
|
32
|
+
MATH_NAMES = ('acos asin atan avg ceil cos exp floor log log10 random round_to_mintick sign sin sqrt sum tan '
|
|
33
|
+
'todegrees toradians').split()
|
|
34
|
+
|
|
35
|
+
# call name before Pine v5 -> the name Pine v5 gives it
|
|
36
|
+
LEGACY_FUNCTION_NAMES: Dict[str, str] = {
|
|
37
|
+
**{name: f'ta.{name}' for name in TECHNICAL_ANALYSIS_NAMES},
|
|
38
|
+
**{name: f'math.{name}' for name in MATH_NAMES},
|
|
39
|
+
'tostring': 'str.tostring',
|
|
40
|
+
'tonumber': 'str.tonumber',
|
|
41
|
+
'security': 'request.security',
|
|
42
|
+
'heikenashi': 'ticker.heikinashi',
|
|
43
|
+
'heikinashi': 'ticker.heikinashi',
|
|
44
|
+
'tickerid': 'ticker.new',
|
|
45
|
+
'renko': 'ticker.renko',
|
|
46
|
+
'linebreak': 'ticker.linebreak',
|
|
47
|
+
'kagi': 'ticker.kagi',
|
|
48
|
+
'pointfigure': 'ticker.pointfigure',
|
|
49
|
+
'financial': 'request.financial',
|
|
50
|
+
'quandl': 'request.quandl',
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _declared_function_names(script: Script) -> Set[str]:
|
|
55
|
+
"""The names of the functions the script itself declares, anywhere in it."""
|
|
56
|
+
return {node.name for node in walk(script) if isinstance(node, FuncDecl) and isinstance(node.name, str)}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _declared_version(script: Script) -> int:
|
|
60
|
+
"""The //@version number, or 0 when absent or not a number."""
|
|
61
|
+
text = script.version.version if script.version else ''
|
|
62
|
+
return int(text) if str(text).isdigit() else 0
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def qualify_legacy_function_names(script: Script) -> Script:
|
|
66
|
+
"""The script with the calls of a Pine v1-v4 script spelled as Pine v5 spells them."""
|
|
67
|
+
if not 1 <= _declared_version(script) <= 4:
|
|
68
|
+
return script
|
|
69
|
+
own_names = _declared_function_names(script)
|
|
70
|
+
renames = {legacy: current for legacy, current in LEGACY_FUNCTION_NAMES.items() if legacy not in own_names}
|
|
71
|
+
for node in walk(script):
|
|
72
|
+
if isinstance(node, FunctionCall) and isinstance(node.func, str) and node.func in renames:
|
|
73
|
+
node.func = renames[node.func]
|
|
74
|
+
return script
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Pine v4 session strings, read the way Pine v5 reads them.
|
|
2
|
+
|
|
3
|
+
A Pine v4 session without days (``"0930-1600"``) means weekdays; Pine v5 reads it as
|
|
4
|
+
every day. PyneComp converts the session argument of ``time()`` and ``time_close()``
|
|
5
|
+
of a Pine v4 script (probe scripts compiled by PyneComp v6.0.80)::
|
|
6
|
+
|
|
7
|
+
time(tf, "0930-1600") time(tf, '0930-1600:23456')
|
|
8
|
+
time(tf, sess) time(tf, sess if sess == '' else sess + ('23456' if string.endswith(sess, ':')
|
|
9
|
+
else '' if string.contains(sess, ':') else ':23456'))
|
|
10
|
+
time(tf, sess + "x") time(tf, __pc_session_days(sess + 'x')) (a helper defined first in main)
|
|
11
|
+
|
|
12
|
+
A literal that names its days (``"0930-1600:1234567"``) is kept.
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import dataclasses
|
|
17
|
+
|
|
18
|
+
from .ast_nodes import BinaryOp, FuncDecl, FunctionCall, Identifier, Literal, Parameter, Script, TernaryOp
|
|
19
|
+
from .ast_walk import walk
|
|
20
|
+
|
|
21
|
+
SESSION_FUNCTIONS = frozenset({'time', 'time_close'})
|
|
22
|
+
SESSION_HELPER = '__pc_session_days'
|
|
23
|
+
WEEKDAYS = '23456'
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _string(value: str) -> Literal:
|
|
27
|
+
return Literal(value=value, literal_type='string')
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def with_weekdays(session) -> TernaryOp:
|
|
31
|
+
"""``session`` read with Pine v4's default days, as an expression of it."""
|
|
32
|
+
def copy():
|
|
33
|
+
return dataclasses.replace(session)
|
|
34
|
+
days = TernaryOp(
|
|
35
|
+
condition=FunctionCall(func='str.endswith', args=[copy(), _string(':')]),
|
|
36
|
+
true_expr=_string(WEEKDAYS),
|
|
37
|
+
false_expr=TernaryOp(condition=FunctionCall(func='str.contains', args=[copy(), _string(':')]),
|
|
38
|
+
true_expr=_string(''), false_expr=_string(f':{WEEKDAYS}')))
|
|
39
|
+
return TernaryOp(condition=BinaryOp(left=copy(), op='==', right=_string('')),
|
|
40
|
+
true_expr=copy(), false_expr=BinaryOp(left=copy(), op='+', right=days))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def qualify_legacy_sessions(script: Script) -> Script:
|
|
44
|
+
"""A Pine v4 script with the sessions of its ``time()``/``time_close()`` calls given days."""
|
|
45
|
+
version = script.version.version if script.version else ''
|
|
46
|
+
if not (str(version).isdigit() and int(version) == 4):
|
|
47
|
+
return script
|
|
48
|
+
needs_helper = False
|
|
49
|
+
for node in walk(script):
|
|
50
|
+
if not (isinstance(node, FunctionCall) and isinstance(node.func, str) and node.func in SESSION_FUNCTIONS
|
|
51
|
+
and len(node.args) >= 2):
|
|
52
|
+
continue
|
|
53
|
+
session = node.args[1]
|
|
54
|
+
if isinstance(session, Literal):
|
|
55
|
+
if session.literal_type == 'string' and ':' not in str(session.value):
|
|
56
|
+
node.args[1] = dataclasses.replace(session, value=f'{session.value}:{WEEKDAYS}')
|
|
57
|
+
elif isinstance(session, Identifier):
|
|
58
|
+
node.args[1] = with_weekdays(session)
|
|
59
|
+
else:
|
|
60
|
+
node.args[1] = FunctionCall(func=SESSION_HELPER, args=[session], line=getattr(session, 'line', 0),
|
|
61
|
+
column=getattr(session, 'column', 0))
|
|
62
|
+
needs_helper = True
|
|
63
|
+
if not needs_helper:
|
|
64
|
+
return script
|
|
65
|
+
line = script.script_decl.line if script.script_decl is not None else 1
|
|
66
|
+
helper = FuncDecl(name=SESSION_HELPER, params=[Parameter(name='s')], body=with_weekdays(Identifier(name='s')),
|
|
67
|
+
line=line)
|
|
68
|
+
return dataclasses.replace(script, declarations=[helper] + list(script.declarations))
|