fast-trading-simulator 0.0.2__py3-none-any.whl
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.
Potentially problematic release.
This version of fast-trading-simulator might be problematic. Click here for more details.
|
File without changes
|
|
@@ -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
|
+

|
|
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,6 @@
|
|
|
1
|
+
fast_trading_simulator/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
fast_trading_simulator/simulate.py,sha256=u0m-gwiVgASThnwxas4Aj4wJlOPTG2YmpRamXqK2Rug,1412
|
|
3
|
+
fast_trading_simulator-0.0.2.dist-info/METADATA,sha256=o1Oop9qu_T0k6dd5P1EunbNRkQew65XvY8GsGsGyak4,2923
|
|
4
|
+
fast_trading_simulator-0.0.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
5
|
+
fast_trading_simulator-0.0.2.dist-info/top_level.txt,sha256=2NFj3ZJtd7rkdp_xQ-YNo8skhP0qrkxqA-dCJsrs8IQ,23
|
|
6
|
+
fast_trading_simulator-0.0.2.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fast_trading_simulator
|