Skip to main content
These examples cover the fundamental operations you’ll use most often: checking your balance, placing orders, and monitoring positions.

Check Your Balance

Always verify your available balance before placing orders:
from polyhush import PolyhushClient

client = PolyhushClient(api_key="your-api-key")

# Get basic balance
balance = client.get_balance()
print(f"Balance: ${balance['balance']:.2f}")
For a complete portfolio overview:
# Include portfolio summary
summary = client.get_balance(include_summary=True)
print(f"Total Value: ${summary['total_value']:.2f}")

Place a Limit Order

Limit orders let you specify your price. They sit on the order book until filled or cancelled:
from polyhush import PolyhushClient, PolyhushAPIError

client = PolyhushClient(api_key="your-api-key")

try:
    # Buy 10 shares at $0.45 each
    result = client.buy(
        token_id="your-token-id",
        shares=10,
        price=0.45
    )
    print(f"Order placed: {result['orderID']}")
    
except PolyhushAPIError as e:
    print(f"Order failed: {e.message}")

Place a Market Order

Market orders execute immediately at the best available price:
# Buy shares at market price (spend $50)
result = client.buy(
    token_id="your-token-id",
    usdc_amount=50,
    order_type="FAK"  # Fill And Kill = market order
)
print(f"Market buy: {result['orderID']}")

# Sell shares at market price
result = client.sell(
    token_id="your-token-id",
    shares=10,
    order_type="FAK"
)
print(f"Market sell: {result['orderID']}")

View Your Positions

Check what you currently own:
# Basic position info
positions = client.get_positions()

for pos in positions:
    print(f"{pos['side']}: {pos['size']} shares @ ${pos['average_price']:.4f}")
For P&L information:
# Detailed positions with P&L
positions = client.get_positions(detailed=True)

for pos in positions:
    print(f"{pos['size']} shares, P&L: ${pos['unrealized_pnl']:.2f} ({pos['unrealized_pnl_pct']:+.1f}%)")

Cancel Orders

Cancel a specific order or all open orders:
# Cancel a specific order
result = client.cancel_orders(order_id="ord_abc123")
print(f"Cancelled: {result['message']}")

# Cancel ALL open orders
result = client.cancel_orders()
print(f"Cancelled {result['cancelled']} orders")

Check Market Price

Get current pricing before trading:
ticker = client.get_ticker(token_id="your-token-id")
print(f"Bid: ${ticker['best_bid']:.4f}, Ask: ${ticker['best_ask']:.4f}, Midpoint: ${ticker['midpoint']:.4f}")

Complete Example: Buy with Safety Checks

A complete workflow with proper validation:
from polyhush import PolyhushClient, PolyhushAPIError

def safe_buy(client, token_id, shares, max_price):
    """Buy shares with balance and price checks."""
    
    # 1. Check balance
    balance = client.get_balance()
    required = shares * max_price
    
    if balance['available_balance'] < required:
        return None
    
    # 2. Check current price
    ticker = client.get_ticker(token_id)
    
    if ticker['best_ask'] > max_price:
        return None
    
    # 3. Place the order
    try:
        result = client.buy(
            token_id=token_id,
            shares=shares,
            price=max_price
        )
        return result
        
    except PolyhushAPIError as e:
        return None

# Usage
client = PolyhushClient(api_key="your-api-key")
safe_buy(client, "your-token-id", shares=10, max_price=0.50)

Next Steps

Position Management

Learn to manage and close positions

Trading Bot

Build an automated trading bot