Skip to content
New: Star-import, signal unraveling, Python 3.12 support, and more

#
Signals

Question

Learn more in Signal development tutorial.

## Cleaning

Only two arrays can be cleaned at a time, for more arrays write a custom Numba function that does the job.

Clean 4 arrays

    @njit
    def custom_clean_nb(long_en, long_ex, short_en, short_ex):
        new_long_en = np.full_like(long_en, False)
        new_long_ex = np.full_like(long_ex, False)
        new_short_en = np.full_like(short_en, False)
        new_short_ex = np.full_like(short_ex, False)

        for col in range(long_en.shape[1]):  # (1)!
            position = 0  # (2)!
            for i in range(long_en.shape[0]):  # (3)!
                if long_en[i, col] and position != 1:
                    new_long_en[i, col] = True  # (4)!
                    position = 1
                elif short_en[i, col] and position != -1:
                    new_short_en[i, col] = True
                    position = -1
                elif long_ex[i, col] and position == 1:
                    new_long_ex[i, col] = True
                    position = 0
                elif short_ex[i, col] and position == -1:
                    new_short_ex[i, col] = True
                    position = 0

        return new_long_en, new_long_ex, new_short_en, new_short_ex

1. Iterate over columns (i.e., assets)
2. Initial position per column: 0 - no position, 1 - long position, -1 - short position
3. Iterate over rows (i.e., bars)
4. Keep a signal only if the above condition is met, otherwise the signal will be skipped

Tip

Convert each input array to NumPy with `arr = vbt.to_2d_array(df)` and then each output array back to Pandas with `new_df = df.vbt.wrapper.wrap(arr)`.

#### Cookie consent

We use cookies to recognize your repeated visits and preferences, as well as to measure the effectiveness of our documentation and whether users find what they're searching for. With your consent, you're helping us to make our documentation better.

- Google Analytics
- GitHub

Manage settings