36 - Rate Limiting Guide

Overview

OpenAlgo uses Flask-Limiter with a moving-window strategy to protect endpoints from abuse. Different rate limits apply to different endpoint categories based on their sensitivity and resource usage.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                        Rate Limiting Architecture                            │
└──────────────────────────────────────────────────────────────────────────────┘

                           Incoming Request


┌──────────────────────────────────────────────────────────────────────────────┐
│                         Flask-Limiter                                         │
│                                                                               │
│  ┌─────────────────────────────────────────────────────────────────────────┐ │
│  │                      Configuration                                       │ │
│  │  key_func = get_remote_address   (Rate limit by IP)                     │ │
│  │  storage_uri = "memory://"       (In-memory storage)                    │ │
│  │  strategy = "moving-window"      (Sliding window algorithm)             │ │
│  └─────────────────────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────────────────┘


┌──────────────────────────────────────────────────────────────────────────────┐
│                    Endpoint Category Detection                               │
│                                                                               │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐           │
│  │   Login     │ │   API       │ │   Order     │ │  Webhook    │           │
│  │ Endpoints   │ │ Endpoints   │ │ Endpoints   │ │ Endpoints   │           │
│  └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘           │
│         │               │               │               │                   │
│         ▼               ▼               ▼               ▼                   │
│  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐           │
│  │ 5/min       │ │ 50/sec      │ │ 10/sec      │ │ 100/min     │           │
│  │ 25/hour     │ │             │ │             │ │             │           │
│  └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘           │
└──────────────────────────────────────────────────────────────────────────────┘

                    ┌─────────────┴─────────────┐
                    │                           │
              Under Limit                  Over Limit
                    │                           │
                    ▼                           ▼
           ┌───────────────┐          ┌───────────────┐
           │   Process     │          │   429 Error   │
           │   Request     │          │ Too Many Reqs │
           └───────────────┘          └───────────────┘

Rate Limit Categories

Environment Variables

Limit Breakdown

Category
Rate Limit
Endpoints
Purpose

Login

5/min, 25/hr

/auth/login, /auth/reset-password

Prevent brute force

API

50/sec

/api/v1/quotes, /api/v1/positions, etc.

General data access

Order

10/sec

/api/v1/placeorder, /api/v1/modifyorder, /api/v1/cancelorder

Trading rate control

Smart Order

2/sec

/api/v1/placesmartorder

Prevent automated abuse

Webhook

100/min

/chartink/webhook, /strategy/webhook

External integrations

Strategy

200/min

Strategy-related operations

Strategy execution

Implementation

Limiter Initialization

Location: limiter.py

Applying Rate Limits

Login Endpoint Example:

Order Endpoint Example:

API Endpoint Example:

Rate Limit Format

Valid Timeunits

Timeunit
Alias

second

s

minute

m

hour

h

day

d

Examples

Error Handling

429 Response Handler

Location: app.py

Client-Side Handling

Endpoint Limits Map

REST API Endpoints

Endpoint
Rate Limit Variable
Default

/api/v1/placeorder

ORDER_RATE_LIMIT

10/sec

/api/v1/modifyorder

ORDER_RATE_LIMIT

10/sec

/api/v1/cancelorder

ORDER_RATE_LIMIT

10/sec

/api/v1/cancelallorder

API_RATE_LIMIT

50/sec

/api/v1/placesmartorder

SMART_ORDER_RATE_LIMIT

2/sec

/api/v1/quotes

API_RATE_LIMIT

50/sec

/api/v1/multiquotes

API_RATE_LIMIT

50/sec

/api/v1/positions

API_RATE_LIMIT

50/sec

/api/v1/orderbook

API_RATE_LIMIT

50/sec

/api/v1/tradebook

API_RATE_LIMIT

50/sec

/api/v1/holdings

API_RATE_LIMIT

50/sec

/api/v1/funds

API_RATE_LIMIT

50/sec

/api/v1/history

API_RATE_LIMIT

50/sec

/api/v1/depth

API_RATE_LIMIT

50/sec

/api/v1/ping

API_RATE_LIMIT

50/sec

/api/v1/intervals

API_RATE_LIMIT

50/sec

/api/v1/options/multiorder

ORDER_RATE_LIMIT

10/sec

Authentication Endpoints

Endpoint
Rate Limit Variable
Default

/auth/login

LOGIN_RATE_LIMIT_MIN + HOUR

5/min, 25/hr

/auth/reset-password

LOGIN_RATE_LIMIT_HOUR

25/hr

/<broker>/callback

LOGIN_RATE_LIMIT_MIN + HOUR

5/min, 25/hr

Webhook Endpoints

Endpoint
Rate Limit Variable
Default

/chartink/webhook

WEBHOOK_RATE_LIMIT

100/min

/strategy/webhook

STRATEGY_RATE_LIMIT

200/min

/flow/trigger/*

WEBHOOK_RATE_LIMIT

100/min

Moving Window Strategy

Algorithm Benefits

Aspect
Moving Window
Fixed Window

Accuracy

Higher

Lower

Burst protection

Better

Prone to bursts at boundaries

Memory

Slightly higher

Lower

Implementation

More complex

Simpler

Configuration Validation

Location: utils/env_check.py

Tuning Recommendations

For High-Frequency Trading

For Webhook-Heavy Usage

For Multi-User Deployments

Consider using Redis for distributed rate limiting:

Key Files Reference

File
Purpose

limiter.py

Flask-Limiter initialization

utils/env_check.py

Rate limit validation

restx_api/*.py

API endpoint rate limits

blueprints/auth.py

Login rate limits

blueprints/chartink.py

Webhook rate limits

blueprints/strategy.py

Strategy rate limits

app.py

429 error handler

Last updated