0
# Math Operations
1
2
Mathematical transformation and operator functions for array manipulation and calculations. These functions provide fundamental mathematical operations on price data arrays, enabling custom indicator development and data preprocessing.
3
4
## Capabilities
5
6
### Basic Arithmetic Operations
7
8
```python { .api }
9
def ADD(real0, real1):
10
"""
11
Vector Arithmetic Add
12
13
Element-wise addition of two arrays.
14
15
Parameters:
16
- real0: array-like, first array
17
- real1: array-like, second array
18
19
Returns:
20
numpy.ndarray: Sum of the two arrays
21
"""
22
23
def SUB(real0, real1):
24
"""
25
Vector Arithmetic Subtraction
26
27
Element-wise subtraction of two arrays.
28
29
Parameters:
30
- real0: array-like, first array (minuend)
31
- real1: array-like, second array (subtrahend)
32
33
Returns:
34
numpy.ndarray: Difference (real0 - real1)
35
"""
36
37
def MULT(real0, real1):
38
"""
39
Vector Arithmetic Multiply
40
41
Element-wise multiplication of two arrays.
42
43
Parameters:
44
- real0: array-like, first array
45
- real1: array-like, second array
46
47
Returns:
48
numpy.ndarray: Product of the two arrays
49
"""
50
51
def DIV(real0, real1):
52
"""
53
Vector Arithmetic Divide
54
55
Element-wise division of two arrays.
56
57
Parameters:
58
- real0: array-like, numerator array
59
- real1: array-like, denominator array
60
61
Returns:
62
numpy.ndarray: Quotient (real0 / real1)
63
"""
64
```
65
66
### Aggregation Functions
67
68
```python { .api }
69
def SUM(real, timeperiod=30):
70
"""
71
Summation
72
73
Calculates rolling sum over specified period.
74
75
Parameters:
76
- real: array-like, input data
77
- timeperiod: int, number of periods to sum (default: 30)
78
79
Returns:
80
numpy.ndarray: Rolling sum values
81
"""
82
83
def MAX(real, timeperiod=30):
84
"""
85
Highest value over a specified period
86
87
Parameters:
88
- real: array-like, input data
89
- timeperiod: int, number of periods to examine (default: 30)
90
91
Returns:
92
numpy.ndarray: Maximum values over rolling window
93
"""
94
95
def MIN(real, timeperiod=30):
96
"""
97
Lowest value over a specified period
98
99
Parameters:
100
- real: array-like, input data
101
- timeperiod: int, number of periods to examine (default: 30)
102
103
Returns:
104
numpy.ndarray: Minimum values over rolling window
105
"""
106
107
def MINMAX(real, timeperiod=30):
108
"""
109
Lowest and highest values over a specified period
110
111
Parameters:
112
- real: array-like, input data
113
- timeperiod: int, number of periods to examine (default: 30)
114
115
Returns:
116
tuple: (min_values, max_values) over rolling window
117
"""
118
```
119
120
### Index Functions
121
122
```python { .api }
123
def MAXINDEX(real, timeperiod=30):
124
"""
125
Index of highest value over a specified period
126
127
Parameters:
128
- real: array-like, input data
129
- timeperiod: int, number of periods to examine (default: 30)
130
131
Returns:
132
numpy.ndarray[int32]: Index positions of maximum values
133
"""
134
135
def MININDEX(real, timeperiod=30):
136
"""
137
Index of lowest value over a specified period
138
139
Parameters:
140
- real: array-like, input data
141
- timeperiod: int, number of periods to examine (default: 30)
142
143
Returns:
144
numpy.ndarray[int32]: Index positions of minimum values
145
"""
146
147
def MINMAXINDEX(real, timeperiod=30):
148
"""
149
Indexes of lowest and highest values over a specified period
150
151
Parameters:
152
- real: array-like, input data
153
- timeperiod: int, number of periods to examine (default: 30)
154
155
Returns:
156
tuple: (min_indexes, max_indexes) as int32 arrays
157
"""
158
```
159
160
### Trigonometric Functions
161
162
```python { .api }
163
def SIN(real):
164
"""Vector Trigonometric Sin"""
165
166
def COS(real):
167
"""Vector Trigonometric Cos"""
168
169
def TAN(real):
170
"""Vector Trigonometric Tan"""
171
172
def ASIN(real):
173
"""Vector Trigonometric ASin"""
174
175
def ACOS(real):
176
"""Vector Trigonometric ACos"""
177
178
def ATAN(real):
179
"""Vector Trigonometric ATan"""
180
181
def SINH(real):
182
"""Vector Trigonometric Sinh"""
183
184
def COSH(real):
185
"""Vector Trigonometric Cosh"""
186
187
def TANH(real):
188
"""Vector Trigonometric Tanh"""
189
```
190
191
### Exponential and Logarithmic Functions
192
193
```python { .api }
194
def EXP(real):
195
"""
196
Vector Arithmetic Exp
197
198
Calculates e^x for each element.
199
200
Parameters:
201
- real: array-like, input data
202
203
Returns:
204
numpy.ndarray: Exponential values
205
"""
206
207
def LN(real):
208
"""
209
Vector Log Natural
210
211
Calculates natural logarithm for each element.
212
213
Parameters:
214
- real: array-like, input data (must be positive)
215
216
Returns:
217
numpy.ndarray: Natural logarithm values
218
"""
219
220
def LOG10(real):
221
"""
222
Vector Log10
223
224
Calculates base-10 logarithm for each element.
225
226
Parameters:
227
- real: array-like, input data (must be positive)
228
229
Returns:
230
numpy.ndarray: Base-10 logarithm values
231
"""
232
233
def SQRT(real):
234
"""
235
Vector Square Root
236
237
Calculates square root for each element.
238
239
Parameters:
240
- real: array-like, input data (must be non-negative)
241
242
Returns:
243
numpy.ndarray: Square root values
244
"""
245
```
246
247
### Rounding Functions
248
249
```python { .api }
250
def CEIL(real):
251
"""
252
Vector Ceil
253
254
Rounds each element up to nearest integer.
255
256
Parameters:
257
- real: array-like, input data
258
259
Returns:
260
numpy.ndarray: Ceiling values
261
"""
262
263
def FLOOR(real):
264
"""
265
Vector Floor
266
267
Rounds each element down to nearest integer.
268
269
Parameters:
270
- real: array-like, input data
271
272
Returns:
273
numpy.ndarray: Floor values
274
"""
275
```
276
277
278
## Usage Examples
279
280
```python
281
import talib
282
import numpy as np
283
284
# Sample price data
285
prices1 = np.array([100, 102, 101, 103, 105, 104, 106, 108, 107, 109])
286
prices2 = np.array([50, 51, 50.5, 52, 53, 52.5, 54, 55, 54.5, 56])
287
high = np.array([101, 103, 102, 104, 106, 105, 107, 109, 108, 110])
288
low = np.array([99, 101, 100, 102, 104, 103, 105, 107, 106, 108])
289
290
# Basic arithmetic operations
291
price_sum = talib.ADD(prices1, prices2)
292
price_diff = talib.SUB(prices1, prices2)
293
price_ratio = talib.DIV(prices1, prices2)
294
295
# Aggregation functions
296
rolling_sum = talib.SUM(prices1, timeperiod=5)
297
highest = talib.MAX(prices1, timeperiod=5)
298
lowest = talib.MIN(prices1, timeperiod=5)
299
min_vals, max_vals = talib.MINMAX(prices1, timeperiod=5)
300
301
# Mathematical transformations
302
log_prices = talib.LN(prices1)
303
sqrt_prices = talib.SQRT(prices1)
304
sin_prices = talib.SIN(prices1 / 100) # Normalize for reasonable sine values
305
306
print("Mathematical Operations Results:")
307
print(f"Latest Price Sum: {price_sum[-1]:.2f}")
308
print(f"Latest Price Difference: {price_diff[-1]:.2f}")
309
print(f"Latest Price Ratio: {price_ratio[-1]:.3f}")
310
print(f"5-Period Rolling Sum: {rolling_sum[-1]:.2f}")
311
print(f"5-Period High: {highest[-1]:.2f}")
312
print(f"5-Period Low: {lowest[-1]:.2f}")
313
314
# Custom indicator example: Price momentum using math functions
315
momentum = talib.SUB(prices1, talib.MAX(prices1, timeperiod=3))
316
momentum_normalized = talib.DIV(momentum, talib.MAX(momentum, timeperiod=5))
317
print(f"Custom Momentum Indicator: {momentum_normalized[-1]:.3f}")
318
```
319
320
## Custom Indicator Development
321
322
These math functions enable creation of custom indicators:
323
324
```python
325
# Example: Custom price spread calculation
326
def custom_price_spread(high_prices, low_prices, period=10):
327
# Calculate price range
328
price_range = talib.SUB(high_prices, low_prices)
329
330
# Calculate average range over period
331
avg_range = talib.SUM(price_range, timeperiod=period)
332
normalized_range = talib.DIV(avg_range, period)
333
334
return normalized_range
335
336
# Example: Composite price index
337
def composite_price_index(high, low, close, open_prices):
338
# Weighted average of OHLC
339
hlc_avg = talib.DIV(talib.ADD(talib.ADD(high, low), close), 3)
340
ohlc_avg = talib.DIV(talib.ADD(hlc_avg, open_prices), 2)
341
342
return ohlc_avg
343
```