39 - Strategy Module

Overview

The Strategy Module provides a webhook-based system for receiving trading signals from external platforms (TradingView, Amibroker, ChartInk) and executing orders through OpenAlgo. It features time-based controls, symbol mappings, automatic square-off, and rate-limited order queuing.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                        Strategy Module Architecture                          │
└──────────────────────────────────────────────────────────────────────────────┘

┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐
│   TradingView   │  │   Amibroker     │  │    ChartInk     │
│   Webhook       │  │   Webhook       │  │    Webhook      │
└────────┬────────┘  └────────┬────────┘  └────────┬────────┘
         │                    │                    │
         └────────────────────┼────────────────────┘


┌──────────────────────────────────────────────────────────────────────────────┐
│                      Strategy Webhook Endpoint                               │
│                      POST /strategy/webhook/<webhook_id>                     │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  1. Rate Limiting (100/min for webhooks)                             │   │
│  │  2. Validate webhook_id → Get strategy                               │   │
│  │  3. Check strategy enabled & time window                             │   │
│  │  4. Parse signal (action, symbol, quantity)                          │   │
│  │  5. Apply symbol mapping overrides                                   │   │
│  │  6. Queue order for execution                                        │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
└──────────────────────────────────────────────────────────────────────────────┘


┌──────────────────────────────────────────────────────────────────────────────┐
│                         Order Queueing System                                │
│                                                                              │
│  ┌──────────────────────┐        ┌──────────────────────┐                   │
│  │   Regular Queue      │        │   Smart Order Queue  │                   │
│  │   (placeorder)       │        │   (placesmartorder)  │                   │
│  │                      │        │                      │                   │
│  │   Rate: 10/sec       │        │   Rate: 1/sec        │                   │
│  │   (ORDER_RATE_LIMIT) │        │   (SMART_ORDER_RATE) │                   │
│  └──────────┬───────────┘        └──────────┬───────────┘                   │
│             │                               │                               │
│             └───────────────┬───────────────┘                               │
│                             │                                               │
│                             ▼                                               │
│                    ┌────────────────┐                                       │
│                    │ Order Processor │                                       │
│                    │ (Background)    │                                       │
│                    └────────┬───────┘                                       │
│                             │                                               │
└─────────────────────────────┼───────────────────────────────────────────────┘


                    ┌────────────────┐
                    │ REST API       │
                    │ /api/v1/...    │
                    └────────────────┘

Strategy Configuration

Database Schema

Location: database/strategy_db.py

Time Validation

Webhook Signal Format

TradingView Format

Amibroker Format

Supported Actions

Action
Description

BUY

Long entry / Short exit

SELL

Long exit / Short entry

Symbol Mapping

Allows mapping external symbols to OpenAlgo format:

Order Queuing System

Dual Queue Architecture

Rate Limiting

Order Type
Rate Limit
Queue

Regular Order

10/second

regular_order_queue

Smart Order

1/second

smart_order_queue

Automatic Square-Off

APScheduler Integration

Square-Off Logic

API Endpoints

Endpoint
Method
Description

/strategy/

GET

List all strategies

/strategy/new

GET/POST

Create new strategy

/strategy/<id>

GET

View strategy details

/strategy/<id>/edit

GET/POST

Edit strategy

/strategy/<id>/delete

POST

Delete strategy

/strategy/<id>/toggle

POST

Enable/disable strategy

/strategy/<id>/symbols

GET/POST

Manage symbol mappings

/strategy/webhook/<webhook_id>

POST

Receive trading signal

Trading Modes

Mode
Allowed Actions
Use Case

LONG

BUY only

Long-only strategies

SHORT

SELL only

Short-only strategies

BOTH

BUY and SELL

Bidirectional trading

Strategy Time Window

Configuration

Environment Variables

Key Files Reference

File
Purpose

blueprints/strategy.py

Strategy blueprint and webhook handler

database/strategy_db.py

Strategy database models

templates/strategy/

Strategy UI templates

frontend/src/pages/Strategy.tsx

React strategy management

Last updated