# Arthur SDK

Simple trading for AI agents on [Orderly Network](https://orderly.network).

Built for [Arthur DEX](https://arthurdex.com).

## Installation

```bash
pip install arthur-sdk
```

For withdrawal support (requires wallet signing):
```bash
pip install arthur-sdk[withdraw]
```

## Quick Start

```python
from arthur_sdk import Arthur

# Initialize client
client = Arthur(
    api_key="ed25519:...",
    secret_key="ed25519:...",
    account_id="0x..."
)

# Trade in one line
client.buy("ETH", usd=100)   # Buy $100 of ETH
client.sell("BTC", usd=50)   # Short $50 of BTC

# Check positions
for pos in client.positions():
    print(f"{pos.symbol}: {pos.side} {pos.size} @ {pos.entry_price}")

# Get account summary
print(client.summary())
```

## Features

### Trading
```python
# Market orders
client.buy("ETH", usd=100)           # Buy $100 worth
client.buy("ETH", size=0.1)          # Buy 0.1 ETH
client.sell("BTC", usd=500)          # Short $500 worth

# Limit orders
client.limit_buy("ETH", price=3000, usd=100)
client.limit_sell("ETH", price=4000, size=0.1)

# Close positions
client.close("ETH")                  # Close entire position
client.close("ETH", size=0.05)       # Partial close
client.close_all()                   # Close all positions

# Cancel orders
client.cancel_all()                  # Cancel all orders
client.cancel_all("ETH")             # Cancel ETH orders only
```

### Account
```python
# Balances
client.balance()           # Available USDC
client.equity()            # Total equity (balance + unrealized PnL)
client.free_collateral()   # Withdrawable amount

# Positions
client.positions()         # List all positions
client.position("ETH")     # Get specific position
client.pnl()               # Total unrealized PnL

# Orders
client.orders()            # List recent orders
```

### Withdrawal (New in 0.2.1)
```python
# Withdraw to wallet
result = client.withdraw(
    amount=100,
    wallet_private_key="0x...",
    chain_id=42161,  # Arbitrum
)
print(f"Withdrawal ID: {result['withdraw_id']}")

# Withdraw all available
result = client.withdraw_all(wallet_private_key="0x...")

# Settle PnL before withdrawing
client.settle_pnl()
```

### Market Data
```python
client.price("ETH")        # Current ETH price
client.prices()            # All prices
client.orderbook("ETH")    # Order book
client.spread("ETH")       # Bid/ask spread
```

## Supported Symbols

Short names: `BTC`, `ETH`, `SOL`, `ARB`, `OP`, `AVAX`, `LINK`, `DOGE`, `SUI`, `TIA`, `WOO`, `ORDER`

Or use full Orderly symbols: `PERP_ETH_USDC`, `PERP_BTC_USDC`, etc.

## Strategy Runner

Run automated trading strategies:

```python
from arthur_sdk import StrategyRunner, StrategyConfig, Signal

def my_strategy(client, config):
    price = client.price("ETH")
    # Your logic here
    return Signal.HOLD

config = StrategyConfig(
    symbol="ETH",
    usd_size=100,
    interval_seconds=60,
)

runner = StrategyRunner(client, my_strategy, config)
runner.run()
```

## Market Making

```python
from arthur_sdk import MarketMaker

mm = MarketMaker(
    client=client,
    symbol="ETH",
    spread_bps=10,        # 0.1% spread
    order_size_usd=100,
    num_levels=3,
)
mm.run()
```

## Configuration

### From file
```python
client = Arthur.from_credentials_file("creds.json")
```

### Testnet
```python
client = Arthur(
    api_key="...",
    secret_key="...",
    account_id="...",
    testnet=True
)
```

## Chain IDs

| Chain | ID |
|-------|-----|
| Arbitrum | 42161 |
| Base | 8453 |
| Optimism | 10 |
| Ethereum | 1 |

## Links

- [Arthur DEX](https://arthurdex.com)
- [Orderly Network](https://orderly.network)
- [API Documentation](https://docs-api.orderly.network)

## License

MIT
