forked from Kalshi/kalshi-starter-code-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (37 loc) · 1.24 KB
/
main.py
File metadata and controls
44 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
from dotenv import load_dotenv
from cryptography.hazmat.primitives import serialization
import asyncio
from clients import KalshiHttpClient, KalshiWebSocketClient, Environment
# Load environment variables
load_dotenv()
env = Environment.DEMO # toggle environment here
KEYID = os.getenv('DEMO_KEYID') if env == Environment.DEMO else os.getenv('PROD_KEYID')
KEYFILE = os.getenv('DEMO_KEYFILE') if env == Environment.DEMO else os.getenv('PROD_KEYFILE')
try:
with open(KEYFILE, "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None # Provide the password if your key is encrypted
)
except FileNotFoundError:
raise FileNotFoundError(f"Private key file not found at {KEYFILE}")
except Exception as e:
raise Exception(f"Error loading private key: {str(e)}")
# Initialize the HTTP client
client = KalshiHttpClient(
key_id=KEYID,
private_key=private_key,
environment=env
)
# Get account balance
balance = client.get_balance()
print("Balance:", balance)
# Initialize the WebSocket client
ws_client = KalshiWebSocketClient(
key_id=KEYID,
private_key=private_key,
environment=env
)
# Connect via WebSocket
asyncio.run(ws_client.connect())