Skip to main content

PyPSX SDK

API-first trading infrastructure for the Pakistan Stock Exchange.

Installation

pip install pypsx

Try it in a notebook

Open In Colab

One-liners — no client to construct

import pypsx

df = pypsx.download("OGDC", period="1y")
result = pypsx.backtest("dual_sma_momentum", "OGDC", period="1y", initial_cash=1_000_000)
quote = pypsx.get_quote("OGDC")
depth = pypsx.get_market_depth("OGDC")

These read PYPSX_API_KEY_ID/PYPSX_API_SECRET_KEY from the environment automatically. For order placement, positions, and account state, use TradingClient below.

Quick Start

Start with paper trading. It is the safety-first way to test your strategy, validate your order flow, and watch your dashboard update in real time before risking real capital.

import os
from dotenv import load_dotenv
from pypsx import TradingClient

load_dotenv()

client = TradingClient(
    api_key=os.getenv("PYPSX_API_KEY_ID"),
    secret_key=os.getenv("PYPSX_API_SECRET_KEY"),
    paper=True,
)

account = client.get_account()
print(f"Connected! Current Balance: PKR {account.cash}")

order = client.place_manual_order(
    symbol="OGDC",
    side="BUY",
    quantity=10,
    order_type="MARKET",
)

print("Submitted:", order["order_id"], order["status"])

You can also load keys directly from environment variables:

from pypsx import TradingClient

client = TradingClient.from_env(paper=True)

Your First Trade

Step 1: Generate a paper key in the PyPSX dashboard.
Step 2: Copy the script above into my_bot.py.
Step 3: Set your own PYPSX_API_KEY_ID and PYPSX_API_SECRET_KEY in .env.
Step 4: Run python my_bot.py while the market is open.
Step 5: Watch orders, fills, and positions appear in the dashboard automatically.

The Power of PyPSX

PyPSX gives algorithmic traders a clean Python interface for the Pakistan Stock Exchange without exposing them to exchange plumbing.

Paper Trading

PyPSX currently operates in paper trading mode: simulated orders, no real money. All requests go to https://paper-api.pypsx.com.

Real-Time Trading Experience

With PyPSX you can:

  • Read positions, orders, and account state from Python
  • Submit orders with a simple REST interface
  • See fills reflected in the web dashboard without manual refresh
  • Build bots around trading logic instead of exchange protocol handling

Developer's Promise

PyPSX handles the operational complexity of PSX integration, including request authentication, endpoint routing, and exchange connectivity. You focus on signal generation, risk rules, and execution logic. We handle the FIX-side complexity behind the API.

Authentication

How To Get Your Keys

  1. Sign in to the PyPSX dashboard.
  2. Open Settings.
  3. Select the account you want to trade.
  4. Click Generate Paper Key or Generate Live Key.
  5. Copy the Public Key ID and Secret Key.

How The SDK Uses Them

Use the credentials directly in TradingClient(...):

from pypsx import TradingClient

client = TradingClient(
    api_key=os.getenv("PYPSX_API_KEY_ID"),
    secret_key=os.getenv("PYPSX_API_SECRET_KEY"),
    paper=True,
)

Under the hood, the SDK automatically sends:

PYPSX-API-KEY-ID: <your-public-key-id>
PYPSX-API-SECRET-KEY: <your-secret-key>

If you are building against the API without the Python SDK, send those same headers yourself.

API Reference

