Skip to main content

Numba accelerated minimalist trading simulator

Project description

Intro

  • Numba accelerated minimalist trading simulator
  • In 28 lines:
    • Multi-symbol
    • Multi-position, long & short, continuous from -1 to 1
    • Timeout, take-profit, stop-loss, trading fee
    • Initial cash, minimum cash, allocation-ratio & use-ratio (risk control)

Usage

pip install fast-trading-simulator
import numpy as np
import talib as ta
from crypto_data_downloader.utils import load_pkl
from trading_models.utils import D_TYPE

from fast_trading_simulator.simulate import simulate
from fast_trading_simulator.utils import plot_trades, pymoo_minimize

path = "futures_data_2025-08-01_2025-11-20.pkl"
data: D_TYPE = load_pkl(path, gz=True)
T = len(data["BTCUSDT"])
cache = {}


def loss_func(P, plot=False):
    sim_data = []
    for sym, arr in data.items():
        if len(arr) == T:
            time, close = arr.T
            period = int(P["period"])
            id = f"{sym}_{period}"
            x = cache.get(id) or close / ta.KAMA(close, period) - 1
            pos = np.zeros(T)
            pos[x > P["high_bar"]] = -1
            pos[x < P["low_bar"]] = 1
            sim_data.append([time, close, pos])

    sim_data = np.array(sim_data).transpose(2, 0, 1)
    trades = simulate(
        sim_data,
        timeout=P["timeout"],
        take_profit=P["take_profit"],
        stop_loss=P["stop_loss"],
        fee=1e-3,
        alloc_ratio=P["alloc_ratio"],
        use_ratio=P["use_ratio"],
    )
    if plot:
        plot_trades(trades)
    gain = trades[-1][-1] / trades[0][-1]
    return -gain


conf = {
    "period": [20, 100, 1],
    "high_bar": [0.01, 0.1, 0.001],
    "low_bar": [-0.1, -0.01, 0.001],
    "timeout": [10, 100, 1],
    "take_profit": [0.01, 0.1, 0.001],
    "stop_loss": [-1, -0.2, 0.001],
    "alloc_ratio": [0.01, 0.1, 0.001],
    "use_ratio": [0.1, 1, 0.001],
}
P0 = {
    "period": 82,
    "high_bar": 0.092,
    "low_bar": -0.017,
    "timeout": 87,
    "take_profit": 0.096,
    "stop_loss": -0.607,
    "alloc_ratio": 0.1,
    "use_ratio": 0.214,
}
if 0:
    pymoo_minimize(loss_func, conf)
else:
    loss_func(P0, plot=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast_trading_simulator-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fast_trading_simulator-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file fast_trading_simulator-0.1.0.tar.gz.

File metadata

  • Download URL: fast_trading_simulator-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for fast_trading_simulator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1cc4d67d2103ea571b32973f80f8e1fa0c727ae4004d3727c7af0bec215581c9
MD5 97a877cdc179c3726431e5f328dc21b8
BLAKE2b-256 e888a1bcca5ff0e5b07acf748874ae8ac64f6318c9d2f3ff25fa4a75f20850ee

See more details on using hashes here.

File details

Details for the file fast_trading_simulator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fast_trading_simulator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f421650bf5a74f880d6ffe5e380a1f4c15501d2bb498e23ef5052eeee5435f66
MD5 e5a864f4e28a9505da99d93ad1b51afe
BLAKE2b-256 d4bc7ab2b471a5a84d09d0feafdd75a746664e20576d7e182c2cd5e78b2f6b1d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page