Skip to content

limnit/Verges

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verges

Versatile Enterprise Risk Gateway Entry System for equities, options and futures, FIX based, pre-trade

VERGES (Verified Enterprise Risk Gateway Entry System)

QuickFIX-Based Risk Management System

A scalable, extensible, and cloud-ready QuickFIX-based risk management system built in Python. Designed to handle equities, options, and futures trading with robust pre-trade and post-trade risk checks. The system integrates with PostgreSQL for data persistence and Polygon.io for market data.

Named after a character in "Much Ado About Nothing" by William Shakespeare, Verges always appears alongside his boss Dogberry. They are both constables who lead the night watch. They are well-intentioned but bumbling and they often make mistakes.

Table of Contents

Features

  • Multi-Asset Support: Handles equities, options, and futures trading.
  • Risk Management: Implements pre-trade risk checks for margin requirements, notional limits, volume limits, and more.
  • Extensible Plugin Architecture: Easily add new risk checks and features using a plugin system.
  • Real-Time Market Data: Integrates with Polygon.io for up-to-date market prices.
  • Database Integration: Uses PostgreSQL for data storage and retrieval.
  • Scalable and Cloud-Ready: Designed to run efficiently in the cloud using Docker.
  • User Management: Supports user authentication and authorization with role-based access control.
  • Audit Logging: Tracks changes and actions for compliance and auditing.

System Architecture

System Architecture Diagram

Note: Include a system architecture diagram in the docs directory.

The system consists of the following key components:

  • FIX Engine Module: Manages FIX protocol sessions using QuickFIX for Python.
  • Risk Management Module: Performs risk checks before orders are accepted.
  • Order Management System (OMS): Tracks orders, executions, and positions.
  • Market Data Module: Fetches real-time market data from Polygon.io.
  • Data Management Module: Handles data persistence using PostgreSQL.
  • Configuration Management: Manages system configurations dynamically.
  • Plugin System: Allows dynamic loading and reloading of risk management plugins.

Getting Started

Prerequisites

  • Python 3.8 or higher
  • PostgreSQL 13 or higher
  • Docker (optional, for containerization)
  • Polygon.io API Key: Sign up at Polygon.io to get a free API key.

Installation

  1. Clone the Repository

    git clone https://github.com/yourusername/trading_system.git
    cd trading_system
    
  2. Create a Virtual Environment

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    
  3. Install Dependencies

    pip install -r requirements.txt
    
  4. Set Up PostgreSQL Database

    • Create the Database: Create a PostgreSQL database named trading_system.

    • Update Database Configuration: Edit the config/database.ini file with your database credentials.

      # config/database.ini
      [postgresql]
      host=localhost
      port=5432
      database=trading_system
      user=your_username
      password=your_password
  5. Initialize the Database

  • Run the initialization script to create tables and set up the database schema.

    python scripts/initialize_db.py
    
  1. Configure the Application

    • Update Configurations: Edit the config/config.yml file to update application settings.

    • Set API Key: Replace YOUR_POLYGON_IO_API_KEY with your actual Polygon.io API key.

      # config/config.yml
      market_data:
        api_key: YOUR_POLYGON_IO_API_KEY

Configuration

QuickFIX Configuration

The QuickFIX settings are defined in config/quickfix.cfg. Adjust the settings according to your environment and requirements.

# config/quickfix.cfg
[DEFAULT]
ConnectionType=acceptor
BeginString=FIX.4.4
SenderCompID=YOUR_SENDER_COMP_ID
FileStorePath=store
FileLogPath=logs
HeartBtInt=30
UseDataDictionary=Y
DataDictionary=path/to/FIX44.xml

[SESSION]
# Additional session settings

Risk Management Plugins

Risk management plugins are specified in config/config.yml under the risk_management section.

risk_management:
  plugins:
    - credit_limit
    - margin_check
    - notional_limit
    - volume_limit
    - trading_mode

Running the Application

Running Locally

Activate your virtual environment and run the application.