Method What it does Returns
get_account(account_id=None) Account snapshot: cash, equity, buying_power, can_trade (attribute access supported) dict
get_portfolio_valuation() Returns the latest equity, cash, positions value, and pricing snapshot dict
get_positions() Returns open positions for the selected paper trading account list[dict]
get_orders(limit=...) Returns recent orders and their current state list[dict]
place_manual_order(...) Submits a market or priced order through the selected environment dict
get_symbols() Fetches available market symbols list[dict]
get_intraday(symbol, days=...) Retrieves recent intraday market data for a symbol list[dict]
get_historical(symbol, start=..., end=...) Retrieves historical daily bars for strategy research and analysis list[dict]
get_historical_intraday(symbols, start=..., end=..., interval=...) Retrieves multi-interval OHLCV candles (1m/5m/15m/1h) list[dict]
get_portfolio(bot_id=None) Raw portfolio dict for the current bot/account scope dict
get_account_config(account_id=None) Account-level configuration dict
get_fundamentals(symbol) pe_ratio, dividend_yield, market_cap, free_float, etc. for a symbol dict
get_dividends(symbol) Dividend history: year, amount, ex_date, payment_date, record_date list[dict]
get_commission_rate() The account's commission rate percentage (cached after first call) float
add_funds(amount, account_id=None, bot_id=None) Add paper cash to an account dict
get_performance(bot_id=None, limit=100) Historical performance snapshots for a bot dict
get_trades(bot_id=None, limit=100) Executed trade history for a bot dict
get_logs(bot_id=None, limit=200) Bot run logs dict
list_bots() List all bots registered under the account list[dict]
create_bot(bot_id, bot_label=None, strategy_name=None, symbols=None, cycle_minutes=None) Register a new cloud bot dict
place_bracket_order(symbol, side, quantity, stop_loss_price, take_profit_price, entry_type="MARKET", ...) Entry order plus a linked stop-loss/take-profit exit pair dict
place_oco_order(symbol, quantity, stop_loss_price, take_profit_price, ...) Attach a linked stop-loss/take-profit pair to an existing position dict
place_stop_order(symbol, quantity, trigger_price, limit_price=None, ...) Standalone stop order dict
get_order_executions(since=None, limit=1000) Raw fill/execution records for the current bot scope list[dict]
close() Closes the underlying HTTP client cleanly None

Examples

Paper Trading

import os
from pypsx import TradingClient

client = TradingClient(
    api_key=os.getenv("PYPSX_API_KEY_ID"),
    secret_key=os.getenv("PYPSX_API_SECRET_KEY"),
    paper=True,
)

valuation = client.get_portfolio_valuation()
positions = client.get_positions()
orders = client.get_orders(limit=25)

print("Equity:", valuation["equity"])
print("Positions:", len(positions))
print("Orders:", len(orders))

Simple Bot Pattern

from pypsx import TradingClient

SYMBOL = "OGDC"

client = TradingClient(
    api_key="PK_xxxxxxxxxxxx",
    secret_key="your_secret_key",
    paper=True,
)

positions = client.get_positions()
already_holding = any(
    position["symbol"] == SYMBOL and float(position["qty"]) > 0
    for position in positions
)

if not already_holding:
    client.place_manual_order(
        symbol=SYMBOL,
        side="BUY",
        quantity=10,
        order_type="MARKET",
    )

Best Practices

  • Use .env files or a secrets manager for credentials. Do not hardcode production keys into source control.
  • Start every new strategy with paper=True.
  • Treat paper trading as your pre-flight checklist before switching to live.
  • Run execution scripts when the market is open so fills, liquidity, and dashboard feedback reflect real conditions.
  • Add explicit guards in your code for position sizing, duplicate orders, and risk limits.
  • Close clients cleanly with client.close() in longer-running scripts or services.

Raw HTTP Example

If you are not using the SDK, this is the equivalent request format:

curl -X POST "https://paper-api.pypsx.com/orders" \
  -H "Content-Type: application/json" \
  -H "PYPSX-API-KEY-ID: $PYPSX_API_KEY_ID" \
  -H "PYPSX-API-SECRET-KEY: $PYPSX_API_SECRET_KEY" \
  -d "{\"symbol\":\"OGDC\",\"side\":\"BUY\",\"quantity\":10,\"order_type\":\"MARKET\",\"mode\":\"PAPER\",\"commission_rate\":0.02}"

Set commission_rate only when you want to override the default fee behavior for a specific order. The value is a percentage, so 0.02 means 0.02%.

Additional Examples

  • examples/pypsx_client_example.py
  • examples/example_bot.py

Download files

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

Source Distribution

pypsx-2.4.1.tar.gz (28.3 kB view details)

Uploaded Source

Built Distributions

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

pypsx-2.4.1-cp312-cp312-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.12Windows x86-64

