To install the OpenAlgo Python library, use pip:
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:
Copy 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
Copy import openalgo
openalgo . __version__
Examples
PlaceOrder example
To place a new order:
Copy 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:
Copy 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:
Copy 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:
Copy 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:
Copy 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:
Copy response = client . cancelorder (
order_id = "123456789" ,
strategy = "Python"
)
print (response)
CancelAllOrder Example
To cancel all open orders and trigger pending orders
Copy response = client . cancelallorder (
strategy = "Python"
)
print (response)
ClosePosition Example
To close all open positions across various exchanges
Copy response = client . closeposition (
strategy = "Python"
)
print (response)
OrderStatus Example
To Get the Current OrderStatus
Copy response = client . orderstatus (
order_id = order_id,
strategy = "Test Strategy"
)
print (response)
OpenPosition Example
To Get the Current OpenPosition
Copy response = client . openposition (
strategy = "Test Strategy" ,
symbol = "YESBANK" ,
exchange = "NSE" ,
product = "CNC"
)
print (response)
Quotes Example
Copy response = client . quotes (symbol = "RELIANCE" , exchange = "NSE" )
print (response)
Depth Example
Copy response = client . depth (symbol = "SBIN" , exchange = "NSE" )
print (response)
History Example
Copy client . history (symbol = "SBIN" ,
exchange = "NSE" ,
interval = "5m" ,
start_date = "2024-11-01" ,
end_date = "2024-12-31"
)
Interval Example
Copy response = client . interval ()
print (response)
Funds Example
Copy response = client . funds ()
print (response)
OrderBook Example
Copy response = client . orderbook ()
print (response)
TradeBook Example
Copy response = client . tradebook ()
print (response)
PositionBook Example
Copy response = client . positionbook ()
print (response)
Holdings Example
Copy response = client . holdings ()
print (response)