Skip to content

Trading Examples

This page provides examples of trading operations with the Ethereal Python SDK.

Creating orders

from ethereal import RESTClient

# Initialize client with your private key
client = RESTClient({
    "base_url": "https://api.etherealtest.net",
    "chain_config": {
        "rpc_url": "https://rpc.etherealtest.net",
        "private_key": "your_private_key"
    }
})

# Place a limit buy order
limit_buy = client.create_order(
    order_type="LIMIT",
    quantity=0.01,  # Buy 0.01 BTC
    side=0,         # 0 for buy
    price=80000.0,  # Price in USD
    ticker="BTCUSD",
    time_in_force="GTD",  # Good Til Date
    post_only=True
)

# Place a limit sell order
limit_sell = client.create_order(
    order_type="LIMIT",
    quantity=0.01, # Sell 0.01 BTC
    side=1,        # 1 for sell
    price=85000.0, # Price in USD
    ticker="BTCUSD"
)

# Place a market buy order
market_buy = client.create_order(
    order_type="MARKET",
    quantity=0.01,  # Buy 0.01 BTC
    side=0,         # 0 for buy
    ticker="BTCUSD"
)

Managing orders

# Get all orders
sid = client.subaccounts[0].id
sname = client.subaccounts[0].name
orders = client.list_orders(subaccount_id=sid)

# Get orders for a specific product
btc_product_id = client.products_by_ticker["BTCUSD"].product_id
btc_orders = client.list_orders(subaccount_id=sid, product_ids=[btc_product_id])

# Get a specific order by ID
order_id = '78231588-f2a4-47b9-9b96-9ff8ed58d034'
order = client.get_order(id=order_id)

# Cancel an order
cancel_order = client.cancel_order(order_id=order_id, sender=client.chain.address, subaccount=sname)

Order fills and trade history

# Get trade history
sid = client.subaccounts[0].id
trades = client.list_trades(subaccount_id=sid)

# Get fills
fills = client.list_fills(subaccount_id=sid)

# Get all fills for a product
btc_product_id = client.products_by_ticker['BTCUSD'].product_id
btc_fills = client.list_fills(subaccount_id=sid, product_id=btc_product_id)

# Display fill information
for fill in fills:
    print(fill.model_dump())

Advanced order types

# Post-only order (will not execute against existing orders)
post_only_order = client.create_order(
    order_type="LIMIT",
    quantity=0.01,
    side=0,
    price=80000.0,
    ticker="BTCUSD",
    time_in_force="GTD",  # Good Til Date
    post_only=True
)

# Immediate-or-Cancel order (execute immediately or cancel)
ioc_order = client.create_order(
    order_type="LIMIT",
    quantity=0.01,
    side=0,
    price=89000.0,
    ticker="BTCUSD",
    time_in_force="IOC" # Immediate-or-Cancel
)

# Fill-or-Kill order (execute complete order immediately or cancel)
fok_order = client.create_order(
    order_type="LIMIT",
    quantity=0.01,
    side=0,
    price=89000.0,
    ticker="BTCUSD",
    time_in_force="FOK" # Fill-or-Kill
)

# Reduce-only order (will not increase position size)
reduce_only_order = client.create_order(
    order_type="LIMIT",
    quantity=0.1,
    side=1,  # Sell side
    price=100000.0,
    ticker="BTCUSD",
    reduce_only=True
)