Skip to content

Latest commit

 

History

History
147 lines (101 loc) · 14 KB

README.md

File metadata and controls

147 lines (101 loc) · 14 KB

1. Workflows

  1. Historical spread calculation algorithm

    Path: Core, Service, Infra
    Test: HistoricalDataAnalysisTests.fs

  2. Identify Cross Traded Pairs

    Path: Core, Service, Infra

  3. MarketDataRetrieval

    Path: Core, Service, Infra

  4. ArbitrageOpportunityIdentifier

    Path: Core, Service, Infra

  5. Order Execution

    Path: Infra

  6. P & L Calculation

    Path: Core, Service, Infra
    Test: PnLCalculationTests.fs

  7. P & L Threshold Management

    Path: Core, Service, Infra
    Test: ManagePnLThresholdTests.fs

  8. Annualized Return (on demand)

    Path: Core, Infra
    Test: AnnualizedReturnCalculationTests.fs

2. Side Effects

  1. Calculate historical spread (arbitrage opportunities)
    ArbitrageGainer/HistoricalDataAnalysisCore.fs: defines F# modules and types for historical market data analysis, including functions for mapping and reducing market data to identify arbitrage opportunities.

    ArbitrageGainer/HistoricalDataAnalysisService.fs: defines a function identifyArbitrageOpportunities for analyzing market data and identifying arbitrage opportunities.

    ArbitrageGainer/HistoricalDataAnalysisInfra.fs: contains infrastructure code for historical data analysis, including database initialization, data saving, reading from files, and printing and writing arbitrage opportunities.

    Test file: ArbitrageGainer.Tests/HistoricalDataAnalysisTests.fs

  2. Identify Cross Traded Pairs
    ArbitrageGainer/IdentifyCrossTradedPairsCore.fs: contains functions for normalizing and cleaning currency pairs, specifically for identifying cross-traded pairs.

    ArbitrageGainer/IdentifyCrossTradedPairsService.fs: contains a module called IdentifyCrossTradedPairsService that defines a function to identify cross-traded pairs between three exchanges.

    ArbitrageGainer/IdentifyCrossTradedPairsInfra.fs: contains F# code that fetches currency pairs from different exchanges, identifies cross-traded pairs, and saves them to a file and a MySQL database.

  3. Order Execution
    File:OrderExecutionInfra.fs:

    sendEmail: Sends an email via SMTP and logs success or failure. mockOrderResponse: Simulates an order response, either as fully or partially filled. insertCompletedTransaction: Inserts transaction details into a database and logs the outcome. updateEmail: Updates the email address based on HTTP request parameters and provides response via an HTTP context.

  4. Market Data Retrieval
    fetchCrossAndHistPairs: Fetch crypto currency information from cloud database connectToWebSocket: connect to web socket receiveData: receive inputs from web socket sendJsonMessage: send messages to Polygon API update-config: receive user inputs of trading parameters startTrading: receive user input to initiate socket and the trading process stopTrading: receive user input to stop the trading process

    ArbitrageInfra

  5. P & L Calculation

  • Infra
    Database Access: The function fetchTransactions interacts with MySQL database. This interaction includes connecting to the database, executing a query, and reading results.
    HTTP Context Manipulations: The pnlHandler function modifies the HTTP context by setting response status codes and bodies based on the outcome of its operations.
  1. P & L Threshold Management
  • Service
    State Changes through MailboxProcessor: The PnLThresholdAgent (here) uses a MailboxProcessor to maintain and update the threshold state asynchronously. This component changes state internally and affects the external behavior based on the threshold values it manages.
    Asynchronous Responses: When setting or getting thresholds(here), the agent communicates asynchronously using AsyncReplyChannel, impacting the system's state asynchronously.

  • Infra
    HTTP Request Handling (here): processes HTTP requests and modifies the HTTP response based on the operations performed, such as updating or retrieving the threshold.

  1. Annualized Return Calculation
  • Infra
    Database Access: The fetchTransactionsForDay function connects to a database and reads data.
    HTTP Response Handling: The annualizedReturnHandler alters HTTP responses based on the computation outcomes and input validation, directly affecting the HTTP state transmitted to the client.

3. Error Handling

  1. Market Data Retrieval
  1. Order Execution File:OrderExecutionInfra.fs:

    executeTransaction - Manages the transaction execution process, handles responses, and updates profit or notifies of failure, encapsulating outcomes in a Result.

  2. P & L Calculation

  • Infra
    Date Parsing and Validation (here): Errors in date parsing or invalid date ranges result in immediate HTTP 400 responses. These are handled explicitly, ensuring users are informed of input errors. Generic Exception Handling (here): This approach ensures that no type of exception goes unhandled, providing a catch-all safety net for any runtime issues that might occur during database operations.
  • Service
    Calculate PnL Functions (here): These functions handle errors in transaction data processing by returning structured results (Profit or Loss), encapsulating potential errors in business logic (e.g., incorrect calculations based on transaction types).
  1. P & L Threshold Management
  • Service
    Error Reporting (here): When a threshold update fails validation, an error message is passed back through the MailboxProcessor, which is then communicated to the caller via the service's public methods.
    Validation of Threshold Values: The validateThreshold function explicitly handles the validation of threshold inputs, returning either Valid or Invalid based on whether the threshold is positive.

  • Infra
    HTTP Error Responses (here): directly handles parsing errors (e.g., when a query parameter is missing or invalid) and returns appropriate HTTP status codes (400 Bad Request).
    Delegating Business Logic Errors (here): It handles errors related to business logic (e.g., setting an invalid threshold) by delegating to the service layer and then converting returned errors into HTTP responses.

  1. Annualized Return Calculation
  • Core
    Divide by Zero: The calculateAnnualizedReturn function raises an exception if DurationYears is zero, which is critical to avoid mathematical errors in calculation.

  • Infra
    Input Validation: The handler function validates the input date format and checks if the start date is in the future, responding with an HTTP 400 (Bad Request) if there are any issues. Generic Exception Handling (here): employ a generic exception handler that catches all exceptions (including database connection error).

  1. Calculate historical spread (arbitrage opportunities)
    initializeDatabase
    saveOpportunitiesToDatabase
    Infra

  2. Identify Cross Traded Pairs
    initializeDatabase
    saveOpportunitiesToDatabase
    fetchCurrencyPairsFromBitfinex
    fetchCurrencyPairsFromBitstamp
    fetchCurrencyPairsFromKraken
    Infra