Expand description
An HTTPS and Websocket wrapper that allows users to write trading bots for the Kalshi events trading platform.
kalshi-rust is asynchronous, performant, and succint. Dash past verbose and annoying HTTPS requests and use this wrapper to quickly write blazingly fast trading bots in Rust!
As of version 0.9.0, HTTPS features are fully complete but websocket support and advanced API access features are not complete. If you’d like to keep up on kalshi-rust’s development, report bugs, or view a sample trading script, feel free to visit the github! A star would also be greatly appreciated, I’m a student developer writing this for free and any recognition is incredibly helpful!
The Kalshi Struct
The Kalshi struct is the central component of this crate. All authentication, order routing, market requests, and position snapshots are handled through the struct and it’s methods.
For more details, see Kalshi.
For a quick tutorial / beginners guide, jump here.
Initializing the Kalshi struct in demo mode.
use kalshi::Kalshi;
use kalshi::TradingEnvironment;
let kalshi_instance = Kalshi::new(TradingEnvironment::DemoMode);
Quick Start Guide
First, list the Kalshi struct as a dependency in your crate.
kalshi = { version = "0.9"}
Initialize the Kalshi Struct and login using your authentication details:
- IMPORTANT: A user’s authentication token expires every thirty minutes, this means that you’ll need to call the login function every thirty minutes in order to ensure that you remain authenticated with a valid token.
- Storing user / password information in plaintext is not recommended, an implementation of extracting user details from local environmental variables is available here
use kalshi::Kalshi;
use kalshi::TradingEnvironment;
let username = "johndoe@example.com";
let password = "example_password";
let mut kalshi_instance = Kalshi::new(TradingEnvironment::DemoMode);
kalshi_instance.login(username, password).await?;
After logging in, you can call any method present in the crate without issue. Here is a script that buys a ‘yes’ contract on November 13th’s New York temperature market.
let new_york_ticker = "HIGHNY-23NOV13-T51".to_string();
let bought_order = kalshi_instance
.create_order(
kalshi::Action::Buy,
None,
1,
kalshi::Side::Yes,
new_york_ticker,
kalshi::OrderType::Limit,
None,
None,
None,
None,
Some(5)).await.unwrap();
Refer to the rest of the documentation for details on all other methods! All methods found in the kalshi API documentation are wrapped around in this crate.
Returned Values
Whenever a user makes a method call using the kalshi struct, data is typically returned in structs that encapsulate the json fields returned by the server. All data in the structs is owned so a user can access the attributes without issue.
Examples:
Obtaining the Exchange’s current status
Returns a struct that represents whether trading or the exchange are currently active.
use kalshi::Kalshi;
use kalshi::TradingEnvironment;
let kalshi_instance = Kalshi::new(TradingEnvironment::DemoMode);
kalshi_instance.get_exchange_status().await.unwrap();
Obtaining 5 miscellaneous market events
Returns a vector of ‘event’ structs and a cursor.
use kalshi::Kalshi;
use kalshi::TradingEnvironment;
let kalshi_instance = Kalshi::new(TradingEnvironment::DemoMode);
kalshi_instance.get_multiple_events(Some(5), None, None, None, None).await.unwrap();
Checking the User’s balance
Returns an i64 representing the user’s balance in cents.
use kalshi::Kalshi;
use kalshi::TradingEnvironment;
let kalshi_instance = Kalshi::new(TradingEnvironment::DemoMode);
kalshi_instance.get_balance();
Structs
- Represents the opening and closing times of the exchange for a single day.
- An event in the Kalshi exchange.
- A user’s position in a specific event on the Kalshi exchange.
- Represents the standard trading hours and maintenance windows of the exchange.
- Represents the status of the exchange, including trading and exchange activity.
- A completed transaction (a ‘fill’) in the Kalshi exchange.
- The Kalshi struct is the core of the kalshi-crate. It acts as the interface between the user and the market, abstracting away the meat of requests by encapsulating authentication information and the client itself.
- A market in the Kalshi exchange.
- A user’s position in a specific market on the Kalshi exchange.
- Represents an order in the Kalshi exchange.
- The order book of a market in the Kalshi exchange.
- Series on the Kalshi exchange.
- A settlement of a market position in the Kalshi exchange.
- A source of a settlement in the Kalshi exchange.
- Snapshot of market data in the Kalshi exchange.
- Contains the daily schedule for each day of the week.
- A trade in the Kalshi exchange.
Enums
- This enum is used to specify the type of action a user wants to take in an order, either buying or selling.
- A comprehensive set of errors that might occur in the Kalshi module.
- The different statuses a market can have on the Kalshi exchange.
- The status of an order in the Kalshi exchange.
- Defines the type of an order in the Kalshi exchange.
- Specific kinds of HTTP request errors encountered in the Kalshi module.
- Possible outcomes of a market settlement on the Kalshi exchange.
- The side of a market position in the Kalshi exchange.
- Defines the trading environment for the Kalshi exchange.