pykalshi 0.1.0__py3-none-any.whl → 0.2.1__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.
- pykalshi/__init__.py +144 -0
- pykalshi/api_keys.py +59 -0
- pykalshi/client.py +526 -0
- pykalshi/enums.py +54 -0
- pykalshi/events.py +87 -0
- pykalshi/exceptions.py +115 -0
- pykalshi/exchange.py +37 -0
- pykalshi/feed.py +592 -0
- pykalshi/markets.py +234 -0
- pykalshi/models.py +552 -0
- pykalshi/orderbook.py +146 -0
- pykalshi/orders.py +144 -0
- pykalshi/portfolio.py +542 -0
- pykalshi/py.typed +0 -0
- pykalshi/rate_limiter.py +171 -0
- {pykalshi-0.1.0.dist-info → pykalshi-0.2.1.dist-info}/METADATA +12 -12
- pykalshi-0.2.1.dist-info/RECORD +35 -0
- pykalshi-0.2.1.dist-info/top_level.txt +1 -0
- pykalshi-0.1.0.dist-info/RECORD +0 -20
- pykalshi-0.1.0.dist-info/top_level.txt +0 -1
- {pykalshi-0.1.0.dist-info → pykalshi-0.2.1.dist-info}/WHEEL +0 -0
- {pykalshi-0.1.0.dist-info → pykalshi-0.2.1.dist-info}/licenses/LICENSE +0 -0
pykalshi/__init__.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Kalshi API Client Library
|
|
3
|
+
|
|
4
|
+
A clean, modular interface for the Kalshi trading API.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
|
|
9
|
+
from .client import KalshiClient
|
|
10
|
+
from .events import Event
|
|
11
|
+
from .markets import Market, Series
|
|
12
|
+
from .orders import Order
|
|
13
|
+
from .portfolio import Portfolio
|
|
14
|
+
from .exchange import Exchange
|
|
15
|
+
from .api_keys import APIKeys
|
|
16
|
+
from .feed import (
|
|
17
|
+
Feed,
|
|
18
|
+
TickerMessage,
|
|
19
|
+
OrderbookSnapshotMessage,
|
|
20
|
+
OrderbookDeltaMessage,
|
|
21
|
+
OrderbookMessage,
|
|
22
|
+
TradeMessage,
|
|
23
|
+
FillMessage,
|
|
24
|
+
PositionMessage,
|
|
25
|
+
MarketLifecycleMessage,
|
|
26
|
+
OrderGroupUpdateMessage,
|
|
27
|
+
)
|
|
28
|
+
from .enums import (
|
|
29
|
+
Side,
|
|
30
|
+
Action,
|
|
31
|
+
OrderType,
|
|
32
|
+
OrderStatus,
|
|
33
|
+
MarketStatus,
|
|
34
|
+
CandlestickPeriod,
|
|
35
|
+
TimeInForce,
|
|
36
|
+
SelfTradePrevention,
|
|
37
|
+
)
|
|
38
|
+
from .models import (
|
|
39
|
+
PositionModel,
|
|
40
|
+
FillModel,
|
|
41
|
+
OrderModel,
|
|
42
|
+
BalanceModel,
|
|
43
|
+
MarketModel,
|
|
44
|
+
EventModel,
|
|
45
|
+
OrderbookResponse,
|
|
46
|
+
CandlestickResponse,
|
|
47
|
+
ExchangeStatus,
|
|
48
|
+
Announcement,
|
|
49
|
+
APILimits,
|
|
50
|
+
APIKey,
|
|
51
|
+
GeneratedAPIKey,
|
|
52
|
+
SeriesModel,
|
|
53
|
+
TradeModel,
|
|
54
|
+
SettlementModel,
|
|
55
|
+
QueuePositionModel,
|
|
56
|
+
OrderGroupModel,
|
|
57
|
+
SubaccountModel,
|
|
58
|
+
SubaccountBalanceModel,
|
|
59
|
+
SubaccountTransferModel,
|
|
60
|
+
ForecastPercentileHistory,
|
|
61
|
+
)
|
|
62
|
+
from .orderbook import OrderbookManager
|
|
63
|
+
from .rate_limiter import RateLimiter, NoOpRateLimiter
|
|
64
|
+
from .exceptions import (
|
|
65
|
+
KalshiError,
|
|
66
|
+
KalshiAPIError,
|
|
67
|
+
AuthenticationError,
|
|
68
|
+
InsufficientFundsError,
|
|
69
|
+
ResourceNotFoundError,
|
|
70
|
+
RateLimitError,
|
|
71
|
+
OrderRejectedError,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Set up logging to NullHandler by default to avoid "No handler found" warnings.
|
|
75
|
+
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
|
76
|
+
|
|
77
|
+
__all__ = [
|
|
78
|
+
# Client
|
|
79
|
+
"KalshiClient",
|
|
80
|
+
# Domain objects
|
|
81
|
+
"Event",
|
|
82
|
+
"Market",
|
|
83
|
+
"Series",
|
|
84
|
+
"Order",
|
|
85
|
+
"Portfolio",
|
|
86
|
+
"Exchange",
|
|
87
|
+
"APIKeys",
|
|
88
|
+
# Feed (WebSocket)
|
|
89
|
+
"Feed",
|
|
90
|
+
"TickerMessage",
|
|
91
|
+
"OrderbookSnapshotMessage",
|
|
92
|
+
"OrderbookDeltaMessage",
|
|
93
|
+
"OrderbookMessage",
|
|
94
|
+
"TradeMessage",
|
|
95
|
+
"FillMessage",
|
|
96
|
+
"PositionMessage",
|
|
97
|
+
"MarketLifecycleMessage",
|
|
98
|
+
"OrderGroupUpdateMessage",
|
|
99
|
+
# Enums
|
|
100
|
+
"Side",
|
|
101
|
+
"Action",
|
|
102
|
+
"OrderType",
|
|
103
|
+
"OrderStatus",
|
|
104
|
+
"MarketStatus",
|
|
105
|
+
"CandlestickPeriod",
|
|
106
|
+
"TimeInForce",
|
|
107
|
+
"SelfTradePrevention",
|
|
108
|
+
# Models
|
|
109
|
+
"PositionModel",
|
|
110
|
+
"FillModel",
|
|
111
|
+
"OrderModel",
|
|
112
|
+
"BalanceModel",
|
|
113
|
+
"MarketModel",
|
|
114
|
+
"EventModel",
|
|
115
|
+
"OrderbookResponse",
|
|
116
|
+
"CandlestickResponse",
|
|
117
|
+
"ExchangeStatus",
|
|
118
|
+
"Announcement",
|
|
119
|
+
"APILimits",
|
|
120
|
+
"APIKey",
|
|
121
|
+
"GeneratedAPIKey",
|
|
122
|
+
"SeriesModel",
|
|
123
|
+
"TradeModel",
|
|
124
|
+
"SettlementModel",
|
|
125
|
+
"QueuePositionModel",
|
|
126
|
+
"OrderGroupModel",
|
|
127
|
+
"ForecastPercentileHistory",
|
|
128
|
+
# Utilities
|
|
129
|
+
"OrderbookManager",
|
|
130
|
+
"RateLimiter",
|
|
131
|
+
"NoOpRateLimiter",
|
|
132
|
+
# Subaccount Models
|
|
133
|
+
"SubaccountModel",
|
|
134
|
+
"SubaccountBalanceModel",
|
|
135
|
+
"SubaccountTransferModel",
|
|
136
|
+
# Exceptions
|
|
137
|
+
"KalshiError",
|
|
138
|
+
"KalshiAPIError",
|
|
139
|
+
"AuthenticationError",
|
|
140
|
+
"InsufficientFundsError",
|
|
141
|
+
"ResourceNotFoundError",
|
|
142
|
+
"RateLimitError",
|
|
143
|
+
"OrderRejectedError",
|
|
144
|
+
]
|
pykalshi/api_keys.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
from .models import APIKey, GeneratedAPIKey, APILimits
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from .client import KalshiClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class APIKeys:
|
|
10
|
+
"""API key management and account limits."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, client: KalshiClient) -> None:
|
|
13
|
+
self._client = client
|
|
14
|
+
|
|
15
|
+
def list(self) -> list[APIKey]:
|
|
16
|
+
"""List all API keys for this account."""
|
|
17
|
+
data = self._client.get("/api_keys")
|
|
18
|
+
return [APIKey.model_validate(k) for k in data.get("api_keys", [])]
|
|
19
|
+
|
|
20
|
+
def create(self, public_key: str, name: str | None = None) -> APIKey:
|
|
21
|
+
"""Create an API key with a provided RSA public key.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
public_key: PEM-encoded RSA public key.
|
|
25
|
+
name: Optional name for the key.
|
|
26
|
+
"""
|
|
27
|
+
body: dict = {"public_key": public_key}
|
|
28
|
+
if name:
|
|
29
|
+
body["name"] = name
|
|
30
|
+
data = self._client.post("/api_keys", body)
|
|
31
|
+
return APIKey.model_validate(data["api_key"])
|
|
32
|
+
|
|
33
|
+
def generate(self, name: str | None = None) -> GeneratedAPIKey:
|
|
34
|
+
"""Generate a new API key pair (Kalshi creates both keys).
|
|
35
|
+
|
|
36
|
+
Returns a GeneratedAPIKey with the private_key field populated.
|
|
37
|
+
The private key is only returned ONCE - store it securely.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
name: Optional name for the key.
|
|
41
|
+
"""
|
|
42
|
+
body: dict = {}
|
|
43
|
+
if name:
|
|
44
|
+
body["name"] = name
|
|
45
|
+
data = self._client.post("/api_keys/generate", body)
|
|
46
|
+
return GeneratedAPIKey.model_validate(data)
|
|
47
|
+
|
|
48
|
+
def delete(self, key_id: str) -> None:
|
|
49
|
+
"""Delete an API key.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
key_id: The API key ID to delete.
|
|
53
|
+
"""
|
|
54
|
+
self._client.delete(f"/api_keys/{key_id}")
|
|
55
|
+
|
|
56
|
+
def get_limits(self) -> APILimits:
|
|
57
|
+
"""Get API rate limits for this account."""
|
|
58
|
+
data = self._client.get("/account/limits")
|
|
59
|
+
return APILimits.model_validate(data)
|