Skip to main content
This example shows how to build a simple portfolio tracker that keeps your local state in sync with Polyhush.
from polyhush import PolyhushClient

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

# Track positions locally for quick access
tracked_positions = {}

def refresh_portfolio():
    """Fetch latest balance and positions, sync if needed."""
    
    # Get current balance
    balance = client.get_balance()
    print(f"Balance: ${balance['available_balance']:.2f} available, ${balance['reserved_balance']:.2f} in orders")
    
    # Get all positions
    positions = client.get_positions(detailed=True)
    
    # Check if positions match our local tracking
    for pos in positions:
        token_id = pos['token_id']
        
        if token_id in tracked_positions:
            local = tracked_positions[token_id]
            
            # If sizes don't match, an order may have filled—sync to be sure
            if local['size'] != pos['size']:
                print(f"Position mismatch for {token_id}, syncing orders...")
                client.sync_orders()
                # Re-fetch positions after sync
                positions = client.get_positions(detailed=True)
                break
        
        # Update local tracking
        tracked_positions[token_id] = {
            'size': pos['size'],
            'avg_price': pos['average_price'],
            'current_price': pos['current_price'],
            'pnl': pos['unrealized_pnl']
        }
    
    return positions

def show_portfolio():
    """Display portfolio summary."""
    positions = refresh_portfolio()
    
    if not positions:
        print("No open positions")
        return
    
    print(f"\n{'Market':<40} {'Size':>8} {'P&L':>12}")
    print("-" * 62)
    
    for pos in positions:
        print(f"{pos['market_question'][:38]:<40} {pos['size']:>8} ${pos['unrealized_pnl']:>+10.2f}")

# Run it
show_portfolio()

Key Methods

MethodPurpose
get_balance()Returns available_balance and reserved_balance USDC amounts
get_positions(detailed=True)Returns all positions with P&L and pricing info
sync_orders()Reconciles local order state with Polymarket (usually automatic)
Order syncing happens automatically via WebSocket. Only call sync_orders() manually if you notice discrepancies or after network interruptions.