trading-engine-interface 0.56.25__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.
Files changed (33) hide show
  1. trading_engine_interface/__init__.py +334 -0
  2. trading_engine_interface/common/__init__.py +0 -0
  3. trading_engine_interface/common/enums.py +163 -0
  4. trading_engine_interface/common/errors.py +259 -0
  5. trading_engine_interface/common/models/__init__.py +3 -0
  6. trading_engine_interface/common/models/base_api_model.py +14 -0
  7. trading_engine_interface/common/utils.py +20 -0
  8. trading_engine_interface/domain/__init__.py +8 -0
  9. trading_engine_interface/domain/fill_simulator.py +114 -0
  10. trading_engine_interface/domain/maker_fill.py +138 -0
  11. trading_engine_interface/domain/position_math.py +386 -0
  12. trading_engine_interface/examples/__init__.py +1 -0
  13. trading_engine_interface/examples/touch_bid_strategy.py +131 -0
  14. trading_engine_interface/py.typed +0 -0
  15. trading_engine_interface/runtime/__init__.py +32 -0
  16. trading_engine_interface/runtime/registry.py +298 -0
  17. trading_engine_interface/runtime/strategy.py +94 -0
  18. trading_engine_interface/trading_engine/__init__.py +0 -0
  19. trading_engine_interface/trading_engine/enums.py +284 -0
  20. trading_engine_interface/trading_engine/models/__init__.py +0 -0
  21. trading_engine_interface/trading_engine/models/accounting_models.py +412 -0
  22. trading_engine_interface/trading_engine/models/backtest_models.py +200 -0
  23. trading_engine_interface/trading_engine/models/market_models.py +452 -0
  24. trading_engine_interface/trading_engine/models/order_models.py +520 -0
  25. trading_engine_interface/trading_engine/models/position_breakdown_models.py +130 -0
  26. trading_engine_interface/trading_engine/models/position_models.py +230 -0
  27. trading_engine_interface/trading_engine/models/strategy_metrics_models.py +204 -0
  28. trading_engine_interface/trading_engine/models/strategy_models.py +1360 -0
  29. trading_engine_interface/trading_engine/order_rules.py +209 -0
  30. trading_engine_interface-0.56.25.dist-info/LICENSE +21 -0
  31. trading_engine_interface-0.56.25.dist-info/METADATA +112 -0
  32. trading_engine_interface-0.56.25.dist-info/RECORD +33 -0
  33. trading_engine_interface-0.56.25.dist-info/WHEEL +4 -0
