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.orders 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)

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=1500
)
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)

Last updated