> ## Documentation Index
> Fetch the complete documentation index at: https://systematica.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Utils

> systematica.signals.utils

## `clean_signals_1d_nb`

```python theme={null}
clean_signals_1d_nb(
    long_entries: numpy.ndarray,
    long_exits: numpy.ndarray,
    short_entries: numpy.ndarray,
    short_exits: numpy.ndarray,
) ‑> Tuple[numpy.ndarray, ...]
```

Cleans 1d entry and exit signals by ensuring that positions
are entered and exited in a logical manner.

The function prevents overlapping or redundant signals, ensuring that a position
is opened only when there is no existing position and closed only when an open
position exists.

**Parameters**:

| Name            | Type         | Default | Description                                   |
| --------------- | ------------ | ------- | --------------------------------------------- |
| `long_entries`  | `tp.Array1d` | `--`    | Boolean array indicating long entry signals.  |
| `long_exits`    | `tp.Array1d` | `--`    | Boolean array indicating long exit signals.   |
| `short_entries` | `tp.Array1d` | `--`    | Boolean array indicating short entry signals. |
| `short_exits`   | `tp.Array1d` | `--`    | Boolean array indicating short exit signals.  |

**Returns**:

| Type                  | Description                                                                                           |
| --------------------- | ----------------------------------------------------------------------------------------------------- |
| `tuple of tp.Array1d` | Tuple containing cleaned boolean arrays for long entries, long exits, short entries, and short exits. |

## `clean_signals_nb`

```python theme={null}
clean_signals_nb(
    long_entries: numpy.ndarray,
    long_exits: numpy.ndarray,
    short_entries: numpy.ndarray,
    short_exits: numpy.ndarray,
) ‑> Tuple[numpy.ndarray, ...]
```

Cleans 2d entry and exit signals by ensuring that positions
are entered and exited in a logical manner for each column (asset/time series).

The function prevents overlapping or redundant signals for each column, ensuring
that a position is opened only when there is no existing position and closed
only when an open position exists.

**Parameters**:

| Name            | Type         | Default | Description                                                  |
| --------------- | ------------ | ------- | ------------------------------------------------------------ |
| `long_entries`  | `tp.Array2d` | `--`    | Boolean array indicating long entry signals (time, assets).  |
| `long_exits`    | `tp.Array2d` | `--`    | Boolean array indicating long exit signals (time, assets).   |
| `short_entries` | `tp.Array2d` | `--`    | Boolean array indicating short entry signals (time, assets). |
| `short_exits`   | `tp.Array2d` | `--`    | Boolean array indicating short exit signals (time, assets).  |

**Returns**:

| Type                  | Description                                                                                                                           |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `tuple of tp.Array2d` | Tuple containing cleaned boolean arrays for long entries, long exits, short entries, and short exits, all in the same shape as input. |

## `fshift_1d_nb`

```python theme={null}
fshift_1d_nb(
    long_entries: numpy.ndarray,
    long_exits: numpy.ndarray,
    short_entries: numpy.ndarray,
    short_exits: numpy.ndarray,
    n: int = 1,
) ‑> Tuple[numpy.ndarray, ...]
```

Shift forward 1d signal arrays by `n` positions.

Numba equivalent to `pd.Series(arr).shift(n)`.

**Parameters**:

| Name            | Type         | Default | Description                                   |
| --------------- | ------------ | ------- | --------------------------------------------- |
| `long_entries`  | `tp.Array1d` | `--`    | Boolean array indicating long entry signals.  |
| `long_exits`    | `tp.Array1d` | `--`    | Boolean array indicating long exit signals.   |
| `short_entries` | `tp.Array1d` | `--`    | Boolean array indicating short entry signals. |
| `short_exits`   | `tp.Array1d` | `--`    | Boolean array indicating short exit signals.  |
| `n`             | `int`        | `1`     | Number of steps. Defaults to `1`.             |

**Returns**:

| Type      | Description      |
| --------- | ---------------- |
| `Signals` | shifted signals. |

## `fshift_nb`

```python theme={null}
fshift_nb(
    long_entries: numpy.ndarray,
    long_exits: numpy.ndarray,
    short_entries: numpy.ndarray,
    short_exits: numpy.ndarray,
    n: int = 1,
) ‑> Tuple[numpy.ndarray, ...]
```

Shift forward 2d signal arrays by `n` positions.

Numba equivalent to `pd.Series(arr).shift(n)`.

**Parameters**:

| Name            | Type         | Default | Description                                   |
| --------------- | ------------ | ------- | --------------------------------------------- |
| `long_entries`  | `tp.Array1d` | `--`    | Boolean array indicating long entry signals.  |
| `long_exits`    | `tp.Array1d` | `--`    | Boolean array indicating long exit signals.   |
| `short_entries` | `tp.Array1d` | `--`    | Boolean array indicating short entry signals. |
| `short_exits`   | `tp.Array1d` | `--`    | Boolean array indicating short exit signals.  |
| `n`             | `int`        | `1`     | Number of steps. Defaults to `1`.             |

**Returns**:

| Type      | Description      |
| --------- | ---------------- |
| `Signals` | shifted signals. |

## `prepare_signals_nb`

```python theme={null}
prepare_signals_nb(
    signal_func: Callable,
) ‑> Callable
```

Wraps a signal function to automatically create threshold arrays for
predictions.

The wrapper ensures that input arrays match the shape of the `model_output`
array and converts them to floating-point type for further processing.

**Parameters**:

| Name          | Type          | Default | Description                                                  |
| ------------- | ------------- | ------- | ------------------------------------------------------------ |
| `signal_func` | `tp.Callable` | `--`    | Function that processes signals using predefined thresholds. |

**Returns**:

| Type          | Description                                                                                                        |
| ------------- | ------------------------------------------------------------------------------------------------------------------ |
| `tp.Callable` | A Numba JIT-compiled function that applies the given signal function with correctly shaped and typed input arrays. |