@@ -0,0 +1,334 @@
1
+ """
2
+ Trading Engine Interface Package
3
+
4
+ Provides models, enums, API clients, and interfaces for trading engine interaction.
5
+ """
6
+
7
+ # Common Enums
8
+ from trading_engine_interface.common.enums import Asset, ServiceStatus, Symbol, TimeInterval, VenueName
9
+
10
+ # Common Errors
11
+ from trading_engine_interface.common.errors import (
12
+ ApiConnectionError,
13
+ ApiConnectTimeout,
14
+ ApiError,
15
+ ApiRateLimitError,
16
+ ApiReadTimeout,
17
+ ApiRequestError,
18
+ ApiResponseError,
19
+ ApiServerError,
20
+ ApiTemporaryError,
21
+ ApiUnavailableError,
22
+ BaseError,
23
+ DomainError,
24
+ ErrorType,
25
+ ServiceError,
26
+ map_api_error,
27
+ )
28
+
29
+ from trading_engine_interface.common.models.base_api_model import BaseApiModel
30
+
31
+ # Common Utils
32
+ from trading_engine_interface.common.utils import gen_id, now
33
+
34
+ # Strategy Runtime Contract — registry only at top level.
35
+ # The ``Strategy`` Protocol stays inside ``trading_engine_interface.runtime``
36
+ # to avoid colliding with the existing ``Strategy`` Pydantic DB model exported
37
+ # from ``trading_engine.models.strategy_models``. Engine and strategies import
38
+ # the Protocol explicitly: ``from trading_engine_interface.runtime import Strategy``.
39
+ from trading_engine_interface.runtime import (
40
+ STRATEGY_REGISTRY,
41
+ RegistryEntry,
42
+ StrategyRegistry,
43
+ register_production_configs,
44
+ register_strategy,
45
+ )
46
+
47
+
48
+ # Trading Engine Enums
49
+ from trading_engine_interface.trading_engine.enums import (
50
+ AccountStatus,
51
+ AccountType,
52
+ AppMode,
53
+ AppStatus,
54
+ EntityType,
55
+ ExecutionMode,
56
+ LifecycleEventType,
57
+ LiquidityType,
58
+ MarketStatus,
59
+ OrderStatus,
60
+ OrderType,
61
+ RestartType,
62
+ RfqQuoteSource,
63
+ Side,
64
+ StrategyErrorReason,
65
+ StrategyStatus,
66
+ StrategyStatusSource,
67
+ TransactionType,
68
+ TransferFlowClass,
69
+ TransferFlowClassSource,
70
+ TransferStatus,
71
+ TransferType,
72
+ VenueType,
73
+ )
74
+
75
+
76
+ # Trading Engine Models - Accounting
77
+ from trading_engine_interface.trading_engine.models.accounting_models import (
78
+ Account,
79
+ Balance,
80
+ BalanceDetail,
81
+ BalanceSnapshot,
82
+ KpiPoint,
83
+ KpiSeries,
84
+ Portfolio,
85
+ PortfolioSnapshot,
86
+ RebalancePlanLeg,
87
+ Transaction,
88
+ Transfer,
89
+ UpdateTransferRequest,
90
+ )
91
+
92
+
93
+ # Trading Engine Models - Backtest
94
+ from trading_engine_interface.trading_engine.models.backtest_models import (
95
+ BacktestRequest,
96
+ BacktestResult,
97
+ BacktestResultDetail,
98
+ BacktestStatus,
99
+ KlineSource,
100
+ )
101
+
102
+
103
+
104
+
105
+ # Trading Engine Models - Market
106
+ from trading_engine_interface.trading_engine.models.market_models import (
107
+ FeeAssetType,
108
+ GlobalOrderBook,
109
+ GlobalOrderBookEntry,
110
+ Kline,
111
+ Market,
112
+ OrderBook,
113
+ OrderBookEntry,
114
+ OrderRule,
115
+ PublicTrade,
116
+ TradeFee,
117
+ )
118
+
119
+ # Trading Engine Models - Order
120
+ from trading_engine_interface.trading_engine.models.order_models import (
121
+ AmendStopRequest,
122
+ BracketOrderRequest,
123
+ CancelOrderRequest,
124
+ CreateOrderRequest,
125
+ Fill,
126
+ Order,
127
+ ReplaceOrderRequest,
128
+ )
129
+
130
+ # Trading Engine Models - Position
131
+ from trading_engine_interface.trading_engine.models.position_breakdown_models import (
132
+ AccountVenueExposureItem,
133
+ AccountVenueExposureReport,
134
+ FundedCapitalItem,
135
+ StrategyVenueBreakdown,
136
+ VenuePosition,
137
+ )
138
+ from trading_engine_interface.trading_engine.models.position_models import Position, PositionGroup
139
+
140
+
141
+
142
+
143
+ # Trading Engine Models - Strategy Metrics
144
+ from trading_engine_interface.trading_engine.models.strategy_metrics_models import (
145
+ ReturnDistribution,
146
+ StrategyMetrics,
147
+ StrategyPositionMetrics,
148
+ StrategyRiskMetrics,
149
+ StrategyTrade,
150
+ )
151
+
152
+ # Trading Engine Models - Strategy
153
+ from trading_engine_interface.trading_engine.models.strategy_models import (
154
+ AccountPermissionConfig,
155
+ BaseStrategyConfig,
156
+ InstanceReconcileReport,
157
+ PositionParams,
158
+ PredictionConfig,
159
+ PublicTradeConfig,
160
+ RiskLimitConfig,
161
+ RuntimeConfig,
162
+ SkipReason,
163
+ SkipSummary,
164
+ Strategy,
165
+ StrategyConfigUpdate,
166
+ StrategyErrorEntry,
167
+ StrategyExecution,
168
+ StrategyInput,
169
+ StrategyInstanceChange,
170
+ StrategyMarketData,
171
+ StrategyOutput,
172
+ StrategyPredictionMeta,
173
+ StrategyPredictionRecord,
174
+ StrategyPredictions,
175
+ StrategyRegistryEntry,
176
+ StrategyRegistryReloadChange,
177
+ StrategyRegistryReloadReport,
178
+ StrategyRegistryReloadRequest,
179
+ StrategyRegistryStatus,
180
+ StrategyState,
181
+ TechnicalIndicatorConfig,
182
+ WheelInstallInfo,
183
+ )
184
+
185
+
186
+
187
+
188
+ # Trading Engine Order Rules
189
+ from trading_engine_interface.trading_engine.order_rules import ORDER_RULES, get_order_rule
190
+
191
+ __all__ = [
192
+ # Common Enums
193
+ "Asset",
194
+ "ServiceStatus",
195
+ "Symbol",
196
+ "TimeInterval",
197
+ "VenueName",
198
+ # Common Errors
199
+ "ApiConnectTimeout",
200
+ "ApiConnectionError",
201
+ "ApiError",
202
+ "ApiRateLimitError",
203
+ "ApiReadTimeout",
204
+ "ApiRequestError",
205
+ "ApiResponseError",
206
+ "ApiServerError",
207
+ "ApiTemporaryError",
208
+ "ApiUnavailableError",
209
+ "BaseError",
210
+ "DomainError",
211
+ "ErrorType",
212
+ "ServiceError",
213
+ "map_api_error",
214
+ # Common Models
215
+ "BaseApiModel",
216
+ # Common Utils
217
+ "gen_id",
218
+ "now",
219
+ # Trading Engine Enums
220
+ "AccountStatus",
221
+ "AccountType",
222
+ "AppMode",
223
+ "AppStatus",
224
+ "EntityType",
225
+ "ExecutionMode",
226
+ "LifecycleEventType",
227
+ "LiquidityType",
228
+ "RfqQuoteSource",
229
+ "MarketStatus",
230
+ "OrderStatus",
231
+ "OrderType",
232
+ "RestartType",
233
+ "Side",
234
+ "StrategyErrorReason",
235
+ "StrategyStatus",
236
+ "StrategyStatusSource",
237
+ "TransactionType",
238
+ "TransferFlowClass",
239
+ "TransferFlowClassSource",
240
+ "TransferStatus",
241
+ "TransferType",
242
+ "VenueType",
243
+ # Trading Engine Models - Accounting
244
+ "Account",
245
+ "Balance",
246
+ "BalanceDetail",
247
+ "BalanceSnapshot",
248
+ "RebalancePlanLeg",
249
+ "Transaction",
250
+ "Transfer",
251
+ "UpdateTransferRequest",
252
+ "Portfolio",
253
+ "PortfolioSnapshot",
254
+ "KpiPoint",
255
+ "KpiSeries",
256
+ # Trading Engine Models - Market
257
+ "FeeAssetType",
258
+ "Kline",
259
+ "Market",
260
+ "GlobalOrderBook",
261
+ "GlobalOrderBookEntry",
262
+ "OrderBook",
263
+ "OrderBookEntry",
264
+ "OrderRule",
265
+ "PublicTrade",
266
+ "TradeFee",
267
+ # Trading Engine Models - Order
268
+ "ReplaceOrderRequest",
269
+ "CancelOrderRequest",
270
+ "AmendStopRequest",
271
+ "BracketOrderRequest",
272
+ "CreateOrderRequest",
273
+ "Fill",
274
+ "Order",
275
+ # Trading Engine Models - Position
276
+ "AccountVenueExposureItem",
277
+ "AccountVenueExposureReport",
278
+ "FundedCapitalItem",
279
+ "Position",
280
+ "PositionGroup",
281
+ "StrategyVenueBreakdown",
282
+ "VenuePosition",
283
+ # Trading Engine Models - Strategy
284
+ "AccountPermissionConfig",
285
+ "BaseStrategyConfig",
286
+ "PositionParams",
287
+ "PredictionConfig",
288
+ "PublicTradeConfig",
289
+ "RiskLimitConfig",
290
+ "RuntimeConfig",
291
+ "SkipReason",
292
+ "SkipSummary",
293
+ "Strategy",
294
+ "StrategyConfigUpdate",
295
+ "StrategyErrorEntry",
296
+ "StrategyExecution",
297
+ "StrategyInput",
298
+ "StrategyMarketData",
299
+ "StrategyOutput",
300
+ "StrategyPredictionMeta",
301
+ "StrategyPredictionRecord",
302
+ "StrategyPredictions",
303
+ "StrategyRegistryEntry",
304
+ "InstanceReconcileReport",
305
+ "StrategyInstanceChange",
306
+ "StrategyRegistryReloadChange",
307
+ "StrategyRegistryReloadReport",
308
+ "StrategyRegistryReloadRequest",
309
+ "StrategyRegistryStatus",
310
+ "StrategyState",
311
+ "TechnicalIndicatorConfig",
312
+ "WheelInstallInfo",
313
+ # Trading Engine Order Rules
314
+ "ORDER_RULES",
315
+ "get_order_rule",
316
+ # Strategy Runtime Contract (Protocol via trading_engine_interface.runtime)
317
+ "STRATEGY_REGISTRY",
318
+ "StrategyRegistry",
319
+ "RegistryEntry",
320
+ "register_strategy",
321
+ "register_production_configs",
322
+ # Trading Engine Models - Strategy Metrics
323
+ "ReturnDistribution",
324
+ "StrategyMetrics",
325
+ "StrategyPositionMetrics",
326
+ "StrategyRiskMetrics",
327
+ "StrategyTrade",
328
+ # Trading Engine Models - Backtest
329
+ "BacktestRequest",
330
+ "BacktestResult",
331
+ "BacktestResultDetail",
332
+ "BacktestStatus",
333
+ "KlineSource",
334
+ ]
File without changes
@@ -0,0 +1,163 @@
1
+ from datetime import timedelta
2
+ from enum import Enum, IntEnum
3
+
4
+
5
+ class StrValueEnum(str, Enum):
6
+ """Base for string enums whose f-string/format output is the VALUE on every Python version.
7
+
8
+ Python 3.11 changed `Enum.__format__` to defer to `Enum.__str__`, so a plain
9
+ `class Foo(str, Enum)` renders `f"{Foo.X}"` as "Foo.X" on 3.11+ but as its value on <=3.10.
10
+ That silently rewrites every derived id built by interpolation (e.g. `Account.venue_account_id`
11
+ -> "VenueName.BINANCE_user_AccountType.SPOT"). Pinning `__format__` to the value keeps behaviour
12
+ identical across the 3.9 -> 3.12 move.
13
+
14
+ Only `__format__` is overridden: `str(Foo.X)` stays "Foo.X" exactly as it is today, so log lines
15
+ and `%s` interpolation are untouched. Subclassing is legal because this base defines no members.
16
+ """
17
+
18
+ def __format__(self, format_spec: str) -> str:
19
+ return format(str(self.value), format_spec)
20
+
21
+
22
+ class ServiceStatus(IntEnum):
23
+ # Normal Life Cycle
24
+ INIT = 0
25
+ RUNNING = 1
26
+ STOPPING = 2
27
+ SHUTDOWN = 3
28
+ # Abnormal
29
+ DEGRADED = -1
30
+ ERROR = -2
31
+ # Add more statuses as needed
32
+
33
+
34
+ # Built once at import; consumed by TimeInterval.timedelta (hot path — see the note there).
35
+ _INTERVAL_TO_TIMEDELTA = {
36
+ "now": timedelta(seconds=0),
37
+ "1s": timedelta(seconds=1),
38
+ "1m": timedelta(minutes=1),
39
+ "5m": timedelta(minutes=5),
40
+ "15m": timedelta(minutes=15),
41
+ "30m": timedelta(minutes=30),
42
+ "1h": timedelta(hours=1),
43
+ "2h": timedelta(hours=2),
44
+ "4h": timedelta(hours=4),
45
+ "6h": timedelta(hours=6),
46
+ "8h": timedelta(hours=8),
47
+ "12h": timedelta(hours=12),
48
+ "1d": timedelta(days=1),
49
+ "1w": timedelta(weeks=1),
50
+ "1M": timedelta(days=30),
51
+ "1y": timedelta(days=365),
52
+ }
53
+
54
+
55
+ class TimeInterval(StrValueEnum):
56
+ NOW = "now" # one-time immediately
57
+ SEC_1 = "1s"
58
+ MIN_1 = "1m"
59
+ MIN_5 = "5m"
60
+ MIN_15 = "15m"
61
+ MIN_30 = "30m"
62
+ HOUR_1 = "1h"
63
+ HOUR_2 = "2h"
64
+ HOUR_4 = "4h"
65
+ HOUR_6 = "6h"
66
+ HOUR_8 = "8h"
67
+ HOUR_12 = "12h"
68
+ DAY_1 = "1d"
69
+ WEEK_1 = "1w"
70
+ MONTH_1 = "1M"
71
+ YEAR_1 = "1y"
72
+
73
+ @property
74
+ def timedelta(self) -> timedelta:
75
+ # Lookup table is module-level (built once at import) — NOT rebuilt per call.
76
+ # This property is hot: a kline-replay backtest calls it ~150×/tick (once per
77
+ # kline re-validated), so the old per-call dict-of-16-timedeltas dominated the
78
+ # profile (~37% of a 1-month run). Same values, just memoized.
79
+ return _INTERVAL_TO_TIMEDELTA[self.value]
80
+
81
+
82
+ class VenueName(StrValueEnum):
83
+ BINANCE = "BINANCE"
84
+ BINANCE_TEST = "BINANCE_TEST"
85
+ OKX = "OKX"
86
+ OKX_TEST = "OKX_TEST"
87
+ HYPERLIQUID = "HYPERLIQUID"
88
+ HYPERLIQUID_TEST = "HYPERLIQUID_TEST"
89
+ MOCK_EXCHANGE = "MOCK_EXCHANGE"
90
+ MOCK_OTC = "MOCK_OTC"
91
+ MOCK_BANK = "MOCK_BANK"
92
+ BACKTEST = "BACKTEST"
93
+
94
+
95
+ # TODO: this is not scalable for Asset: consider Asset/Symbol data model or just using str.
96
+ # Decision (2026-06): keep the enum for now — pairs are added rarely and via release.
97
+ # Revisit the str-subclass migration if the add-a-pair cadence becomes config-driven.
98
+ class Asset(StrValueEnum):
99
+ # Original
100
+ BTC = "BTC"
101
+ ETH = "ETH"
102
+
103
+ # Fiat & Stablecoins
104
+ USD = "USD"
105
+ USDT = "USDT"
106
+ USDC = "USDC"
107
+ BUSD = "BUSD"
108
+
109
+ # Top alts (By 2026-06)
110
+ BNB = "BNB"
111
+ XRP = "XRP"
112
+ SOL = "SOL"
113
+ TRX = "TRX"
114
+ HYPE = "HYPE"
115
+ DOGE = "DOGE"
116
+ ZEC = "ZEC"
117
+ XLM = "XLM"
118
+ XMR = "XMR"
119
+ ADA = "ADA"
120
+ LINK = "LINK"
121
+ AVAX = "AVAX"
122
+ LTC = "LTC"
123
+
124
+
125
+ # TODO: this is not scalable; consider using data model to construct base and quote assets if we need to add more symbols
126
+ class Symbol(StrValueEnum):
127
+ BTC_USD = "BTC-USD"
128
+ BTC_USDT = "BTC-USDT"
129
+ BTC_USDC = "BTC-USDC"
130
+
131
+ ETH_USD = "ETH-USD"
132
+ ETH_USDT = "ETH-USDT"
133
+ ETH_USDC = "ETH-USDC"
134
+
135
+ USDT_USD = "USDT-USD"
136
+ USDC_USD = "USDC-USD"
137
+ USDT_USDC = "USDT-USDC"
138
+ USDC_USDT = "USDC-USDT"
139
+
140
+ BNB_USDT = "BNB-USDT"
141
+ HYPE_USDC = "HYPE-USDC"
142
+ HYPE_USDT = "HYPE-USDT"
143
+
144
+ # Top alts USDT (By 2026-06)
145
+ XRP_USDT = "XRP-USDT"
146
+ SOL_USDT = "SOL-USDT"
147
+ TRX_USDT = "TRX-USDT"
148
+ DOGE_USDT = "DOGE-USDT"
149
+ ZEC_USDT = "ZEC-USDT"
150
+ XLM_USDT = "XLM-USDT"
151
+ XMR_USDT = "XMR-USDT"
152
+ ADA_USDT = "ADA-USDT"
153
+ LINK_USDT = "LINK-USDT"
154
+ AVAX_USDT = "AVAX-USDT"
155
+ LTC_USDT = "LTC-USDT"
156
+
157
+ @property
158
+ def base_asset(self) -> Asset:
159
+ return Asset(self.value.split("-")[0])
160
+
161
+ @property
162
+ def quote_asset(self) -> Asset:
163
+ return Asset(self.value.split("-")[1])