OpenAlgo Python Library
A Python library for algorithmic trading using OpenAlgo's REST APIs. This library provides a comprehensive interface for order management, market data, account operations, and strategy automation.
Installation
pip install openalgo
Quick Start
from openalgo import api
client = api(
api_key="your_api_key",
host="http://127.0.0.1:5000"
)
API Categories
1. Strategy API
Strategy Management Module
OpenAlgo's Strategy Management Module allows you to automate your trading strategies using webhooks. This enables seamless integration with any platform or custom system that can send HTTP requests. The Strategy class provides a simple interface to send signals that trigger orders based on your strategy configuration in OpenAlgo.
from openalgo import Strategy
import requests
client = Strategy(
host_url="http://127.0.0.1:5000",
webhook_id="your-webhook-id"
)
try:
response = client.strategyorder("RELIANCE", "BUY", 1)
print(f"Long entry successful: {response}")
response = client.strategyorder("ZOMATO", "SELL", 1)
print(f"Short entry successful: {response}")
response = client.strategyorder("RELIANCE", "SELL", 0)
response = client.strategyorder("ZOMATO", "BUY", 0)
except requests.exceptions.RequestException as e:
print(f"Error sending order: {e}")
Strategy Modes:
- LONG_ONLY: Only processes BUY signals for long-only strategies
- SHORT_ONLY: Only processes SELL signals for short-only strategies
- BOTH: Processes both BUY and SELL signals with position sizing
The Strategy Management Module can be integrated with:
- Custom trading systems
- Technical analysis platforms
- Alert systems
- Automated trading bots
- Any system capable of making HTTP requests
2. Accounts API
Funds
Get funds and margin details of the trading account.
result = client.funds()
{
"data": {
"availablecash": "18083.01",
"collateral": "0.00",
"m2mrealized": "0.00",
"m2munrealized": "0.00",
"utiliseddebits": "0.00"
},
"status": "success"
}
Orderbook
Get orderbook details with statistics.
result = client.orderbook()
Tradebook
Get execution details of trades.
result = client.tradebook()
Positionbook
Get current positions across all segments.
result = client.positionbook()
Holdings
Get stock holdings with P&L details.
result = client.holdings()
3. Orders API
Place Order
Place a regular order.
result = client.placeorder(
symbol="RELIANCE",
exchange="NSE",
action="BUY",
quantity=1,
price_type="MARKET",
product="MIS"
)
Place Smart Order
Place an order with position sizing.
result = client.placesmartorder(
symbol="RELIANCE",
exchange="NSE",
action="BUY",
quantity=1,
position_size=100,
price_type="MARKET",
product="MIS"
)
Basket Order
Place multiple orders simultaneously.
orders = [
{
"symbol": "RELIANCE",
"exchange": "NSE",
"action": "BUY",
"quantity": 1,
"pricetype": "MARKET",
"product": "MIS"
},
{
"symbol": "INFY",
"exchange": "NSE",
"action": "SELL",
"quantity": 1,
"pricetype": "MARKET",
"product": "MIS"
}
]
result = client.basketorder(orders=orders)
Split Order
Split a large order into smaller ones.
result = client.splitorder(
symbol="YESBANK",
exchange="NSE",
action="SELL",
quantity=105,
splitsize=20,
price_type="MARKET",
product="MIS"
)
Order Status
Check status of a specific order.
result = client.orderstatus(
order_id="24120900146469",
strategy="Test Strategy"
)
Open Position
Get current open position for a symbol.
result = client.openposition(
symbol="YESBANK",
exchange="NSE",
product="CNC"
)
Modify Order
Modify an existing order.
result = client.modifyorder(
order_id="24120900146469",
symbol="RELIANCE",
action="BUY",
exchange="NSE",
quantity=2,
price="2100",
product="MIS",
price_type="LIMIT"
)
Cancel Order
Cancel a specific order.
result = client.cancelorder(
order_id="24120900146469"
)
Cancel All Orders
Cancel all open orders.
result = client.cancelallorder()
Close Position
Close all open positions.
result = client.closeposition()
4. Data API
Quotes
Get real-time quotes for a symbol.
result = client.quotes(
symbol="RELIANCE",
exchange="NSE"
)
Market Depth
Get market depth (order book) data.
result = client.depth(
symbol="RELIANCE",
exchange="NSE"
)
Historical Data
Get historical price data.
result = client.history(
symbol="RELIANCE",
exchange="NSE",
interval="5m",
start_date="2024-01-01",
end_date="2024-01-31"
)
Intervals
Get supported time intervals for historical data.
result = client.interval()
Examples
Check the examples directory for detailed usage:
- account_test.py: Test account-related functions
- order_test.py: Test order management functions
- data_examples.py: Test market data functions
Publishing to PyPI
-
Update version in openalgo/__init__.py
-
Build the distribution:
python -m pip install --upgrade build
python -m build
- Upload to PyPI:
python -m pip install --upgrade twine
python -m twine upload dist/*
License
This project is licensed under the MIT License - see the LICENSE file for details.