Get the FREE Ultimate OpenClaw Setup Guide →

trading-core

npx machina-cli add skill marketcalls/openalgo-claude-plugin/trading-core --openclaw
Files (1)
SKILL.md
8.0 KB

OpenAlgo Trading Core

Execute trading operations using OpenAlgo's unified Python SDK. Supports NSE, BSE, NFO, MCX, and currency derivatives with a single API across 25+ brokers.

Environment Setup

The OPENALGO_API_KEY must be set. Get your API key from your OpenAlgo application.

from openalgo import api

client = api(
    api_key='your_api_key_here',
    host='http://127.0.0.1:5000'  # Your OpenAlgo server
)

Quick Start Scripts

Place Market Order

python scripts/place_order.py --symbol RELIANCE --exchange NSE --action BUY --quantity 1 --product MIS

Place Smart Order (Position-Aware)

python scripts/smart_order.py --symbol TATAMOTORS --exchange NSE --action SELL --quantity 5 --position-size 10

Basket Order (Multiple Symbols)

python scripts/basket_order.py --orders '[{"symbol":"INFY","action":"BUY","quantity":1},{"symbol":"TCS","action":"BUY","quantity":1}]'

Split Order (Large Quantities)

python scripts/split_order.py --symbol YESBANK --exchange NSE --action SELL --quantity 500 --split-size 100

Order Constants

Exchanges

CodeDescription
NSENSE Equity
BSEBSE Equity
NFONSE Futures & Options
BFOBSE Futures & Options
CDSNSE Currency Derivatives
BCDBSE Currency Derivatives
MCXMCX Commodity
NCDEXNCDEX Commodity
NSE_INDEXNSE Indices (for quotes only)
BSE_INDEXBSE Indices (for quotes only)

Product Types

CodeDescriptionUse Case
CNCCash & CarryEquity delivery (hold overnight)
NRMLNormalF&O positions (hold overnight)
MISIntradayAuto square-off at market close

Price Types

CodeDescription
MARKETMarket Order (immediate execution)
LIMITLimit Order (specify price)
SLStop Loss Limit Order
SL-MStop Loss Market Order

Actions

CodeDescription
BUYBuy order
SELLSell order

Core API Methods

1. Place Order

Place a single order with full control over parameters:

response = client.placeorder(
    strategy="MyStrategy",
    symbol="RELIANCE",
    action="BUY",
    exchange="NSE",
    price_type="MARKET",
    product="MIS",
    quantity=1
)
# Response: {'orderid': '250408000989443', 'status': 'success'}

Limit Order Example:

response = client.placeorder(
    strategy="MyStrategy",
    symbol="YESBANK",
    action="BUY",
    exchange="NSE",
    price_type="LIMIT",
    product="MIS",
    quantity=1,
    price=16.50,
    trigger_price=0,
    disclosed_quantity=0
)

Stop Loss Order Example:

response = client.placeorder(
    strategy="MyStrategy",
    symbol="SBIN",
    action="SELL",
    exchange="NSE",
    price_type="SL",
    product="MIS",
    quantity=10,
    price=750,           # Limit price
    trigger_price=752    # Trigger price
)

2. Smart Order (Position-Aware)

Automatically adjusts order quantity based on current position:

response = client.placesmartorder(
    strategy="SmartBot",
    symbol="TATAMOTORS",
    action="SELL",
    exchange="NSE",
    price_type="MARKET",
    product="MIS",
    quantity=1,
    position_size=5  # Desired final position
)
# If current position is 0, sells 5 to reach -5
# If current position is 3, sells 8 to reach -5

Use Cases:

  • Rebalancing: Set position_size to target position
  • Reversal: Set action opposite to current direction
  • Scale-in/out: Adjust quantity dynamically

3. Basket Order

Execute multiple orders simultaneously:

basket_orders = [
    {
        "symbol": "INFY",
        "exchange": "NSE",
        "action": "BUY",
        "quantity": 1,
        "pricetype": "MARKET",
        "product": "MIS"
    },
    {
        "symbol": "TCS",
        "exchange": "NSE",
        "action": "BUY",
        "quantity": 1,
        "pricetype": "MARKET",
        "product": "MIS"
    },
    {
        "symbol": "WIPRO",
        "exchange": "NSE",
        "action": "BUY",
        "quantity": 1,
        "pricetype": "MARKET",
        "product": "MIS"
    }
]

response = client.basketorder(orders=basket_orders)
# Response includes status for each order

4. Split Order

Break large orders into smaller chunks to avoid market impact:

response = client.splitorder(
    symbol="YESBANK",
    exchange="NSE",
    action="SELL",
    quantity=500,
    splitsize=100,  # Each order will be max 100
    price_type="MARKET",
    product="MIS"
)
# Creates 5 orders of 100 each

Response:

{
  "status": "success",
  "split_size": 100,
  "total_quantity": 500,
  "results": [
    {"order_num": 1, "orderid": "123", "quantity": 100, "status": "success"},
    {"order_num": 2, "orderid": "124", "quantity": 100, "status": "success"},
    ...
  ]
}

Order Management

Modify Order

response = client.modifyorder(
    order_id="250408001002736",
    strategy="MyStrategy",
    symbol="YESBANK",
    action="BUY",
    exchange="NSE",
    price_type="LIMIT",
    product="MIS",
    quantity=1,
    price=17.00  # New price
)

