fast-trading-simulator 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ include requirements.txt
@@ -0,0 +1,97 @@
1
+ Metadata-Version: 2.4
2
+ Name: fast-trading-simulator
3
+ Version: 0.0.2
4
+ Summary: Numba accelerated minimalist trading simulator
5
+ Home-page: https://github.com/SerenaTradingResearch/fast-trading-simulator
6
+ Author: Ricky Ding
7
+ Author-email: e0134117@u.nus.edu
8
+ License: MIT
9
+ Keywords: trading,simulator,numba,quant,minimalist
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Topic :: Office/Business :: Financial
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+ Requires-Dist: numba
16
+ Requires-Dist: numpy
17
+ Dynamic: author
18
+ Dynamic: author-email
19
+ Dynamic: classifier
20
+ Dynamic: description
21
+ Dynamic: description-content-type
22
+ Dynamic: home-page
23
+ Dynamic: keywords
24
+ Dynamic: license
25
+ Dynamic: requires-dist
26
+ Dynamic: requires-python
27
+ Dynamic: summary
28
+
29
+
30
+ ## Intro
31
+
32
+ - `Numba` accelerated `minimalist` trading simulator
33
+ - In `28 lines`:
34
+ - Multi-symbol
35
+ - Multi-position, long & short, continuous from -1 to 1
36
+ - Timeout, take-profit, stop-loss, trading fee
37
+ - Initial cash, minimum cash, **allocation ratio (risk control)**
38
+
39
+ ![](https://raw.githubusercontent.com/SerenaTradingResearch/fast-trading-simulator/refs/heads/main/test/simulate.png)
40
+
41
+ ## Usage
42
+
43
+ ```bash
44
+ pip install fast-trading-simulator
45
+ ```
46
+
47
+ - [Simulation data (2025-07-01 to 2025-08-01)](https://raw.githubusercontent.com/SerenaTradingResearch/fast-trading-simulator/refs/heads/main/test/futures_sim_data_2025-07-01_2025-08-01.pkl)
48
+
49
+ ```py
50
+ from typing import Dict
51
+
52
+ import numpy as np
53
+ from crypto_data_downloader.utils import load_pkl
54
+ from trading_models.utils import plot_general
55
+
56
+ from fast_trading_simulator.simulate import simulate
57
+
58
+ data: Dict = load_pkl("futures_sim_data_2025-07-01_2025-08-01.pkl", gz=True)
59
+ sim_data: Dict[str, Dict[str, np.ndarray]] = data["sim_data"]
60
+ # for sym, x in sim_data.items():
61
+ # x["position"] = custom_strategy(x["close"])
62
+ symbols = list(sim_data.keys())
63
+ fields = list(sim_data["BTCUSDT"].keys())
64
+ arr = np.array([list(x.values()) for x in sim_data.values()])
65
+ arr = arr.transpose((2, 0, 1))
66
+
67
+ print(f"data keys: {list(data.keys())}")
68
+ print(f"arr.shape: {arr.shape} (time, symbols, fields)")
69
+ print(f"{len(symbols)} symbols: {symbols[:3]}...")
70
+ print(f"{len(fields)} fields: {fields}")
71
+ """
72
+ timeout: int, number of time steps
73
+ take_profit: float, e.g. 0.01 (1%)
74
+ stop_loss: float, e.g. -0.3 (-30%)
75
+ fee: float, buy+sell total, e.g. 7e-4 (0.07%)
76
+ """
77
+
78
+ data["sim_data"] = arr
79
+ trades = np.array(simulate(**data, init_cash=10e3, alloc_ratio=0.005))
80
+ plots = {
81
+ f"worth ({len(trades)} trades)": trades[:, -1],
82
+ "position_hist": trades[:, 2],
83
+ "duration_hist": trades[:, -3],
84
+ "profit_hist": trades[:, -2],
85
+ }
86
+ plot_general(plots, "simulate")
87
+
88
+ ```
89
+
90
+ - Output
91
+
92
+ ```bash
93
+ data keys: ['sim_data', 'timeout', 'take_profit', 'stop_loss', 'fee']
94
+ arr.shape: (8417, 482, 3) (time, symbols, fields)
95
+ 482 symbols: ['BTCUSDT', 'ETHUSDT', 'BCHUSDT']...
96
+ 3 fields: ['open_time', 'close', 'position']
97
+ ```
@@ -0,0 +1,69 @@
1
+
2
+ ## Intro
3
+
4
+ - `Numba` accelerated `minimalist` trading simulator
5
+ - In `28 lines`:
6
+ - Multi-symbol
7
+ - Multi-position, long & short, continuous from -1 to 1
8
+ - Timeout, take-profit, stop-loss, trading fee
9
+ - Initial cash, minimum cash, **allocation ratio (risk control)**
10
+
11
+ ![](https://raw.githubusercontent.com/SerenaTradingResearch/fast-trading-simulator/refs/heads/main/test/simulate.png)
12
+
13
+ ## Usage
14
+
15
+ ```bash
16
+ pip install fast-trading-simulator
17
+ ```
18
+
19
+ - [Simulation data (2025-07-01 to 2025-08-01)](https://raw.githubusercontent.com/SerenaTradingResearch/fast-trading-simulator/refs/heads/main/test/futures_sim_data_2025-07-01_2025-08-01.pkl)
20
+
21
+ ```py
22
+ from typing import Dict
23
+
24
+ import numpy as np
25
+ from crypto_data_downloader.utils import load_pkl
26
+ from trading_models.utils import plot_general
27
+
28
+ from fast_trading_simulator.simulate import simulate
29
+
30
+ data: Dict = load_pkl("futures_sim_data_2025-07-01_2025-08-01.pkl", gz=True)
31
+ sim_data: Dict[str, Dict[str, np.ndarray]] = data["sim_data"]
32
+ # for sym, x in sim_data.items():
33
+ # x["position"] = custom_strategy(x["close"])
34
+ symbols = list(sim_data.keys())
35
+ fields = list(sim_data["BTCUSDT"].keys())
36
+ arr = np.array([list(x.values()) for x in sim_data.values()])
37
+ arr = arr.transpose((2, 0, 1))
38
+
39
+ print(f"data keys: {list(data.keys())}")
40
+ print(f"arr.shape: {arr.shape} (time, symbols, fields)")
41
+ print(f"{len(symbols)} symbols: {symbols[:3]}...")
42
+ print(f"{len(fields)} fields: {fields}")
43
+ """
44
+ timeout: int, number of time steps
45
+ take_profit: float, e.g. 0.01 (1%)
46
+ stop_loss: float, e.g. -0.3 (-30%)
47
+ fee: float, buy+sell total, e.g. 7e-4 (0.07%)
48
+ """
49
+
50
+ data["sim_data"] = arr
51
+ trades = np.array(simulate(**data, init_cash=10e3, alloc_ratio=0.005))
52
+ plots = {
53
+ f"worth ({len(trades)} trades)": trades[:, -1],
54
+ "position_hist": trades[:, 2],
55
+ "duration_hist": trades[:, -3],
56
+ "profit_hist": trades[:, -2],
57
+ }
58
+ plot_general(plots, "simulate")
59
+
60
+ ```
61
+
62
+ - Output
63
+
64
+ ```bash
65
+ data keys: ['sim_data', 'timeout', 'take_profit', 'stop_loss', 'fee']
66
+ arr.shape: (8417, 482, 3) (time, symbols, fields)
67
+ 482 symbols: ['BTCUSDT', 'ETHUSDT', 'BCHUSDT']...
68
+ 3 fields: ['open_time', 'close', 'position']
69
+ ```
@@ -0,0 +1,47 @@
1
+ import numba
2
+ import numpy as np
3
+
4
+
5
+ @numba.njit
6
+ def simulate(
7
+ sim_data: np.ndarray,
8
+ timeout: int,
9
+ take_profit: float,
10
+ stop_loss: float,
11
+ fee: float,
12
+ init_cash=10e3,
13
+ min_cash=10,
14
+ alloc_ratio=0.01,
15
+ min_pos=0.5,
16
+ ):
17
+ worth = cash_left = init_cash
18
+ open_trades = {}
19
+ done_trades = []
20
+
21
+ TIME, SYMBOL, FIELD = sim_data.shape
22
+ sim_data[:, :, 2] = np.round(sim_data[:, :, 2], 2)
23
+
24
+ for t in range(TIME):
25
+
26
+ for id, trade in list(open_trades.items()):
27
+ t1, s1, pos1, price1, cash1 = trade[:5]
28
+ time2, price2, _ = sim_data[t, int(s1)]
29
+ dt = t - t1
30
+ pr = np.sign(pos1) * (price2 / price1 - 1) - fee
31
+ if dt >= timeout or pr >= take_profit or pr <= stop_loss:
32
+ cash_left += cash1 * (1 + pr)
33
+ worth += cash1 * pr
34
+ del open_trades[id]
35
+ trade[-3:] = dt, pr, worth
36
+ done_trades.append(trade)
37
+
38
+ for s in range(SYMBOL):
39
+ time, price, pos = sim_data[t, s]
40
+ if abs(pos) >= min_pos:
41
+ cash = int(min(cash_left, worth * alloc_ratio * abs(pos)))
42
+ id = int((s + 1) * np.sign(pos))
43
+ if cash >= min_cash and id not in open_trades:
44
+ cash_left -= cash
45
+ open_trades[id] = np.array([t, s, pos, price, cash, 0.0, 0.0, 0.0])
46
+
47
+ return done_trades
@@ -0,0 +1,97 @@
1
+ Metadata-Version: 2.4
2
+ Name: fast-trading-simulator
3
+ Version: 0.0.2
4
+ Summary: Numba accelerated minimalist trading simulator
5
+ Home-page: https://github.com/SerenaTradingResearch/fast-trading-simulator
6
+ Author: Ricky Ding
7
+ Author-email: e0134117@u.nus.edu
8
+ License: MIT
9
+ Keywords: trading,simulator,numba,quant,minimalist
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Topic :: Office/Business :: Financial
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+ Requires-Dist: numba
16
+ Requires-Dist: numpy
17
+ Dynamic: author
18
+ Dynamic: author-email
19
+ Dynamic: classifier
20
+ Dynamic: description
21
+ Dynamic: description-content-type
22
+ Dynamic: home-page
23
+ Dynamic: keywords
24
+ Dynamic: license
25
+ Dynamic: requires-dist
26
+ Dynamic: requires-python
27
+ Dynamic: summary
28
+
29
+
30
+ ## Intro
31
+
32
+ - `Numba` accelerated `minimalist` trading simulator
33
+ - In `28 lines`:
34
+ - Multi-symbol
35
+ - Multi-position, long & short, continuous from -1 to 1
36
+ - Timeout, take-profit, stop-loss, trading fee
37
+ - Initial cash, minimum cash, **allocation ratio (risk control)**
38
+
39
+ ![](https://raw.githubusercontent.com/SerenaTradingResearch/fast-trading-simulator/refs/heads/main/test/simulate.png)
40
+
41
+ ## Usage
42
+
43
+ ```bash
44
+ pip install fast-trading-simulator
45
+ ```
46
+
47
+ - [Simulation data (2025-07-01 to 2025-08-01)](https://raw.githubusercontent.com/SerenaTradingResearch/fast-trading-simulator/refs/heads/main/test/futures_sim_data_2025-07-01_2025-08-01.pkl)
48
+
49
+ ```py
50
+ from typing import Dict
51
+
52
+ import numpy as np
53
+ from crypto_data_downloader.utils import load_pkl
54
+ from trading_models.utils import plot_general
55
+
56
+ from fast_trading_simulator.simulate import simulate
57
+
58
+ data: Dict = load_pkl("futures_sim_data_2025-07-01_2025-08-01.pkl", gz=True)
59
+ sim_data: Dict[str, Dict[str, np.ndarray]] = data["sim_data"]
60
+ # for sym, x in sim_data.items():
61
+ # x["position"] = custom_strategy(x["close"])
62
+ symbols = list(sim_data.keys())
63
+ fields = list(sim_data["BTCUSDT"].keys())
64
+ arr = np.array([list(x.values()) for x in sim_data.values()])
65
+ arr = arr.transpose((2, 0, 1))
66
+
67
+ print(f"data keys: {list(data.keys())}")
68
+ print(f"arr.shape: {arr.shape} (time, symbols, fields)")
69
+ print(f"{len(symbols)} symbols: {symbols[:3]}...")
70
+ print(f"{len(fields)} fields: {fields}")
71
+ """
72
+ timeout: int, number of time steps
73
+ take_profit: float, e.g. 0.01 (1%)
74
+ stop_loss: float, e.g. -0.3 (-30%)
75
+ fee: float, buy+sell total, e.g. 7e-4 (0.07%)
76
+ """
77
+
78
+ data["sim_data"] = arr
79
+ trades = np.array(simulate(**data, init_cash=10e3, alloc_ratio=0.005))
80
+ plots = {
81
+ f"worth ({len(trades)} trades)": trades[:, -1],
82
+ "position_hist": trades[:, 2],
83
+ "duration_hist": trades[:, -3],
84
+ "profit_hist": trades[:, -2],
85
+ }
86
+ plot_general(plots, "simulate")
87
+
88
+ ```
89
+
90
+ - Output
91
+
92
+ ```bash
93
+ data keys: ['sim_data', 'timeout', 'take_profit', 'stop_loss', 'fee']
94
+ arr.shape: (8417, 482, 3) (time, symbols, fields)
95
+ 482 symbols: ['BTCUSDT', 'ETHUSDT', 'BCHUSDT']...
96
+ 3 fields: ['open_time', 'close', 'position']
97
+ ```
@@ -0,0 +1,12 @@
1
+ MANIFEST.in
2
+ README.md
3
+ requirements.txt
4
+ setup.py
5
+ fast_trading_simulator/__init__.py
6
+ fast_trading_simulator/simulate.py
7
+ fast_trading_simulator.egg-info/PKG-INFO
8
+ fast_trading_simulator.egg-info/SOURCES.txt
9
+ fast_trading_simulator.egg-info/dependency_links.txt
10
+ fast_trading_simulator.egg-info/requires.txt
11
+ fast_trading_simulator.egg-info/top_level.txt
12
+ test/test.py
@@ -0,0 +1 @@
1
+ fast_trading_simulator
@@ -0,0 +1,2 @@
1
+ numba
2
+ numpy
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,25 @@
1
+ from setuptools import find_packages, setup
2
+
3
+ with open("README.md", encoding="utf-8") as f:
4
+ long_description = f.read()
5
+
6
+ setup(
7
+ name="fast-trading-simulator",
8
+ version="0.0.2",
9
+ author="Ricky Ding",
10
+ author_email="e0134117@u.nus.edu",
11
+ description="Numba accelerated minimalist trading simulator",
12
+ long_description=long_description,
13
+ long_description_content_type="text/markdown",
14
+ url="https://github.com/SerenaTradingResearch/fast-trading-simulator",
15
+ packages=find_packages(),
16
+ install_requires=open("requirements.txt").read().splitlines(),
17
+ python_requires=">=3.8",
18
+ license="MIT",
19
+ keywords=["trading", "simulator", "numba", "quant", "minimalist"],
20
+ classifiers=[
21
+ "License :: OSI Approved :: MIT License",
22
+ "Programming Language :: Python :: 3",
23
+ "Topic :: Office/Business :: Financial",
24
+ ],
25
+ )
@@ -0,0 +1,37 @@
1
+ from typing import Dict
2
+
3
+ import numpy as np
4
+ from crypto_data_downloader.utils import load_pkl
5
+ from trading_models.utils import plot_general
6
+
7
+ from fast_trading_simulator.simulate import simulate
8
+
9
+ data: Dict = load_pkl("futures_sim_data_2025-07-01_2025-08-01.pkl", gz=True)
10
+ sim_data: Dict[str, Dict[str, np.ndarray]] = data["sim_data"]
11
+ # for sym, x in sim_data.items():
12
+ # x["position"] = custom_strategy(x["close"])
13
+ symbols = list(sim_data.keys())
14
+ fields = list(sim_data["BTCUSDT"].keys())
15
+ arr = np.array([list(x.values()) for x in sim_data.values()])
16
+ arr = arr.transpose((2, 0, 1))
17
+
18
+ print(f"data keys: {list(data.keys())}")
19
+ print(f"arr.shape: {arr.shape} (time, symbols, fields)")
20
+ print(f"{len(symbols)} symbols: {symbols[:3]}...")
21
+ print(f"{len(fields)} fields: {fields}")
22
+ """
23
+ timeout: int, number of time steps
24
+ take_profit: float, e.g. 0.01 (1%)
25
+ stop_loss: float, e.g. -0.3 (-30%)
26
+ fee: float, buy+sell total, e.g. 7e-4 (0.07%)
27
+ """
28
+
29
+ data["sim_data"] = arr
30
+ trades = np.array(simulate(**data, init_cash=10e3, alloc_ratio=0.005))
31
+ plots = {
32
+ f"worth ({len(trades)} trades)": trades[:, -1],
33
+ "position_hist": trades[:, 2],
34
+ "duration_hist": trades[:, -3],
35
+ "profit_hist": trades[:, -2],
36
+ }
37
+ plot_general(plots, "simulate")