Skip to content

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Telegram Paper Trading Bot (Postgres + yfinance)

A professional, user-friendly paper trading bot that runs in Telegram with interactive features, real-time market data, and a competitive leaderboard system.

Features

๐ŸŽฏ Core Trading

  • /start creates your account with โ‚น50,000 starting cash
  • /help comprehensive guide with command examples
  • /search <QUERY> enhanced search with full stock details, live price, charts, and quick buy buttons
  • /chart <TICKER> [PERIOD] sends interactive price chart images
  • /buy <TICKER> <SHARES> buys shares at current live price
  • /sell <TICKER> <SHARES> sells shares at current live price (FIFO)
  • /portfolio shows cash, live stock value, net worth, P/L% with refresh & leaderboard buttons
  • /leaderboard view top 10 traders ranked by net worth with medals ๐Ÿฅ‡๐Ÿฅˆ๐Ÿฅ‰

๐Ÿ’ณ Money Transfer

  • /send <@username or USER_ID> <AMOUNT> send money to other users
  • /donate <@username or USER_ID> <AMOUNT> donate money (admins have unlimited funds)

๐ŸŽฎ Interactive Features

  • Quick Buy Buttons: Buy 1, 5, or 10 shares directly from search results
  • Chart Period Buttons: Switch between 1M and 3M charts with one tap
  • Refresh Portfolio: Update live prices instantly with a button
  • Leaderboard Access: View rankings from portfolio with one tap
  • Professional UI: Emoji-rich, well-organized displays with visual separators

๐Ÿ‘‘ Admin & Owner System

  • Hardcoded owner (1350561286) with supreme authority
  • /promote and /demote for admin management (owner only)
  • /grant, /addcash, /ban, /unban (admin commands)
  • Owner can ban/demote any user including admins
  • Admins have unlimited funds for donations

Requirements

  • Python 3.10+ (works on Python 3.13)
  • A Telegram bot token from BotFather
  • PostgreSQL database (local Docker Postgres or Railway Postgres)

Project Files

  • main.py โ€” Telegram bot
  • schema.sql โ€” PostgreSQL tables
  • requirements.txt โ€” dependencies
  • .env โ€” local env vars (ignored by git)

Local Setup (recommended with Docker)

1) Start PostgreSQL in Docker

From the project folder:

docker run --name paper-pg -e POSTGRES_PASSWORD=password -e POSTGRES_DB=trading_bot -p 5432:5432 -d postgres:15

Confirm it is running:

docker ps

2) Create tables (apply schema)

docker cp schema.sql paper-pg:/tmp/schema.sql

docker exec -it paper-pg psql -U postgres -d trading_bot -f /tmp/schema.sql

docker exec -it paper-pg psql -U postgres -d trading_bot -c "\\dt"

You should see users and portfolio tables.

3) Install Python dependencies

python -m pip install -r requirements.txt

4) Set environment variables

This bot reads environment variables using os.getenv. For local testing, set them in your terminal session.

PowerShell (Windows):

$env:TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
$env:DATABASE_URL = "postgresql://postgres:password@localhost:5432/trading_bot"

5) Run the bot

python main.py

Open Telegram and message your bot.

How to Use (Telegram Commands)

๐Ÿ” /search <QUERY>

Enhanced stock search with full information display:

  • Live price with change % (๐Ÿ“ˆ green for up, ๐Ÿ“‰ red for down)
  • Previous close, day range, 52-week range
  • Volume and market cap
  • Quick buy buttons (1, 5, 10 shares)
  • Chart period buttons (1M, 3M)
  • Lists additional results if multiple matches

Examples:

/search reliance
/search tata motors
/search tesla

๐Ÿ“ˆ /chart <TICKER> [PERIOD]

Sends interactive chart image of closing prices.

Periods: 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, max

Examples:

/chart TCS.NS 3mo
/chart AAPL 1y
/chart BTC-USD 6mo

๐Ÿ“ฑ /portfolio

View your complete portfolio with:

  • Cash balance
  • Live stock values
  • Net worth & total P/L%
  • Detailed P/L for each holding
  • ๐Ÿ”„ Refresh button to update live prices
  • ๐Ÿ“Š Leaderboard button to see rankings