source venv/bin/activate  # On Windows use `venv\Scripts\activate`
python src/application.py

Using Docker

1. Build the Docker Image

docker build -t trading_system .

2. Build the Docker Image

docker run -d -p 9876:9876 --name trading_system trading_system

Using Docker Compose

If you prefer to run the application along with PostgreSQL using Docker Compose:

1. Create a docker-compose.yml File

Ensure you have the docker-compose.yml file in the root directory.

version: '3.8'
services:
  db:
    image: postgres:13
    environment:
      POSTGRES_USER: your_username
      POSTGRES_PASSWORD: your_password
      POSTGRES_DB: trading_system
    volumes:
      - db_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
  app:
    build: .
    depends_on:
      - db
    ports:
      - "9876:9876"
    volumes:
      - .:/app
    environment:
      - DATABASE_HOST=db
      - DATABASE_PORT=5432
      - DATABASE_USER=your_username
      - DATABASE_PASSWORD=your_password
      - DATABASE_NAME=trading_system
volumes:
  db_data:

2. Run Docker Compose

docker-compose up -d

Project Structure

trading_system/
├── config/
│   ├── config.yml
│   ├── database.ini
│   └── quickfix.cfg
├── data/
├── docs/
│   └── system_architecture.png
├── logs/
├── risk_plugins/
│   ├── __init__.py
│   ├── base.py
│   ├── credit_limit.py
│   ├── margin_check.py
│   ├── notional_limit.py
│   ├── trading_mode.py
│   └── volume_limit.py
├── market_data/
│   ├── __init__.py
│   └── polygon_io.py
├── scripts/
│   ├── initialize_db.py
│   ├── start.sh
│   ├── restart.sh
│   └── daily_tasks.sh
├── src/
│   ├── __init__.py
│   ├── application.py
│   ├── fix_engine.py
│   ├── risk_management.py
│   ├── order_manager.py
│   ├── database.py
│   └── utils.py
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
└── VERGES_README.md

Database Schemas

The database schemas are defined in scripts/initialize_db.py and include tables for:

  • Users and Roles
  • Clients and Accounts
  • Orders and Executions
  • Positions
  • Risk Limits and Parameters
  • Instruments and Market Data
  • Audit Logs

To set up the database, run:

python scripts/initialize_db.py

SQL Scripts

Below are the SQL scripts to create the necessary tables.

