RUST
OpenAlgo Rust SDK
To install the OpenAlgo Rust library, use cargo:
cargo add openalgo tokio --features tokio/fullOr add to your Cargo.toml:
[dependencies]
openalgo = "1.1.0"
tokio = { version = "1", features = ["full"] }Get the OpenAlgo apikey
Make Sure that your OpenAlgo Application is running. Login to OpenAlgo Application with valid credentials and get the OpenAlgo apikey
For detailed function parameters refer to the API Documentation
Getting Started with OpenAlgo
First, import the OpenAlgo client and initialize it with your API key:
use openalgo::OpenAlgo;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Replace 'your_api_key_here' with your actual API key
// Default host is http://127.0.0.1:5000
let client = OpenAlgo::new("your_api_key_here");
// Or with custom host
let client = OpenAlgo::with_config(
"your_api_key_here",
"http://127.0.0.1:5000",
"v1",
"ws://127.0.0.1:8765"
);
Ok(())
}Check OpenAlgo Version
Examples
Please refer to the documentation on order constants, and consult the API reference for details on optional parameters
PlaceOrder example
To place a new market order:
Place Market Order Response
To place a new limit order:
Place Limit Order Response
PlaceSmartOrder Example
To place a smart order considering the current position size:
Place Smart Market Order Response
OptionsOrder Example
To place ATM options order
Place Options Order Response
To place ITM options order
Place Options Order Response
OptionsMultiOrder Example
To place Iron Condor options order (Same Expiry)
Place OptionsMultiOrder Response
BasketOrder example
To place a new basket order:
Basket Order Response
SplitOrder example
To place a new split order:
SplitOrder Response
ModifyOrder Example
To modify an existing order:
The /modifyorder endpoint requires every one of apikey, strategy, exchange, symbol, orderid, action, product, pricetype, price, quantity, disclosed_quantity and trigger_price. None omits the field from the JSON body, so pass Some("0") rather than None for disclosed_quantity and trigger_price.
Modify Order Response
CancelOrder Example
To cancel an existing order:
CancelOrder Response
CancelAllOrder Example
To cancel all open orders and trigger pending orders
CancelAllOrder Response
ClosePosition Example
To close all open positions across various exchanges
ClosePosition Response
OrderStatus Example
To Get the Current OrderStatus
OrderStatus Response
OpenPosition Example
To Get the Current OpenPosition
OpenPosition Response
Quotes Example
Quotes Response
MultiQuotes Example
MultiQuotes Response
Depth Example
Depth Response
History Example
History Response
Intervals Example
Intervals Response
intervals reports only what the connected broker supports. The interval field of /history accepts this full set, and rejects anything else: 1s, 5s, 10s, 15s, 30s, 45s, 1m, 2m, 3m, 5m, 10m, 15m, 20m, 30m, 1h, 2h, 3h, 4h, D, W, M, Q, Y.
OptionChain Example
expiry_date is mandatory on /optionchain. option_chain returns the entire chain for that expiry; to limit it to a window around the ATM strike use client.data.option_chain_strikes, which adds a strike_count (1 to 100).
OptionChain Response
Symbol Example
Symbol Response
Search Example
Search Response
OptionSymbol Example
ATM Option
OptionSymbol Response
ITM Option
OptionSymbol Response
OTM Option
OptionSymbol Response
SyntheticFuture Example
SyntheticFuture Response
OptionGreeks Example
OptionGreeks Response
Expiry Example
Expiry Response
Instruments Example
/instruments is the one v1 market-data endpoint that is a GET rather than a POST; it takes apikey, an optional exchange and an optional format (json or csv) as query parameters. Passing None for the exchange makes the SDK loop over every supported exchange and combine the results client-side.
Instruments Response
Telegram Alert Example
Telegram Alert Response
With priority:
Funds Example
Funds Response
Margin Example
Margin Response
OrderBook Example
OrderBook Response
TradeBook Example
TradeBook Response
PositionBook Example
PositionBook Response
Holdings Example
Holdings Response
Holidays Example
Holidays Response
Timings Example
Timings Response
Analyzer Status Example
Analyzer Status Response
Analyzer Toggle Example
Analyzer Toggle Response
Endpoints not wrapped by the SDK
The Rust SDK does not expose helpers for the GTT endpoints, multioptiongreeks or ping. Reach them by posting to the REST endpoint directly at http://127.0.0.1:5000/api/v1/<endpoint>, passing the same apikey field the SDK sends.
GTT (Good Till Triggered)
Four endpoints, all POST with a flat JSON body: placegttorder, modifygttorder, cancelgttorder and gttorderbook. trigger_type is SINGLE or OCO, and product accepts only CNC or NRML; MIS is rejected because a GTT can sit with the broker for days.
SINGLE, buy IDEA if it dips to 9.55:
For SINGLE send exactly one of triggerprice_sl (trigger sits below LTP) or triggerprice_tg (trigger sits above LTP) and leave the other at 0. For OCO send all four of triggerprice_sl, stoploss, triggerprice_tg and target, with triggerprice_sl strictly less than triggerprice_tg. modifygttorder takes the same body plus trigger_id, cancelgttorder takes apikey, strategy and trigger_id, and gttorderbook takes apikey alone and returns the active triggers under data.
MultiOptionGreeks
optiongreeks prices one symbol at a time. multioptiongreeks prices 1 to 50 option symbols in a single call, with interest_rate and expiry_time set once for the whole batch:
Individual items can fail while the batch still returns "status": "success", so inspect each entry in data and the summary block.
Ping
ping confirms the API key is valid and reports the connected broker:
WebSocket connection notes
The proxy listens on ws://127.0.0.1:8765. Every client authenticates with its OpenAlgo API key before subscribing, and a connection that has not authenticated within 15 seconds is closed. Subscriptions carry a mode: 1 for LTP, 2 for Quote and 3 for Depth. The strings LTP, Quote and Depth are accepted as well and are matched case-insensitively; Quote is the default when the field is omitted. LTP updates are throttled to one per symbol per 50 ms, so a fast-moving symbol still delivers at most 20 LTP messages a second.
LTP Data (Streaming WebSocket)
client.websocket() returns an OpenAlgoWebSocket. Calling connect() opens the socket to ws://127.0.0.1:8765, sends the {"action": "authenticate", "api_key": ...} handshake for you, and hands back a command sender plus a data receiver. Wrap the sender in a WsSubscriber to subscribe, and read ticks off the receiver as WsData values. There is no callback closure form.
LTP ticks are throttled by the proxy to one update per symbol per 50 ms.
Quotes (Streaming WebSocket)
Depth (Streaming WebSocket)
Cached Snapshots
OpenAlgoWebSocket also keeps a local snapshot cache, updated as ticks arrive. Pass None for both arguments to get every cached entry.
Order Updates (Streaming WebSocket)
The same proxy on port 8765 also carries account-scoped order updates. The Rust SDK does not wrap them, so send the raw frames on your own WebSocket connection: authenticate first, then subscribe.
The server acknowledges the subscription:
Every subsequent status change on any order in the account then arrives as:
{"action": "unsubscribe_orders"} stops the stream. Unlike a market-data subscription there is no symbol, exchange or mode: the subscription covers the whole account.
Last updated
Was this helpful?