README
¶
SpaceMolt Auto-Trader
Autonomous trading bot for SpaceMolt with multiple strategies, inter-empire trade routes, and intelligent market operations.
Overview
The auto-trader is a fully autonomous agent that performs trading operations across multiple strategies. It can execute simple sell-all operations, crafting-integrated trading, or complex inter-empire trade routes with return legs. The agent runs indefinitely until stopped, making it perfect for passive credit accumulation through trade.
Features
Core Functionality
- Automatic Station Operations - Travels to stations and executes cargo management
- Market-Aware Trading - Intelligent pricing based on market order books
- Captain's Log - Tracks mission progress and status across sessions
- Continuous Operation - Runs indefinitely, adapting to game state changes
Trading Strategies
Choose from multiple trading strategies:
- Sell (Default) - Sell all cargo immediately at station
- Craft + Sell - Craft items from resources, then sell everything
- Craft + Deposit - Craft items from resources, then deposit to storage
- Inter-Empire Trade - Execute profitable trade routes between empires
Inter-Empire Trade Routes
The trade strategy implements sophisticated inter-empire trading:
- Route Discovery - Auto-selects most profitable routes for your empire
- Round-Trip Trading - Executes outbound and return legs for maximum profit
- State Persistence - Survives crashes and resumes from last phase
- Smart Ship Selection - Switches to best cargo ship for maximum capacity
- Market Order Trading - Sells into buy orders at optimal prices
- Cargo Insurance - Automatically insures high-value cargo
Quick Start
Basic Usage
# Run with default strategy (sell all)
go run ./cmd/auto-trader trader-1
# Explicitly specify strategy
go run ./cmd/auto-trader trader-1 sell
# Craft items then sell
go run ./cmd/auto-trader trader-1 craft-sell
# Craft items then deposit to storage
go run ./cmd/auto-trader trader-1 craft-deposit
# Inter-empire trade (auto-select best route)
go run ./cmd/auto-trader trader-1 trade
# Inter-empire trade (explicit route)
go run ./cmd/auto-trader trader-1 trade ore_silicon crimson
Building
# Build the binary
go build -o bin/auto-trader ./cmd/auto-trader
# Run the built binary
./bin/auto-trader trader-1 sell
Strategies
Strategy: sell (Default)
Best for: Quick station operations, monitoring markets
Behavior:
- Travel to nearest station
- Dock at station
- Sell all cargo immediately
- Refuel and repair
- Repeat
Example:
go run ./cmd/auto-trader trader-1 sell
Strategy: craft-sell
Best for: Adding value to raw resources through crafting
Behavior:
- Travel to station with cargo
- Query crafting server for craftable recipes
- Craft items from available cargo resources
- Sell all crafted items and remaining raw materials
- Refuel and repair
- Repeat
Example:
go run ./cmd/auto-trader trader-1 craft-sell
Requirements: Crafting MCP server must be configured
Strategy: craft-deposit
Best for: Stockpiling crafted items for later use
Behavior:
- Travel to station with cargo
- Query crafting server for craftable recipes
- Craft items from available cargo resources
- Deposit all items to station storage
- Refuel and repair
- Repeat
Example:
go run ./cmd/auto-trader trader-1 craft-deposit
Requirements: Crafting MCP server must be configured
Strategy: trade
Best for: Maximum profit through inter-empire arbitrage
Behavior:
- Analyze available trade routes for your empire
- Buy ore at home empire (cheap)
- Travel to destination empire
- Sell ore at destination (expensive)
- Buy return cargo at destination
- Travel back to home empire
- Sell return cargo at home
- Repeat
Phases:
BUY_AT_HOME- Purchase outbound cargo at home stationTRAVEL_TO_SELL- Navigate to destination empireSELL_AT_DEST- Sell outbound cargo at destinationBUY_RETURN- Purchase return cargo at destinationTRAVEL_HOME- Navigate back to home systemSELL_AT_HOME- Sell return cargo and complete trip
Auto-Select Route:
go run ./cmd/auto-trader trader-1 trade
Explicit Route:
go run ./cmd/auto-trader trader-1 trade ore_silicon crimson
Crafting Integration
The auto-trader integrates with the SpaceMolt Crafting MCP Server for craft-sell and craft-deposit strategies. See the Quick Crafting Setup section below for setup instructions.
Quick Crafting Setup
# Build the crafting server
go build -o bin/crafting-server ./cmd/crafting-server
# Add to PATH
export PATH="$PATH:$(pwd)/bin"
# Verify installation
crafting-server -help
The auto-trader will automatically initialize crafting when using craft-sell or craft-deposit strategies.
Inter-Empire Trade Routes
Route Discovery
The agent automatically discovers profitable trade routes based on your empire:
- Federation - Trades with Crimson, Dynasty, Syndicate
- Crimson - Trades with Federation, Dynasty, Syndicate
- Dynasty - Trades with Federation, Crimson, Syndicate
- Syndicate - Trades with Federation, Crimson, Dynasty
Route Selection
When using trade strategy without specifying ore/target:
- Agent queries all available routes for your empire
- Routes are ranked by priority (profitability × volume)
- Highest priority route is selected
- Return leg is automatically calculated if available
Example output:
[trader-1] Available routes for federation:
[trader-1] 1. Silicon: Federation→Crimson (priority: 1, margin: 60 cr/unit)
[trader-1] 2. Iron: Federation→Dynasty (priority: 2, margin: 45 cr/unit)
[trader-1] 3. Copper: Federation→Syndicate (priority: 3, margin: 30 cr/unit)
[trader-1] Selected: Silicon: Federation→Crimson
State Persistence
Trade state is automatically persisted to data/agents/{agent-id}/trade_state.json:
{
"phase": "BUY_AT_HOME",
"outbound_route": {
"name": "Silicon: Federation→Crimson",
"ore_id": "ore_silicon",
"buy_empire": "federation",
"sell_empire": "crimson"
},
"trip_count": 5,
"total_profit": 15000.0,
"last_updated": "2026-02-23T15:30:00Z"
}
If the agent crashes, it will:
- Load previous trade state
- Validate route matches current configuration
- Resume from last phase
- Continue trading seamlessly
Smart Ship Switching
Before buying outbound cargo at home station, the agent:
- Lists all owned ships at current station
- Queries knowledge database for cargo capacities
- Switches to ship with highest cargo capacity
- Executes trade with maximum profit potential
Example output:
[trader-1] Checking 3 owned ships for better cargo capacity (current: 50.0)...
[trader-1] Found stored ship: Dart (dart) - cargo capacity: 5
[trader-1] Found stored ship: Hauler (hauler) - cargo capacity: 100
[trader-1] Switching to Hauler (hauler) - cargo capacity: 50.0 → 100.0
[trader-1] Now flying Hauler - cargo: 0/100
Market-Aware Selling
The agent implements intelligent market operations:
- Fetch Market Order Book - Get all buy/sell orders for each item
- Sell Into Buy Orders - Match orders at >= 80% of base value
- Create Sell Orders - List remaining units at 80% of base value
- Price Floor Protection - Never sell below 80% of item value
Example output:
[trader-1] Selling 100.0 x Silicon Ore (base value: 50 cr, min price: 40 cr)...
[trader-1] Selling 50.0 units into buy order at 45 cr/unit (total: 2250 cr)
[trader-1] Selling 50.0 units into buy order at 42 cr/unit (total: 2100 cr)
[trader-1] Sold! Sale revenue: 4350.0 cr | Total credits: 15435.0
Cargo Insurance
High-value cargo is automatically insured before travel:
- Threshold: 10,000 credits
- Coverage: 100 ticks
- Cost: Variable based on cargo value
Example output:
[trader-1] Cargo value 12000 cr exceeds threshold, purchasing insurance (100 ticks)...
[trader-1] Insurance purchased for 100 ticks
Captain's Log
The auto-trader maintains a captain's log that persists across sessions:
Location: data/agents/{agent-id}/captains_log_latest.json
Contents:
- Current mission goal
- Current location (system and POI)
- Trading runs completed
- Credits earned
- Ship status (hull, fuel, cargo)
- Trade phase and route (for
tradestrategy) - Last update timestamp
Architecture
Trading Loop
The auto-trader uses a simple monitoring loop for non-trade strategies:
TradingLoop:
For each run until stopped:
1. Find nearest station
2. Travel to station if not docked
3. Dock at station
4. Execute station action (sell/craft-sell/craft-deposit)
5. Refuel and repair
6. Update captain's log
7. Repeat
Trade Loop
The trade strategy uses a sophisticated phase-based loop:
TradeLoop:
Restore persisted state (if available)
For each tick:
Execute current phase:
BUY_AT_HOME → TRAVEL_TO_SELL → SELL_AT_DEST →
BUY_RETURN → TRAVEL_HOME → SELL_AT_HOME → BUY_AT_HOME
Save state to disk
Update captain's log
Station Actions
Station actions are implemented as strategies in pkg/game/mining.go:
StationActionSellAll()- Sell all cargoStationActionCraftAndSell()- Craft then sellStationActionCraftAndDeposit()- Craft then deposit
Configuration
Command-Line Arguments
Usage: auto-trader <agent-id> [strategy] [ore] [target-empire]
Arguments:
agent-id Agent identifier (e.g., trader-1, merchant-1)
strategy Station action strategy (optional, default: sell)
ore Ore ID for trade strategy (e.g., ore_silicon)
target-empire Target empire for trade strategy (e.g., crimson)
Strategies:
sell Sell all cargo immediately (default)
craft-sell Craft items from resources, then sell all
craft-deposit Craft items from resources, then deposit to storage
trade Inter-empire trade routes (auto-select or explicit)
Examples
# Sell everything (simplest)
go run ./cmd/auto-trader trader-1
# Craft then sell (add value)
go run ./cmd/auto-trader trader-1 craft-sell
# Auto-select best trade route
go run ./cmd/auto-trader trader-1 trade
# Explicit trade route
go run ./cmd/auto-trader trader-1 trade ore_silicon crimson
Examples
Example 1: Simple Sell Bot
# Start a basic trading bot that sells everything
go run ./cmd/auto-trader trader-1
Output:
[trader-1] Starting autonomous trading agent...
[trader-1] Agent: trader-1 | Empire: Federation | Credits: 1000.00 | Ship: Dart
[trader-1] Starting autonomous trading loop...
[trader-1] Station action strategy: sell
[trader-1] ═══ Trading Run #1 ═══
[trader-1] Credits: 1000.00 | Fuel: 100/100 | Hull: 100/100 | Cargo: 10/50
[trader-1] Docking at station...
[trader-1] Executing station action strategy: sell
[trader-1] 💰 Selling all cargo (10 items)...
[trader-1] ✅ Sold all cargo!
Example 2: Inter-Empire Trade Bot
# Start a bot that trades between empires
go run ./cmd/auto-trader trader-1 trade
Output:
[trader-1] Available routes for federation:
[trader-1] 1. Silicon: Federation→Crimson (priority: 1, margin: 60 cr/unit)
[trader-1] Selected: Silicon: Federation→Crimson
[trader-1] ═══ Trade Route: Silicon: Federation→Crimson ═══
[trader-1] Outbound: Buy ore_silicon at federation, sell at crimson (est. margin: 60 cr/unit)
[trader-1] Home: SOL | Dest: CRIMSON_01 | Starting phase: BUY_AT_HOME
[trader-1] Buying 100.0 x Silicon Ore at 40.0 cr/unit (total: 4000.0)...
[trader-1] Navigating to CRIMSON_01...
[trader-1] Selling 100.0 x Silicon Ore (base value: 50 cr, min price: 40 cr)...
[trader-1] Sold! Sale revenue: 6000.0 cr | Total credits: 7000.0
[trader-1] ═══ Trip 1 complete! Profit: 2000.0 cr (5000.0 → 7000.0) | Lifetime profit: 2000.0 cr ═══
Example 3: Explicit Trade Route
# Trade specific ore between specific empires
go run ./cmd/auto-trader trader-1 trade ore_iron dynasty
Output:
[trader-1] ═══ Trade Route: Iron: Federation→Dynasty ═══
[trader-1] Outbound: Buy ore_iron at federation, sell at dynasty (est. margin: 45 cr/unit)
[trader-1] Return: Buy ore_copper at dynasty, sell at federation (est. margin: 35 cr/unit)
[trader-1] Home: SOL | Dest: DYNASTY_01 | Starting phase: BUY_AT_HOME
Troubleshooting
Issue: "No trade routes available for empire"
Cause: No routes defined for your empire in the game configuration.
Solution:
- Check that your empire is supported (Federation, Crimson, Dynasty, Syndicate)
- Verify trade route data is loaded in
pkg/game/trade.go - Use
sell,craft-sell, orcraft-depositstrategy instead
Issue: "Crafting not configured, skipping to deposit"
Cause: The crafting MCP server is not available in PATH.
Solution:
- Build the crafting server:
go build -o bin/crafting-server ./cmd/crafting-server - Add to PATH:
export PATH="$PATH:$(pwd)/bin" - Verify:
crafting-server -help
Note: This is not an error - the agent will gracefully continue without crafting.
Issue: "Failed to load trade state"
Cause: Corrupted or incompatible trade state file.
Solution:
- Remove the trade state file:
rm data/agents/{agent-id}/trade_state.json - Restart the agent - it will start fresh
- Trade state will be recalculated on next run
Issue: Agent stops unexpectedly
Cause: Various reasons (connection loss, error, etc.)
Solution:
- Check the captain's log for last status:
cat data/agents/{agent-id}/captains_log_latest.json - Restart the agent - it will resume from where it left off (for
tradestrategy) - Check logs for specific error messages
Performance
Typical Performance
- Trading Cycle Time: 1-3 minutes (depends on distance to station)
- Trade Route Time: 10-20 minutes (depends on route length)
- Market Query Time: 1-2 seconds
- Credits per Hour: Varies based on strategy and market conditions
Strategy Comparison
| Strategy | Speed | Profit | Complexity | Best For |
|---|---|---|---|---|
sell |
⭐⭐⭐ | ⭐⭐ | Simple | Quick operations |
craft-sell |
⭐⭐ | ⭐⭐⭐ | Moderate | Adding value |
craft-deposit |
⭐⭐ | ⭐⭐⭐⭐ | Moderate | Stockpiling |
trade |
⭐ | ⭐⭐⭐⭐⭐ | Complex | Maximum profit |
Related Documentation
- Crafting Server README - Full crafting server documentation
- Game Client Documentation - Game client API reference
- Trade Routes - Trade route implementation
License
Part of the SpaceMolt project.
Documentation
¶
There is no documentation for this package.