Skip to content

danishdynamic/algoallocationbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

27 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

FastAPI React Docker PostgreSQL License: MIT

๐Ÿ’ฐ Algo Asset Allocation Bot

End-to-End Asset Allocation Platform with FastAPI + React (TypeScript)

This project is a full-stack quantitative asset allocation system that combines:

  • ๐Ÿ“ˆ Python-based portfolio & momentum backtesting
  • โš™๏ธ FastAPI backend API
  • ๐Ÿ–ฅ๏ธ React + TypeScript frontend
  • ๐Ÿšฆ Rate-limited endpoints
  • ๐Ÿ“ค CSV export for results
  • ๐Ÿ“Š Extensible architecture for future models

The system allows users to run asset allocation strategies via a REST API and visualize results through a modern web UI.


๐Ÿง  Core Concepts

  • Momentum-based asset allocation
  • Moving-average crossover strategies
  • Portfolio performance metrics (Sharpe, volatility)
  • Separation of quant logic from API and UI

โœจ Features

๐Ÿ”ง Backend (FastAPI)

  • REST API for running allocation/backtests
  • Modular Python architecture
  • Rate-limited endpoints (API abuse protection)
  • CSV export of transactions & portfolio results
  • Clean request/response schemas (Pydantic)

Database integration (PostgreSQL) with docker compose

๐Ÿ“Š Database SchemaThe application uses PostgreSQL to persist backtest results. The schema is automatically managed by SQLAlchemy.

๐Ÿ“Š Database Records Example

id symbol sharpe volatility final_value created_at
1 GOOGL 2.37 0.251 $178,033.04 2026-01-07 10:00:00
2 NVDA 1.04 0.228 $125,781.71 2026-01-07 10:05:30
3 MSFT 0.56 0.120 $108,203.76 2026-01-07 10:10:15

Getting Started

  1. Environment SetupCreate a .env file in the root directory.

  2. Code snippetDATABASE_URL

postgresql://postgres:postgres@db:5432/postgres
  1. Run with DockerThis will launch the Postgres database, the FastAPI backend, and the React frontend simultaneously:
docker-compose up --build 

๐Ÿ–ฅ๏ธ Frontend (React + TypeScript)

  • Modern Vite-based React app
  • Component-based UI (Header, Footer, Pages)
  • API service abstraction
  • Extensible for charts & dashboards
  • Clean separation of concerns
  • Recharts to visualize backtest

๐Ÿ“Š Quant Engine

  • Historical price data via yfinance

  • Momentum strategy with:

    • Short/long moving averages
    • Market regime filter (ACWI)
  • Backtest engine with:

    • Transaction tracking
    • Sharpe ratio
    • Volatility
  • CSV export for offline analysis


๐Ÿ—๏ธ Project Structure

algoallocationbot/
โ”‚
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ main.py            # FastAPI entry point
โ”‚   โ”‚   โ”œโ”€โ”€ assetbot.py        # Core backtest & allocation logic
โ”‚   โ”‚   โ”œโ”€โ”€ schemas.py         # Request/response models
โ”‚   โ”‚   โ”œโ”€โ”€ rate_limit.py      # API rate limiting
|   |   |โ”€โ”€ database.py        # database
|   |   |โ”€โ”€ models.py          # for database models 
โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ requirements.txt
โ”‚
โ”œโ”€โ”€ frontend/
โ”‚   โ””โ”€โ”€ asset-ui/
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ”œโ”€โ”€ components/    # Header, Footer, UI components
โ”‚       โ”‚   โ”œโ”€โ”€ pages/         # Home, future views
โ”‚       โ”‚   โ”œโ”€โ”€ services/      # API calls
โ”‚       โ”‚   โ”œโ”€โ”€ App.tsx
โ”‚       โ”‚   โ””โ”€โ”€ main.tsx
โ”‚       โ””โ”€โ”€ package.json
โ”‚
โ”œโ”€โ”€ README.md
|โ”€โ”€ docker-compose.yml         #postgres database in docker
โ””โ”€โ”€ .gitignore

๐Ÿ› ๏ธ Tech Stack

Backend

  • Python 3.9+
  • FastAPI
  • Pydantic
  • yfinance
  • pandas / numpy
  • Uvicorn

Frontend

  • React
  • TypeScript
  • Vite
  • Axios / Fetch API

Data

  • Yahoo Finance (market data)
  • CSV exports
  • PostgreSQL

๐Ÿš€ Getting Started

1๏ธโƒฃ Clone the Repository

git clone https://github.com/your-username/algoallocationbot.git
cd algoallocationbot

โš™๏ธ Backend Setup (FastAPI)

cd backend
python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install -r requirements.txt

Start the API:

uvicorn app.main:app --reload

API docs available at:

http://localhost:8000/docs

๐Ÿ–ฅ๏ธ Frontend Setup (React + TypeScript)

cd frontend/asset-ui
npm install
npm run dev

Frontend runs at:

http://localhost:5173

๐Ÿ“ˆ Charts Overview

This project uses Recharts to visualize historical stock prices and backtest results.Recharts is a React-based charting library built on SVG. It is lightweight, composable, and works well with responsive dashboards.

<ResponsiveContainer width="100%" aspect={3}>
  <LineChart data={data}>
    <Line dataKey="price" />
  </LineChart>
</ResponsiveContainer>

๐Ÿ”Œ API Overview

POST /allocate

Runs an asset allocation/backtest.

Request

{
  "symbol": "AAPL",
  "initial_money": 100000
}

Response

{
  "symbol": "AAPL",
  "sharpe": 1.42,
  "volatility": 0.18,
  "final_account_value": 132450,
  "transactions": {...}
}

๐Ÿ“ค CSV Export

  • Portfolio transactions can be exported as CSV

  • Useful for:

    • Excel analysis
    • Research notebooks
    • Auditing strategies

๐Ÿšฆ Rate Limiting

  • API endpoints are rate-limited
  • Prevents abuse and accidental overload
  • Ready for production hardening

๐Ÿงช Development Workflow

  • Feature branches (feature/ui-fastapi-react)

  • Pull Requests with code reviews

  • Clean separation between:

    • Quant logic
    • API layer
    • UI layer

๐Ÿงฉ Future Enhancements

๐Ÿ”ฎ Backend

  • PostgreSQL integration
  • Multi-asset portfolios
  • User authentication
  • Async backtests
  • Caching (Redis)

๐Ÿ“Š Frontend

  • Strategy parameter controls

๐Ÿ“ˆ Quant Models

  • Risk parity
  • CVaR optimization
  • Blackโ€“Litterman
  • Reinforcement learning allocation

๐Ÿค Contributions

Contributions are welcome!

  1. Fork the repo
  2. Create a feature branch
  3. Commit with clear messages
  4. Open a Pull Request with context

๐Ÿ“œ License

MIT License โ€” free to use, modify, and distribute.


If you want, next I can:

  • โœจ Add badges (FastAPI, React, License)
  • ๐Ÿ“Š Add architecture diagram
  • ๐Ÿงช Add example API calls
  • ๐Ÿ“ Shorten it for recruiters
  • ๐Ÿง  Make a technical deep-dive README