Skip to main content

Intro

  • Numba accelerated minimalist trading simulator

Usage

pip install fast-trading-simulator
from typing import Dict

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.sim import map_trades, simulate
from fast_trading_simulator.utils import pymoo_minimize

path = "./futures_data_2025-08-01_2025-11-20.pkl"
data: D_TYPE = load_pkl(path, gz=True)
ref_sym = "BTCUSDT"
price_idx = 1
T = len(data[ref_sym])
data = {k: v for k, v in data.items() if len(v) == T}
market = np.array(list(data.values()))
tot_fee = 1e-3
liq_fee = np.full(len(data), 0.02)


def loss_fn(p: Dict, plot=False):
    MA = [ta.SMA, ta.KAMA][int(p["ma_idx"])]
    all_act = []
    for sym, v in data.items():
        price = v[:, price_idx]
        obs = price / MA(price, int(p["ma_period"])) - 1
        pos = np.where(obs < -p["delta"], 1, 0)
        pos = np.where(obs > p["delta"], -1, pos)
        lev = np.full(T, int(p["lev"]))
        timeout = np.full(T, p["timeout"])
        take_profit = np.full(T, p["take_profit"])
        stop_loss = np.full(T, p["stop_loss"])
        act = np.array([pos, lev, timeout, take_profit, stop_loss]).T
        all_act.append(act)
    action = np.array(all_act)
    trades = simulate(market, action, tot_fee, liq_fee, use_ratio=0.2, alloc_ratio=0.01)
    res = map_trades(trades, plot=plot)
    final_worth = res["worth"][-1]
    return -final_worth


if 0:
    conf = {
        "ma_idx": [0, 1, 1],
        "ma_period": [10, 100, 1],
        "delta": [0.01, 0.1, 0.005],
        "lev": [1, 3, 1],
        "timeout": [10, 100, 1],
        "take_profit": [0.01, 0.1, 0.01],
        "stop_loss": [-0.5, -0.1, 0.01],
    }
    pymoo_minimize(loss_fn, conf)
else:
    p_best = {
        "ma_idx": 0,
        "ma_period": 26,
        "delta": 0.1,
        "lev": 3,
        "timeout": 74,
        "take_profit": 0.01,
        "stop_loss": -0.47,
    }
    loss_fn(p_best, plot=True)

Metadata

Release files for fast-trading-simulator 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fast-trading-simulator 0.1.9
File Size Uploaded
fast_trading_simulator-0.1.9.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fast-trading-simulator 0.1.9
File Interpreter ABI Platform
fast_trading_simulator-0.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / fast_trading_simulator-0.1.9.tar.gz

Download URL fast_trading_simulator-0.1.9.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b78959e24a995734b6e1150b4f1811125e04118c6ac7326483eb01c843e8cd7e
BLAKE2b-256 checksum
How to use checksums
21f312f940a974bcd34bc9691aae92710e3a60fc60c995a7864518f6a4c95f5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / fast_trading_simulator-0.1.9-py3-none-any.whl

Download URL fast_trading_simulator-0.1.9-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7973bb88c95d5d489280c5bc56b5de67193d350c4b1ee74a76e34770ed6840f9
BLAKE2b-256 checksum
How to use checksums
0938f6cc3c662eea75807371174b69fb7a432c3dfcbeb10f33f44dde522c2743
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.1.9 This release

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page