#trading #bots #finance #api-wrapper #api-request

kalshi

An HTTPS and Websocket wrapper that allows users to write trading bots for the Kalshi events trading platform

2 unstable releases

0.9.0 Nov 14, 2023
0.1.0 Apr 18, 2023

#91 in WebSocket

36 downloads per month

MIT/Apache

83KB
1K SLoC

kalshi-rust

Rust Wrapper for the Kalshi trading API

This is a wrapper for the Kalshi trading API written by and for those using Rust. This wrapper is asynchronous and typically more performant than the official Python API provided by the developers, presented here: KalshiDevAPI. Access more details about the project on github.


lib.rs:

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();

Dependencies

~7–19MB
~290K SLoC