๐Ÿ† /leaderboard

View top 10 traders ranked by net worth:

  • ๐Ÿฅ‡๐Ÿฅˆ๐Ÿฅ‰ Medal badges for top 3
  • Net worth and P/L% for each trader
  • Your position highlighted with โžค
  • Motivational message to climb ranks

๐Ÿ’ธ /buy <TICKER> <SHARES>

Buy stocks at current market price.

Examples:

/buy AAPL 2
/buy RELIANCE.NS 10
/buy BTC-USD 0.5

Alternative: Use quick buy buttons from /search results!

๐Ÿ’ต /sell <TICKER> <SHARES>

Sell stocks at current market price (FIFO).

Examples:

/sell TCS.NS 5
/sell TSLA 1

๐Ÿ’Œ /send <@username or USER_ID> <AMOUNT>

Send cash to another user.

Examples:

/send @friend 1000
/send 123456789 500

Important:

  • Type @ and select recipient from Telegram's autocomplete
  • Or use numeric user ID
  • Plain text @username won't work (Telegram Bot API limitation)

๐ŸŽ /donate <@username or USER_ID> <AMOUNT>

Donate money to another user.

  • Normal users: Deducts from your balance (same as /send)
  • Admin users: Credits target without deducting (unlimited funds)

โ“ /help

Comprehensive guide showing:

  • All commands with examples
  • Supported markets (US, NSE, BSE, Crypto, Forex, Commodities)
  • Ticker format examples
  • Admin/owner commands (if applicable)

๐Ÿ‘‘ Admin Commands

Available to admin users and owner (user ID 1350561286):

  • /grant <USER_ID> <AMOUNT> โ€” Add funds to any user
  • /addcash <AMOUNT> โ€” Add funds to yourself
  • /ban <USER_ID> [REASON] โ€” Ban a user from the bot
  • /unban <USER_ID> โ€” Unban a user

โญ Owner Commands

Exclusive to owner (user ID 1350561286):

  • /promote <USER_ID> โ€” Grant admin rights to a user
  • /demote <USER_ID> โ€” Revoke admin rights (even from admins)

Owner privileges:

  • Can ban/demote any user including admins
  • Supreme authority over all bot operations

More ticker examples:

  • US: AAPL, TSLA, MSFT
  • NSE (India): RELIANCE.NS, TCS.NS, INFY.NS
  • BSE (India): RELIANCE.BO, TCS.BO
  • Indices: ^NSEI (NIFTY 50), ^NSEBANK (NIFTY Bank), ^GSPC (S&P 500)
  • Crypto: BTC-USD, ETH-USD

What it does:

  • Fetches current live price from yfinance
  • Checks if you have enough cash
  • Deducts cash from users.balance
  • Inserts a new lot into portfolio with (ticker, shares, buy_price)

/sell <TICKER> <SHARES>

Example:

/sell AAPL 1

NSE sell example:

/sell RELIANCE.NS 1

What it does:

  • Fetches current live price from yfinance
  • Ensures you have enough shares
  • Removes shares from your stored lots (FIFO)
  • Adds proceeds back to users.balance

/portfolio

Shows:

  • Cash balance
  • Each ticker: shares, avg cost, live price, live value, unrealized P/L
  • Net worth = cash + total live stock value
  • Total P/L% = (net worth โˆ’ 10000) / 10000 * 100

Notes / Common Issues

  • Tables missing (relation users does not exist): Run the schema step again.
  • Local Postgres SSL error: Local Docker Postgres doesnโ€™t use SSL by default; the code auto-disables SSL when host is localhost.
  • Market data: yfinance is best-effort. Some tickers/markets can be delayed or unavailable.

Deploy on Railway

  1. Create a Railway project
  2. Add a PostgreSQL database
  3. Set variables:
    • TELEGRAM_BOT_TOKEN
    • DATABASE_URL (Railway provides this)
  4. Run schema.sql once on the Railway database
  5. Deploy the repo (Procfile runs python main.py)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages