KafkaTrader is a Python-based framework designed for algorithmic trading and data monitoring in Kalshi markets. It connects to Kalshi's API, processes market data in real-time, and enables users to implement and test custom trading strategies.
This project provides tools to subscribe to market events, manage orders and positions, and experiment with strategies while leveraging Kafka for data streaming.
- Real-time Market Data Processing: Subscribe to Kalshi events and stream them into Kafka for analysis.
- Customizable Strategies: Develop and test strategies with plug-and-play support.
- Order Management: Track positions and active orders across strategies.
- Demo & Production Modes: Seamlessly switch between environments for testing and live execution.
- Python 3.10+
- Kalshi API key (prod and/or demo)
- Running Kafka server (Setup Kafka)
- Assumes default port of 9092
- Clone the repository:
git clone <repository_url>
cd KafkaTrader- install the required packages
pip install -r requirements.txtEnsure the following environment variables are set:
| Variable | Purpose |
|---|---|
KALSHI_PROD_EMAIL |
Production API email |
KALSHI_PROD_PASSWORD |
Production API password |
KALSHI_DEMO_EMAIL |
Demo API email |
KALSHI_DEMO_PASSWORD |
Demo API password |
Switch between production and demo environments by updating the kalshi_api variable in KalshiAPI.py to either prod_kalshi_api or demo_kalshi_api. |
-
MarketDataService Streams Kalshi market data events into Kafka.
-
MarketMonitor Reads data from Kafka and forwards it to handlers. Example handler:
print_orderbook, which displays the current order book. -
User Strategies Create custom strategies to act on market data events. Example:
TestStrategysends a market order based on predefined conditions. -
OrderManagementSystem Manages open orders and tracks positions across strategies.
Run the main script and specify components using the --components flag:
python run.py --components monitor strategy --ticker POPVOTEMOVSMALLER-24monitor: Launches theMarketMonitorwith the print_orderbook` handler.strategy: Activates theTestStrategy.--ticker: Specifies the market to subscribe to. Omit this flag to use the predefinedmarket_listinSeries.py.
To monitor a market and execute a test strategy:
python run.py --components monitor strategy --ticker POPVOTEMOVSMALLER-24Strategies are modular and easy to create. Implement a new strategy by subclassing and overriding methods in the Strategy base class. For example, modify the TestStrategy to define your conditions and trading logic.
Contributions are welcome! Feel free to fork the repository and submit a pull request.
For feedback or issues, please open a ticket in the Issues section of the repository.