Demark Sequential Using Numpy

1
import numpy as np
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def np_shift(np_array: np.array, n: int, fill=np.nan):
    e = np.empty_like(np_array)
    if n >= 0:
        e[:n] = fill
        e[n:] = np_array[:-n]
    else:
        e[n:] = fill
        e[:n] = np_array[-n:]
    return e


def td_sequential(close: list) -> np.array:
    shift_const: int = 4
    close_np = np.array(close)
    close_shift = np_shift(close_np, shift_const, 0)
    compare_array = close_np > close_shift
    result = np.empty(len(close_np), int)
    counting_number: int = 0
    for i in range(len(close_np)):
        if np.isnan(close_shift[i]):
            result[i] = 0
        else:
            compare_bool = compare_array[i]
            if compare_bool:
                if counting_number >= 0:
                    counting_number += 1
                else:
                    counting_number = 1
            else:
                if counting_number <= 0:
                    counting_number -= 1
                else:
                    counting_number = -1
            result[i] = counting_number
    return result

Comments