Cancel Order

response = client.cancelorder(
    order_id="250408001002736",
    strategy="MyStrategy"
)

Cancel All Orders

response = client.cancelallorder(strategy="MyStrategy")
# Cancels all open and trigger-pending orders

Close All Positions

response = client.closeposition(strategy="MyStrategy")
# Squares off all open positions

Get Order Status

response = client.orderstatus(
    order_id="250408001002736",
    strategy="MyStrategy"
)
# Returns: order_status, average_price, quantity, timestamp

Get Open Position

response = client.openposition(
    strategy="MyStrategy",
    symbol="RELIANCE",
    exchange="NSE",
    product="MIS"
)
# Returns: {'quantity': '10', 'status': 'success'}

Symbol Format

OpenAlgo uses standardized symbol formats across all brokers:

Equity

  • RELIANCE, INFY, TCS, SBIN

Futures

  • Format: [SYMBOL][DDMMMYY]FUT
  • Examples: NIFTY30JAN25FUT, BANKNIFTY30JAN25FUT

Options

  • Format: [SYMBOL][DDMMMYY][STRIKE][CE/PE]
  • Examples: NIFTY30JAN2526000CE, BANKNIFTY30JAN2555000PE

Common Patterns

Intraday Scalping

# Entry
entry = client.placeorder(
    strategy="Scalper",
    symbol="SBIN",
    action="BUY",
    exchange="NSE",
    price_type="MARKET",
    product="MIS",
    quantity=100
)

# Exit with profit target (use limit order)
exit_order = client.placeorder(
    strategy="Scalper",
    symbol="SBIN",
    action="SELL",
    exchange="NSE",
    price_type="LIMIT",
    product="MIS",
    quantity=100,
    price=current_price * 1.005  # 0.5% profit
)

Swing Trading Entry

response = client.placeorder(
    strategy="SwingTrader",
    symbol="TATASTEEL",
    action="BUY",
    exchange="NSE",
    price_type="LIMIT",
    product="CNC",  # Delivery
    quantity=10,
    price=150.00
)

Risk Management - Stop Loss

# Place stop loss immediately after entry
sl_order = client.placeorder(
    strategy="Scalper",
    symbol="SBIN",
    action="SELL",
    exchange="NSE",
    price_type="SL-M",
    product="MIS",
    quantity=100,
    trigger_price=entry_price * 0.995  # 0.5% stop loss
)

Error Handling

response = client.placeorder(...)

if response.get('status') == 'success':
    print(f"Order placed: {response['orderid']}")
else:
    print(f"Error: {response.get('message', 'Unknown error')}")

Notes

  • Always verify OPENALGO_API_KEY is set before trading
  • Use MIS for intraday, CNC/NRML for positional trades
  • Test with small quantities first
  • Use Analyzer mode for paper trading: client.analyzertoggle(mode=True)

Source

git clone https://github.com/marketcalls/openalgo-claude-plugin/blob/main/plugins/openalgo-python/skills/trading-core/SKILL.mdView on GitHub

Overview

OpenAlgo Trading Core provides a single Python SDK to execute trades across NSE, BSE, NFO, MCX, and currency derivatives with 25+ brokers. It supports various order types (market, limit, SL, SL-M) plus advanced modes like basket and split orders, as well as position-aware smart orders.

How This Skill Works

Authenticate with your OPENALGO_API_KEY and host, then create a client via api(api_key='your_api_key', host='http://127.0.0.1:5000'). Use core methods like placeorder, placesmartorder, basket_order, and split_order to execute trades across supported exchanges and product types in a unified way.

When to Use It

  • When you need quick market execution across multiple Indian exchanges
  • When you want a position-aware order to reach a target holding via smart orders
  • When placing multiple symbol orders at once using a basket order
  • When executing large quantities by splitting into smaller chunks to manage impact
  • When trading equities, derivatives, and currency derivatives with a single API

Quick Start

  1. Step 1: Set OPENALGO_API_KEY and create the client: client = api(api_key='your_api_key', host='http://127.0.0.1:5000')
  2. Step 2: Place a sample market order: client.placeorder(strategy='MyStrategy', symbol='RELIANCE', action='BUY', exchange='NSE', price_type='MARKET', product='MIS', quantity=1)
  3. Step 3: Try smart, basket, and split orders: client.placesmartorder(...), client.basket_order(...), client.split_order(...)

Best Practices

  • Securely store and load OPENALGO_API_KEY; verify the host URL before trading
  • Match the product type (CNC, NRML, MIS) to your risk and holding strategy
  • Prefer MARKET or SL types for clear execution and risk control; test with small quantities
  • Leverage placesmartorder to target a specific final position and rebalance as needed
  • Validate symbol and exchange codes against the supported list to avoid errors

Example Use Cases

  • Place Market Order: BUY 1 RELIANCE on NSE (MIS)
  • Place Smart Order: SELL 5 TATAMOTORS on NSE with position_size 10
  • Basket Order: BUY INFY 1 and BUY TCS 1 on NSE
  • Split Order: SELL 500 YESBANK with split_size 100
  • Stop Loss Order: SELL 10 SBIN with price 750 and trigger 752

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers