← Rajandran R's posts

OpenAlgo CLI: Trade, Stream and Automate Your Broker from the Terminal

View Original ↗

A hands-on tutorial for the new open source command line for OpenAlgo, built for scripts and AI agents

OpenAlgo already gives traders one API across 30+ brokers: REST, WebSocket, a Python SDK, MCP for AI assistants, and integrations with TradingView, Amibroker, ChartInk and Excel. Today we are adding one more way in: the OpenAlgo CLI, an open source command line tool that puts the whole OpenAlgo API in your terminal.

With one command you can check funds, pull a quote, place a sandbox order, build an option chain or stream live ticks. Every command returns clean JSON, so the same commands work in shell scripts, cron jobs, CI pipelines and, above all, AI agents.

In this tutorial we install the CLI, connect it to an OpenAlgo server, and walk through real examples. Every screenshot below comes from a live OpenAlgo instance connected to Zerodha and running in sandbox (analyzer) mode.

Source code: github.com/marketcalls/openalgo-cli (Apache 2.0)

Why a CLI?

The CLI is designed agent-first. It is not an interactive trading terminal. There are no menus or confirmation prompts. Instead:

  • Every parameter is an explicit flag such as --symbol SBIN --exchange NSE. There is nothing positional to guess.
  • Every API command prints JSON on stdout.
  • Errors are JSON on stderr, with exit codes: 0 success, 1 API error, 2 authentication error.
  • --help-all prints the full command tree, and --schema shows the response shape without calling the API, so an agent can learn the tool at runtime.
  • --dry-run on every order-changing command prints the exact request body without sending it.

That makes the CLI a natural “hands” layer for AI agents. It also makes it just as useful for traders who like to automate with a few lines of shell.

Major use cases

  1. AI agent harnesses. Give Claude Code, Codex or your own agent a shell tool and this CLI. The agent can then read positions and quotes, reason, and place or manage orders. A bundled agent skill teaches it the tool.
  2. Human-in-the-loop execution. The agent proposes an order with --dry-run, you approve the exact body, and the same body is sent.
  3. Scheduled automation. Cron jobs that square off, snapshot P&L or alert on rejected orders, each in a few lines of shell.
  4. Multiple brokers and instances. One profile per OpenAlgo instance, so one script can work across all your accounts.
  5. Research and data pipelines. Historical candles, option chains and instrument masters go straight into pandas, DuckDB or Excel with --csv.
  6. Live monitoring. Stream LTP, quotes, depth or order updates as newline-delimited JSON.
  7. Strategy operations and debugging. Start, stop and audit strategies. Use doctor, --debug and --trace to see exactly what is sent, with the API key always redacted.
  8. A universal adapter. Anything that can run a process (Excel VBA, Node, Rust, n8n) can drive OpenAlgo without an SDK.

