Python

To install the OpenAlgo Python library, use pip:

pip install openalgo

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 api class from the OpenAlgo library and initialize it with your API key:

from openalgo import api

# Replace 'your_api_key_here' with your actual API key
# Specify the host URL with your hosted domain or ngrok domain. 
# If running locally in windows then use the default host value. 
client = api(api_key='your_api_key_here', host='http://127.0.0.1:5000')

Check OpenAlgo Version

import openalgo 
openalgo.__version__

Examples

PlaceOrder example

To place a new order:

response = client.placeorder(
    strategy="Python",
    symbol="RELIANCE",
    action="BUY",
    exchange="NSE",
    price_type="MARKET",
    product="MIS",
    quantity=1
)
print(response)

PlaceSmartOrder Example

To place a smart order considering the current position size:

response = client.placesmartorder(
    strategy="Python",
    symbol="TATAMOTORS",
    action="SELL",
    exchange="NSE",
    price_type="MARKET",
    product="MIS",
    quantity=1,
    position_size=5
)
print(response)

BasketOrder example

To place a new basket order:

basket_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"
        }
    ]
response = client.basketorder(orders=basket_orders)
print(response)

SplitOrder example

To place a new split order:

response = client.splitorder(
    symbol="YESBANK",
    exchange="NSE",
    action="SELL",
    quantity=105,
    splitsize=20,
    price_type="MARKET",
    product="MIS"
    )
print(response)

ModifyOrder Example

To modify an existing order:

response = client.modifyorder(
    order_id="123456789",
    strategy="Python",
    symbol="INFY",
    action="SELL",
    exchange="NSE",
    price_type="LIMIT",
    product="CNC",
    quantity=2,
    price=1900
)
print(response)

CancelOrder Example

To cancel an existing order:

response = client.cancelorder(
    order_id="123456789",
    strategy="Python"
)
print(response)

CancelAllOrder Example

To cancel all open orders and trigger pending orders

response = client.cancelallorder(
    strategy="Python"
)
print(response)

ClosePosition Example

To close all open positions across various exchanges

response = client.closeposition(
    strategy="Python"
)
print(response)

OrderStatus Example

To Get the Current OrderStatus

response = client.orderstatus(
    order_id=order_id,
    strategy="Test Strategy"
    )
print(response)

OpenPosition Example

To Get the Current OpenPosition

response = client.openposition(
            strategy="Test Strategy",
            symbol="YESBANK",
            exchange="NSE",
            product="CNC"
        )
print(response)

Quotes Example

response = client.quotes(symbol="RELIANCE", exchange="NSE")
print(response)

Depth Example

response = client.depth(symbol="SBIN", exchange="NSE")
print(response)

History Example

client.history(symbol="SBIN", 
    exchange="NSE", 
    interval="5m", 
    start_date="2024-11-01", 
    end_date="2024-12-31"
    )

Interval Example

response = client.interval()
print(response)

Funds Example

response = client.funds()
print(response)

OrderBook Example

response = client.orderbook()
print(response)

TradeBook Example

response = client.tradebook()
print(response)

PositionBook Example

response = client.positionbook()
print(response)

Holdings Example

response = client.holdings()
print(response)

Last updated