-- Users Table
CREATE TABLE users (
    user_id SERIAL PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    full_name VARCHAR(100),
    email VARCHAR(100),
    is_active BOOLEAN DEFAULT TRUE,
    last_login TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Roles Table
CREATE TABLE roles (
    role_id SERIAL PRIMARY KEY,
    role_name VARCHAR(50) UNIQUE NOT NULL
);

-- User Roles Association Table
CREATE TABLE user_roles (
    user_id INTEGER REFERENCES users(user_id) ON DELETE CASCADE,
    role_id INTEGER REFERENCES roles(role_id) ON DELETE CASCADE,
    PRIMARY KEY (user_id, role_id)
);

-- Clients Table
CREATE TABLE clients (
    client_id SERIAL PRIMARY KEY,
    client_name VARCHAR(100) NOT NULL,
    status VARCHAR(20) CHECK (status IN ('ACTIVE', 'INACTIVE')),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Accounts Table
CREATE TABLE accounts (
    account_id SERIAL PRIMARY KEY,
    client_id INTEGER REFERENCES clients(client_id) ON DELETE CASCADE,
    account_number VARCHAR(50) UNIQUE NOT NULL,
    account_type VARCHAR(20) CHECK (account_type IN ('CASH', 'MARGIN', 'PORTFOLIO_MARGIN')),
    status VARCHAR(20) CHECK (status IN ('ACTIVE', 'INACTIVE')),
    cash_balance DECIMAL(20,5) DEFAULT 0.0,
    margin_balance DECIMAL(20,5) DEFAULT 0.0,
    equity DECIMAL(20,5) DEFAULT 0.0,
    available_funds DECIMAL(20,5) DEFAULT 0.0,
    maintenance_margin DECIMAL(20,5) DEFAULT 0.0,
    trading_mode VARCHAR(20) DEFAULT 'NORMAL' CHECK (trading_mode IN ('NORMAL', 'CLOSING_ONLY', 'RISK_REDUCE')),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Risk Limits Table
CREATE TABLE risk_limits (
    limit_id SERIAL PRIMARY KEY,
    account_id INTEGER REFERENCES accounts(account_id),
    max_position_value DECIMAL(20,5),
    max_order_value DECIMAL(20,5),
    max_daily_volume INTEGER,
    max_order_volume INTEGER,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(50)
);

-- Risk Parameters Table
CREATE TABLE risk_parameters (
    ticker VARCHAR(10) PRIMARY KEY,
    margin_requirement DECIMAL(5,2),
    volatility DECIMAL(10,5),
    beta DECIMAL(10,5),
    var DECIMAL(10,5),
    is_margin_overridden BOOLEAN DEFAULT FALSE,
    overridden_margin DECIMAL(5,2),
    is_volatility_overridden BOOLEAN DEFAULT FALSE,
    overridden_volatility DECIMAL(10,5),
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(50)
);

-- Margin Requirements Table
CREATE TABLE margin_requirements (
    ticker VARCHAR(10) PRIMARY KEY,
    initial_margin_rate DECIMAL(5,4),
    maintenance_margin_rate DECIMAL(5,4)
);

-- Borrow Status Table
CREATE TABLE borrow_status (
    ticker VARCHAR(10) PRIMARY KEY,
    borrow_status VARCHAR(3) CHECK (borrow_status IN ('ETB', 'HTB')),
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(50)
);

-- Tradable Tickers Table
CREATE TABLE tradable_tickers (
    ticker VARCHAR(10) PRIMARY KEY,
    is_tradable BOOLEAN DEFAULT TRUE,
    reason TEXT,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(50)
);

-- Instruments Table
CREATE TABLE instruments (
    ticker VARCHAR(10) PRIMARY KEY,
    instrument_type VARCHAR(20) CHECK (instrument_type IN ('EQUITY', 'OPTION', 'FUTURE')),
    underlying_ticker VARCHAR(10),
    expiration_date DATE,
    strike_price DECIMAL(15,5),
    option_type VARCHAR(4) CHECK (option_type IN ('CALL', 'PUT')),
    contract_size INTEGER,
    exchange VARCHAR(50),
    currency VARCHAR(10) DEFAULT 'USD'
);

-- Positions Table
CREATE TABLE positions (
    position_id SERIAL PRIMARY KEY,
    account_id INTEGER REFERENCES accounts(account_id),
    ticker VARCHAR(10) NOT NULL,
    quantity INTEGER NOT NULL,
    average_price DECIMAL(15,5),
    market_value DECIMAL(20,5),
    initial_margin_requirement DECIMAL(20,5),
    maintenance_margin_requirement DECIMAL(20,5),
    realized_pnl DECIMAL(20,5) DEFAULT 0,
    unrealized_pnl DECIMAL(20,5) DEFAULT 0,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Orders Table
CREATE TABLE orders (
    order_id SERIAL PRIMARY KEY,
    client_order_id VARCHAR(50) UNIQUE NOT NULL,
    account_id INTEGER REFERENCES accounts(account_id),
    ticker VARCHAR(10) NOT NULL,
    side VARCHAR(4) CHECK (side IN ('BUY', 'SELL')),
    order_type VARCHAR(20) CHECK (order_type IN ('MARKET', 'LIMIT', 'STOP', 'STOP_LIMIT', 'SPREAD', 'COMPLEX')),
    quantity INTEGER NOT NULL CHECK (quantity > 0),
    price DECIMAL(15,5),
    stop_price DECIMAL(15,5),
    status VARCHAR(20) CHECK (status IN ('NEW', 'PARTIALLY_FILLED', 'FILLED', 'CANCELED', 'REJECTED')),
    asset_class VARCHAR(20) CHECK (asset_class IN ('EQUITY', 'OPTION', 'FUTURE')),
    received_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(50)
);

-- Executions Table
CREATE TABLE executions (
    execution_id SERIAL PRIMARY KEY,
    order_id INTEGER REFERENCES orders(order_id) ON DELETE CASCADE,
    execution_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    quantity INTEGER NOT NULL CHECK (quantity > 0),
    price DECIMAL(15,5) NOT NULL,
    execution_type VARCHAR(20) CHECK (execution_type IN ('FILL', 'PARTIAL_FILL')),
    exchange VARCHAR(50),
    trade_id VARCHAR(50),
    commission DECIMAL(10,5),
    net_amount DECIMAL(15,5),
    updated_by VARCHAR(50)
);

-- Audit Logs Table
CREATE TABLE audit_logs (
    audit_id SERIAL PRIMARY KEY,
    user_id INTEGER REFERENCES users(user_id),
    action VARCHAR(50) NOT NULL,
    table_name VARCHAR(50) NOT NULL,
    record_id VARCHAR(50),
    changed_data JSONB,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Configurations Table
CREATE TABLE configurations (
    config_key VARCHAR(50) PRIMARY KEY,
    config_value TEXT,
    last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(50)
);

Risk Management

The risk management system uses a plugin architecture, allowing for easy addition and modification of risk checks. Current plugins include:

  • Credit Limit Check
  • Margin Requirement Check
  • Notional Value Limit Check
  • Volume Limit Check
  • Trading Mode Check

Plugins are located in the risk_plugins directory. Each plugin inherits from the RiskPlugin base class and implements the check method.

Adding a New Risk Plugin

  1. Create a New Plugin File

    touch risk_plugins/new_risk_check.py
  2. Implement the Plugin

# risk_plugins/new_risk_check.py

from .base import RiskPlugin

class NewRiskCheck(RiskPlugin):
    def check(self, order, account):
        # Implement the new risk check logic
        return True, ""
  1. Add the Plugin to Configuration
# config/config.yml
risk_management:
  plugins:
    - new_risk_check
  1. Reload Plugins

The system monitors plugins and reloads them automatically. Alternatively, restart the application.

Thought for 22 seconds

Certainly! Here's the markdown for the entire Risk Management section. You can copy and paste this into your .md file.

markdown Copy code

Risk Management

The risk management system uses a plugin architecture, allowing for easy addition and modification of risk checks. Current plugins include:

  • Credit Limit Check
  • Margin Requirement Check
  • Notional Value Limit Check
  • Volume Limit Check
  • Trading Mode Check

Plugins are located in the risk_plugins directory. Each plugin inherits from the RiskPlugin base class and implements the check method.

Adding a New Risk Plugin

  1. Create a New Plugin File

    touch risk_plugins/new_risk_check.py
  2. Implement the Plugin

# risk_plugins/new_risk_check.py

from .base import RiskPlugin

class NewRiskCheck(RiskPlugin):
    def check(self, order, account):
        # Implement the new risk check logic
        return True, ""
  1. Add the Plugin to Configuration
Copy code
# config/config.yml
risk_management:
  plugins:
    - new_risk_check
  1. Reload Plugins

The system monitors plugins and reloads them automatically. Alternatively, restart the application.

Risk Plugin Structure

Each risk plugin should inherit from the RiskPlugin base class and implement the check method, which takes an order and an account as parameters and returns a tuple (result, message).

  • result: A boolean indicating whether the order passes the risk check.
  • message: An optional string providing information about the risk check result.

Example base class:

# risk_plugins/base.py

class RiskPlugin:
    def __init__(self, database):
        self.database = database

    def check(self, order, account):
        raise NotImplementedError("Risk plugins must implement the 'check' method.")

Existing Risk Plugins

Credit Limit Check

Ensures that the order does not exceed the account's credit limits.

# risk_plugins/credit_limit.py

from .base import RiskPlugin

class CreditLimitCheck(RiskPlugin):
    def check(self, order, account):
        # Implement credit limit logic
        return True, ""

Margin Requirement Check

Verifies that the account has sufficient margin for the order.

# risk_plugins/margin_check.py

from .base import RiskPlugin

class MarginCheck(RiskPlugin):
    def check(self, order, account):
        # Implement margin requirement checks
        return True, ""

Volume Limit Check

Ensures the order quantity does not exceed volume limits.

# risk_plugins/volume_limit.py

from .base import RiskPlugin

class VolumeLimitCheck(RiskPlugin):
    def check(self, order, account):
        # Implement volume limit checks
        return True, ""

Trading Mode Check

Checks if the account's trading mode allows the order.

# risk_plugins/trading_mode.py

from .base import RiskPlugin

class TradingModeCheck(RiskPlugin):
    def check(self, order, account):
        # Implement trading mode checks
        return True, ""

Risk Management Module

The risk management module loads and manages the risk plugins.

# src/risk_management.py

import importlib
import threading
import logging

class RiskManagement:
    def __init__(self, config, database):
        self.config = config
        self.database = database
        self.plugins = {}
        self.load_plugins()
        threading.Thread(target=self.monitor_plugins).start()

    def load_plugins(self):
        plugin_names = self.config['plugins']
        for name in plugin_names:
            module = importlib.import_module(f"risk_plugins.{name}")
            plugin_class = getattr(module, ''.join([part.title() for part in name.split('_')]) + "Check")
            self.plugins[name] = plugin_class(self.database)
        logging.info(f"Risk plugins loaded: {list(self.plugins.keys())}")

    def reload_plugins(self):
        self.load_plugins()
        logging.info("Risk plugins reloaded.")

    def check_order(self, order, account):
        for plugin in self.plugins.values():
            result, message = plugin.check(order, account)
            if not result:
                return False, message
        return True, ""

    def monitor_plugins(self):
        import time
        while True:
            # Implement logic to detect changes in plugin files
            time.sleep(60)  # Check every minute

Thought for 22 seconds

Certainly! Here's the markdown for the entire Risk Management section. You can copy and paste this into your .md file.

markdown Copy code

Risk Management

The risk management system uses a plugin architecture, allowing for easy addition and modification of risk checks. Current plugins include:

  • Credit Limit Check
  • Margin Requirement Check
  • Notional Value Limit Check
  • Volume Limit Check
  • Trading Mode Check

Plugins are located in the risk_plugins directory. Each plugin inherits from the RiskPlugin base class and implements the check method.

Adding a New Risk Plugin

  1. Create a New Plugin File

    touch risk_plugins/new_risk_check.py

Implement the Plugin

# risk_plugins/new_risk_check.py

from .base import RiskPlugin

class NewRiskCheck(RiskPlugin):
    def check(self, order, account):
        # Implement the new risk check logic
        return True, ""

Add the Plugin to Configuration

# config/config.yml
risk_management:
  plugins:
    - new_risk_check

Reload Plugins

The system monitors plugins and reloads them automatically. Alternatively, restart the application.

Risk Plugin Structure

Each risk plugin should inherit from the RiskPlugin base class and implement the check method, which takes an order and an account as parameters and returns a tuple (result, message).

result: A boolean indicating whether the order passes the risk check. message: An optional string providing information about the risk check result. Example base class:

# risk_plugins/base.py

class RiskPlugin:
    def __init__(self, database):
        self.database = database

    def check(self, order, account):
        raise NotImplementedError("Risk plugins must implement the 'check' method.")

Existing Risk Plugins

Credit Limit Check

Ensures that the order does not exceed the account's credit limits.

# risk_plugins/credit_limit.py

from .base import RiskPlugin

class CreditLimitCheck(RiskPlugin):
    def check(self, order, account):
        # Implement credit limit logic
        return True, ""

Margin Requirement Check

Verifies that the account has sufficient margin for the order.

# risk_plugins/margin_check.py

from .base import RiskPlugin

class MarginCheck(RiskPlugin):
    def check(self, order, account):
        # Implement margin requirement checks
        return True, ""

Notional Value Limit Check

Checks if the notional value of the order exceeds predefined limits.

# risk_plugins/notional_limit.py

from .base import RiskPlugin

class NotionalLimitCheck(RiskPlugin):
    def check(self, order, account):
        # Implement notional value checks
        return True, ""

Volume Limit Check

Ensures the order quantity does not exceed volume limits.

# risk_plugins/volume_limit.py

from .base import RiskPlugin

class VolumeLimitCheck(RiskPlugin):
    def check(self, order, account):
        # Implement volume limit checks
        return True, ""

Trading Mode Check

Checks if the account's trading mode allows the order.

# risk_plugins/trading_mode.py

from .base import RiskPlugin

class TradingModeCheck(RiskPlugin):
    def check(self, order, account):
        # Implement trading mode checks
        return True, ""

Risk Management Module

The risk management module loads and manages the risk plugins.

# src/risk_management.py

import importlib
import threading
import logging

class RiskManagement:
    def __init__(self, config, database):
        self.config = config
        self.database = database
        self.plugins = {}
        self.load_plugins()
        threading.Thread(target=self.monitor_plugins).start()

    def load_plugins(self):
        plugin_names = self.config['plugins']
        for name in plugin_names:
            module = importlib.import_module(f"risk_plugins.{name}")
            plugin_class = getattr(module, ''.join([part.title() for part in name.split('_')]) + "Check")
            self.plugins[name] = plugin_class(self.database)
        logging.info(f"Risk plugins loaded: {list(self.plugins.keys())}")

    def reload_plugins(self):
        self.load_plugins()
        logging.info("Risk plugins reloaded.")

    def check_order(self, order, account):
        for plugin in self.plugins.values():
            result, message = plugin.check(order, account)
            if not result:
                return False, message
        return True, ""

    def monitor_plugins(self):
        import time
        while True:
            # Implement logic to detect changes in plugin files
            time.sleep(60)  # Check every minute

Integrating Risk Management with the Application

The main application integrates the risk management module to perform risk checks before processing orders.

Market Data Integration

The market_data module integrates with Polygon.io to fetch real-time market data.

  • API Key: Obtain an API key from Polygon.io.
  • Usage: The PolygonIO class in market_data/polygon_io.py provides methods to fetch market prices.

Example Usage

# market_data/polygon_io.py

import requests
import logging

class PolygonIO:
    def __init__(self, api_key):
        self.api_key = api_key
        self.base_url = 'https://api.polygon.io'

    def get_last_trade(self, ticker):
        url = f"{self.base_url}/v2/last/trade/{ticker}"
        params = {'apiKey': self.api_key}
        response = requests.get(url, params=params)
        if response.status_code == 200:
            data = response.json()
            return data['results']['price']
        else:
            logging.error(f"Polygon.io API error: {response.text}")
            return None

    # Additional methods to fetch market data...

Integrating Market Data into the Application

In the main application, you can use the PolygonIO class to fetch market data for risk checks and order processing.

# src/application.py

# ... [other imports]
from market_data.polygon_io import PolygonIO

class TradingApplication:
    def __init__(self, config_file):
        # ... [other initializations]
        self.market_data = PolygonIO(self.config['market_data']['api_key'])
        # ... [rest of the code]

    # Example method using market data
    def get_market_price(self, ticker):
        price = self.market_data.get_last_trade(ticker)
        return price

    # ... [rest of the code]

Notes

  • Error Handling: Ensure to handle exceptions and API errors when fetching market data.
  • Rate Limits: Be mindful of API rate limits imposed by Polygon.io.
  • Data Caching: Implement caching mechanisms if necessary to reduce API calls.

Testing Market Data Integration

You can test the market data module independently.

# test_market_data.py

from market_data.polygon_io import PolygonIO

def test_get_last_trade():
    api_key = 'YOUR_POLYGON_IO_API_KEY'
    polygon = PolygonIO(api_key)
    ticker = 'AAPL'
    price = polygon.get_last_trade(ticker)
    if price:
        print(f"Last trade price for {ticker}: {price}")
    else:
        print("Failed to fetch market data.")

if __name__ == '__main__':
    test_get_last_trade()

Deployment

Cloud Deployment

The application is cloud-ready and can be deployed using Docker containers.

Steps to Deploy

  1. Build the Docker Image

    docker build -t trading_system .
  2. Push the Docker Image to a Registry

    • Docker Hub:

      docker tag trading_system your_dockerhub_username/trading_system:latest
      docker push your_dockerhub_username/trading_system:latest
    • AWS ECR:

      aws ecr create-repository --repository-name trading_system
      aws ecr get-login-password --region your_region | docker login --username AWS --password-stdin your_aws_account_id.dkr.ecr.your_region.amazonaws.com
      docker tag trading_system your_aws_account_id.dkr.ecr.your_region.amazonaws.com/trading_system:latest
      docker push your_aws_account_id.dkr.ecr.your_region.amazonaws.com/trading_system:latest
  3. Set Up Cloud Environment

    • AWS ECS:

      • Create an ECS Cluster: Use the AWS Management Console or AWS CLI.
      • Define a Task Definition: Specify your Docker image and configure container settings.
      • Create a Service: Link the task definition to the cluster and configure scaling.
      • Configure Networking: Set up load balancers, security groups, and VPC settings.
    • Azure Container Instances:

      • Deploy Using Azure CLI:

        az container create \
          --resource-group your_resource_group \
          --name trading-system \
          --image your_dockerhub_username/trading_system:latest \
          --ports 9876 \
          --environment-variables DATABASE_HOST=db_host DATABASE_USER=db_user DATABASE_PASSWORD=db_pass \
          --cpu 2 \
          --memory 4
    • Google Cloud Run:

      • Deploy Using gcloud CLI:

        gcloud run deploy trading-system \
          --image your_dockerhub_username/trading_system:latest \
          --platform managed \
          --port 9876 \
          --allow-unauthenticated \
          --set-env-vars DATABASE_HOST=db_host,DATABASE_USER=db_user,DATABASE_PASSWORD=db_pass \
          --memory 2Gi

Security Considerations

  • Environment Variables: Use environment variables or secrets management services to handle sensitive information like database credentials and API keys.

  • Secure Network Communication: Ensure all communications, especially FIX protocol connections, are secured using SSL/TLS encryption.

  • Access Control: Implement proper authentication and authorization mechanisms. Use cloud provider security features like IAM roles, security groups, and network policies.

  • Firewall Settings: Configure firewalls to restrict access to necessary ports only.

Scaling and Load Balancing

  • Horizontal Scaling: Use container orchestration platforms to scale out instances based on load metrics.

    • AWS: Use ECS Service Auto Scaling.
    • Azure: Use Virtual Machine Scale Sets or AKS scaling features.
    • Google Cloud: Use GKE cluster autoscaler.
  • Load Balancers: Set up load balancers to distribute incoming traffic evenly across instances.

    • AWS: Use Application Load Balancer (ALB) or Network Load Balancer (NLB).
    • Azure: Use Azure Load Balancer or Application Gateway.
    • Google Cloud: Use Cloud Load Balancing.

Monitoring and Logging

  • Monitoring Tools:

    • AWS CloudWatch: Monitor resources and applications.
    • Azure Monitor: Collect, analyze, and act on telemetry data.
    • Google Cloud Monitoring: Gain insights into your applications and infrastructure.
  • Logging: Centralize logs using services like:

    • AWS CloudWatch Logs
    • Azure Log Analytics
    • Google Cloud Logging
  • Alerting: Set up alerts for critical metrics and events.

Backup and Recovery

  • Database Backups:

    • Schedule regular backups of your PostgreSQL database.
    • Use cloud-native database services with automated backup features (e.g., AWS RDS, Azure Database for PostgreSQL).
  • Disaster Recovery Plan:

    • Define Recovery Time Objectives (RTO) and Recovery Point Objectives (RPO).
    • Implement multi-region deployments if necessary.
    • Regularly test your disaster recovery procedures.

Continuous Integration and Deployment (CI/CD)

  • Set Up CI/CD Pipelines:

    • GitHub Actions: Automate build, test, and deployment workflows.
    • Jenkins: Use for complex CI/CD pipelines.
    • AWS CodePipeline, Azure DevOps, Google Cloud Build: Cloud-native CI/CD services.
  • Automate Testing:

    • Implement unit tests, integration tests, and end-to-end tests.
    • Use testing frameworks like pytest for Python.
  • Automate Deployment:

    • Deploy to staging environments before production.
    • Use infrastructure as code tools like Terraform or CloudFormation.

Compliance and Auditing

  • Compliance Standards:

    • Ensure the system complies with relevant industry standards (e.g., PCI DSS, GDPR, FINRA).
    • Use cloud services that are compliant with these standards.
  • Audit Trails:

    • Maintain detailed logs for all transactions and system changes.
    • Use immutable storage for logs to prevent tampering.
  • Access Auditing:

    • Monitor and audit user access to systems and data.
    • Use cloud provider tools for identity and access management.

Cost Management

  • Resource Optimization:

    • Use appropriate instance sizes.
    • Implement auto-scaling to adjust resources based on demand.
  • Cost Monitoring:

    • Use cloud provider cost management tools.
    • Set up budgets and alerts for cost thresholds.

Documentation and Support

  • Documentation:

    • Maintain up-to-date documentation for deployment processes.
    • Document infrastructure configurations and code repositories.
  • Support Plans:

    • Consider support plans from cloud providers for production environments.

Additional Resources

Contributing

Contributions are welcome! Please follow these steps to contribute to the project:

  1. Fork the Repository

    Click the "Fork" button at the top right corner of the repository page to create a copy of the repository in your GitHub account.

  2. Clone Your Fork

    git clone https://github.com/yourusername/trading_system.git
    cd trading_system
  3. Create a Feature Branch

    git checkout -b feature/your-feature-name
  4. Make Your Changes

    • Implement your feature or fix.
    • Ensure your code follows the project's coding standards.
    • Write unit tests for your code if applicable.
    • Update documentation if necessary.
  5. Commit Your Changes

    git add .
    git commit -m "Add feature: your feature name"
  6. Push to Your Fork

    git push origin feature/your-feature-name
  7. Open a Pull Request

    • Go to the original repository on GitHub.
    • Click on the "Pull Requests" tab.
    • Click the "New Pull Request" button.
    • Select your feature branch and create the pull request.
    • Provide a clear and descriptive title and description for your pull request.

Code Style Guidelines

  • PEP 8 Compliance: Ensure your code follows PEP 8 style guidelines.
  • Comments and Documentation: Write clear comments and update documentation as needed.
  • Testing: Include unit tests for new functionalities using frameworks like unittest or pytest.
  • Commit Messages: Use descriptive commit messages that explain the changes you have made.

Reporting Issues

If you encounter any problems or have suggestions for improvements, please open an issue on GitHub:

  1. Go to the Issues Tab

    • Navigate to the repository's "Issues" tab.
  2. Click "New Issue"

    • Provide a descriptive title and detailed description of the issue.
  3. Label the Issue

    • Use appropriate labels to categorize the issue (e.g., bug, enhancement, question).

Community Guidelines

  • Be respectful and considerate in your communications.
  • Provide constructive feedback and be open to feedback on your contributions.
  • Follow the Contributor Covenant Code of Conduct.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

Contact

For any inquiries or support, please contact:

About

Versatile Enterprise Risk Gateway Entry System for equities, options and futures, FIX based, pre-trade

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages