Skip to main content

A Python library for interacting with OpenAlgo's trading APIs

Project description

OpenAlgo - Python API Client for Automated Trading

Installation

pip install openalgo

Complete Code Examples

Order Management Example

from openalgo import api

# Initialize the API client
client = api(
    api_key="your_api_key",
    host="http://127.0.0.1:5000"
)

# Place a market order
market_order = client.placeorder(
    symbol="RELIANCE",  # Symbol without -EQ suffix
    action="BUY",
    exchange="NSE",
    price_type="MARKET",
    product="MIS",
    quantity="1"  # Numeric values as strings
)
print("Market Order:", market_order)

# Place a limit order
limit_order = client.placeorder(
    symbol="TATAMOTORS",
    action="SELL",
    exchange="NSE",
    price_type="LIMIT",
    product="MIS",
    quantity="1",
    price="800.00"  # Price as string
)
print("Limit Order:", limit_order)

# Place a smart order
smart_order = client.placesmartorder(
    symbol="TATAMOTORS",
    action="SELL",
    exchange="NSE",
    price_type="MARKET",
    product="MIS",
    quantity="1",
    position_size="5"
)
print("Smart Order:", smart_order)

# Modify an order
modify_order = client.modifyorder(
    order_id="12345678",
    symbol="INFY",
    action="SELL",
    exchange="NSE",
    price_type="LIMIT",
    product="CNC",
    quantity="2",
    price="1500.00",
    disclosed_quantity="0",  # Required parameter
    trigger_price="0"       # Required parameter
)
print("Modify Order:", modify_order)

# Cancel an order
cancel_order = client.cancelorder(
    order_id="12345678",
    strategy="Python"
)
print("Cancel Order:", cancel_order)

# Cancel all orders
cancel_all = client.cancelallorder(
    strategy="Python"
)
print("Cancel All Orders:", cancel_all)

# Close all positions
close_positions = client.closeposition(
    strategy="Python"
)
print("Close Positions:", close_positions)

Market Data Example

from openalgo import api
import pandas as pd

# Initialize the API client
client = api(
    api_key="your_api_key",
    host="http://127.0.0.1:5000"
)

def print_dataframe(title, df):
    print(f"\n{title}:")
    if isinstance(df, pd.DataFrame):
        print("\nFirst few rows:")
        print(df.head().to_string())
        print("\nDataFrame Info:")
        print(df.info())
    else:
        print(df)

# Get real-time quotes
quotes = client.quotes(
    symbol="RELIANCE",
    exchange="NSE"
)
print("\nReal-time Quotes:", quotes)

# Get market depth
depth = client.depth(
    symbol="SBIN",
    exchange="NSE"
)
print("\nMarket Depth:", depth)

# Get supported intervals
intervals = client.interval()
print("\nSupported Intervals:", intervals)

# Get 1-minute data
minute_data = client.history(
    symbol="SBIN",
    exchange="NSE",
    interval="1m",
    start_date="2024-12-01",
    end_date="2024-12-31"
)
print_dataframe("1-Minute Data", minute_data)

# Get 5-minute data
five_min_data = client.history(
    symbol="SBIN",
    exchange="NSE",
    interval="5m",
    start_date="2024-12-01",
    end_date="2024-12-31"
)
print_dataframe("5-Minute Data", five_min_data)

# Get hourly data
hourly_data = client.history(
    symbol="SBIN",
    exchange="NSE",
    interval="1h",
    start_date="2024-12-01",
    end_date="2024-12-31"
)
print_dataframe("Hourly Data", hourly_data)

# Get daily data
daily_data = client.history(
    symbol="SBIN",
    exchange="NSE",
    interval="D",
    start_date="2024-12-01",
    end_date="2024-12-31"
)
print_dataframe("Daily Data", daily_data)

# Example of data analysis
if isinstance(daily_data, pd.DataFrame):
    # Calculate daily returns
    daily_data['returns'] = daily_data['close'].pct_change()
    
    # Calculate simple moving averages
    daily_data['SMA_5'] = daily_data['close'].rolling(window=5).mean()
    daily_data['SMA_20'] = daily_data['close'].rolling(window=20).mean()
    
    print("\nDaily Data with Indicators:")
    print(daily_data.tail().to_string())
    
    print("\nSummary Statistics:")
    print(daily_data.describe().to_string())

Example Output

# Intraday data (1-minute):
#                                     close    high     low    open  volume
# timestamp                                                        
# 2024-12-02 09:15:00+05:30  836.40  841.10  836.00  838.95  121671
# 2024-12-02 09:16:00+05:30  835.75  836.60  835.10  836.15   40517

# Daily data:
#                      close    high    low    open    volume
# timestamp                                          
# 2024-12-02  836.40  842.00  832.7  838.95   6651119
# 2024-12-03  853.95  856.60  836.9  838.00  12186182

Important Notes

  1. Order Parameters:

    • All numeric values (quantity, price, etc.) must be passed as strings
    • Symbols should be used without -EQ suffix
    • Modify orders require disclosed_quantity and trigger_price parameters
  2. Historical Data:

    • Intraday data (seconds, minutes, hours) includes IST timezone (+05:30)
    • Daily data uses clean date format without timezone
    • OHLCV columns with proper data types
    • Timestamps are sorted chronologically

For more detailed usage and additional methods, refer to the OpenAlgo REST API Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openalgo-1.0.3.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openalgo-1.0.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file openalgo-1.0.3.tar.gz.

File metadata

  • Download URL: openalgo-1.0.3.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for openalgo-1.0.3.tar.gz
Algorithm Hash digest
SHA256 91f4b1cdaf9ca442696b774f85bba602671d7cb38627ba69f7505b4b9b122bd5
MD5 d74d8c5a9dbc0679cbbd879d275f31f2
BLAKE2b-256 695693455d8236390638df1920a4443b19b92cb9e710b2f3def6acf4cd0497d4

See more details on using hashes here.

File details

Details for the file openalgo-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: openalgo-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for openalgo-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1853d0e82b9a88554c0f591035f6913789c4be1ed8df86acf735024c98f440e2
MD5 9f887cd446538d5cb07bb96de5db55b2
BLAKE2b-256 fa0574d14a12c2dcc7bf35bffe955bd90c0a20a7e32e1f30ecab5d1ff3d0ed2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page