/*
 * Copyright (c) 2026 Sensmetry, UAB. All Rights Reserved.
 */

package 'Algorithmic Trading Requirements' {
    private import RequirementDerivation::*;

    alias ReqDefs for 'Algorithmic Trading Definitions'::'Requirement Definitions';

    package Context {
        doc Scope
        /* The requirements included in this document drive the development of the 
        * Algorithmic Trading System and ensure that the final deliverable aligns with 
        * regulatory compliance, risk management needs, and trading performance objectives. 
        * This document serves as a foundation for system design, development, testing, and validation.
        */
    }

    package 'Stakeholder Requirements' {
        doc Description
        /* Requirements from the perspective of stakeholders in the
         * algorithmic trading system, following ISO 15288 Stakeholder
         * Requirements Definition Process.
         */

        package 'Executive Stakeholder Requirements' {
            doc Description
            /* Requirements from executive management and organizational
             * perspectives regarding business goals and strategic objectives.
             */

            requirement 'STKR-EXEC-001' : ReqDefs::'Trading Firm Requirement' {
                doc Description
                /* System shall generate consistent risk-adjusted returns
                 * while maintaining regulatory compliance.
                 */

                doc Justification
                /* Primary business objective to deliver value to investors
                 * while managing operational and regulatory risk.
                 */

                doc SuccessCriteria
                /* Achieve minimum Sharpe ratio of 2.0 over rolling 12-month
                 * periods with zero regulatory violations.
                 */

                allocate 'Algorithmic Trading System Design'::'System Instances'::'Algorithmic Trading Platform' to self;
            }

            requirement 'STKR-EXEC-002' : ReqDefs::'Trading Firm Requirement' {
                doc Description
                /* System shall comply with all applicable financial
                 * regulations including MiFID II and SEC requirements.
                 */

                doc Justification
                /* Legal obligation to operate in regulated markets and
                 * avoid fines, sanctions, or license revocation.
                 */

                doc SuccessCriteria
                /* Pass all regulatory audits with no major findings and
                 * maintain trading authorization across all target markets.
                 */
            }

            #derivation connection 'STKR-EXEC-002 Derivation' {
                end #original original_req references 'Stakeholder Requirements'::'Executive Stakeholder Requirements'::'STKR-EXEC-002';
                end #derive derived_req_1 references 'Stakeholder Requirements'::'User Stakeholder Requirements'::'Compliance Requirements'::'STKR-COMP-001';
                end #derive derived_req_2 references 'Stakeholder Requirements'::'User Stakeholder Requirements'::'Compliance Requirements'::'STKR-COMP-002';
            }

            requirement 'STKR-EXEC-003' : ReqDefs::'Trading Firm Requirement' {
                doc Description
                /* System shall prevent catastrophic losses through
                 * automated risk controls, position monitors, and
                 * circuit breakers.
                 */

                doc Justification
                /* Protect firm capital and prevent business-ending losses
                 * as seen in historical algorithmic trading failures.
                 */

                doc SuccessCriteria
                /* Less than 0.1% trading incidents resulting in
                 * losses exceeding daily loss limits or requiring
                 * manual intervention.
                 */
            }

            #derivation connection 'STKR-EXEC-003 Derivation' {
                end #original original_req references 'Stakeholder Requirements'::'Executive Stakeholder Requirements'::'STKR-EXEC-003';
                end #derive derived_req_1 references 'Stakeholder Requirements'::'User Stakeholder Requirements'::'Risk Management Requirements'::'STKR-RISK-001';
                end #derive derived_req_2 references 'Stakeholder Requirements'::'User Stakeholder Requirements'::'Risk Management Requirements'::'STKR-RISK-002';
                end #derive derived_req_3 references 'Stakeholder Requirements'::'User Stakeholder Requirements'::'Risk Management Requirements'::'STKR-RISK-005';
            }
        }

        package 'User Stakeholder Requirements' {
            doc Description
            /* Requirements from operational users of the algorithmic
             * trading system including traders, risk managers, and
             * compliance officers.
             */

            package 'Trading Team Requirements' {
                requirement 'STKR-USER-001' : ReqDefs::'Trading Team Requirement' {
                    doc Description
                    /* Trading team shall be able to backtest strategies on
                     * historical market data with realistic execution simulation.
                     */

                    doc Justification
                    /* Validate strategy profitability and risk characteristics
                     * before deployment to avoid losses from untested algorithms.
                     */

                    doc SuccessCriteria
                    /* Backtesting environment produces results within 5% of
                     * actual live trading performance over 3-month validation.
                     */
                }

                requirement 'STKR-USER-002' : ReqDefs::'Trading Team Requirement' {
                    doc Description
                    /* Trading team shall be able to deploy strategies to
                     * production with configurable risk parameters.
                     */

                    doc Justification
                    /* Enable rapid strategy iteration and deployment while
                     * maintaining risk oversight and control.
                     */

                    doc SuccessCriteria
                    /* Strategies deploy to production within 10 minutes with
                     * all risk parameters correctly applied and verified.
                     */
                }

                requirement 'STKR-USER-003' : ReqDefs::'Trading Team Requirement' {
                    doc Description
                    /* Trading team shall be able to monitor strategy
                     * performance in real-time with P&L tracking.
                     */

                    doc Justification
                    /* Enables prompt response to performance degradation or
                     * changing market conditions to protect capital.
                     */

                    doc SuccessCriteria
                    /* P&L updates display with < 1 second latency and
                     * dashboards remain accessible during all market conditions.
                     */
                }

                requirement 'STKR-USER-004' : ReqDefs::'Trading Team Requirement' {
                    doc Description
                    /* Trading team shall be able to halt or modify strategies
                     * based on changing market conditions.
                     */

                    doc Justification
                    /* Market conditions change rapidly; manual override
                     * capability is essential for risk management.
                     */

                    doc SuccessCriteria
                    /* Strategy halt commands execute within 500 milliseconds
                     * with confirmation displayed to operator.
                     */
                }
            }

            package 'Risk Management Requirements' {
                requirement 'STKR-RISK-001' : ReqDefs::'Risk Management Requirement' {
                    doc Description
                    /* System shall enforce position limits per strategy,
                     * instrument, and aggregate portfolio.
                     */

                    doc Justification
                    /* Prevent concentration risk and limit exposure to
                     * individual instruments or strategies.
                     */

                    doc SuccessCriteria
                    /* 100% of orders exceeding position limits are rejected
                     * before submission to market.
                     */
                }

                #derivation connection 'STKR-RISK-001 Derivation' {
                    end #original original_req references 'Stakeholder Requirements'::'User Stakeholder Requirements'::'Risk Management Requirements'::'STKR-RISK-001';
                    end #derive derived_req_1 references 'System Requirements'::'Risk Control Requirements'::'SYS-RISK-001';
                    end #derive derived_req_2 references 'System Requirements'::'Risk Control Requirements'::'SYS-RISK-002';
                }

                requirement 'STKR-RISK-002' : ReqDefs::'Risk Management Requirement' {
                    doc Description
                    /* System shall monitor P&L in real-time and halt trading
                     * when daily loss limits are breached.
                     */

                    doc Justification
                    /* Cap maximum daily losses to prevent drawdowns that
                     * exceed risk tolerance or threaten firm viability.
                     */

                    doc SuccessCriteria
                    /* Loss limit breach detection and trading halt occurs
                     * within 100 milliseconds.
                     */
                }

                requirement 'STKR-RISK-003' : ReqDefs::'Risk Management Requirement' {
                    doc Description
                    /* System shall perform pre-trade risk checks on all orders
                     * before submission to exchanges.
                     */

                    doc Justification
                    /* Regulatory requirement and best practice to prevent
                     * erroneous orders from reaching market.
                     */

                    doc SuccessCriteria
                    /* Pre-trade checks complete with < 50 microseconds latency
                     * and 100% of orders are validated before submission.
                     */
                }

                requirement 'STKR-RISK-004' : ReqDefs::'Risk Management Requirement' {
                    doc Description
                    /* System shall detect and prevent erroneous orders such as
                     * fat-finger errors and duplicate submissions.
                     */

                    doc Justification
                    /* Prevent costly trading errors that can result in
                     * significant losses and market disruption.
                     */

                    doc SuccessCriteria
                    /* Detect and reject 100% of orders with quantities >3
                     * standard deviations from historical mean for that strategy.
                     */
                }

                requirement 'STKR-RISK-005' : ReqDefs::'Risk Management Requirement' {
                    doc Description
                    /* Risk managers shall have access to emergency kill switch
                     * to immediately halt all automated trading.
                     */

                    doc Justification
                    /* Final safeguard for extreme situations requiring
                     * immediate cessation of all trading activity.
                     */

                    doc SuccessCriteria
                    /* Kill switch activates within 100 milliseconds and
                     * cancels all pending orders.
                     */
                }
            }

            package 'Compliance Requirements' {
                requirement 'STKR-COMP-001' : ReqDefs::'Compliance Requirement' {
                    doc Description
                    /* System shall maintain complete, immutable audit trail
                     * of all trading activity with nanosecond timestamps.
                     */

                    doc Justification
                    /* Required for regulatory audits and post-trade analysis. */

                    doc SuccessCriteria
                    /* Audit trail captures 100% of trading events with
                     * timestamp accuracy ±100 nanoseconds and zero data loss.
                     */
                }

                requirement 'STKR-COMP-002' : ReqDefs::'Compliance Requirement' {
                    doc Description
                    /* System shall generate regulatory reports compliant with
                     * MiFID II transaction reporting and SEC audit trail requirements.
                     */

                    doc Justification
                    /* Legal obligation for market participants to report
                     * trading activity to regulators within specified timeframes.
                     */

                    doc SuccessCriteria
                    /* Reports generated daily by T+1 deadline with 100%
                     * accuracy and zero regulatory rejections.
                     */
                }

                requirement 'STKR-COMP-003' : ReqDefs::'Compliance Requirement' {
                    doc Description
                    /* System shall monitor trading activity for potential
                     * market abuse patterns including wash trades and layering.
                     */

                    doc Justification
                    /* Prevent market manipulation and ensure compliance with
                     * market conduct regulations to avoid penalties.
                     */

                    doc SuccessCriteria
                    /* Surveillance system flags 95% of known market abuse
                     * patterns with <1% false positive rate.
                     */
                }

                requirement 'STKR-COMP-004' : ReqDefs::'Compliance Requirement' {
                    doc Description
                    /* System shall enforce best execution requirements by
                     * monitoring execution quality across venues.
                     */

                    doc Justification
                    /* Regulatory obligation to obtain best possible execution
                     * for client orders and demonstrate due diligence.
                     */

                    doc SuccessCriteria
                    /* Execution quality within top quartile of industry
                     * benchmarks with documented venue selection rationale.
                     */
                }
            }
        }
    }

    package 'System Requirements' {
        doc Description
        /* Technical requirements for the algorithmic trading system,
         * following ISO 15288 System Requirements Definition Process.
         * These requirements are derived from stakeholder requirements.
         */

        package 'Performance Requirements' {
            requirement 'SYS-PERF-001' : ReqDefs::'Performance Requirement' {
                doc Description
                /* System shall process market data with end-to-end latency
                 * less than 1 millisecond from exchange feed to strategy.
                 */

                doc Justification
                /* Low latency is critical for competitive advantage in
                 * high-frequency trading and market-making strategies.
                 */

                doc SuccessCriteria
                /* 99.9th percentile latency measured at <1ms during
                 * normal market conditions.
                 */
            }

            requirement 'SYS-PERF-002' : ReqDefs::'Performance Requirement' {
                doc Description
                /* System shall submit orders with latency less than
                 * 1 millisecond from signal generation to exchange.
                 */

                doc Justification
                /* Minimize slippage and maximize probability of execution
                 * at desired prices in fast-moving markets.
                 */

                doc SuccessCriteria
                /* Mean order submission latency <500 microseconds with
                 * 99th percentile <1ms measured via hardware timestamps.
                 */
            }

            requirement 'SYS-PERF-003' : ReqDefs::'Performance Requirement' {
                doc Description
                /* System shall handle throughput exceeding 100,000
                 * orders per second.
                 */

                doc Justification
                /* Support high-frequency strategies and market-making
                 * across multiple instruments simultaneously.
                 */

                doc SuccessCriteria
                /* Sustained throughput of 100,000 orders/second with
                 * no message loss and latency degradation <10%.
                 */
            }

            requirement 'SYS-PERF-004' : ReqDefs::'Performance Requirement' {
                doc Description
                /* System shall maintain 99.99% uptime during market hours. */

                doc Justification
                /* Downtime results in missed trading opportunities and
                 * potential failure to meet market-making obligations.
                 */

                doc SuccessCriteria
                /* Maximum unplanned downtime of 5 minutes per month
                 * during trading hours.
                 */
            }
        }

        package 'Functional Requirements' {
            requirement 'SYS-FUNC-001' : ReqDefs::'Functional Requirement' {
                doc Description
                /* System shall ingest real-time market data from multiple
                 * exchanges simultaneously.
                 */

                doc Justification
                /* Access to multiple venues enables arbitrage opportunities
                 * and best execution across markets.
                 */

                doc SuccessCriteria
                /* Simultaneous connection to minimum 5 exchanges with
                 * no data gaps >100ms during market hours.
                 */
            }

            requirement 'SYS-FUNC-002' : ReqDefs::'Functional Requirement' {
                doc Description
                /* System shall normalize market data across different
                 * exchange formats into unified representation.
                 */

                doc Justification
                /* Unified data format simplifies strategy development and
                 * enables cross-venue analysis and trading.
                 */

                doc SuccessCriteria
                /* All supported exchanges mapped to common schema with
                 * 100% field accuracy validated against exchange specs.
                 */
            }

            requirement 'SYS-FUNC-003' : ReqDefs::'Functional Requirement' {
                doc Description
                /* System shall track complete order lifecycle including
                 * submission, acknowledgment, fills, and cancellations.
                 */

                doc Justification
                /* Accurate order state tracking is essential for position
                 * management, risk control, and regulatory compliance.
                 */

                doc SuccessCriteria
                /* Order state transitions tracked with 100% accuracy and
                 * reconciliation discrepancies <0.01% of total orders.
                 */
            }

            requirement 'SYS-FUNC-004' : ReqDefs::'Functional Requirement' {
                doc Description
                /* System shall calculate real-time P&L across all
                 * positions with mark-to-market pricing.
                 */

                doc Justification
                /* Real-time P&L enables intraday risk management and
                 * supports loss limit monitoring requirements.
                 */

                doc SuccessCriteria
                /* P&L calculations update within 100ms of price changes
                 * with accuracy ±0.1% vs end-of-day reconciliation.
                 */
            }
        }

        package 'Risk Control Requirements' {
            requirement 'SYS-RISK-001' : ReqDefs::'Risk Control Requirement' {
                doc Description
                /* System shall enforce maximum order size limits before
                 * order submission.
                 */

                doc Justification
                /* Prevent single large orders from causing excessive
                 * market impact or unintended exposure.
                 */

                doc SuccessCriteria
                /* 100% of orders exceeding configured size limits are
                 * rejected with detailed error message.
                 */
            }

            requirement 'SYS-RISK-002' : ReqDefs::'Risk Control Requirement' {
                doc Description
                /* System shall enforce maximum position limits per
                 * instrument before accepting new orders.
                 */

                doc Justification
                /* Technical implementation of position limit risk controls
                 * derived from stakeholder risk requirements.
                 */

                doc SuccessCriteria
                /* Position limits checked on every order with <10
                 * microseconds latency and zero bypasses.
                 */
            }

            requirement 'SYS-RISK-003' : ReqDefs::'Risk Control Requirement' {
                doc Description
                /* System shall reject orders with prices beyond
                 * configurable percentage from current market price.
                 */

                doc Justification
                /* Prevents erroneous orders from impacting market. */
            }

            #derivation connection 'STKR-RISK-003 Derivation' {
                end #original original_req references 'Stakeholder Requirements'::'User Stakeholder Requirements'::'Risk Management Requirements'::'STKR-RISK-003';
                end #derive derived_req_1 references 'System Requirements'::'Risk Control Requirements'::'SYS-RISK-001';
                end #derive derived_req_2 references 'System Requirements'::'Risk Control Requirements'::'SYS-RISK-002';
                end #derive derived_req_3 references 'System Requirements'::'Risk Control Requirements'::'SYS-RISK-003';
                end #derive derived_req_4 references 'System Requirements'::'Risk Control Requirements'::'SYS-RISK-004';
            }

            requirement 'SYS-RISK-004' : ReqDefs::'Risk Control Requirement' {
                doc Description
                /* System shall detect and block duplicate order
                 * submissions within configurable time window.
                 */

                doc Justification
                /* Prevent accidental double-fills from software errors or
                 * network retransmissions that could double intended exposure.
                 */

                doc SuccessCriteria
                /* Duplicate detection catches 100% of identical orders
                 * within 500ms window with no false positives.
                 */
            }

            requirement 'SYS-RISK-005' : ReqDefs::'Risk Control Requirement' {
                doc Description
                /* System shall halt all trading and cancel pending orders
                 * within 100 milliseconds when kill switch activated.
                 */

                doc Justification
                /* Technical implementation of emergency kill switch
                 * capability required by risk management stakeholders.
                 */

                doc SuccessCriteria
                /* Complete trading halt within 100ms with confirmation of
                 * all order cancellations received from exchanges.
                 */
            }

            requirement 'SYS-RISK-006' : ReqDefs::'Risk Control Requirement' {
                doc Description
                /* System shall alert risk managers when correlation
                 * between strategies exceeds configured thresholds.
                 */

                doc Justification
                /* Detect hidden concentration risk from strategies
                 * that appear independent but move together.
                 */

                doc SuccessCriteria
                /* Correlation alerts triggered within 30 seconds of
                 * threshold breach with affected strategy pairs identified.
                 */
            }
        }

        package 'Compliance Requirements' {
            requirement 'SYS-COMP-001' : ReqDefs::'System Compliance Requirement' {
                doc Description
                /* System shall log all order parameters including
                 * timestamps, prices, quantities for regulatory reporting.
                 */

                doc Justification
                /* Technical implementation of audit trail requirements
                 * for regulatory compliance and investigation support.
                 */

                doc SuccessCriteria
                /* All mandatory fields per MiFID II/SEC rules logged with
                 * zero data loss and tamper-evident storage.
                 */
            }

            requirement 'SYS-COMP-002' : ReqDefs::'System Compliance Requirement' {
                doc Description
                /* System shall implement best execution monitoring by
                 * comparing execution prices against market benchmarks.
                 */

                doc Justification
                /* Demonstrate compliance with best execution obligations
                 * through objective measurement and documentation.
                 */

                doc SuccessCriteria
                /* Execution quality metrics calculated for 100% of trades
                 * with benchmarks (VWAP, arrival price) within 1 minute.
                 */
            }

            requirement 'SYS-COMP-003' : ReqDefs::'System Compliance Requirement' {
                doc Description
                /* System shall prevent trading in securities on
                 * restricted lists maintained by compliance.
                 */

                doc Justification
                /* Enforce trading restrictions to prevent conflicts of
                 * interest, insider trading, and regulatory violations.
                 */

                doc SuccessCriteria
                /* 100% of orders in restricted securities blocked before
                 * submission with alerts to compliance team.
                 */
            }
        }
    }
}
