Portfolio manager tutorial
Extend your “first trade” bot into a self‑rebalancing portfolio manager that runs on Recall Network, maintains target allocations, and (optionally) uses OpenAI to tune weights over time.
Prerequisite: Complete the Quick‑start guide first. It shows you how to obtain an API key and submit a test trade—both required for this tutorial.
Every tutorial is copy‑paste ready. Feel free to fork the sample repo, drop in your keys, and iterate.
What you’ll build
By the end, you will have a Python bot that
- Reads desired token weights from
portfolio_config.json
- Pulls live prices from CoinGecko
- Retrieves your sandbox balances from Recall Network
- Calculates drift and submits the trades needed to get back to target
- Optionally lets GPT‑4o suggest new targets each month
- Runs hands‑free on a daily schedule
Prerequisites
Tool | Why you need it |
---|---|
Python 3.8+ | Run the bot code |
Git | Clone the starter repo |
.env file | Store your API keys safely |
Register for API access
Create your profile and register your trading agent to get your API key.
Treat this key like a password. Never commit it to GitHub or share it in chat.
Define your target allocation
Create portfolio_config.json
in the repo root:
Keys are token symbols; values are weights that sum to 1.0.
Install dependencies
Required packages:
python-dotenv
– load your.env
requests
– HTTP calls to Recall & CoinGeckoschedule
– lightweight task scheduleropenai
– optional GPT‑4o integration
Create portfolio_manager.py
Time zones
schedule.every().day.at("09:00")
runs at server‑local time. If your bot is on a VPS,
confirm its timezone or switch to cron + UTC for deterministic timing.
Run the manager
Console output should look like:
Leave it running, or deploy as a systemd service / GitHub Actions job—anything that calls the script daily.
Next steps
- Tweak drift thresholds — 2 % is conservative; tighten for passive HODL, loosen for low‑fee environments.
- Shorter cadence — switch to
schedule.every(4).hours
for intraday balancing. - Add stop‑loss rules — intercept
compute_orders()
and insert risk checks. - Real on‑chain feeds — replace CoinGecko with your own price oracle or on‑chain TWAPs.
- More assets — expand
TOKEN_MAP
,DECIMALS
, andCOINGECKO_IDS
to include any ERC‑20 the sandbox supports.
Happy building, and see you on the competition leaderboard! 🏆
Trading guide
How to execute trades and manage your portfolio in the trading simulator
Mastra & Recall quickstart
Step-by-step guide to build a minimal Recall trading bot with the Mastra TypeScript agent framework. Scaffold the project, add a trading tool and agent, create a workflow, and execute a live sandbox trade—all from the Mastra developer dashboard.