Java
OpenAlgo Java SDK
OpenAlgo Java SDK for algorithmic trading - Java client library for OpenAlgo API. Supports order placement, market data, options trading, and real-time WebSocket streaming.
Installation
Maven
Add the following dependency to your pom.xml:
<dependency>
<groupId>in.openalgo</groupId>
<artifactId>openalgo</artifactId>
<version>1.1.0</version>
</dependency>Gradle
Add the following to your build.gradle:
implementation 'in.openalgo:openalgo:1.1.0'Compatibility
Java 11
LTS (Long Term Support)
Java 17
LTS (Long Term Support)
Java 21
LTS (Long Term Support) - Recommended
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 Java SDK
First, import the OpenAlgo class and initialize it with your API key:
Examples
Please refer to the documentation on order constants, and consult the API reference for details on optional parameters
API Reference
PlaceOrder Example
To place a new market order (simplest form):
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
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:
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 order status:
OrderStatus Response
OpenPosition Example
To get the current open position:
OpenPosition Response
Quotes Example
Quotes 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.
Symbol Example
Symbol Response
Search Example
Search Response
MultiQuotes Example
MultiQuotes Response
Expiry Example
instrumenttype is futures or options, and the exchange must be a derivatives exchange: NFO, BFO, MCX, CDS, NCO, BCD, NCDEX or CRYPTO.
Expiry Response
OptionSymbol Example
OptionSymbol Response
SyntheticFuture Example
SyntheticFuture 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, and the SDK issues that GET for you.
Instruments Response
Margin Example
Up to 50 positions per request. quantity, price and trigger_price go over the wire as strings.
Margin Response
OptionsOrder Example
To place ATM options order:
OptionsOrder Response
OptionsMultiOrder Example
To place multi-leg options strategy (e.g., Bull Call Spread):
OptionsMultiOrder Response
OptionChain Example
OptionChain Response
OptionGreeks Example
OptionGreeks Response
Funds Example
Funds Response
OrderBook Example
OrderBook Response
TradeBook Example
TradeBook Response
PositionBook Example
PositionBook Response
Holdings Example
Holdings Response
Telegram Alert Example
Telegram Alert 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 Java 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 Streaming
LTP Data (Streaming WebSocket)
LTP Response
Quotes (Streaming WebSocket)
Quote Response
Depth (Streaming WebSocket)
Depth Response
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 delivers at most 20 LTP messages a second.
Order Updates (Streaming WebSocket)
The same proxy on port 8765 also carries account-scoped order updates. The Java 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.
Error Handling
Complete API Reference
Order Management
placeorder()
Place a new order
placesmartorder()
Place a smart order with position sizing
modifyorder()
Modify an existing order
cancelorder()
Cancel a specific order
cancelallorder()
Cancel all open orders
closeposition()
Close all open positions
orderstatus()
Get status of a specific order
openposition()
Get current open position quantity
Basket & Split Orders
basketorder()
Place multiple orders in a single request
splitorder()
Split large order into smaller chunks
Options Trading
optionsorder()
Place ATM/ITM/OTM option order
optionsmultiorder()
Place multi-leg option strategy
optionsymbol()
Get option symbol by offset
optionchain()
Get full option chain data
optiongreeks()
Calculate option Greeks
syntheticfuture()
Calculate synthetic future price
expiry()
Get expiry dates for symbol
Market Data
quotes()
Get real-time quotes for a symbol
multiquotes()
Get quotes for multiple symbols
depth()
Get market depth (order book)
history()
Get historical OHLCV data
intervals()
Get supported time intervals
Symbol & Search
symbol()
Get symbol details
search()
Search for symbols
instruments()
Download all instruments
Account & Portfolio
funds()
Get funds and margin details
margin()
Calculate margin requirements
orderbook()
Get order book
tradebook()
Get trade book
positionbook()
Get position book
holdings()
Get stock holdings
Utilities
holidays()
Get trading holidays for a year
timings()
Get exchange timings for a date
telegram()
Send Telegram alert message
analyzerstatus()
Get analyzer mode status
analyzertoggle()
Toggle analyze/live mode
WebSocket Streaming
connect()
Connect to WebSocket server
disconnect()
Disconnect from WebSocket
subscribeLtp()
Subscribe to LTP updates
unsubscribeLtp()
Unsubscribe from LTP
subscribeQuote()
Subscribe to Quote updates
unsubscribeQuote()
Unsubscribe from Quote
subscribeDepth()
Subscribe to Depth updates
unsubscribeDepth()
Unsubscribe from Depth
getLtp()
Get cached LTP data
getQuotes()
Get cached Quote data
getDepth()
Get cached Depth data
License
This project is licensed under the MIT License - see the LICENSE file for details.
Links
Last updated