pypsx-2.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (27.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pypsx-2.4.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl (26.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

pypsx-2.4.1-cp311-cp311-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.11Windows x86-64

pypsx-2.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (24.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pypsx-2.4.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl (23.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

pypsx-2.4.1-cp310-cp310-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.10Windows x86-64

pypsx-2.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (23.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

pypsx-2.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl (22.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

File details

Details for the file pypsx-2.4.1.tar.gz.

File metadata

  • Download URL: pypsx-2.4.1.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for pypsx-2.4.1.tar.gz
Algorithm Hash digest
SHA256 11cf1a9b9d31960d21db95e9709e6197084f23aedc1c4c60c194cb3020b240d8
MD5 51176f31ad6ef27f10f4b6cff03e6a6b
BLAKE2b-256 823b3e010cd4f5195a0275e21f31a3df2a6a907ffce7734a5fab35efe777fd34

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1.tar.gz:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: pypsx-2.4.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for pypsx-2.4.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 cad660184b986b9d2b24d3c8edd2de5fb6bf017cca1bef3d4e3a29b6a3a7fd52
MD5 6b1f94fdf8f9bed00e8fbbca71501095
BLAKE2b-256 f97bcc45f7ac58cf9722f61ec047cdbe2595eb7b9f7b5dd1003b48dc47b736fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp312-cp312-win_amd64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pypsx-2.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9be95f6d08c30293f969d23e134802fbc1c69ca868ccaa225782917ce61fe89d
MD5 b0e7eb6af42027404d1b28c2a503c831
BLAKE2b-256 e1b114641c626e7122960f71bfa4d4a5bddd9e29fa381e7932d27f0bff00add4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pypsx-2.4.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d22ec018e678e5f699cc5a29e6461b5f0940fa1b0a58d8c4197502f0c7fb1157
MD5 77818f37500948d989c5d60d0483b318
BLAKE2b-256 2a86ebae26ecb77d26c0b36a83999de9330188caa28a9455bfd3e23dbabebc7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: pypsx-2.4.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for pypsx-2.4.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 550b7a0c6964bad165ebdc3dd6059bdfe8ea37ae272bec59f18eea532d0231d6
MD5 0811665c16cac19fbeec1483f1d04b02
BLAKE2b-256 a64e4f337d68ab260f339f0d288ef07b9a138aa194665f52919575a7a540b112

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp311-cp311-win_amd64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pypsx-2.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 aeeaa81c67d6f78f7bfe54385a90efe8de567419cab7629fd064bbaa8d61f163
MD5 30b02ef2e31d5635a6e1faaf555bd811
BLAKE2b-256 d8947f945467d240f2a8233305492f8ee7b9356bdf32b4b164a396e604a42908

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pypsx-2.4.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ba472ed6ca7b3094eae7ab98863e85ad5efd62ae011aeaa0a97936138e7a7518
MD5 7ece40c2e34a95788d9fadf6fd3e53c6
BLAKE2b-256 a34a70a8e990c0373d21ffbcba670d5650a354ccc86b972705ac86d763bfd76f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: pypsx-2.4.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 3.8 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for pypsx-2.4.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a132e20fc4df5b2f145349d4b88f3c30207f054f5abadc9a78cf576b2ade0ef7
MD5 d99d04218d8c39566692835f59689901
BLAKE2b-256 aaef3cbceda29752a6120815a8db48910705728b21a324d82f64d1e1f4e1701c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp310-cp310-win_amd64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pypsx-2.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9a5dfc5f41e4f998ded0682bac135846a45cc082fab37f18418e5b0499b08165
MD5 c2537cac9c33c8b411d6d14942d02256
BLAKE2b-256 238f61711df180b8031a467442f426353d095c55690ba5627b4be8fdcf33a10f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypsx-2.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pypsx-2.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3f8b4a0a86223a6772409f1b4c52fcad9a37482230beb49afa66af1a42b6fd1e
MD5 cc3a37c86d8b7698b6561ea783edd9e5
BLAKE2b-256 8899a3ba9ca88cf6cd3798be5eeb6fd9a5cae09b0fb32d9a5c1cad36b1b4b3d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypsx-2.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_28_aarch64.whl:

Publisher: workflow.yml on pypsx/libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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