# Arthur SDK

[![PyPI version](https://badge.fury.io/py/arthur-sdk.svg)](https://badge.fury.io/py/arthur-sdk)
[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Simple trading for AI agents on Arthur DEX.**

Trade in 3 lines of code. No complex signatures. No confusing structs. Just trade.

```python
from arthur_sdk import Arthur

client = Arthur.from_credentials_file("credentials.json")
client.buy("ETH", usd=100)
```

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

## Installation

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

## Quick Start

```python
from arthur_sdk import Arthur

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

# Or load from file
client = Arthur.from_credentials_file("credentials.json")

# Trade
client.buy("ETH", usd=100)      # Buy $100 worth of ETH
client.sell("BTC", size=0.01)   # Sell 0.01 BTC
client.close("ETH")             # Close ETH position
client.close_all()              # Close all positions

# Check status
print(client.balance())         # Available USDC
print(client.pnl())             # Total unrealized PnL
print(client.positions())       # All open positions
```

## Features

### Simple Trading

```python
# Market orders (instant execution)
client.buy("ETH", usd=100)      # Buy by USD value
client.buy("BTC", size=0.01)    # Buy by size

# Limit orders
client.buy("ETH", size=0.1, price=2000)

# Close positions
client.close("ETH")             # Close specific position
client.close("ETH", size=0.05)  # Partial close
client.close_all()              # Close everything
```

### Position Management

```python
# Get all positions
for pos in client.positions():
    print(f"{pos.symbol}: {pos.side} {pos.size}")
    print(f"  Entry: ${pos.entry_price}")
    print(f"  PnL: ${pos.unrealized_pnl} ({pos.pnl_percent}%)")

# Get specific position
eth_pos = client.position("ETH")

# Total PnL
total_pnl = client.pnl()
```

### Market Making

```python
# Place two-sided quotes
quotes = client.quote(
    symbol="ETH",
    bid_price=2000,
    ask_price=2010,
    size=0.1
)

# Post-only limit orders
client.limit_buy("ETH", price=2000, size=0.1, post_only=True)

# Get spread info
spread = client.spread("ETH")
print(f"Spread: {spread['spread_bps']:.1f} bps")
```

### Strategy Execution

```python
from arthur_sdk import StrategyRunner

# Run strategies from JSON configs
runner = StrategyRunner(client)
result = runner.run("strategies/momentum.json")
```

## CLI

```bash
# Get prices
arthur price BTC ETH SOL

# Check account status
arthur status -c credentials.json

# Execute trades
arthur trade buy ETH --usd 100 -c credentials.json

# Run strategies
arthur run strategy.json -c credentials.json
```

## Credentials File

```json
{
    "api_key": "ed25519:xxx",
    "secret_key": "ed25519:xxx",
    "account_id": "0x..."
}
```

## Supported Symbols

Short symbols are automatically converted:

| Short | Full Symbol |
|-------|-------------|
| BTC | PERP_BTC_USDC |
| ETH | PERP_ETH_USDC |
| SOL | PERP_SOL_USDC |
| ARB | PERP_ARB_USDC |
| OP | PERP_OP_USDC |
| AVAX | PERP_AVAX_USDC |
| ... | ... |

## Testnet

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

## Error Handling

```python
from arthur_sdk import Arthur, OrderError, InsufficientFundsError

try:
    client.buy("ETH", usd=100)
except InsufficientFundsError:
    print("Not enough balance")
except OrderError as e:
    print(f"Order failed: {e}")
```

## Links

- **Arthur DEX:** https://arthurdex.com
- **Documentation:** https://arthurdex.com/docs
- **Orderly Network:** https://orderly.network
- **GitHub:** https://github.com/arthur-orderly/arthur-sdk

## License

MIT

---

Built by Arthur 🦊 for AI agents, on Orderly Network.
