3rd Place, IHEC CodeLab 2.0
FAIN, AI Trading Ecosystem

- Role
- Co-Architect, Distributed Systems & AI
- Timeline
- February 2026
- Duration
- Hackathon sprint
- Team
- Team: Makarouna Kadheba
Overview
FAIN is a trading terminal for the Tunis stock exchange, built at the IHEC CodeLab hackathon. It tracks all 82 listed companies live, forecasts short-term prices, builds a portfolio and explains its choices, reads the mood from Tunisian financial news, and answers questions about market rules through a chatbot.
The problem
The Tunis market has almost no modern tooling. Investors work off fragmented, manual processes with no real quantitative help, and nothing brings live prices, forecasting, explained portfolio advice, and local sentiment together in one place.
What I built
- 01
One gateway, many services
A FastAPI gateway routes to a dozen small services, each doing one job: prices, market data, forecasting, anomaly detection, sentiment, portfolio, notifications, and more.
- 02
Short-term price forecasts
An XGBoost model trains on the fly for each request, using more than 30 technical indicators, and returns a five-day forecast with confidence bands that widen the further out it looks.
- 03
A portfolio that explains itself
A reinforcement-learning agent allocates the portfolio using live macro data from the World Bank, the IMF, and the Tunisian central bank. SHAP then shows which factors drove each choice, and an LLM writes the reasoning up in plain French.
- 04
Market mood from local news
It scrapes Tunisian financial outlets and scores sentiment per company, with live social-media checks layered on top.
- 05
A chatbot for the rulebook
A retrieval chatbot answers questions over the official exchange and regulator documents, keeping context across a conversation.
Architecture
A Next.js frontend talks only to a FastAPI gateway. The gateway reads prices straight from Postgres and proxies everything else to the downstream services: forecasting, anomaly detection, sentiment, portfolio, auth, and notifications. A separate jobs service runs the scheduled work, like the market pulse and the daily report.
Tech stack
FastAPI (API Gateway)
Central gateway on :8000, direct DB reads for stocks/history + HTTP proxy to 6 downstream services
NestJS (Core Backend)
JWT auth, market data, and stock management on :8006 with its own PostgreSQL schema
XGBoost + RandomForest
On-the-fly 5-day price forecasting (30+ features: RSI, MACD, Bollinger, candlestick) + liquidity classifier
Stable-Baselines3 PPO
RL portfolio optimization in Gymnasium env; reward = Sharpe − drawdown penalty
SHAP + OpenRouter LLM
KernelExplainer surfaces macro/price feature importance; LLM writes plain-French investment rationale
Gemini Flash Lite
Classifies article sentiment per ticker from BeautifulSoup-scraped Tunisian financial news
Perplexity Sonar
Real-time social media search for live ticker sentiment from online discussions
Llama 3.3 70B + ChromaDB
RAG chatbot via OpenRouter; ChromaDB stores BVMT/CMF docs with all-MiniLM-L6-v2 embeddings
PostgreSQL + Docker Compose
Shared Neon PostgreSQL persistence layer; single docker compose up launches all 12 services
Results
3rd Place
IHEC CodeLab 2.0 Hackathon, February 2026
12 Microservices
FastAPI Gateway + 6 proxied services + stock/market/jobs services on shared DB
82 Stocks
All BVMT-listed equities tracked with 15-min market pulse refresh
30+ ML Features
RSI, MACD, EMA, Bollinger Bands, volume ratios powering XGBoost on-the-fly training
PPO RL Agent
Sharpe-optimized portfolio across 8 BVMT bank stocks using real World Bank/IMF/BCT macro data
3 LLMs Integrated
Gemini Flash Lite (sentiment), Perplexity Sonar (social), Llama 3.3 70B (RAG chatbot)
Moments


What I took away
- 01
Choosing XGBoost over LSTM for forecasting was the right hackathon call, on-the-fly training per request with early stopping converges in seconds, while LSTM fine-tuning per stock would take hours. Directional accuracy on thin BVMT data was comparable.
- 02
Using PPO Reinforcement Learning for portfolio allocation instead of classical Markowitz lets the agent learn non-linear risk/return tradeoffs and directly incorporate macro signals (BCT policy rate, IMF debt/GDP) into the reward function, something mean-variance optimization cannot do.
- 03
The two-layer sentiment approach (Gemini Flash Lite for scraped archives + Perplexity Sonar for real-time social) proved essential: static scrapers lag by hours on breaking news, while Sonar returns live discussion threads within seconds.
- 04
SHAP on top of the PPO agent's feature set was the single biggest trust-builder in the demo, once judges could see that 'BCT policy rate' and 'BIAT volatility' were the top drivers of a recommendation, skepticism about the RL black box evaporated instantly.
- 05
The API Gateway pattern was the most important architectural decision for team velocity, each engineer owned one service on its own port, and the gateway unified everything behind a single URL without ever touching the frontend routing code.