Prerequisites

  • A running OpenAlgo server (for example http://127.0.0.1:5000) that is logged in to your broker.
  • An OpenAlgo API key, generated on the API Key page of the OpenAlgo dashboard.
  • Go is not required. The installers download a prebuilt single binary for macOS, Linux and Windows. You need Go 1.24+ only if you prefer go install or want to build from source.

Step 1: Install

macOS and Linux:

curl -fsSL https://raw.githubusercontent.com/marketcalls/openalgo-cli/main/install.sh | sh

Windows (PowerShell):

irm https://raw.githubusercontent.com/marketcalls/openalgo-cli/main/install.ps1 | iex

With Go (1.24+):

go install github.com/marketcalls/openalgo-cli/cmd/openalgo@latest

The installers detect your OS and CPU, download the latest release, verify its SHA-256 checksum and put openalgo on your path. Check the install with:

openalgo version

Step 2: Connect to your OpenAlgo server

Run openalgo profile login. It asks for the host and your API key (the key is typed without echo), validates the key against the server, and saves a profile with restricted file permissions. Then run openalgo doctor to check the whole setup:

doctor confirms that the server is reachable, which broker the key resolves to, and whether the server is in analyzer (sandbox) mode. In sandbox mode every order is simulated against live market data instead of going to your broker. That is the right place to start.

For scripts, CI and agents you can skip the profile and use environment variables, so the key never touches disk:

export OPENALGO_API_KEY=your_api_key
export OPENALGO_HOST=http://127.0.0.1:5000
openalgo account funds

Step 3: Explore the commands

Commands are grouped the way you think about trading: order, gtt, position, option, strategy, account, data, symbol, market, analyzer, stream and more. All 65 OpenAlgo REST operations are covered. Three ways to discover them:

openalgo --help-all            # every command and flag
openalgo order place --help # one command, with examples
openalgo order list --schema # response fields, no API call

Flags that take a fixed set of values (exchange, product, price type, action) tab-complete in bash, zsh, fish and PowerShell. Run openalgo completion zsh to set it up.

Step 4: Funds and market data

Every command prints the full OpenAlgo JSON response. The built-in --jq flag (no separate jq install needed) trims it to exactly what you need. This matters for AI agents, because fewer tokens means cheaper and faster reasoning:

openalgo account funds
openalgo data quote --symbol SBIN --exchange NSE --jq '.data.ltp'
openalgo data depth --symbol SBIN --exchange NSE
openalgo data history --symbol SBIN --exchange NSE --interval D \
--start-date 2026-01-01 --end-date 2026-09-25 --csv > sbin.csv

Step 5: Place, check and cancel an order (safely)

First, preview the order. --dry-run builds the exact request body and prints it without sending anything. The API key is never printed.

openalgo order place --symbol SBIN --exchange NSE --action BUY --quantity 1 \
--product CNC --pricetype LIMIT --price 950 --dry-run
openalgo order place --symbol SBIN --exchange NSE --action BUY --quantity 1 \
--product CNC --pricetype LIMIT --price 950
openalgo order status --orderid 26092619979053
openalgo order cancel --orderid 26092619979053

Note "mode": "analyze" in the response: the order went to the sandbox, not to the exchange. A few details worth knowing:

  • Symbols use the OpenAlgo format across every broker: SBIN, BANKNIFTY24APR24FUT, NIFTY28MAR2420800CE.
  • Use CNC for equity delivery and NRML for F&O. In sandbox mode, intraday MIS orders are refused after the square-off time.
  • --strategy defaults to openalgo-cli, so CLI orders are easy to spot in your order book.
  • Rate-limited requests (HTTP 429) are retried automatically. Order-changing requests are never replayed after a server error, because a retry could place a duplicate order.

Other order types follow the same pattern: order smart, order basket, order split, order modify, gtt place, and the options order commands below.

Step 6: Options and CSV output

openalgo option symbol --underlying NIFTY --exchange NSE_INDEX \
--expiry-date 29SEP26 --offset ATM --option-type CE
openalgo option chain --underlying NIFTY --exchange NSE_INDEX \
--expiry-date 29SEP26 --strike-count 10 --csv > chain.csv
openalgo option greeks --symbol NIFTY29SEP2623150CE --exchange NFO

--csv turns any list (order book, positions, option chain, instruments) into a spreadsheet-ready table. Options orders resolve strikes by offset, so an ATM call buy is a single command:

openalgo option order --underlying NIFTY --exchange NSE_INDEX --expiry-date 29SEP26 \
--offset ATM --option-type CE --action BUY --quantity 65 --product NRML

Step 7: Stream live market data

openalgo stream ltp --symbols NSE:RELIANCE,NSE:SBIN
openalgo stream quote --symbols NSE:SBIN --count 10
openalgo stream depth --symbols NSE:SBIN --depth 5 --duration 30s
openalgo stream orders

Streams print newline-delimited JSON until Ctrl-C, --count messages, or --duration. Pipe them into anything: an alerting script, a dashboard, or an agent watching order updates.

Step 8: Built for automation: schemas and structured errors

A script or agent never has to parse human text. It can branch on the exit code and read the hint to correct itself. Here is a complete end-of-day square-off job you could run from cron:

#!/bin/sh
# 15:10 IST: square off every open position and log the result
openalgo analyzer status --jq '.data.mode' >> ~/squareoff.log
openalgo position close-all --quiet >> ~/squareoff.log
openalgo account funds --jq '.data.m2mrealized' >> ~/squareoff.log

Destructive commands are truly destructive. position close-all squares off every open position, and order cancel-all cancels every open order. There are no confirmation prompts, by design, so test in sandbox mode first.

Multiple OpenAlgo instances

OpenAlgo is a single-user app, but many traders run several instances side by side, one per broker. Instance n listens on port 5000+n-1, with its WebSocket on 8765+n-1. Create one profile per instance and switch with -p:

openalgo profile login --name zerodha --host http://127.0.0.1:5000
openalgo profile login --name dhan --host http://127.0.0.1:5001
openalgo -p zerodha position list
openalgo -p dhan account funds
openalgo -p dhan stream ltp --symbols NSE:SBIN # ws://127.0.0.1:8766

Each profile keeps its own host and API key. A key is only ever sent to the server it belongs to.

Using the CLI with AI agents

The repository ships an agent skill at .agents/skills/openalgo-cli/SKILL.md. Load it into Claude Code, Codex or any agent that supports skills, give the agent a shell, and you can ask things like:

Check if OpenAlgo is in sandbox mode. If it is, buy 1 share of SBIN as a CNC limit order 2% below the last price, show me the dry run first, then place it and report the order status.

The agent discovers the right commands from --help, checks analyzer status, previews with --dry-run, places the order and reads the JSON response. Every step is visible and auditable in the shell history.

CLI or MCP? OpenAlgo also has an MCP server. They complement each other. The CLI is one command per call with a minimal context cost, which is ideal for harnesses, scripts and automation. MCP is a persistent tool server that suits long conversational sessions in Claude Desktop or Cursor.

Other handy commands

openalgo symbol search --query RELIANCE --exchange NSE
openalgo symbol expiry --symbol NIFTY --exchange NFO --instrumenttype options
openalgo market timings --date 2026-09-29
openalgo strategy list
openalgo api POST /funds # raw access to any endpoint
openalgo update # self-update

Get started

curl -fsSL https://raw.githubusercontent.com/marketcalls/openalgo-cli/main/install.sh | sh
openalgo profile login
openalgo doctor

The CLI is in alpha, and commands and flags may still change between releases. Issues and pull requests are welcome.

Disclaimer: the OpenAlgo CLI is a tool for talking to your own OpenAlgo server. Nothing in this article is investment advice. Trading involves risk; always test in sandbox mode first.