Skip to main content

A python based SDK developed for interacting with Ostium, a leveraged trading application for trading currencies, commodities, indices, crypto and more.

Project description

Ostium Python SDK

A python based SDK developed for interacting with Ostium v1 Trading Platform (https://ostium.app/)

Ostium is a decentralized perpetuals exchange on Arbitrum (Ethereum L2) with a focus on providing a seamless experience for traders for trading currencies, commodities, indices, crypto and more.

This SDK is designed to be used by developers who want to build applications on top of Ostium and automate their trading strategies.

Pip Install

The SDK can be installed via pip:

pip install ostium-python-sdk

Requirements

Developed using:

  python=3.8

Usage Example

Opening a Trade

from ostium_python_sdk import OstiumSDK
from dotenv import load_dotenv

# Load environment variables if using .env file
load_dotenv()

# Get private key from environment variable
private_key = os.getenv('PRIVATE_KEY')
if not private_key:
    raise ValueError("PRIVATE_KEY not found in .env file")

rpc_url = os.getenv('RPC_URL')
if not rpc_url:
    raise ValueError("RPC_URL not found in .env file")

# Initialize SDK
config = NetworkConfig.testnet()
sdk = OstiumSDK(config, private_key)

# Or initialize with explicit private key & rpc url
# sdk = OstiumSDK(
#     network="arbitrum",
#     private_key="your_private_key_here",
#     rpc_url="https://arb1.arbitrum.io/rpc"
# )

# Get all available pairs
pairs = await sdk.subgraph.get_pairs()

print("\nPair Information:")
print("----------------------------------------")

for pair in pairs:
    # Get detailed information for each pair from the Graph API
    pair_details = await sdk.subgraph.get_pair_details(pair['id'])
    print("\nPair Details:")
    print("----------------------------------------")
    # Print all available fields in pair_details
    for key, value in pair_details.items():
        print(f"{key}: {value}")
    print("----------------------------------------")


# Define trade parameters
trade_params = {
    'collateral': 100,        # USDC amount
    'leverage': 10,           # Leverage multiplier
    'asset_type': 0,          # 0 for BTC, see pair_details above for other asset types 
    'direction': True,        # True for Long, False for Short
    'order_type': 'MARKET'    # 'MARKET', 'LIMIT', or 'STOP'
}

try:
  # Get latest price for BTC
  latest_price, _ = await sdk.price.get_price("BTC", "USD")
  print(f"Latest price: {latest_price}")
  # Execute trade at current market price
  receipt = sdk.ostium.perform_trade(trade_params, at_price=latest_price)
  print(f"Trade successful! Transaction hash: {receipt['transactionHash'].hex()}")

  # Wait for the transaction to be confirmed
  await asyncio.sleep(10)

  # Get public address from private key
  account = Account.from_key(private_key)
  trader_public_address = account.address

  # Get the trade details
  open_trades = await sdk.subgraph.get_open_trades(trader_public_address)
  for trade_index, trade_data in enumerate(open_trades):
      print(f"Trade {trade_index + 1}: {trade_data}\n")

  if len(open_trades) == 0:
      print(
          "No open trades found. Maybe the trade failed? enough USDC and ETH in the account?")
  else:
      opened_trade = open_trades[len(open_trades) - 1]
      print(f"Opened trade: {opened_trade}\n")

      sdk.ostium.update_tp(
          opened_trade['pair']['id'], opened_trade['index'], latest_price * 1.02)
      print(f"Trade Take Profit set to 2% above the current price!\n")

      await asyncio.sleep(10)

      sdk.ostium.update_sl(
          opened_trade['pair']['id'], opened_trade['index'], latest_price * 0.99)
      print(f"Trade Stop Loss set to 1% below the current price!\n")

      await asyncio.sleep(10)

      receipt = sdk.ostium.close_trade(
          opened_trade['pair']['id'], opened_trade['index'])
      print(
          f"Closed trade! Transaction hash: {receipt['transactionHash'].hex()}\n")



except Exception as e:
  print(f"Trade failed: {str(e)}")

Example Usage Script

Read Block Number

To run the example:

python examples/example-read-block-number.py

See example-read-block-number.py for an example of how to use the SDK.

Read Positions

To run the example:

python examples/example-read-positions.py

See example-read-positions.py for an example of how to use the SDK.

Get Feed Prices

To open a trade you need the latest feed price.

See this example script on how to get the latest feed prices.

python examples/example-get-prices.py

See example-get-prices.py for an example of how to use the SDK.

Get Balance of an Address

See this example script on how to get the latest feed prices.

python examples/example-get-balance.py

See example-get-balance.py for an example of how to use the SDK.

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

ostium_python_sdk-0.1.13.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

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

ostium_python_sdk-0.1.13-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file ostium_python_sdk-0.1.13.tar.gz.

File metadata

  • Download URL: ostium_python_sdk-0.1.13.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for ostium_python_sdk-0.1.13.tar.gz
Algorithm Hash digest
SHA256 6d8d92327eb52ee59780862edec6fcf7e72fdb4a5a2b9c265664b61cc5f40e1e
MD5 650201c5466030884382c141be45daf9
BLAKE2b-256 c6436cf61f2a0a045058433affea2d9352d5f9b15b23f6a7f2b55db30a792aab

See more details on using hashes here.

File details

Details for the file ostium_python_sdk-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for ostium_python_sdk-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 658358ce53edc76299b9fa006d8445527475d24e8cc666c380d7d47c70b49bf9
MD5 4dcc8a6acfe1df1883f24e0e3b76dc59
BLAKE2b-256 736ce6ca64e01d0229a90df37cac884df14e931356db8398aaf3154058bcd792

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