oakscriptpy 0.1.0__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.
oakscriptpy/math_.py ADDED
@@ -0,0 +1,184 @@
1
+ """Math namespace — mirrors PineScript math.* functions (scalar-only initially)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import math as _math
6
+ import random as _random
7
+
8
+ pi = _math.pi
9
+ e = _math.e
10
+ phi = 1.618033988749895
11
+ rphi = 0.618033988749895
12
+
13
+
14
+ def abs(value: float) -> float:
15
+ return _math.fabs(value)
16
+
17
+
18
+ def ceil(value: float) -> int:
19
+ return _math.ceil(value)
20
+
21
+
22
+ def floor(value: float) -> int:
23
+ return _math.floor(value)
24
+
25
+
26
+ def round(value: float, precision: int | None = None) -> float:
27
+ if precision is None:
28
+ return builtins_round(value)
29
+ multiplier = 10 ** precision
30
+ return builtins_round(value * multiplier) / multiplier
31
+
32
+
33
+ builtins_round = __builtins__["round"] if isinstance(__builtins__, dict) else getattr(__builtins__, "round")
34
+
35
+
36
+ def max(*values: float) -> float:
37
+ return builtins_max(values)
38
+
39
+
40
+ builtins_max = __builtins__["max"] if isinstance(__builtins__, dict) else getattr(__builtins__, "max")
41
+
42
+
43
+ def min(*values: float) -> float:
44
+ return builtins_min(values)
45
+
46
+
47
+ builtins_min = __builtins__["min"] if isinstance(__builtins__, dict) else getattr(__builtins__, "min")
48
+
49
+
50
+ def avg(*values: float) -> float:
51
+ return builtins_sum(values) / len(values)
52
+
53
+
54
+ builtins_sum = __builtins__["sum"] if isinstance(__builtins__, dict) else getattr(__builtins__, "sum")
55
+
56
+
57
+ def sum(source: list[float], length: int) -> list[float]:
58
+ result: list[float] = []
59
+ for i in range(len(source)):
60
+ if i < length - 1:
61
+ result.append(float("nan"))
62
+ else:
63
+ total = 0.0
64
+ for j in range(length):
65
+ total += source[i - j]
66
+ result.append(total)
67
+ return result
68
+
69
+
70
+ def sqrt(value: float) -> float:
71
+ return _math.sqrt(value)
72
+
73
+
74
+ def pow(base: float, exponent: float) -> float:
75
+ return base ** exponent
76
+
77
+
78
+ def exp(value: float) -> float:
79
+ return _math.exp(value)
80
+
81
+
82
+ def log(value: float) -> float:
83
+ return _math.log(value)
84
+
85
+
86
+ def log10(value: float) -> float:
87
+ return _math.log10(value)
88
+
89
+
90
+ def log2(value: float) -> float:
91
+ return _math.log2(value)
92
+
93
+
94
+ def sin(value: float) -> float:
95
+ return _math.sin(value)
96
+
97
+
98
+ def cos(value: float) -> float:
99
+ return _math.cos(value)
100
+
101
+
102
+ def tan(value: float) -> float:
103
+ return _math.tan(value)
104
+
105
+
106
+ def asin(value: float) -> float:
107
+ return _math.asin(value)
108
+
109
+
110
+ def acos(value: float) -> float:
111
+ return _math.acos(value)
112
+
113
+
114
+ def atan(value: float) -> float:
115
+ return _math.atan(value)
116
+
117
+
118
+ def atan2(y: float, x: float) -> float:
119
+ return _math.atan2(y, x)
120
+
121
+
122
+ def sinh(value: float) -> float:
123
+ return _math.sinh(value)
124
+
125
+
126
+ def cosh(value: float) -> float:
127
+ return _math.cosh(value)
128
+
129
+
130
+ def tanh(value: float) -> float:
131
+ return _math.tanh(value)
132
+
133
+
134
+ def toradians(degrees: float) -> float:
135
+ return degrees * (_math.pi / 180)
136
+
137
+
138
+ def todegrees(radians: float) -> float:
139
+ return radians * (180 / _math.pi)
140
+
141
+
142
+ def random(min_val: float | None = None, max_val: float | None = None, _seed: int | None = None) -> float:
143
+ rand = _random.random()
144
+ if min_val is not None and max_val is not None:
145
+ return min_val + rand * (max_val - min_val)
146
+ return rand
147
+
148
+
149
+ def sign(value: float) -> int:
150
+ if value > 0:
151
+ return 1
152
+ if value < 0:
153
+ return -1
154
+ return 0
155
+
156
+
157
+ def round_to_mintick(number: float, mintick: float | None = None) -> float:
158
+ if _math.isnan(number):
159
+ return float("nan")
160
+ if mintick is None:
161
+ raise ValueError("math.round_to_mintick() requires mintick value.")
162
+ if mintick == 0:
163
+ return number
164
+ return builtins_round(number / mintick) * mintick
165
+
166
+
167
+ def factorial(n: int) -> int:
168
+ return _math.factorial(n)
169
+
170
+
171
+ def combination(n: int, k: int) -> int:
172
+ return _math.comb(n, k)
173
+
174
+
175
+ def permutation(n: int, k: int) -> int:
176
+ return _math.perm(n, k)
177
+
178
+
179
+ def gcd(a: int, b: int) -> int:
180
+ return _math.gcd(a, b)
181
+
182
+
183
+ def lcm(a: int, b: int) -> int:
184
+ return _math.lcm(a, b)