diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 0863e6f..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "configurations": [ - { - "name": "Mac", - "compileCommands": "${workspaceFolder}/build/compile_commands.json", - "includePath": [ - "${workspaceFolder}/external/libpqxx/include" - ] - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 0ab02a7..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,67 +0,0 @@ -dsh{ - "files.associations": { - "unordered_map": "cpp", - "__bit_reference": "cpp", - "__hash_table": "cpp", - "__locale": "cpp", - "__node_handle": "cpp", - "__split_buffer": "cpp", - "__threading_support": "cpp", - "__tree": "cpp", - "__verbose_abort": "cpp", - "any": "cpp", - "array": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "complex": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "execution": "cpp", - "memory": "cpp", - "forward_list": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "list": "cpp", - "locale": "cpp", - "map": "cpp", - "mutex": "cpp", - "new": "cpp", - "optional": "cpp", - "ostream": "cpp", - "print": "cpp", - "queue": "cpp", - "ratio": "cpp", - "regex": "cpp", - "set": "cpp", - "source_location": "cpp", - "span": "cpp", - "sstream": "cpp", - "stack": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "string": "cpp", - "string_view": "cpp", - "tuple": "cpp", - "typeinfo": "cpp", - "valarray": "cpp", - "variant": "cpp", - "vector": "cpp", - "algorithm": "cpp" - } -} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index f779a8a..3f52923 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,9 @@ endif() # before project()/CXX language enablement so the toolchain is detected with # import-std support. Requires the Ninja generator plus a toolchain that ships a # libc++ `std` module. Configure with: cmake -G Ninja -B build -if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.3" AND CMAKE_VERSION VERSION_LESS "4.4") +if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.4" AND CMAKE_VERSION VERSION_LESS "4.5") + set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "f35a9ac6-8463-4d38-8eec-5d6008153e7d") +elseif(CMAKE_VERSION VERSION_GREATER_EQUAL "4.3" AND CMAKE_VERSION VERSION_LESS "4.4") set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "451f2fe2-a8a2-47c3-bc32-94786d8fc91b") elseif(CMAKE_VERSION VERSION_GREATER_EQUAL "4.2" AND CMAKE_VERSION VERSION_LESS "4.3") set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444") @@ -93,6 +95,18 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # module flags for loadCommand.cppm — are always present for the editor. set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +# The macOS 27 SDK's includes asking only for INFINITY/NAN +# (__need_infinity_nan), but libc++'s float.h wrapper sets its include guard +# anyway, so any later is skipped and the DBL_*/LDBL_* macros never +# appear. Any TU that includes before trips on it: Catch2's +# reporter helpers lose DBL_MAX_10_EXP, and boost-decimal's bit_layouts.hpp +# sees no LDBL_MANT_DIG, so it takes long double as unsupported and fails to +# compile. Pre-including the full sets the guard with every macro +# defined. Set before the add_subdirectory() calls so vendored targets get it. +if(APPLE) + add_compile_options("$<$:SHELL:-include float.h>") +endif() + # Clang 22 made "reduced BMI" the default for C++20 modules, and reduced BMIs # drop the predeclared global operator new/delete from the global module # (llvm/llvm-project#166068). A TU that imports std alongside enough of this @@ -112,7 +126,7 @@ set(PQXX_LIBRARIES_INSTALL ON) set(SKIP_BUILD_TEST ON) set(SKIP_CONFIGURE_LIBPQXX OFF) -# Disable warningsfor external libraries +# Disable warnings for external libraries set(PREV_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w") @@ -142,6 +156,9 @@ find_package(PostgreSQL REQUIRED) # Build libpqxx from source add_subdirectory(external/libpqxx EXCLUDE_FROM_ALL) +# Restore compiler flags for project targets +set(CMAKE_CXX_FLAGS ${PREV_CMAKE_CXX_FLAGS}) + # Collect all .cpp files in the src directory. CONFIGURE_DEPENDS re-runs the # glob when .cpp files are added/removed (matching the MODULE_INTERFACES glob # below), so a new translation unit is picked up without a manual reconfigure. @@ -169,6 +186,18 @@ target_sources(BacktestingEngineLib set_target_properties(BacktestingEngineLib PROPERTIES CXX_MODULE_STD ON) target_compile_features(BacktestingEngineLib PUBLIC cxx_std_23) +if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") + # -Wmissing-designated-field-initializers (part of newer Clangs' -Wextra) + # fires on partial designated initializers, but every struct in this + # project default-initializes its fields, so omitted fields are always + # well-defined. Partial initializers are the intended style — e.g. the + # test mocks set only the field under test. + target_compile_options(BacktestingEngineLib PUBLIC + -Wall -Wextra -Wno-missing-designated-field-initializers) +elseif(MSVC) + target_compile_options(BacktestingEngineLib PUBLIC /W4) +endif() + # Include directories — PUBLIC so they propagate to the executable (and any # other consumer) through target_link_libraries. Every project #include is # source-relative and path-qualified (e.g. "shared/utilities/decimalJson.hpp", @@ -227,7 +256,12 @@ target_link_libraries(BacktestingEngineLib PUBLIC CURL::libcurl $<$:rt>) +set(SAVED_FLAGS ${CMAKE_CXX_FLAGS}) +if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w") +endif() add_subdirectory(external/boost-decimal) +set(CMAKE_CXX_FLAGS ${SAVED_FLAGS}) target_link_libraries(BacktestingEngineLib PUBLIC Boost::decimal) target_link_libraries(BacktestingEngineLib PUBLIC pqxx OpenMP::OpenMP_CXX) @@ -265,6 +299,11 @@ endif() # of the default `all` build until the test target pulls it in. include(CTest) if(BUILD_TESTING) + set(SAVED_FLAGS ${CMAKE_CXX_FLAGS}) + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w") + endif() add_subdirectory(external/Catch2 EXCLUDE_FROM_ALL) + set(CMAKE_CXX_FLAGS ${SAVED_FLAGS}) add_subdirectory(tests) endif() diff --git a/README.md b/README.md index beac8a2..68b3426 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The engine is C++23 (modules, `import std;`) and one binary with eight subcomman I'm extracting results and creating various graphs for trend analyses using SciPy for calculations and Plotly for visualization. -![alt text](documents/images/random-indices-sp500-variable.svg) +![Equity curves for 25 random S&P 500 backtests across a 5×5 stop-loss/take-profit parameter sweep (20–100), growing portfolio equity from roughly 5k to 12k over the test window.](documents/images/random-indices-sp500-variable.svg) *Read more results on https://mccaffers.com/quantitative_analysis/randomly_trading/* diff --git a/documents/questdb.md b/documents/questdb.md index 5c34216..0d71a50 100644 --- a/documents/questdb.md +++ b/documents/questdb.md @@ -25,11 +25,18 @@ One table per symbol, table name == symbol (`EURUSD`, ...): - `ask`, `bid` — scaled fixed-point INT32 "points", **not** floats: the real price times the symbol's multiplier (FX majors ×100000, JPY pairs & metals ×1000, indices/commodities ×100 — `source/shared/models/priceData.cppm`, `source/shared/utilities/symbolScale.cppm`). e.g. EURUSD 1.10001 is stored as 110001. - designated `timestamp` — written in nanoseconds, the QuestDB HTTP default precision. +- `source` — `SYMBOL`, the feed the tick came from: `IG` (IG Markets) or `MX` (Massive, formerly Polygon). Rows from before source tracking, and any tick with an empty or unrecognised source, are `NULL`. -The write side emits ILP lines of the form `{symbol} ask={..}i,bid={..}i {tsNanos}` (`ingestCommand.cppm`). The read side names columns explicitly and parses positionally as (symbol, ask, bid, timestamp) (`source/shared/questdb/sqlManager.cppm`), so tables must serve exactly those columns. +The write side emits ILP lines of the form `{symbol},source={IG|MX} ask={..}i,bid={..}i {tsNanos}` (`source/ingest/tickLine.cppm`), omitting the `source` tag when the feed id isn't one of the two known values. The read side names columns explicitly and parses positionally as (symbol, ask, bid, timestamp) (`source/shared/questdb/sqlManager.cppm`), so tables must serve those columns; `source` is not read yet, so backtests see both feeds' ticks together. Symbols are whitelisted: `SqlManager` throws `std::invalid_argument` for any symbol not in the `symbolScale` table, so a table named outside the whitelist is unreadable by the engine. +### How the `source` column appears + +- `source` must stay an ILP **tag**, which QuestDB stores as `SYMBOL`. If it were ever sent as a field (`source="IG"`), a new table would get a string column and every later tagged write to it would fail (counted as `post-failed`). +- QuestDB adds the column to an existing table on that table's first tagged line; no migration is needed. Existing rows are not rewritten and read as `NULL`. Backfilling them with `UPDATE` would rewrite every affected partition, so it is slow on large tables. +- A table created from scratch by `ingest` gets `source` from its first line. + ## Reachability `scripts/ingest.sh` probes `http://$QUESTDB_HOST:$QUESTDB_ILP_PORT/exec?query=SELECT%201` before starting — non-fatal, since the ingest writer buffers (and eventually sheds) while QuestDB is down. diff --git a/external/boost-decimal b/external/boost-decimal index e995162..a4958c2 160000 --- a/external/boost-decimal +++ b/external/boost-decimal @@ -1 +1 @@ -Subproject commit e9951622837f63cf0020b22834ad96b01293fd07 +Subproject commit a4958c2dcf00e33afe616fed46168ed1b9b0d265 diff --git a/external/libpqxx b/external/libpqxx index 698860c..950b9e1 160000 --- a/external/libpqxx +++ b/external/libpqxx @@ -1 +1 @@ -Subproject commit 698860cb4aef373d2da29ef14f13a08ef418e155 +Subproject commit 950b9e15e6cf9c3eaa409ea416e6cf7badcc2236 diff --git a/scripts/analysis.sh b/scripts/analysis.sh new file mode 100755 index 0000000..dac10e1 --- /dev/null +++ b/scripts/analysis.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# This executes the analysis script — run.sh's twin for the experiment queue: +# drains BACKTESTING_QUEUE_EXPERIMENT_RUN and evaluates each experiment chain +# against the QuestDB tick stream, reporting one aggregate document per +# experiment x symbol group to the weekly backtesting-experiments index. + +required_vars=(ELASTIC_HOST ELASTIC_USER ELASTIC_USER_PASSWORD REDIS_HOST) +missing=() +for var in "${required_vars[@]}"; do + [[ -z "${!var}" ]] && missing+=("$var") +done +if [[ ${#missing[@]} -gt 0 ]]; then + echo "Error: missing required environment variables: ${missing[*]}" + echo "eg. infisical run -- sh ./scripts/analysis.sh (if using infisical)" + echo "or ELASTIC_HOST=localhost ELASTIC_USER=elastic ELASTIC_USER_PASSWORD=password REDIS_HOST=localhost sh scripts/analysis.sh" + exit 1 +fi + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Build the source code +source "$current_dir/clean.sh" +if ! source "$current_dir/build.sh"; then + echo "Error: Build failed. Aborting." + exit 1 +fi + +# Debug: Check if the executable exists +if [ -f "$BUILD_DIR/$EXECUTABLE_NAME" ]; then + echo "Executable $EXECUTABLE_NAME found in $BUILD_DIR." +else + echo "Error: Executable $EXECUTABLE_NAME not found in $BUILD_DIR." + ls -la "$BUILD_DIR" # List the contents of the build directory for debugging + exit 1 +fi + +if ! redis-cli -h "$REDIS_HOST" ping >/dev/null 2>&1; then + echo "redis-server not reachable on $REDIS_HOST:6379 — skipping" + exit 0 +fi + +start_time=$(date +%s%N) +# Invoke the `analysis` subcommand: BacktestingEngine peeks a run descriptor +# off the experiment Redis queue and evaluates its experiment chains against +# the QuestDB host passed as the second argument (here, localhost). Daemon — +# Ctrl+C to stop (analysis has no shared-memory control channel; that is +# run-only). +./"$BUILD_DIR/$EXECUTABLE_NAME" analysis localhost +end_time=$(date +%s%N) +elapsed=$(( (end_time - start_time) / 1000000 )) +echo "Execution time: ${elapsed}ms" diff --git a/scripts/build.sh b/scripts/build.sh index e1ae778..5ef55cd 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -48,9 +48,16 @@ if command -v brew &>/dev/null; then LLVM_PREFIX="$(brew --prefix llvm)" export COMPILER_PATH="$LLVM_PREFIX/lib/c++" + # Pin OpenSSL 3: Homebrew's default `openssl` is now 4.x, which dropped + # ASN1_STRING_data, and the AWS SDK's static s2n still calls it. The + # engine's FindOpenSSL and s2n's Findcrypto (via CMAKE_PREFIX_PATH) must + # both resolve here, or the link pulls in OpenSSL 4 and fails. + OPENSSL_PREFIX="$(brew --prefix openssl@3)" TOOLCHAIN_ARGS+=( -DCMAKE_C_COMPILER="$LLVM_PREFIX/bin/clang" -DCMAKE_CXX_COMPILER="$LLVM_PREFIX/bin/clang++" + -DOPENSSL_ROOT_DIR="$OPENSSL_PREFIX" + -DCMAKE_PREFIX_PATH="$OPENSSL_PREFIX" ) else # Off Homebrew (Linux/CI): use Clang + libc++ so `import std` works, matching diff --git a/scripts/build_dep.sh b/scripts/build_dep.sh index 341935b..0cd1276 100755 --- a/scripts/build_dep.sh +++ b/scripts/build_dep.sh @@ -85,6 +85,14 @@ if ! command -v brew &>/dev/null; then -DCMAKE_CXX_COMPILER="${CXX:-clang++}" ) AWS_CXX_FLAGS="-w -stdlib=libc++" +else + # Build s2n/aws-c-cal against OpenSSL 3, matching build.sh: Homebrew's + # default `openssl` is now 4.x, which dropped APIs s2n still uses. + OPENSSL_PREFIX="$(brew --prefix openssl@3)" + AWS_CMAKE_ARGS+=( + -DOPENSSL_ROOT_DIR="$OPENSSL_PREFIX" + -DCMAKE_PREFIX_PATH="$OPENSSL_PREFIX" + ) fi AWS_SRC="$EXTERNAL_DIR/aws" diff --git a/scripts/experiments.sh b/scripts/experiments.sh new file mode 100755 index 0000000..3a5d892 --- /dev/null +++ b/scripts/experiments.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# Builds the engine and runs the `experiments` subcommand: expands the given +# experiment sweep's parameter grid into keyed Redis payloads plus a run +# descriptor on BACKTESTING_QUEUE_EXPERIMENT_RUN (see +# shared/utilities/queueKeys.hpp), and prepares the batch's weekly +# backtesting-experiments Elasticsearch index + -current alias. +# +# Usage mirrors load.sh: +# sh scripts/experiments.sh dipRecovery # queue one named sweep +# sh scripts/experiments.sh # queue the default sweep set +# +# Interactive: the engine prints the total sweep size and waits for Enter on +# stdin before touching Redis — run it from a TTY, or pipe the newline for +# non-interactive use (closed stdin counts as a decline and aborts). + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +experiments_args=("$@") + +# One batch label for every invocation below: the label names the weekly +# backtesting-experiments index (backtesting-experiments-2026-28, ...), and +# pinning it here keeps a load that straddles the Sunday-midnight-UTC ISO-week +# boundary from splitting one batch across two labels. Pre-set BACKTEST_BATCH +# wins — that is the re-run/test override. +export BACKTEST_BATCH="${BACKTEST_BATCH:-$(date -u +%G-%V)}" + +# Sweeps queued by a no-arg run. +all_sweeps=(dipRecovery rangeRelativeDipRecovery) + +if ! source "$current_dir/build.sh"; then + echo "Error: Build failed. Aborting." + exit 1 +fi + +if [ ! -f "$BUILD_DIR/$EXECUTABLE_NAME" ]; then + echo "Error: Executable $EXECUTABLE_NAME not found in $BUILD_DIR." + ls -la "$BUILD_DIR" + exit 1 +fi + +if ! redis-cli -h "${REDIS_HOST:-127.0.0.1}" ping >/dev/null 2>&1; then + echo "redis-server not reachable on ${REDIS_HOST:-127.0.0.1}:6379 — skipping" + exit 0 +fi + +if [ ${#experiments_args[@]} -gt 0 ]; then + exec ./"$BUILD_DIR/$EXECUTABLE_NAME" experiments "${experiments_args[@]}" +fi + +i=0 +for sweep in "${all_sweeps[@]}"; do + echo "" + echo "=== [$((++i))/${#all_sweeps[@]}] Loading experiment sweep: $sweep ===" + if ! ./"$BUILD_DIR/$EXECUTABLE_NAME" experiments "$sweep"; then + echo "Error: experiments failed for '$sweep'. Aborting remaining sweeps." + exit 1 + fi +done diff --git a/source/analysis/analysisCommand.cppm b/source/analysis/analysisCommand.cppm index a82dc4c..9754b63 100644 --- a/source/analysis/analysisCommand.cppm +++ b/source/analysis/analysisCommand.cppm @@ -29,4 +29,5 @@ int AnalysisCommand::run(const int argc, const char* argv[]) { // Redis host from the environment, mirroring `run` (runCommand.cppm). return AnalysisRunner::run(argv[2], env::getOr("REDIS_HOST", "127.0.0.1")); + } diff --git a/source/analysis/queue/drainExperiments.hpp b/source/analysis/queue/drainExperiments.hpp index 403a4e9..25e4029 100644 --- a/source/analysis/queue/drainExperiments.hpp +++ b/source/analysis/queue/drainExperiments.hpp @@ -6,6 +6,10 @@ #pragma once +// Boost.Redis's libc++ double converter uses HUGE_VAL and std::strtod without +// including /; include them first so this header stands alone. +#include +#include #include #include diff --git a/source/experiments/config/rangeRelativeDipRecovery/makeRangeRelativeDipRecovery.cppm b/source/experiments/config/rangeRelativeDipRecovery/makeRangeRelativeDipRecovery.cppm new file mode 100644 index 0000000..366a75f --- /dev/null +++ b/source/experiments/config/rangeRelativeDipRecovery/makeRangeRelativeDipRecovery.cppm @@ -0,0 +1,55 @@ +// Backtesting Engine in C++ +// +// (c) 2026 Ryan McCaffery | https://mccaffers.com +// This code is licensed under MIT license (see LICENSE.txt for details) +// --------------------------------------- + +module; + +#include +#include + +#include "shared/experiments/experimentConfig.hpp" + +export module makeRangeRelativeDipRecovery; + +import std; +import sweepCombination; // sweep::Combination + +export namespace sweep { + +// Map one swept parameter combination onto a rangeRelativeDipRecovery experiment: +// "Leg 1: RangeRelativeMove dip (DIRECTION = -1) with LEG1_LOOKBACK_MINUTES, +// LEG1_WINDOW_MINUTES, and LEG1_ATR_MULTIPLE. +// Leg 2: RangeRelativeMove recovery (DIRECTION = 1) with LEG1_LOOKBACK_MINUTES, +// LEG2_WINDOW_MINUTES, and LEG2_ATR_MULTIPLE". +experiments::ExperimentConfig makeRangeRelativeDipRecoveryExperiment( + const sweep::Combination& combo) { + using experiments::Activity; + using experiments::ActivityType; + return experiments::ExperimentConfig{ + .UUID = boost::uuids::to_string(boost::uuids::random_generator()()), + .NAME = "rangeRelativeDipRecovery", + .CHAIN = { + // Leg 1: the dip — RangeRelativeMove downwards (DIRECTION = -1) + Activity{ + .TYPE = ActivityType::RangeRelativeMove, + .WINDOW_SECONDS = combo.getInt("LEG1_WINDOW_MINUTES") * 60, + .LOOKBACK_SECONDS = combo.getInt("LEG1_LOOKBACK_MINUTES") * 60, + .DIRECTION = -1, + .ATR_MULTIPLE = combo.get("LEG1_ATR_MULTIPLE"), + }, + // Leg 2: the recovery — RangeRelativeMove upwards (DIRECTION = 1) + // Basis lookback matches leg 1's lookback basis + Activity{ + .TYPE = ActivityType::RangeRelativeMove, + .WINDOW_SECONDS = combo.getInt("LEG2_WINDOW_MINUTES") * 60, + .LOOKBACK_SECONDS = combo.getInt("LEG1_LOOKBACK_MINUTES") * 60, + .DIRECTION = 1, + .ATR_MULTIPLE = combo.get("LEG2_ATR_MULTIPLE"), + }, + }, + }; +} + +} // namespace sweep diff --git a/source/experiments/config/rangeRelativeDipRecovery/rangeRelativeDipRecoverySweep.cppm b/source/experiments/config/rangeRelativeDipRecovery/rangeRelativeDipRecoverySweep.cppm new file mode 100644 index 0000000..a6498c1 --- /dev/null +++ b/source/experiments/config/rangeRelativeDipRecovery/rangeRelativeDipRecoverySweep.cppm @@ -0,0 +1,44 @@ +// Backtesting Engine in C++ +// +// (c) 2026 Ryan McCaffery | https://mccaffers.com +// This code is licensed under MIT license (see LICENSE.txt for details) +// --------------------------------------- + +module; + +#include "shared/experiments/experimentConfig.hpp" + +export module rangeRelativeDipRecoverySweep; + +export import parameterGenerator; // sweep::ParameterGenerator, Combination + +import std; +import dipRecoverySweep; // sweep::ExperimentSweepSpec, ExperimentFactory +import makeRangeRelativeDipRecovery; // sweep::makeRangeRelativeDipRecoveryExperiment + +export namespace sweep { + +// Experiment sweep using ActivityType::RangeRelativeMove: +// How often does a volatility-scaled dip recover? +// Grid: +// LEG1_LOOKBACK_MINUTES {15, 30, 60} +// LEG1_ATR_MULTIPLE {1.0, 1.5, 2.0, 2.5} +// LEG1_WINDOW_MINUTES {5, 10, 30} +// LEG2_ATR_MULTIPLE {0.25, 0.5, 1.0} +// LEG2_WINDOW_MINUTES {5, 10, 30} +ExperimentSweepSpec buildRangeRelativeDipRecoverySweep() { + ParameterGenerator generator; + generator.addList("LEG1_LOOKBACK_MINUTES", {15, 30, 60}); + generator.addList("LEG1_ATR_MULTIPLE", {1.0, 1.5, 2.0, 2.5}); + generator.addList("LEG1_WINDOW_MINUTES", {5, 10, 30}); + generator.addList("LEG2_ATR_MULTIPLE", {0.25, 0.5, 1.0}); + generator.addList("LEG2_WINDOW_MINUTES", {5, 10, 30}); + return ExperimentSweepSpec{ + .generator = std::move(generator), + .factory = makeRangeRelativeDipRecoveryExperiment, + .lastMonths = 9, + .offsetMonths = 0, + }; +} + +} // namespace sweep diff --git a/source/experiments/experimentsCommand.cppm b/source/experiments/experimentsCommand.cppm index a1f2135..df395a2 100644 --- a/source/experiments/experimentsCommand.cppm +++ b/source/experiments/experimentsCommand.cppm @@ -26,6 +26,7 @@ import backtestLog; // backtest_log::logLine export import dipRecoverySweep; // sweep::ExperimentSweepSpec/ExperimentFactory, // buildDipRecoverySweep (re-exported so tests // reach the whole seam via one import) +export import rangeRelativeDipRecoverySweep; // sweep::buildRangeRelativeDipRecoverySweep import runConfigurationBuilder; // sweep::resolveSymbolGroups, currentBatchStamp import symbolGroups; // sweep::cleanSymbols @@ -61,6 +62,7 @@ std::string withThousands(const std::size_t n) { s.insert(pos, ","); } return s; + } // Payloads per pipelined Redis request / progress heartbeat — LoadCommand's @@ -155,10 +157,12 @@ int ExperimentsCommand::run(const int argc, const char* argv[]) { sweep::ExperimentSweepSpec spec; if (sweepName == "dipRecovery") { spec = sweep::buildDipRecoverySweep(); + } else if (sweepName == "rangeRelativeDipRecovery") { + spec = sweep::buildRangeRelativeDipRecoverySweep(); } else { std::println(stderr, "ExperimentsCommand: unknown experiment sweep '{}' " - "(valid: dipRecovery)", + "(valid: dipRecovery, rangeRelativeDipRecovery)", sweepName); return 1; } diff --git a/source/ingest/ingestCommand.cppm b/source/ingest/ingestCommand.cppm index c6c58b2..238cc20 100644 --- a/source/ingest/ingestCommand.cppm +++ b/source/ingest/ingestCommand.cppm @@ -5,9 +5,10 @@ // --------------------------------------- // // ingestCommand — the `ingest` subcommand. Receives tick datagrams over UDP, -// decodes them (tickPacket) and persists each into the local QuestDB via ILP -// over HTTP (questdbIngestClient), so the backtester's `run` path can read them -// back through the unchanged DatabaseConnection. +// decodes them (tickPacket), formats each as an ILP line tagged with its feed +// source (tickLine) and persists it into the local QuestDB via ILP over HTTP +// (questdbIngestClient), so the backtester's `run` path can read them back +// through the unchanged DatabaseConnection. // // The GMF only #includes Asio-/curl-free headers (the receiver and writer hide // those behind pimpls), so it is safe to `import std` here. @@ -23,8 +24,8 @@ export module ingestCommand; import std; import backtestLog; // backtest_log::logLine — timestamped, flushed stdout -import priceData; // PriceData -import tickPacket; // tick_packet::decodeTick +import tickLine; // tick_line::formatLine, isKnownSource +import tickPacket; // tick_packet::decodeTick, kSourceIG export class IngestCommand { public: @@ -66,6 +67,12 @@ int IngestCommand::run(const int argc, const char* argv[]) { std::atomic received{0}; std::atomic dropped{0}; + // Ticks received per feed source, logged so the feed mix is visible. + // "other" = empty or unrecognised source, written with a NULL source. + std::atomic fromIG{0}; + std::atomic fromMassive{0}; + std::atomic fromOther{0}; + net::UdpReceiver receiver( bindAddr, bindPort, [&](std::span bytes) { const auto tick = tick_packet::decodeTick(bytes); @@ -73,15 +80,13 @@ int IngestCommand::run(const int argc, const char* argv[]) { dropped.fetch_add(1, std::memory_order_relaxed); return; } - // ILP line: table = symbol, integer fields ask/bid, designated - // timestamp in nanoseconds (QuestDB HTTP default precision). This - // yields exactly the ask/bid/timestamp columns the read path expects. - const auto tsNanos = std::chrono::duration_cast( - tick->timestamp.time_since_epoch()) - .count(); - writer.enqueueLine(std::format("{} ask={}i,bid={}i {}\n", - tick->symbol, tick->ask, tick->bid, tsNanos)); + writer.enqueueLine(tick_line::formatLine(*tick)); received.fetch_add(1, std::memory_order_relaxed); + + auto& bySource = !tick_line::isKnownSource(tick->source) ? fromOther + : tick->source == tick_packet::kSourceIG ? fromIG + : fromMassive; + bySource.fetch_add(1, std::memory_order_relaxed); }); logLine("IngestCommand: starting; binding udp://{}:{} -> QuestDB ILP http://{}:{}/write", @@ -108,8 +113,12 @@ int IngestCommand::run(const int argc, const char* argv[]) { const auto delta = total - lastReceived; lastReceived = total; logLine("IngestCommand: received={} (+{}, ~{}/s); " + "sources IG={}, MX={}, other={}; " "decode-dropped={}, queue-dropped={}, post-failed={}", total, delta, delta / 60, + fromIG.load(std::memory_order_relaxed), + fromMassive.load(std::memory_order_relaxed), + fromOther.load(std::memory_order_relaxed), dropped.load(std::memory_order_relaxed), writer.droppedLines(), writer.failedLines()); } @@ -127,9 +136,10 @@ int IngestCommand::run(const int argc, const char* argv[]) { // decode-dropped: bad size / unknown symbol. queue-dropped: shed by the // writer when QuestDB couldn't keep up and the buffer hit its cap. // post-failed: lines a POST couldn't persist (transport error / non-2xx). - logLine("IngestCommand: shutting down (received={}, decode-dropped={}, " - "queue-dropped={}, post-failed={})", - received.load(), dropped.load(), writer.droppedLines(), + logLine("IngestCommand: shutting down (received={}, sources IG={}, MX={}, " + "other={}, decode-dropped={}, queue-dropped={}, post-failed={})", + received.load(), fromIG.load(), fromMassive.load(), fromOther.load(), + dropped.load(), writer.droppedLines(), writer.failedLines()); return 0; } diff --git a/source/ingest/questdbIngestClient.hpp b/source/ingest/questdbIngestClient.hpp index b52207f..983e00c 100644 --- a/source/ingest/questdbIngestClient.hpp +++ b/source/ingest/questdbIngestClient.hpp @@ -18,6 +18,7 @@ // thread and flushed in batches by an internal worker thread, so a slow QuestDB // never stalls the UDP receive loop. // +// // The libcurl implementation and the worker thread live entirely in the .cpp // (pimpl), keeping this header light enough to #include from a module's global // module fragment. diff --git a/source/ingest/tickLine.cppm b/source/ingest/tickLine.cppm new file mode 100644 index 0000000..fd27798 --- /dev/null +++ b/source/ingest/tickLine.cppm @@ -0,0 +1,48 @@ +// Backtesting Engine in C++ +// +// (c) 2026 Ryan McCaffery | https://mccaffers.com +// This code is licensed under MIT license (see LICENSE.txt for details) +// --------------------------------------- +// +// tickLine — formats one decoded tick as a QuestDB ILP line for the ingest. +// +// {symbol},source={IG|MX} ask={..}i,bid={..}i {tsNanos}\n +// +// table = symbol, `source` is an ILP tag (so QuestDB stores it as a SYMBOL +// column, added to each table on its first tagged line), integer fields +// ask/bid, and the designated timestamp in nanoseconds (QuestDB HTTP default +// precision). This yields the ask/bid/timestamp columns the read path expects, +// plus `source`. +// +// Only the known feed ids are written as tags. An empty or unrecognised source +// is written without the tag (QuestDB stores NULL), which keeps arbitrary bytes +// out of the line (tag values would otherwise need ILP escaping) and stops a +// stray value becoming a permanent entry in the SYMBOL table. + +export module tickLine; + +import std; // , , , +import priceData; // PriceData +import tickPacket; // tick_packet::DecodedTick, kSourceIG, kSourceMassive + +export namespace tick_line { + +// True for the feed ids the producers send ("IG", "MX"). +[[nodiscard]] inline bool isKnownSource(std::string_view source) noexcept { + return source == tick_packet::kSourceIG || source == tick_packet::kSourceMassive; +} + +[[nodiscard]] inline std::string formatLine(const tick_packet::DecodedTick& tick) { + const PriceData& price = tick.price; + const auto tsNanos = std::chrono::duration_cast( + price.timestamp.time_since_epoch()) + .count(); + if (isKnownSource(tick.source)) { + return std::format("{},source={} ask={}i,bid={}i {}\n", price.symbol, tick.source, + price.ask, price.bid, tsNanos); + } + return std::format("{} ask={}i,bid={}i {}\n", price.symbol, price.ask, price.bid, + tsNanos); +} + +} // namespace tick_line diff --git a/source/live/liveCommand.cppm b/source/live/liveCommand.cppm index 1590058..cc9387a 100644 --- a/source/live/liveCommand.cppm +++ b/source/live/liveCommand.cppm @@ -157,7 +157,7 @@ int LiveCommand::run(const int argc, const char* argv[]) { dropped.fetch_add(1, std::memory_order_relaxed); return; } - runner.onTick(*tick); + runner.onTick(tick->price); received.fetch_add(1, std::memory_order_relaxed); }); diff --git a/source/live/monitoring/liveTrace.cppm b/source/live/monitoring/liveTrace.cppm index 022c331..dd191eb 100644 --- a/source/live/monitoring/liveTrace.cppm +++ b/source/live/monitoring/liveTrace.cppm @@ -80,6 +80,10 @@ void init(std::string tradingEnv); // exercise init(); production never disarms. void disarm(); +// Restore the pre-init defaults — the test-only full reset so the "inert +// before init" assertion holds regardless of Catch2's randomised ordering. +void reset(); + // One relaxed atomic load. Call sites wrap emits in `if (enabled())` so a // disarmed trace also skips evaluating its field expressions. [[nodiscard]] bool enabled(); @@ -174,6 +178,13 @@ void disarm() { backtest_log::setSink(nullptr); } +void reset() { + armedFlag().store(false, std::memory_order_release); + envName() = "unknown"; + hostName() = "unknown"; + backtest_log::setSink(nullptr); +} + bool enabled() { return armedFlag().load(std::memory_order_relaxed); } std::string_view tradingEnv() { return envName(); } diff --git a/source/run/queue/drainRuns.hpp b/source/run/queue/drainRuns.hpp index 05a10e1..27dbfc6 100644 --- a/source/run/queue/drainRuns.hpp +++ b/source/run/queue/drainRuns.hpp @@ -6,6 +6,10 @@ #pragma once +// Boost.Redis's libc++ double converter uses HUGE_VAL and std::strtod without +// including /; include them first so this header stands alone. +#include +#include #include #include diff --git a/source/run/queue/runQueue.hpp b/source/run/queue/runQueue.hpp index f9fab5a..cee53e7 100644 --- a/source/run/queue/runQueue.hpp +++ b/source/run/queue/runQueue.hpp @@ -6,6 +6,10 @@ #pragma once +// Boost.Redis's libc++ double converter uses HUGE_VAL and std::strtod without +// including /; include them first so this header stands alone. +#include +#include #include #include #include diff --git a/source/shared/experiments/chainMatcher.cppm b/source/shared/experiments/chainMatcher.cppm index 8e4faf2..2f793fd 100644 --- a/source/shared/experiments/chainMatcher.cppm +++ b/source/shared/experiments/chainMatcher.cppm @@ -6,23 +6,31 @@ // // chainMatcher — the pure half of the analysis worker (trackingReport style): // counts non-overlapping occurrences of an experiment's activity chain in a -// tick stream. Everything here is I/O-free — ticks arrive as (mid, timestamp) -// pairs, the return values are plain data — so the unit tests need no servers. +// tick stream. Everything here is I/O-free — ticks arrive as (ask, bid, timestamp) +// tuples, the return values are plain data — so the unit tests need no servers. // // Matcher semantics (pinned by tests/chainMatcher.cpp): // // - Touch-within-window: a leg completes at the FIRST tick where its // condition holds with tickTs - anchorTs <= WINDOW_SECONDS. "Drops 1% in // 10m" = touches -1% at any tick inside the window. -// - Leg 1 is rolling, not anchored: tracked via trailing-window extremes -// (monotonic max/min deques with TIMESTAMP-based expiry — the +// - Leg 1 triggers on mid: rolling, not anchored, tracked via trailing-window +// extremes (monotonic max/min deques with TIMESTAMP-based expiry — the // rangeBarBuilder.cppm deque pattern adapted from tick-count to time // expiry; legitimate here because experiments are explicitly time-window // questions, and evaluation still advances only on tick timestamps — no // clock anywhere). -// - Anchoring: leg N+1 anchors at leg N's completion tick (price = that -// tick's mid, window from its timestamp); evaluation of leg N+1 starts on -// the NEXT tick, so a chain can never telescope through one tick. +// - Real execution prices: +// - Long entry fills at ask, take-profit triggers on bid, stop-loss / +// excursions measured on bid. +// - Short entry fills at bid, take-profit triggers on ask, stop-loss / +// excursions measured on ask. +// - Excursion / MAE immediately reflects spread at entry (entry_ask - bid +// for long, ask - entry_bid for short). +// - Anchoring: leg N+1 anchors at leg N's completion tick (entry fill price +// at leg 1 completion, completion price for subsequent legs; window from its +// timestamp); evaluation of leg N+1 starts on the NEXT tick, so a chain +// can never telescope through one tick. // - Expiry: the first tick past the window fails the whole chain (the // condition can no longer be touched inside the window) — reset to // scanning-for-leg-1 and re-evaluate leg 1 on that same tick. Leg-1 @@ -33,8 +41,8 @@ // - Serial attempts: a single anchor in flight; new leg-1 triggers are // picked up only after the current attempt fails (documented v1 // undercount vs multi-anchor). -// - Per-attempt recorder (phase 2): every attempt tracks its excursion -// above/below the LEG-1 anchor mid from the anchor tick to resolution +// - Per-attempt recorder: every attempt tracks its excursion +// above/below the leg-1 entry price from the anchor tick to resolution // (the resolving tick included, pre-anchor and post-resolution prices // excluded), the anchor->completion duration for completed attempts, and // the ask-bid spread at the anchor tick. Samples are exact up to @@ -82,122 +90,69 @@ export module chainMatcher; import std; import priceData; // PriceData — evaluateExperiment's input stream - -namespace { +import rollingTracker; // chain_matcher::Tracker — trailing-window extremes using TimePoint = std::chrono::system_clock::time_point; using Seconds = std::chrono::seconds; -// Trailing time-window extremes plus tick coverage, the rangeBarBuilder -// monotonic-deque pattern with timestamp expiry. Entries are valid while -// entry.ts + window >= ts (boundary inclusive, matching the anchored legs' -// tickTs - anchorTs <= WINDOW test). -class Tracker { -public: - explicit Tracker(const Seconds window) : window_(window) {} - - // Feed one tick: expire, snapshot the pre-insert extremes (NewExtreme's - // strict "beats every PRIOR tick in the lookback" test), insert, and - // record the inter-tick gap for the coverage measure. - void feed(const TimePoint ts, const std::int32_t mid) { - // Expire entries (and gap records) older than the trailing window. - while (!maxDeque_.empty() && maxDeque_.front().ts + window_ < ts) { - maxDeque_.pop_front(); - } - while (!minDeque_.empty() && minDeque_.front().ts + window_ < ts) { - minDeque_.pop_front(); - } - while (!gapDeque_.empty() && gapDeque_.front().ts + window_ < ts) { - gapDeque_.pop_front(); - } - - preMax_ = maxDeque_.empty() ? std::optional{} - : maxDeque_.front().price; - preMin_ = minDeque_.empty() ? std::optional{} - : minDeque_.front().price; +export namespace chain_matcher { - // Monotonic insert: popping equals keeps the newer entry, which - // survives expiry longer (rangeBarBuilder's rule). - while (!maxDeque_.empty() && maxDeque_.back().price <= mid) { - maxDeque_.pop_back(); - } - maxDeque_.push_back({ts, mid}); - while (!minDeque_.empty() && minDeque_.back().price >= mid) { - minDeque_.pop_back(); - } - minDeque_.push_back({ts, mid}); - - // Coverage: track the largest inter-tick gap still inside the window - // via its own monotonic max deque. The first tick ever fed counts as - // an infinite gap, so covered() only turns true once a full window of - // gap-free ticks has accumulated — warm-up and weekend gaps take the - // same path. - const auto gap = lastTick_ ? ts - *lastTick_ - : std::chrono::system_clock::duration::max(); - lastTick_ = ts; - while (!gapDeque_.empty() && gapDeque_.back().gap <= gap) { - gapDeque_.pop_back(); - } - gapDeque_.push_back({ts, gap}); - } +// Per-matcher cap on attempt SAMPLES (excursions/durations). Counts are +// never capped — beyond this, quantiles come from a truncated population and +// the outcome's samplesTruncated flag says so. +inline constexpr std::size_t kMaxAttemptSamples = 100'000; - [[nodiscard]] bool hasData() const { return !maxDeque_.empty(); } - [[nodiscard]] std::int32_t max() const { return maxDeque_.front().price; } - [[nodiscard]] std::int32_t min() const { return minDeque_.front().price; } - [[nodiscard]] std::optional preMax() const { return preMax_; } - [[nodiscard]] std::optional preMin() const { return preMin_; } +namespace detail { - // A full window of real tick history: no gap as long as the window itself - // remains inside it (the first-tick infinite gap covers warm-up). - [[nodiscard]] bool covered() const { - return !gapDeque_.empty() && gapDeque_.front().gap < window_; +// Which excursion side counts as FAVORABLE (MFE), from the final leg's +// implied direction. StaysInBand has none — "up" is the documented default. +inline bool favorableUp(const experiments::Activity& finalLeg) { + switch (finalLeg.TYPE) { + case experiments::ActivityType::DirectionalMove: + return finalLeg.MOVE_PERCENT >= 0.0; + case experiments::ActivityType::NewExtreme: + case experiments::ActivityType::RangeRelativeMove: + return finalLeg.DIRECTION >= 0; + case experiments::ActivityType::StaysInBand: + return true; } + return true; +} - void reset() { - maxDeque_.clear(); - minDeque_.clear(); - gapDeque_.clear(); - lastTick_.reset(); - preMax_.reset(); - preMin_.reset(); +// Determines whether the trade entered upon Leg 1 completion is Long or Short. +// For a multi-leg chain, the trade is entered to capture Leg 2 (and any +// subsequent legs), so its direction is defined by the first directional leg +// after Leg 1. For a single-leg chain, defaults to the single leg's direction. +inline bool isLongTrade(const std::vector& chain) { + if (chain.empty()) { + return true; } + for (std::size_t i = 1; i < chain.size(); ++i) { + const auto& leg = chain[i]; + switch (leg.TYPE) { + case experiments::ActivityType::DirectionalMove: + return leg.MOVE_PERCENT >= 0.0; + case experiments::ActivityType::NewExtreme: + case experiments::ActivityType::RangeRelativeMove: + return leg.DIRECTION >= 0; + case experiments::ActivityType::StaysInBand: + continue; + } + } + return favorableUp(chain.back()); +} -private: - struct Entry { - TimePoint ts; - std::int32_t price; - }; - struct GapEntry { - TimePoint ts; - std::chrono::system_clock::duration gap; - }; - - Seconds window_; - std::deque maxDeque_; - std::deque minDeque_; - std::deque gapDeque_; - std::optional lastTick_; - std::optional preMax_; - std::optional preMin_; -}; - -} // namespace - -export namespace chain_matcher { - -// Per-matcher cap on attempt SAMPLES (excursions/durations). Counts are -// never capped — beyond this, quantiles come from a truncated population and -// the outcome's samplesTruncated flag says so. -inline constexpr std::size_t kMaxAttemptSamples = 100'000; +} // namespace detail // One resolved attempt's excursion record: max points above/below the leg-1 -// anchor over the attempt's lifetime, plus the anchor itself so percent +// entry price over the attempt's lifetime, plus the entry price itself so percent // variants can be derived at aggregation time (FP once per attempt at most, // never per tick). struct ExcursionSample { std::int64_t abovePoints = 0; std::int64_t belowPoints = 0; - std::int32_t anchorMid = 0; + std::int32_t entryPrice = 0; + std::int32_t anchorMid = 0; // backwards-compatibility alias }; struct MatchStats { @@ -243,11 +198,15 @@ public: legs_.push_back(makeLeg(config.CHAIN[i], i)); } stats_.failuresByLeg.assign(legs_.size(), 0); + isLong_ = detail::isLongTrade(config.CHAIN); } - void onTick(const std::int32_t mid, const std::int32_t spread, + void onTick(const std::int32_t ask, const std::int32_t bid, const TimePoint ts) { - // All legs' rolling trackers are fed every tick (so a failed attempt + const auto mid = static_cast( + (static_cast(ask) + bid) / 2); + + // All legs' rolling trackers are fed every tick with mid (so a failed attempt // can re-evaluate leg 1 with no re-scan, and a chained leg's basis is // already warm when its turn comes); the state machine below consults // only the active leg. @@ -261,12 +220,19 @@ public: } if (active_ > 0) { - // The in-flight attempt's excursion vs its LEG-1 anchor — this + // The in-flight attempt's excursion vs its entry price — this // tick included, whether it advances, resolves, or expires the // attempt. - const auto cur = static_cast(mid); - attemptAbove_ = std::max(attemptAbove_, cur - attemptAnchorMid_); - attemptBelow_ = std::max(attemptBelow_, attemptAnchorMid_ - cur); + // Long: measured on bid. Short: measured on ask. + if (isLong_) { + const auto cur = static_cast(bid); + attemptAbove_ = std::max(attemptAbove_, cur - attemptAnchorPrice_); + attemptBelow_ = std::max(attemptBelow_, attemptAnchorPrice_ - cur); + } else { + const auto cur = static_cast(ask); + attemptAbove_ = std::max(attemptAbove_, cur - attemptAnchorPrice_); + attemptBelow_ = std::max(attemptBelow_, attemptAnchorPrice_ - cur); + } const LegRuntime& leg = legs_[active_]; if (ts - anchorTs_ > leg.window) { @@ -277,20 +243,26 @@ public: recordAttempt(/*completed=*/false, ts); ++stats_.failuresByLeg[active_]; active_ = 0; - } else if (conditionHolds(active_, mid)) { - completeActiveLeg(mid, spread, ts); + anchorThresholdValid_ = false; + anchorThreshold_ = 0; + } else if (conditionHolds(active_, isLong_ ? bid : ask)) { + completeActiveLeg(ask, bid, ts); return; } else { return; } } - // Scanning for leg 1 (rolling, no anchor). + // Scanning for leg 1 (rolling, no anchor, evaluates on mid). if (conditionHolds(0, mid)) { - completeActiveLeg(mid, spread, ts); + completeActiveLeg(ask, bid, ts); } } + void onTick(const std::int32_t price, const TimePoint ts) { + onTick(price, price, ts); + } + [[nodiscard]] const MatchStats& stats() const { return stats_; } // 0-based index of the leg currently being sought — for tests. @@ -437,7 +409,7 @@ private: return (cur - rollMin) * 100'000'000 >= rollMin * leg.pctScaled; } - const auto anchor = static_cast(anchorMid_); + const auto anchor = static_cast(anchorPrice_); if (leg.negativeMove) { return (anchor - cur) * 100'000'000 >= anchor * leg.pctScaled; } @@ -489,7 +461,7 @@ private: if (!anchorThresholdValid_) { return false; } - const auto anchor = static_cast(anchorMid_); + const auto anchor = static_cast(anchorPrice_); if (leg.direction < 0) { return anchor - cur >= anchorThreshold_; } @@ -510,7 +482,8 @@ private: return; } samples.push_back({attemptAbove_, attemptBelow_, - static_cast(attemptAnchorMid_)}); + static_cast(attemptAnchorPrice_), + static_cast(attemptAnchorPrice_)}); if (completed) { stats_.completionSeconds.push_back( std::chrono::duration_cast( @@ -523,20 +496,30 @@ private: // (count it, clear everything, resume next tick) or the next leg anchors // here — and, since evaluation returns to the caller, it is first // evaluated on the NEXT tick, never its own anchor tick. - void completeActiveLeg(const std::int32_t mid, const std::int32_t spread, + void completeActiveLeg(const std::int32_t ask, const std::int32_t bid, const TimePoint ts) { if (active_ == 0) { ++stats_.attempts; // a leg-1 completion IS an attempt - // Arm the attempt recorder at the leg-1 anchor: excursions are - // measured from HERE for the whole chain (anchorMid_ moves at - // every later leg; this baseline must not), and the spread is - // sampled at this tick only. - attemptAnchorMid_ = mid; - attemptAnchorTs_ = ts; - attemptAbove_ = 0; - attemptBelow_ = 0; + const auto spread = + std::max(0, static_cast(ask) - bid); stats_.spreadAtTriggerSum += spread; ++stats_.spreadAtTriggerCount; + + attemptAnchorTs_ = ts; + attemptAnchorPrice_ = isLong_ ? ask : bid; + + if (legs_.size() > 1) { + if (isLong_) { + attemptAbove_ = 0; + attemptBelow_ = spread; + } else { + attemptAbove_ = spread; + attemptBelow_ = 0; + } + } else { + attemptAbove_ = 0; + attemptBelow_ = 0; + } } if (active_ + 1 == legs_.size()) { ++stats_.occurrences; @@ -545,7 +528,8 @@ private: return; } ++active_; - anchorMid_ = mid; + anchorPrice_ = + (active_ == 1) ? (isLong_ ? ask : bid) : (isLong_ ? bid : ask); anchorTs_ = ts; // RangeRelativeMove anchored legs freeze their threshold from the @@ -569,6 +553,8 @@ private: // (documented slight undercount). void resetAfterMatch() { active_ = 0; + anchorThresholdValid_ = false; + anchorThreshold_ = 0; for (LegRuntime& leg : legs_) { if (leg.moveTracker) { leg.moveTracker->reset(); @@ -583,12 +569,13 @@ private: MatchStats stats_; std::size_t maxSamples_ = kMaxAttemptSamples; std::size_t active_ = 0; // index of the leg currently sought - std::int32_t anchorMid_ = 0; // active leg's anchor (legs > 0) + std::int32_t anchorPrice_ = 0; // active leg's anchor price (legs > 0) TimePoint anchorTs_{}; std::int64_t anchorThreshold_ = 0; // frozen RangeRelativeMove threshold bool anchorThresholdValid_ = false; - // The in-flight attempt's recorder state (leg-1 anchor baseline). - std::int64_t attemptAnchorMid_ = 0; + bool isLong_ = true; + // The in-flight attempt's recorder state (leg-1 entry baseline). + std::int64_t attemptAnchorPrice_ = 0; TimePoint attemptAnchorTs_{}; std::int64_t attemptAbove_ = 0; std::int64_t attemptBelow_ = 0; @@ -610,21 +597,6 @@ inline experiments::QuantilePair quantiles(std::vector& values) { return {nearestRank(values, 0.5), nearestRank(values, 0.9)}; } -// Which excursion side counts as FAVORABLE (MFE), from the final leg's -// implied direction. StaysInBand has none — "up" is the documented default. -inline bool favorableUp(const experiments::Activity& finalLeg) { - switch (finalLeg.TYPE) { - case experiments::ActivityType::DirectionalMove: - return finalLeg.MOVE_PERCENT >= 0.0; - case experiments::ActivityType::NewExtreme: - case experiments::ActivityType::RangeRelativeMove: - return finalLeg.DIRECTION >= 0; - case experiments::ActivityType::StaysInBand: - return true; - } - return true; -} - // Summarise one attempt population; nullopt when empty (the doc then reads // null, never fake zeros). inline std::optional summariseExcursions( @@ -644,8 +616,9 @@ inline std::optional summariseExcursions( static_cast(up ? s.belowPoints : s.abovePoints); mfePoints.push_back(favorable); maePoints.push_back(adverse); - mfePercent.push_back(100.0 * favorable / s.anchorMid); - maePercent.push_back(100.0 * adverse / s.anchorMid); + const auto base = s.entryPrice != 0 ? s.entryPrice : s.anchorMid; + mfePercent.push_back(base != 0 ? 100.0 * favorable / base : 0.0); + maePercent.push_back(base != 0 ? 100.0 * adverse / base : 0.0); } return experiments::ExcursionStats{ .samples = samples.size(), @@ -689,8 +662,6 @@ experiments::ExperimentOutcome evaluateExperiment( TimePoint minTs = TimePoint::max(); TimePoint maxTs = TimePoint::min(); for (const PriceData& tick : ticks) { - const auto mid = static_cast( - (static_cast(tick.ask) + tick.bid) / 2); ChainMatcher& matcher = matchers.try_emplace(tick.symbol, config).first->second; // Delta-detection: a matcher completes at most one occurrence per @@ -698,7 +669,7 @@ experiments::ExperimentOutcome evaluateExperiment( // calendar bucketing lives HERE, keeping the matcher core free of // it. const std::uint64_t before = matcher.stats().occurrences; - matcher.onTick(mid, tick.ask - tick.bid, tick.timestamp); + matcher.onTick(tick.ask, tick.bid, tick.timestamp); if (matcher.stats().occurrences > before) { const auto day = std::chrono::floor(tick.timestamp); @@ -763,7 +734,7 @@ experiments::ExperimentOutcome evaluateExperiment( outcome.samplesTruncated || stats.samplesTruncated; } - const bool up = detail::favorableUp(config.CHAIN.back()); + const bool up = detail::isLongTrade(config.CHAIN); outcome.excursionOrientation = up ? "up" : "down"; outcome.completedAttempts = detail::summariseExcursions(completed, up); outcome.failedAttempts = detail::summariseExcursions(failed, up); diff --git a/source/shared/experiments/rollingTracker.cppm b/source/shared/experiments/rollingTracker.cppm new file mode 100644 index 0000000..bc15987 --- /dev/null +++ b/source/shared/experiments/rollingTracker.cppm @@ -0,0 +1,117 @@ +// Backtesting Engine in C++ +// +// (c) 2026 Ryan McCaffery | https://mccaffers.com +// This code is licensed under MIT license (see LICENSE.txt for details) +// --------------------------------------- +// +// rollingTracker — chainMatcher's trailing-window plumbing, on its own so the +// matcher file is state machine only. Trailing time-window extremes plus tick +// coverage, the rangeBarBuilder monotonic-deque pattern with timestamp expiry. +// Entries are valid while entry.ts + window >= ts (boundary inclusive, +// matching the anchored legs' tickTs - anchorTs <= WINDOW test). + +module; + +export module rollingTracker; + +import std; // replaces , , + +export namespace chain_matcher { + +// One trailing window over a tick stream: feed every (mid, timestamp) pair +// in stream order; query the extremes, their pre-insert snapshots (NewExtreme's +// strict "beats every PRIOR tick in the lookback" test), and whether a full +// window of gap-free ticks has accumulated (the first tick ever fed counts as +// an infinite gap, so covered() also encodes warm-up). +class Tracker { +public: + using TimePoint = std::chrono::system_clock::time_point; + + explicit Tracker(const std::chrono::seconds window) : window_(window) {} + + // Expire entries (and gap records) older than the trailing window, + // snapshot the pre-insert extremes, insert, and record the inter-tick + // gap for the coverage measure. + void feed(const TimePoint ts, const std::int32_t mid) { + // Expire entries (and gap records) older than the trailing window. + while (!maxDeque_.empty() && maxDeque_.front().ts + window_ < ts) { + maxDeque_.pop_front(); + } + while (!minDeque_.empty() && minDeque_.front().ts + window_ < ts) { + minDeque_.pop_front(); + } + while (!gapDeque_.empty() && gapDeque_.front().ts + window_ < ts) { + gapDeque_.pop_front(); + } + + preMax_ = maxDeque_.empty() ? std::optional{} + : maxDeque_.front().price; + preMin_ = minDeque_.empty() ? std::optional{} + : minDeque_.front().price; + + // Monotonic insert: popping equals keeps the newer entry, which + // survives expiry longer (rangeBarBuilder's rule). + while (!maxDeque_.empty() && maxDeque_.back().price <= mid) { + maxDeque_.pop_back(); + } + maxDeque_.push_back({ts, mid}); + while (!minDeque_.empty() && minDeque_.back().price >= mid) { + minDeque_.pop_back(); + } + minDeque_.push_back({ts, mid}); + + // Coverage: track the largest inter-tick gap still inside the window + // via its own monotonic max deque. The first tick ever fed counts as + // an infinite gap, so covered() only turns true once a full window + // of gap-free ticks has accumulated — warm-up and weekend gaps take + // the same path. + const auto gap = lastTick_ ? ts - *lastTick_ + : std::chrono::system_clock::duration::max(); + lastTick_ = ts; + while (!gapDeque_.empty() && gapDeque_.back().gap <= gap) { + gapDeque_.pop_back(); + } + gapDeque_.push_back({ts, gap}); + } + + [[nodiscard]] bool hasData() const { return !maxDeque_.empty(); } + [[nodiscard]] std::int32_t max() const { return maxDeque_.front().price; } + [[nodiscard]] std::int32_t min() const { return minDeque_.front().price; } + [[nodiscard]] std::optional preMax() const { return preMax_; } + [[nodiscard]] std::optional preMin() const { return preMin_; } + + // A full window of real tick history: no gap as long as the window itself + // remains inside it (the first-tick infinite gap covers warm-up). + [[nodiscard]] bool covered() const { + return !gapDeque_.empty() && gapDeque_.front().gap < window_; + } + + void reset() { + maxDeque_.clear(); + minDeque_.clear(); + gapDeque_.clear(); + lastTick_.reset(); + preMax_.reset(); + preMin_.reset(); + } + +private: + struct Entry { + TimePoint ts; + std::int32_t price; + }; + struct GapEntry { + TimePoint ts; + std::chrono::system_clock::duration gap; + }; + + std::chrono::seconds window_; + std::deque maxDeque_; + std::deque minDeque_; + std::deque gapDeque_; + std::optional lastTick_; + std::optional preMax_; + std::optional preMin_; +}; + +} // namespace chain_matcher diff --git a/source/shared/net/tickPacket.cppm b/source/shared/net/tickPacket.cppm index 59b2729..66814ba 100644 --- a/source/shared/net/tickPacket.cppm +++ b/source/shared/net/tickPacket.cppm @@ -15,7 +15,10 @@ // 8 8 double ask // 16 8 int64 tsMicros // Unix epoch microseconds, UTC // 24 16 char symbol[16] // ASCII, NUL-padded, e.g. "EURUSD\0\0..." -// // total = 40 bytes +// 40 8 char source[8] // ASCII, NUL-padded feed id: "IG" | "MX" +// // total = 48 bytes +// +// The C# producer is vortex/shared/TickSerializer.cs (feed ids in TickSource.cs). // // x86-64 and ARM64 (the only deploy targets) are little-endian, so the bytes map // straight onto the native scalars with no byte-swapping; the C# sender writes @@ -24,8 +27,10 @@ // decodeTick turns those bytes into the engine's existing PriceData: it scales // the real bid/ask into the stored INT32 "points" via the symbol's price // multiplier (symbol_scale::getPriceScale) so the ingest writes exactly what the -// backtester's read path already expects. Shared by the ingest and live -// subcommands — both consume the same wire format, on different ports. +// backtester's read path already expects. The source rides alongside in +// DecodedTick rather than in PriceData, so the backtester's per-tick storage +// doesn't grow. Shared by the ingest and live subcommands — both consume the +// same wire format, on different ports. export module tickPacket; @@ -42,7 +47,20 @@ inline constexpr std::size_t kAskOffset = 8; inline constexpr std::size_t kTsOffset = 16; inline constexpr std::size_t kSymbolOffset = 24; inline constexpr std::size_t kSymbolSize = 16; -inline constexpr std::size_t kPacketSize = 40; +inline constexpr std::size_t kSourceOffset = 40; +inline constexpr std::size_t kSourceSize = 8; +inline constexpr std::size_t kPacketSize = 48; + +// Feed ids the producers send (== vortex/shared/TickSource.cs). +inline constexpr std::string_view kSourceIG = "IG"; // IG Markets Lightstreamer +inline constexpr std::string_view kSourceMassive = "MX"; // Massive (formerly Polygon) + +// A decoded datagram: the engine's PriceData plus the feed it came from +// ("IG", "MX"; empty if the producer left the field blank). +struct DecodedTick { + PriceData price; + std::string source; +}; namespace detail { @@ -56,11 +74,23 @@ template return std::bit_cast(raw); } +// Copy a fixed-width NUL-padded ASCII field out of `bytes`, trimming at the +// first NUL (a field filled to full width has no NUL and is taken whole). +[[nodiscard]] inline std::string readString(std::span bytes) { + std::string out(bytes.size(), '\0'); + std::ranges::transform(bytes, out.begin(), + [](std::byte b) { return static_cast(b); }); + if (const auto nul = out.find('\0'); nul != std::string::npos) { + out.resize(nul); + } + return out; +} + } // namespace detail -// Decode one datagram into a PriceData. Returns nullopt — i.e. "drop this tick" -// rather than a hard error, since UDP is best-effort and a stray/garbled or -// malformed datagram must not take the ingest down — for any of: +// Decode one datagram into a DecodedTick. Returns nullopt — i.e. "drop this +// tick" rather than a hard error, since UDP is best-effort and a stray/garbled +// or malformed datagram must not take the ingest down — for any of: // - a wrong-sized packet, // - a bid or ask that isn't finite and strictly positive — zero is the // bad-data signature found in the historical feed (the C# producer already @@ -73,7 +103,7 @@ template // - an unknown symbol (multiplier 0), // - a scaled price that overflows INT32 (latent: real prices sit ~300x below // the limit, but we never silently store a truncated value). -[[nodiscard]] inline std::optional decodeTick(std::span bytes) { +[[nodiscard]] inline std::optional decodeTick(std::span bytes) { if (bytes.size() != kPacketSize) { return std::nullopt; } @@ -100,13 +130,8 @@ template return std::nullopt; } - // Symbol: NUL-padded ASCII in a fixed 16-byte field. Copy out the bytes and - // trim at the first NUL. - std::array symBuf{}; - std::ranges::transform(bytes.subspan(kSymbolOffset, kSymbolSize), symBuf.begin(), - [](std::byte b) { return static_cast(b); }); - const auto nul = std::ranges::find(symBuf, '\0'); - const std::string symbol(symBuf.begin(), nul); + // Symbol: NUL-padded ASCII in a fixed 16-byte field. + const std::string symbol = detail::readString(bytes.subspan(kSymbolOffset, kSymbolSize)); const int multiplier = symbol_scale::getPriceScale(symbol); if (multiplier == symbol_scale::kUnknown) { @@ -138,7 +163,13 @@ template std::chrono::duration_cast( std::chrono::microseconds(tsMicros))); - return PriceData(*askScaled, *bidScaled, timestamp, symbol); + // Source: NUL-padded ASCII feed id. Not validated against a list — an + // unexpected id is still a good tick; the ingest writes it with a NULL + // source and counts it (see tickLine). + std::string source = detail::readString(bytes.subspan(kSourceOffset, kSourceSize)); + + return DecodedTick{PriceData(*askScaled, *bidScaled, timestamp, symbol), + std::move(source)}; } } // namespace tick_packet diff --git a/source/shared/net/udpPorts.hpp b/source/shared/net/udpPorts.hpp index 431e8e7..4d0611f 100644 --- a/source/shared/net/udpPorts.hpp +++ b/source/shared/net/udpPorts.hpp @@ -11,7 +11,7 @@ // The C# streamer (vortex/shared/UDPPorts.cs) fans its serialized feeds out to // several UDP sinks. This engine consumes three of them: the persistence // stream (ingest -> QuestDB) and the live stream (live -> strategy execution), -// both carrying 40-byte tick packets, plus the deal stream (tracking -> +// both carrying 48-byte tick packets, plus the deal stream (tracking -> // position updates) carrying 256-byte deal packets. Keep these in lockstep // with UDPPorts.cs. Each bind port is also overridable at runtime (CLI arg / // $INGEST_UDP_PORT / $LIVE_UDP_PORT / $TRACKING_UDP_PORT). diff --git a/source/shared/tradingDefinitions.hpp b/source/shared/tradingDefinitions.hpp index 8a04289..ab3f048 100644 --- a/source/shared/tradingDefinitions.hpp +++ b/source/shared/tradingDefinitions.hpp @@ -13,4 +13,4 @@ #include "shared/tradingDefinitions/variables/strategyVariables.hpp" #include "shared/tradingDefinitions/strategyConfig.hpp" #include "shared/tradingDefinitions/config/configuration.hpp" -#include "shared/tradingDefinitions/config/runConfiguration.hpp" +#include "shared/tradingDefinitions/config/runConfiguration.hpp" \ No newline at end of file diff --git a/source/shared/utilities/base64.cpp b/source/shared/utilities/base64.cpp index 9d16c2d..cdc6049 100644 --- a/source/shared/utilities/base64.cpp +++ b/source/shared/utilities/base64.cpp @@ -6,23 +6,40 @@ #include "shared/utilities/base64.hpp" -static const char* const B64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +#include +#include +#include + +namespace { + +constexpr char B64chars[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +constexpr auto buildDecodeTable() { + std::array table{}; + table.fill(-1); + for (int i = 0; i < 26; ++i) { + table[static_cast('A' + i)] = static_cast(i); + table[static_cast('a' + i)] = static_cast(26 + i); + } + for (int i = 0; i < 10; ++i) { + table[static_cast('0' + i)] = static_cast(52 + i); + } + table[static_cast('+')] = 62; + table[static_cast('-')] = 62; // URL-safe alternative + table[static_cast('/')] = 63; + table[static_cast('_')] = 63; // URL-safe alternative + return table; +} + +constexpr auto B64decodeTable = buildDecodeTable(); + +} // namespace -static const int B64index[256] = -{ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 63, - 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 -}; - -// Code adapted from Stack Overflow https://stackoverflow.com/a/37109258/20806857 const std::string Base64::b64encode(const unsigned char* data, const size_t &len) { + if (data == nullptr || len == 0) return ""; + std::string result((len + 2) / 3 * 4, '='); const unsigned char *p = data; char *str = &result[0]; @@ -49,33 +66,70 @@ const std::string Base64::b64encode(const unsigned char* data, const size_t &len const std::string Base64::b64decode(const unsigned char* data, const size_t &len) { - if (len == 0) return ""; + if (data == nullptr || len == 0) return ""; - const unsigned char *p = data; - size_t j = 0, - pad1 = len % 4 || p[len - 1] == '=', - pad2 = pad1 && (len % 4 > 2 || p[len - 2] != '='); - const size_t last = (len - pad1) / 4 << 2; - std::string result(last / 4 * 3 + pad1 + pad2, '\0'); - unsigned char *str = (unsigned char*) &result[0]; - - for (size_t i = 0; i < last; i += 4) - { - int n = B64index[p[i]] << 18 | B64index[p[i + 1]] << 12 | B64index[p[i + 2]] << 6 | B64index[p[i + 3]]; - str[j++] = n >> 16; - str[j++] = n >> 8 & 0xFF; - str[j++] = n & 0xFF; + if (len % 4 != 0) { + throw std::invalid_argument( + "Base64::b64decode: input length must be a multiple of 4 (got " + + std::to_string(len) + ")"); } - if (pad1) - { - int n = B64index[p[last]] << 18 | B64index[p[last + 1]] << 12; - str[j++] = n >> 16; - if (pad2) - { - n |= B64index[p[last + 2]] << 6; - str[j++] = n >> 8 & 0xFF; + + size_t padCount = 0; + if (data[len - 1] == '=') ++padCount; + if (len >= 2 && data[len - 2] == '=') ++padCount; + + if (padCount > 2) { + throw std::invalid_argument("Base64::b64decode: invalid padding"); + } + + const size_t outputSize = (len / 4) * 3 - padCount; + std::string result; + result.reserve(outputSize); + + const size_t numChunks = len / 4; + for (size_t c = 0; c < numChunks; ++c) { + const size_t i = c * 4; + const unsigned char c0 = data[i]; + const unsigned char c1 = data[i + 1]; + const unsigned char c2 = data[i + 2]; + const unsigned char c3 = data[i + 3]; + + const int8_t v0 = B64decodeTable[c0]; + const int8_t v1 = B64decodeTable[c1]; + + if (v0 < 0 || v1 < 0) { + throw std::invalid_argument("Base64::b64decode: invalid character"); + } + + if (c == numChunks - 1 && padCount > 0) { + if (padCount == 2) { + if (c2 != '=' || c3 != '=') { + throw std::invalid_argument( + "Base64::b64decode: invalid padding sequence"); + } + result.push_back(static_cast((v0 << 2) | (v1 >> 4))); + } else { + const int8_t v2 = B64decodeTable[c2]; + if (v2 < 0 || c3 != '=') { + throw std::invalid_argument( + "Base64::b64decode: invalid padding sequence or character"); + } + result.push_back(static_cast((v0 << 2) | (v1 >> 4))); + result.push_back(static_cast(((v1 & 0x0F) << 4) | (v2 >> 2))); + } + } else { + const int8_t v2 = B64decodeTable[c2]; + const int8_t v3 = B64decodeTable[c3]; + if (v2 < 0 || v3 < 0) { + throw std::invalid_argument( + "Base64::b64decode: invalid character or mid-stream padding"); + } + result.push_back(static_cast((v0 << 2) | (v1 >> 4))); + result.push_back(static_cast(((v1 & 0x0F) << 4) | (v2 >> 2))); + result.push_back(static_cast(((v2 & 0x03) << 6) | v3)); } } + return result; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 25ebc8c..da3df3d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(unit_tests symbolScale.cpp marketHours.cpp jsonParser.cpp + base64.cpp ohlc.cpp ema.cpp atr.cpp @@ -33,6 +34,7 @@ add_executable(unit_tests tradeManager.cpp tradeDocument.cpp tickPacket.cpp + tickLine.cpp dealPacket.cpp liveWinners.cpp liveRunner.cpp diff --git a/tests/base64.cpp b/tests/base64.cpp new file mode 100644 index 0000000..e366332 --- /dev/null +++ b/tests/base64.cpp @@ -0,0 +1,87 @@ +// Backtesting Engine in C++ +// +// (c) 2025 Ryan McCaffery | https://mccaffers.com +// This code is licensed under MIT license (see LICENSE.txt for details) +// --------------------------------------- + +#include + +#include +#include +#include + +#include "shared/utilities/base64.hpp" + +TEST_CASE("Base64 RFC 4648 test vectors", "[base64]") { + CHECK(Base64::b64encode("") == ""); + CHECK(Base64::b64decode("") == ""); + + CHECK(Base64::b64encode("f") == "Zg=="); + CHECK(Base64::b64decode("Zg==") == "f"); + + CHECK(Base64::b64encode("fo") == "Zm8="); + CHECK(Base64::b64decode("Zm8=") == "fo"); + + CHECK(Base64::b64encode("foo") == "Zm9v"); + CHECK(Base64::b64decode("Zm9v") == "foo"); + + CHECK(Base64::b64encode("foob") == "Zm9vYg=="); + CHECK(Base64::b64decode("Zm9vYg==") == "foob"); + + CHECK(Base64::b64encode("fooba") == "Zm9vYmE="); + CHECK(Base64::b64decode("Zm9vYmE=") == "fooba"); + + CHECK(Base64::b64encode("foobar") == "Zm9vYmFy"); + CHECK(Base64::b64decode("Zm9vYmFy") == "foobar"); +} + +TEST_CASE("Base64 binary data round-trips correctly", "[base64]") { + std::string allBytes; + allBytes.reserve(256); + for (int i = 0; i < 256; ++i) { + allBytes.push_back(static_cast(i)); + } + + const std::string encoded = Base64::b64encode(allBytes); + const std::string decoded = Base64::b64decode(encoded); + CHECK(decoded == allBytes); +} + +TEST_CASE("Base64 URL-safe characters decode identically", "[base64]") { + // '+' -> '-' (index 62) and '/' -> '_' (index 63) + // "????" with binary values 62, 63, 62, 63: + // Standard: "+/+/" + // URL-safe: "-_-_" + const std::string standard = "+/+/"; + const std::string urlSafe = "-_-_"; + CHECK(Base64::b64decode(standard) == Base64::b64decode(urlSafe)); +} + +TEST_CASE("Base64 rejects strings whose length is not a multiple of 4", "[base64]") { + CHECK_THROWS_AS(Base64::b64decode("a"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("ab"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("abc"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("abcde"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("abcdef"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("abcdefg"), std::invalid_argument); +} + +TEST_CASE("Base64 rejects invalid characters", "[base64]") { + CHECK_THROWS_AS(Base64::b64decode("Zm9 v"), std::invalid_argument); // space + CHECK_THROWS_AS(Base64::b64decode("Zm9\nv"), std::invalid_argument); // newline + CHECK_THROWS_AS(Base64::b64decode("Zm@v"), std::invalid_argument); // '@' + CHECK_THROWS_AS(Base64::b64decode("Zm#v"), std::invalid_argument); // '#' + CHECK_THROWS_AS(Base64::b64decode("Zm$v"), std::invalid_argument); // '$' + CHECK_THROWS_AS(Base64::b64decode("Zm%v"), std::invalid_argument); // '%' +} + +TEST_CASE("Base64 rejects invalid padding configurations", "[base64]") { + CHECK_THROWS_AS(Base64::b64decode("===="), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("A==="), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("AA=A"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("A=AA"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("=AAA"), std::invalid_argument); + CHECK_THROWS_AS(Base64::b64decode("=AA="), std::invalid_argument); + // Mid-stream padding + CHECK_THROWS_AS(Base64::b64decode("Zm==Zm9v"), std::invalid_argument); +} diff --git a/tests/chainMatcher.cpp b/tests/chainMatcher.cpp index 440a398..7a96b76 100644 --- a/tests/chainMatcher.cpp +++ b/tests/chainMatcher.cpp @@ -54,22 +54,22 @@ Activity move(const double signedPercent, const int windowSeconds) { TEST_CASE("DirectionalMove fires on a touch within the trailing window", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600)})); - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99600, 0, at(60)); + matcher.onTick(100000, at(0)); + matcher.onTick(99600, at(60)); CHECK(matcher.stats().occurrences == 0); // Exactly -1% from the rolling max, well inside the 10-minute window. - matcher.onTick(99000, 0, at(120)); + matcher.onTick(99000, at(120)); CHECK(matcher.stats().occurrences == 1); } TEST_CASE("DirectionalMove does not fire when the drop is slower than the window", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600)})); - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99500, 0, at(550)); + matcher.onTick(100000, at(0)); + matcher.onTick(99500, at(550)); // The 100000 print expired from the trailing 600s window (deque expiry), // so the rolling max is 99500 and the remaining drop is only 0.5%. - matcher.onTick(99000, 0, at(1101)); + matcher.onTick(99000, at(1101)); CHECK(matcher.stats().occurrences == 0); } @@ -79,22 +79,22 @@ TEST_CASE("split drop still fires via rolling extremes (anti-re-anchor)", // re-anchored at the first partial drop would miss this; the rolling max // sees the cumulative 1.1%. ChainMatcher matcher(chainOf({move(-1.0, 600)})); - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99400, 0, at(100)); + matcher.onTick(100000, at(0)); + matcher.onTick(99400, at(100)); CHECK(matcher.stats().occurrences == 0); - matcher.onTick(98900, 0, at(200)); + matcher.onTick(98900, at(200)); CHECK(matcher.stats().occurrences == 1); } TEST_CASE("two-leg chain anchors at leg 1's completion tick", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600), move(0.5, 600)})); - matcher.onTick(100000, 0, at(0)); + matcher.onTick(100000, at(0)); CHECK(matcher.activeLeg() == 0); - matcher.onTick(99000, 0, at(60)); // leg 1 completes: anchor (99000, 60) + matcher.onTick(99000, at(60)); // leg 1 completes: anchor (99000, 60) CHECK(matcher.activeLeg() == 1); CHECK(matcher.stats().occurrences == 0); // +0.5% from the ANCHOR (99000 -> 99495), inside leg 2's own window. - matcher.onTick(99495, 0, at(120)); + matcher.onTick(99495, at(120)); CHECK(matcher.stats().occurrences == 1); CHECK(matcher.activeLeg() == 0); } @@ -111,27 +111,27 @@ TEST_CASE("a leg cannot complete on its own anchor tick", "[chainMatcher]") { .LOOKBACK_SECONDS = 300}, })); for (long t = 0; t <= 300; t += 30) { - matcher.onTick(100000, 0, at(t)); + matcher.onTick(100000, at(t)); } CHECK(matcher.activeLeg() == 0); - matcher.onTick(99000, 0, at(330)); // -1%: leg 1 completes here + matcher.onTick(99000, at(330)); // -1%: leg 1 completes here CHECK(matcher.activeLeg() == 1); // The band (range 1000 <= 5% of mid, fully covered) held at the anchor // tick too — but only the NEXT tick may complete the chain. CHECK(matcher.stats().occurrences == 0); - matcher.onTick(99000, 0, at(360)); + matcher.onTick(99000, at(360)); CHECK(matcher.stats().occurrences == 1); } TEST_CASE("an expired attempt counts a failure against the leg being sought", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600), move(0.5, 60)})); - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99000, 0, at(100)); // leg 1 completes: attempt 1 anchors + matcher.onTick(100000, at(0)); + matcher.onTick(99000, at(100)); // leg 1 completes: attempt 1 anchors CHECK(matcher.stats().attempts == 1); // 61s > leg 2's window, and this tick is only -0.6% off the rolling max — // the chain dies (attributed to leg 2) WITHOUT re-anchoring. - matcher.onTick(99400, 0, at(161)); + matcher.onTick(99400, at(161)); CHECK(matcher.activeLeg() == 0); CHECK(matcher.stats().attempts == 1); CHECK(matcher.stats().occurrences == 0); @@ -143,36 +143,36 @@ TEST_CASE("an expired attempt counts a failure against the leg being sought", TEST_CASE("mid-chain expiry fails the chain and the failing tick can start a new attempt", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600), move(0.5, 60)})); - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99000, 0, at(100)); // leg 1 completes: anchor (99000, 100) + matcher.onTick(100000, at(0)); + matcher.onTick(99000, at(100)); // leg 1 completes: anchor (99000, 100) CHECK(matcher.activeLeg() == 1); // 61s > leg 2's 60s window: the attempt fails — and this same tick is a // fresh -2% off the still-live rolling max, so leg 1 completes AGAIN on // the failing tick (trackers were fed all along, no re-scan). - matcher.onTick(98000, 0, at(161)); + matcher.onTick(98000, at(161)); CHECK(matcher.stats().occurrences == 0); CHECK(matcher.activeLeg() == 1); // The failing tick counted BOTH the failure and the fresh attempt. CHECK(matcher.stats().attempts == 2); CHECK(matcher.stats().failuresByLeg[1] == 1); // The new attempt is anchored at (98000, 161): +0.5% completes it. - matcher.onTick(98490, 0, at(200)); + matcher.onTick(98490, at(200)); CHECK(matcher.stats().occurrences == 1); } TEST_CASE("non-overlap: back-to-back matches count, pre-match extremes cannot seed", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600)})); - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99000, 0, at(60)); + matcher.onTick(100000, at(0)); + matcher.onTick(99000, at(60)); CHECK(matcher.stats().occurrences == 1); // first match clears ALL trackers // 1.5% below the PRE-match max — but that extreme is gone; the fresh // tracker's max is this tick itself, so nothing fires. - matcher.onTick(98500, 0, at(120)); + matcher.onTick(98500, at(120)); CHECK(matcher.stats().occurrences == 1); // A full -1% against the POST-match max (98500 -> 98515 needed; 985 // points is exactly 1%): back-to-back occurrence number two. - matcher.onTick(97515, 0, at(180)); + matcher.onTick(97515, at(180)); CHECK(matcher.stats().occurrences == 2); // Single-leg chains: every leg-1 completion IS the whole chain, so // attempts == occurrences by construction (and nothing can expire). @@ -183,15 +183,15 @@ TEST_CASE("non-overlap: back-to-back matches count, pre-match extremes cannot se TEST_CASE("serial attempts: an interleaved second trigger yields one occurrence", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600), move(0.5, 600)})); - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99000, 0, at(60)); // attempt 1 anchors at 99000 + matcher.onTick(100000, at(0)); + matcher.onTick(99000, at(60)); // attempt 1 anchors at 99000 CHECK(matcher.activeLeg() == 1); // A further drop that would qualify as a NEW leg-1 trigger — ignored, // a single anchor is in flight (documented v1 undercount). - matcher.onTick(98000, 0, at(120)); + matcher.onTick(98000, at(120)); CHECK(matcher.activeLeg() == 1); // Recovery to +0.5% off the FIRST anchor completes exactly one chain. - matcher.onTick(99495, 0, at(240)); + matcher.onTick(99495, at(240)); CHECK(matcher.stats().occurrences == 1); CHECK(matcher.activeLeg() == 0); } @@ -204,10 +204,10 @@ TEST_CASE("StaysInBand requires a full covered window before it can fire", // Dead flat from the first tick — but the band cannot fire until a full // 300s of real tick history has accumulated (warm-up is coverage too). for (long t = 0; t <= 300; t += 30) { - matcher.onTick(100000, 0, at(t)); + matcher.onTick(100000, at(t)); CHECK(matcher.stats().occurrences == 0); } - matcher.onTick(100000, 0, at(330)); + matcher.onTick(100000, at(330)); CHECK(matcher.stats().occurrences == 1); } @@ -219,7 +219,7 @@ TEST_CASE("StaysInBand: a gap voids coverage until a fresh window accumulates", // Oscillate outside the band (range ~5.8% > 5%) so nothing fires while // the tracker is warm and covered... for (long t = 0; t <= 600; t += 30) { - matcher.onTick(t % 60 == 0 ? 100000 : 106000, 0, at(t)); + matcher.onTick(t % 60 == 0 ? 100000 : 106000, at(t)); } CHECK(matcher.stats().occurrences == 0); // ...then a 400s silent gap (> the 300s window). The post-gap window @@ -227,10 +227,10 @@ TEST_CASE("StaysInBand: a gap voids coverage until a fresh window accumulates", // gap must NOT satisfy "stays in band": coverage is void until 300s of // dense post-gap ticks accumulate. for (long t = 1000; t <= 1300; t += 30) { - matcher.onTick(100000, 0, at(t)); + matcher.onTick(100000, at(t)); CHECK(matcher.stats().occurrences == 0); } - matcher.onTick(100000, 0, at(1330)); + matcher.onTick(100000, at(1330)); CHECK(matcher.stats().occurrences == 1); } @@ -242,10 +242,10 @@ TEST_CASE("NewExtreme fires on a strict new high, never before coverage", // Steadily rising: every tick beats every prior print, but nothing may // fire until the lookback is fully covered. for (long t = 0; t <= 300; t += 30) { - matcher.onTick(static_cast(100000 + t / 3), 0, at(t)); + matcher.onTick(static_cast(100000 + t / 3), at(t)); CHECK(matcher.stats().occurrences == 0); } - matcher.onTick(100110, 0, at(330)); + matcher.onTick(100110, at(330)); CHECK(matcher.stats().occurrences == 1); } @@ -257,10 +257,10 @@ TEST_CASE("NewExtreme is strict: equalling the lookback high does not fire", // Dead flat, fully covered: the current mid always EQUALS the trailing // high — a new high must strictly exceed it. for (long t = 0; t <= 600; t += 30) { - matcher.onTick(100000, 0, at(t)); + matcher.onTick(100000, at(t)); } CHECK(matcher.stats().occurrences == 0); - matcher.onTick(100001, 0, at(630)); // strictly above: fires + matcher.onTick(100001, at(630)); // strictly above: fires CHECK(matcher.stats().occurrences == 1); } @@ -274,12 +274,12 @@ TEST_CASE("RangeRelativeMove clamps the threshold to >= 1 point", "[chainMatcher // would be 0 and a zero move would "fire" on every tick. The >= 1 clamp // keeps a flat market silent. for (long t = 0; t <= 600; t += 30) { - matcher.onTick(100000, 0, at(t)); + matcher.onTick(100000, at(t)); CHECK(matcher.stats().occurrences == 0); } // A 1-point drop: the basis range grows to 1, 1 x 0.3 rounds to 0, the // clamp lifts it back to 1 point — met exactly by this move. - matcher.onTick(99999, 0, at(630)); + matcher.onTick(99999, at(630)); CHECK(matcher.stats().occurrences == 1); } @@ -297,17 +297,17 @@ TEST_CASE("anchored RangeRelativeMove freezes its threshold at the anchor", // Basis range at the anchor = 101000 - 99980 = 1020, so leg 2's frozen // threshold is 2040 points below the anchor (99980). for (long t = 0; t <= 330; t += 30) { - matcher.onTick(101000, 0, at(t)); + matcher.onTick(101000, at(t)); } - matcher.onTick(99980, 0, at(360)); + matcher.onTick(99980, at(360)); CHECK(matcher.activeLeg() == 1); // 2020 points below the anchor: 20 short of the frozen threshold. - matcher.onTick(97960, 0, at(420)); + matcher.onTick(97960, at(420)); CHECK(matcher.stats().occurrences == 0); // 2040 points below the anchor fires. Had the threshold been recomputed // live, the drop itself would have widened the basis range to ~3060 // (threshold 6120) and this could never fire — freezing is the contract. - matcher.onTick(97940, 0, at(480)); + matcher.onTick(97940, at(480)); CHECK(matcher.stats().occurrences == 1); } @@ -414,10 +414,10 @@ TEST_CASE("the attempt recorder tracks excursions from anchor to resolution only // COMPLETED attempt: anchor at (99000, 60); dips 200 below, completes // 495 above at t=180 (duration 120s). The pre-anchor 100000 print must // not register as excursion. - matcher.onTick(100000, 0, at(0)); - matcher.onTick(99000, 0, at(60)); - matcher.onTick(98800, 0, at(120)); - matcher.onTick(99495, 0, at(180)); + matcher.onTick(100000, at(0)); + matcher.onTick(99000, at(60)); + matcher.onTick(98800, at(120)); + matcher.onTick(99495, at(180)); CHECK(matcher.stats().occurrences == 1); REQUIRE(matcher.stats().completedExcursions.size() == 1); CHECK(matcher.stats().completedExcursions[0].abovePoints == 495); @@ -428,16 +428,16 @@ TEST_CASE("the attempt recorder tracks excursions from anchor to resolution only // Post-resolution tick: trackers were cleared, nothing may extend the // recorded sample or start an attempt on its own. - matcher.onTick(97000, 0, at(240)); + matcher.onTick(97000, at(240)); CHECK(matcher.stats().attempts == 1); // FAILED attempt: anchor at (96030, 300); rises 70, dips 230, then the // window expires at t=902 — the sample lands in the FAILED population. - matcher.onTick(96030, 0, at(300)); + matcher.onTick(96030, at(300)); CHECK(matcher.stats().attempts == 2); - matcher.onTick(96100, 0, at(400)); - matcher.onTick(95800, 0, at(460)); - matcher.onTick(96000, 0, at(902)); + matcher.onTick(96100, at(400)); + matcher.onTick(95800, at(460)); + matcher.onTick(96000, at(902)); CHECK(matcher.activeLeg() == 0); REQUIRE(matcher.stats().failedExcursions.size() == 1); CHECK(matcher.stats().failedExcursions[0].abovePoints == 70); @@ -449,9 +449,9 @@ TEST_CASE("the attempt recorder tracks excursions from anchor to resolution only TEST_CASE("spread is sampled at the anchor tick only", "[chainMatcher]") { ChainMatcher matcher(chainOf({move(-1.0, 600), move(0.5, 600)})); - matcher.onTick(100000, 50, at(0)); // pre-anchor spread: ignored - matcher.onTick(99000, 30, at(60)); // ANCHOR: sampled - matcher.onTick(99495, 70, at(120)); // post-anchor spread: ignored + matcher.onTick(100025, 99975, at(0)); // pre-anchor spread: ignored (spread 50) + matcher.onTick(99015, 98985, at(60)); // ANCHOR: sampled (spread 30, mid 99000 -> -1%) + matcher.onTick(99585, 99515, at(120)); // post-anchor spread: ignored (bid 99515 >= 99015 + 496 -> +0.5%) CHECK(matcher.stats().occurrences == 1); CHECK(matcher.stats().spreadAtTriggerCount == 1); CHECK(matcher.stats().spreadAtTriggerSum == 30); @@ -465,10 +465,10 @@ TEST_CASE("the sample cap stops sampling while counts stay exact", std::int32_t price = 100000; for (int i = 0; i < 3; ++i) { const long t = i * 200; - matcher.onTick(price, 0, at(t)); + matcher.onTick(price, at(t)); price -= price / 100 + 1; // just over -1% off the fresh tracker // (+1 outruns the integer-division floor) - matcher.onTick(price, 0, at(t + 60)); + matcher.onTick(price, at(t + 60)); } CHECK(matcher.stats().occurrences == 3); CHECK(matcher.stats().attempts == 3); @@ -489,7 +489,7 @@ TEST_CASE("evaluateExperiment summarises excursions oriented by the final leg", tick(100000, 0), tick(99000, 60), tick(98800, 120), - tick(99495, 180), + tick(99516, 180), }; const auto outcome = chain_matcher::evaluateExperiment( @@ -499,12 +499,13 @@ TEST_CASE("evaluateExperiment summarises excursions oriented by the final leg", CHECK(outcome.excursionOrientation == "up"); REQUIRE(outcome.completedAttempts.has_value()); CHECK(outcome.completedAttempts->samples == 1); - CHECK(outcome.completedAttempts->mfePoints.p50 == 495.0); - CHECK(outcome.completedAttempts->mfePoints.p90 == 495.0); - CHECK(outcome.completedAttempts->maePoints.p50 == 200.0); - // Percent variants are relative to the attempt's own anchor (99000). - CHECK(outcome.completedAttempts->mfePercent.p50 == 100.0 * 495 / 99000); - CHECK(outcome.completedAttempts->maePercent.p50 == 100.0 * 200 / 99000); + CHECK(outcome.completedAttempts->mfePoints.p50 == 496.0); + CHECK(outcome.completedAttempts->mfePoints.p90 == 496.0); + CHECK(outcome.completedAttempts->maePoints.p50 == 220.0); + CHECK(outcome.completedAttempts->maePoints.p90 == 220.0); + // Percent variants are relative to the attempt's entry price (99010). + CHECK(outcome.completedAttempts->mfePercent.p50 == 100.0 * 496 / 99010); + CHECK(outcome.completedAttempts->maePercent.p50 == 100.0 * 220 / 99010); // No failed attempts: null population, not fake zeros. CHECK_FALSE(outcome.failedAttempts.has_value()); REQUIRE(outcome.completionSeconds.has_value()); @@ -567,3 +568,247 @@ TEST_CASE("evaluateExperiment reports per-symbol coverage alongside the v1 map", // attempts == occurrences). CHECK(outcome.attempts == 1); } + +TEST_CASE("real execution prices for long trade: entry at ask, TP on bid, MAE reflects spread", + "[chainMatcher]") { + ChainMatcher matcher(chainOf({move(-1.0, 600), move(0.5, 600)})); + // Pre-anchor + matcher.onTick(100010, 99990, at(0)); + // Mid drops to 99000 (-1% from 100000): Leg 1 completes on mid. + // Spread is 30 (ask 99015, bid 98985). Long entry fills at ask = 99015. + matcher.onTick(99015, 98985, at(60)); + CHECK(matcher.activeLeg() == 1); + CHECK(matcher.stats().attempts == 1); + + // Mid rises to 99510 (+0.51% from 99000, would trigger on mid alone). + // But bid is only 99500 (99500 - 99015 = 485 < 496 points needed). TP must NOT trigger on mid. + matcher.onTick(99520, 99500, at(120)); + CHECK(matcher.stats().occurrences == 0); + CHECK(matcher.activeLeg() == 1); + + // Bid reaches 99515 (99515 - 99015 = 500 >= 496): TP triggers on bid. + matcher.onTick(99535, 99515, at(180)); + CHECK(matcher.stats().occurrences == 1); + CHECK(matcher.activeLeg() == 0); + + REQUIRE(matcher.stats().completedExcursions.size() == 1); + const auto& s = matcher.stats().completedExcursions[0]; + CHECK(s.entryPrice == 99015); + CHECK(s.abovePoints == 500); + // MAE was 30 (spread at entry, since price never dipped below bid at entry) + CHECK(s.belowPoints == 30); +} + +TEST_CASE("real execution prices for short trade: entry at bid, TP on ask, MAE reflects spread", + "[chainMatcher]") { + // Leg 1: rally (+1%), Leg 2: fade (-0.5%) + ChainMatcher matcher(chainOf({move(1.0, 600), move(-0.5, 600)})); + matcher.onTick(100010, 99990, at(0)); + // Mid rises to 101000 (+1% from 100000): Leg 1 completes on mid. + // Spread is 40 (ask 101020, bid 100980). Short entry fills at bid = 100980. + matcher.onTick(101020, 100980, at(60)); + CHECK(matcher.activeLeg() == 1); + CHECK(matcher.stats().attempts == 1); + + // Mid drops to 100480 (-0.51% from 101000, would trigger on mid alone). + // But ask is only 100500 (100980 - 100500 = 480 < 505 points needed). TP must NOT trigger. + matcher.onTick(100500, 100460, at(120)); + CHECK(matcher.stats().occurrences == 0); + CHECK(matcher.activeLeg() == 1); + + // Ask drops to 100470 (100980 - 100470 = 510 >= 505): TP triggers on ask. + matcher.onTick(100470, 100430, at(180)); + CHECK(matcher.stats().occurrences == 1); + CHECK(matcher.activeLeg() == 0); + + REQUIRE(matcher.stats().completedExcursions.size() == 1); + const auto& s = matcher.stats().completedExcursions[0]; + CHECK(s.entryPrice == 100980); + CHECK(s.belowPoints == 510); // favorable excursion on ask + CHECK(s.abovePoints == 40); // adverse excursion on ask immediately reflects spread at entry +} + +TEST_CASE("failed attempt MAE immediately reflects spread at entry", + "[chainMatcher]") { + ChainMatcher matcher(chainOf({move(-1.0, 600), move(0.5, 60)})); + matcher.onTick(100010, 99990, at(0)); + // Leg 1 completes on mid=99000. Entry ask = 99015, bid = 98985 (spread 30). + matcher.onTick(99015, 98985, at(60)); + CHECK(matcher.stats().attempts == 1); + + // Window expires and leg 1 rolling window expires (t=661). + // Price stayed flat at entry: ask=99015, bid=98985. + matcher.onTick(99015, 98985, at(661)); + CHECK(matcher.activeLeg() == 0); + REQUIRE(matcher.stats().failedExcursions.size() == 1); + const auto& s = matcher.stats().failedExcursions[0]; + CHECK(s.entryPrice == 99015); + CHECK(s.abovePoints == 0); + // MAE immediately reflected spread at entry (99015 - 98985 = 30) even with no further drop! + CHECK(s.belowPoints == 30); +} + +TEST_CASE("real execution prices for RangeRelativeMove long trade: entry at ask, TP on bid, MAE reflects spread", + "[chainMatcher]") { + ChainMatcher matcher(chainOf({ + Activity{.TYPE = ActivityType::RangeRelativeMove, + .WINDOW_SECONDS = 600, + .LOOKBACK_SECONDS = 300, + .DIRECTION = -1, + .ATR_MULTIPLE = 1.0}, + Activity{.TYPE = ActivityType::RangeRelativeMove, + .WINDOW_SECONDS = 600, + .LOOKBACK_SECONDS = 300, + .DIRECTION = 1, + .ATR_MULTIPLE = 0.5}, + })); + // Warm the basis flat at 101000 + for (long t = 0; t <= 300; t += 30) { + matcher.onTick(101000, 101000, at(t)); + } + CHECK(matcher.activeLeg() == 0); + + // Drop to mid=99980 (1020 points drop >= 1020 basis range * 1.0 ATR). + // Leg 1 completes on mid. Spread is 30 (ask 99995, bid 99965). + // Long entry fills at ask = 99995. + matcher.onTick(99995, 99965, at(330)); + CHECK(matcher.activeLeg() == 1); + CHECK(matcher.stats().attempts == 1); + + // Leg 2 basis range was 1020, ATR_MULTIPLE = 0.5 -> threshold is 510. + // Price rises to mid=100495 (+515 from 99980, would trigger on mid alone). + // But bid is only 100480 (100480 - 99995 = 485 < 510 needed). TP must NOT trigger. + matcher.onTick(100510, 100480, at(360)); + CHECK(matcher.stats().occurrences == 0); + CHECK(matcher.activeLeg() == 1); + + // Bid reaches 100510 (100510 - 99995 = 515 >= 510): TP triggers on bid. + matcher.onTick(100540, 100510, at(390)); + CHECK(matcher.stats().occurrences == 1); + CHECK(matcher.activeLeg() == 0); + + REQUIRE(matcher.stats().completedExcursions.size() == 1); + const auto& s = matcher.stats().completedExcursions[0]; + CHECK(s.entryPrice == 99995); + CHECK(s.abovePoints == 515); + // MAE was 30 (spread at entry, price never dipped below entry bid) + CHECK(s.belowPoints == 30); +} + +TEST_CASE("real execution prices for RangeRelativeMove short trade: entry at bid, TP on ask, MAE reflects spread", + "[chainMatcher]") { + ChainMatcher matcher(chainOf({ + Activity{.TYPE = ActivityType::RangeRelativeMove, + .WINDOW_SECONDS = 600, + .LOOKBACK_SECONDS = 300, + .DIRECTION = 1, + .ATR_MULTIPLE = 1.0}, + Activity{.TYPE = ActivityType::RangeRelativeMove, + .WINDOW_SECONDS = 600, + .LOOKBACK_SECONDS = 300, + .DIRECTION = -1, + .ATR_MULTIPLE = 0.5}, + })); + // Warm the basis flat at 99000 + for (long t = 0; t <= 300; t += 30) { + matcher.onTick(99000, 99000, at(t)); + } + CHECK(matcher.activeLeg() == 0); + + // Rally to mid=100020 (1020 points rise >= 1020 basis range * 1.0 ATR). + // Leg 1 completes on mid. Spread is 40 (ask 100040, bid 100000). + // Short entry fills at bid = 100000. + matcher.onTick(100040, 100000, at(330)); + CHECK(matcher.activeLeg() == 1); + CHECK(matcher.stats().attempts == 1); + + // Leg 2 basis range was 1020, ATR_MULTIPLE = 0.5 -> threshold is 510. + // Price drops to mid=99490 (100020 - 99490 = 530 >= 510, would trigger on mid alone). + // But ask is 99510 (100000 - 99510 = 490 < 510 needed). TP must NOT trigger. + matcher.onTick(99510, 99470, at(360)); + CHECK(matcher.stats().occurrences == 0); + CHECK(matcher.activeLeg() == 1); + + // Ask drops to 99480 (100000 - 99480 = 520 >= 510): TP triggers on ask. + matcher.onTick(99480, 99440, at(390)); + CHECK(matcher.stats().occurrences == 1); + CHECK(matcher.activeLeg() == 0); + + REQUIRE(matcher.stats().completedExcursions.size() == 1); + const auto& s = matcher.stats().completedExcursions[0]; + CHECK(s.entryPrice == 100000); + CHECK(s.belowPoints == 520); // favorable excursion on ask + CHECK(s.abovePoints == 40); // adverse excursion on ask immediately reflects spread at entry +} + +TEST_CASE("3-leg chain with non-directional final leg determines trade direction from trade leg", + "[chainMatcher]") { + // Leg 1: rally (+1%), Leg 2: fade (-0.5%), Leg 3: StaysInBand (non-directional) + // The trade entered upon Leg 1 completion is SHORT (defined by Leg 2). + ChainMatcher matcher(chainOf({ + move(1.0, 600), + move(-0.5, 600), + Activity{.TYPE = ActivityType::StaysInBand, + .MOVE_PERCENT = 5.0, + .WINDOW_SECONDS = 600, + .LOOKBACK_SECONDS = 300}, + })); + // Warm the basis tracker so StaysInBand is covered (needs 300s of ticks) + for (long t = 0; t <= 300; t += 30) { + matcher.onTick(100010, 99990, at(t)); + } + CHECK(matcher.activeLeg() == 0); + + // Leg 1 completes on mid=101000 (+1% rally from 100000). + // Spread is 40 (ask 101020, bid 100980). Short entry fills at bid = 100980. + matcher.onTick(101020, 100980, at(360)); + CHECK(matcher.activeLeg() == 1); + CHECK(matcher.stats().attempts == 1); + + // Ask drops to 100470 (100980 - 100470 = 510 >= 505 needed): Leg 2 TP triggers on ask. + matcher.onTick(100470, 100430, at(420)); + CHECK(matcher.activeLeg() == 2); + CHECK(matcher.stats().occurrences == 0); + + // Leg 3 StaysInBand evaluates on next tick (basis covered, range ~1020 <= 5% of mid). + matcher.onTick(100470, 100430, at(480)); + CHECK(matcher.stats().occurrences == 1); + CHECK(matcher.activeLeg() == 0); + + REQUIRE(matcher.stats().completedExcursions.size() == 1); + const auto& s = matcher.stats().completedExcursions[0]; + CHECK(s.entryPrice == 100980); + CHECK(s.belowPoints >= 510); + CHECK(s.abovePoints == 40); // entry spread adverse +} + +TEST_CASE("evaluateExperiment correctly orients a multi-leg chain with trailing non-directional leg", + "[chainMatcher]") { + const auto tick = [](const std::int32_t mid, const long seconds) { + return PriceData(mid + 20, mid - 20, at(seconds), "EURUSD"); + }; + // 3-leg short trade: rally (+1%), fade (-0.5%), stays in band + std::vector ticks; + for (long t = 0; t <= 300; t += 30) { + ticks.push_back(tick(100000, t)); + } + ticks.push_back(tick(101000, 360)); // rally +1%: entry at bid = 100980 (ask = 101020, spread 40) + ticks.push_back(tick(100450, 420)); // fade completes: ask = 100470 (100980 - 100470 = 510 >= 505) + ticks.push_back(tick(100450, 480)); // band completes + + const auto outcome = chain_matcher::evaluateExperiment( + ticks, chainOf({ + move(1.0, 600), + move(-0.5, 600), + Activity{.TYPE = ActivityType::StaysInBand, + .MOVE_PERCENT = 5.0, + .WINDOW_SECONDS = 600, + .LOOKBACK_SECONDS = 300}, + })); + + CHECK(outcome.excursionOrientation == "down"); + REQUIRE(outcome.completedAttempts.has_value()); + CHECK(outcome.completedAttempts->samples == 1); + CHECK(outcome.completedAttempts->mfePoints.p50 >= 510.0); + CHECK(outcome.completedAttempts->maePoints.p50 == 40.0); // entry spread +} diff --git a/tests/dealPacket.cpp b/tests/dealPacket.cpp index 825e71a..ccb8795 100644 --- a/tests/dealPacket.cpp +++ b/tests/dealPacket.cpp @@ -159,8 +159,8 @@ TEST_CASE("decodeDeal rejects wrong-sized datagrams", "[dealPacket]") { std::array tooLarge{}; CHECK_FALSE(deal_packet::decodeDeal(tooLarge).has_value()); } - SECTION("a 40-byte tick packet is not a deal") { - std::array tick{}; + SECTION("a 48-byte tick packet is not a deal") { + std::array tick{}; CHECK_FALSE(deal_packet::decodeDeal(tick).has_value()); } } diff --git a/tests/experimentSweep.cpp b/tests/experimentSweep.cpp index 8310368..07bb39b 100644 --- a/tests/experimentSweep.cpp +++ b/tests/experimentSweep.cpp @@ -22,8 +22,10 @@ #include "load/redisLoader.hpp" import experimentsCommand; // sweep::buildExperimentChunk (+ re-exported - // dipRecoverySweep: ExperimentSweepSpec, factory) + // dipRecoverySweep, rangeRelativeDipRecoverySweep: + // ExperimentSweepSpec, factory) import makeDipRecovery; // sweep::makeDipRecoveryExperiment +import makeRangeRelativeDipRecovery; // sweep::makeRangeRelativeDipRecoveryExperiment using experiments::ActivityType; @@ -145,3 +147,109 @@ TEST_CASE("buildExperimentChunk keys each payload by run id and its UUID", end) .empty()); } + +TEST_CASE("buildRangeRelativeDipRecoverySweep expands to the product of its range sizes", + "[experimentSweep]") { + const auto spec = sweep::buildRangeRelativeDipRecoverySweep(); + const auto counts = spec.generator.rangeValueCounts(); + REQUIRE(!counts.empty()); + + // Config-agnostic: however the grid is tuned, its size must be the + // product of the declared range sizes. + std::size_t product = 1; + for (const auto& [name, count] : counts) { + INFO(name << " declares no values"); + CHECK(count > 0); + product *= count; + } + CHECK(spec.generator.combinationCount() == product); + // 3 * 4 * 3 * 3 * 3 = 324 combinations + CHECK(product == 324); + + for (const auto& combo : + {spec.generator.combinationAt(0), + spec.generator.combinationAt(spec.generator.combinationCount() - 1)}) { + for (const char* name : + {"LEG1_LOOKBACK_MINUTES", "LEG1_ATR_MULTIPLE", + "LEG1_WINDOW_MINUTES", "LEG2_ATR_MULTIPLE", + "LEG2_WINDOW_MINUTES"}) { + INFO("Combination is missing " << name); + CHECK(combo.has(name)); + } + } + + CHECK(spec.lastMonths == 9); + CHECK(spec.offsetMonths == 0); + CHECK(spec.factory == sweep::makeRangeRelativeDipRecoveryExperiment); +} + +TEST_CASE("makeRangeRelativeDipRecoveryExperiment maps a combination onto the chain", + "[experimentSweep]") { + sweep::Combination combo; + combo.set("LEG1_LOOKBACK_MINUTES", 30); + combo.set("LEG1_ATR_MULTIPLE", 1.5); + combo.set("LEG1_WINDOW_MINUTES", 10); + combo.set("LEG2_ATR_MULTIPLE", 0.5); + combo.set("LEG2_WINDOW_MINUTES", 20); + + const auto config = sweep::makeRangeRelativeDipRecoveryExperiment(combo); + + CHECK_FALSE(config.UUID.empty()); + CHECK(config.NAME == "rangeRelativeDipRecovery"); + REQUIRE(config.CHAIN.size() == 2); + + // Leg 1: RangeRelativeMove downwards (DIRECTION = -1) + CHECK(config.CHAIN[0].TYPE == ActivityType::RangeRelativeMove); + CHECK(config.CHAIN[0].DIRECTION == -1); + CHECK(config.CHAIN[0].ATR_MULTIPLE == 1.5); + CHECK(config.CHAIN[0].WINDOW_SECONDS == 10 * 60); + CHECK(config.CHAIN[0].LOOKBACK_SECONDS == 30 * 60); + + // Leg 2: RangeRelativeMove upwards (DIRECTION = 1), shares leg 1's basis lookback + CHECK(config.CHAIN[1].TYPE == ActivityType::RangeRelativeMove); + CHECK(config.CHAIN[1].DIRECTION == 1); + CHECK(config.CHAIN[1].ATR_MULTIPLE == 0.5); + CHECK(config.CHAIN[1].WINDOW_SECONDS == 20 * 60); + CHECK(config.CHAIN[1].LOOKBACK_SECONDS == 30 * 60); + + // Each call mints a FRESH UUID — payload keys must never collide. + CHECK(sweep::makeRangeRelativeDipRecoveryExperiment(combo).UUID != config.UUID); +} + +TEST_CASE("buildExperimentChunk keys each payload by run id and its UUID for rangeRelativeDipRecovery", + "[experimentSweep]") { + const auto spec = sweep::buildRangeRelativeDipRecoverySweep(); + const std::string runId = "test-run-rr"; + + const std::size_t begin = 10; + const std::size_t end = 15; + const auto chunk = sweep::buildExperimentChunk(spec.generator, spec.factory, + runId, begin, end); + REQUIRE(chunk.size() == end - begin); + + std::set uuids; + for (std::size_t offset = 0; offset < chunk.size(); ++offset) { + const auto config = nlohmann::json::parse(chunk[offset].rawJson) + .get(); + CHECK_FALSE(config.UUID.empty()); + uuids.insert(config.UUID); + CHECK(chunk[offset].key == + queue_keys::experimentPayloadKey(runId, config.UUID)); + + const auto expected = + spec.factory(spec.generator.combinationAt(begin + offset)); + CHECK(config.NAME == expected.NAME); + REQUIRE(config.CHAIN.size() == expected.CHAIN.size()); + for (std::size_t leg = 0; leg < config.CHAIN.size(); ++leg) { + CHECK(config.CHAIN[leg].TYPE == expected.CHAIN[leg].TYPE); + CHECK(config.CHAIN[leg].DIRECTION == expected.CHAIN[leg].DIRECTION); + CHECK(config.CHAIN[leg].ATR_MULTIPLE == + expected.CHAIN[leg].ATR_MULTIPLE); + CHECK(config.CHAIN[leg].WINDOW_SECONDS == + expected.CHAIN[leg].WINDOW_SECONDS); + CHECK(config.CHAIN[leg].LOOKBACK_SECONDS == + expected.CHAIN[leg].LOOKBACK_SECONDS); + } + } + CHECK(uuids.size() == chunk.size()); +} diff --git a/tests/liveTrace.cpp b/tests/liveTrace.cpp index 8f422be..038d929 100644 --- a/tests/liveTrace.cpp +++ b/tests/liveTrace.cpp @@ -70,10 +70,12 @@ bool isIsoUtcMillis(const std::string& ts) { } // namespace -// Declared first in this TU on purpose: in a single-process run it must see -// the gate before any test below arms it (ctest runs each case in its own -// process anyway). +// Declared first in this TU on purpose. Catch2 randomises case order within a +// single process, so tests below that call init() can leave global state +// behind. This case calls reset() first to assert the true pre-init defaults +// regardless of ordering. TEST_CASE("tracing is inert before init", "[liveTrace]") { + live_trace::reset(); CHECK_FALSE(live_trace::enabled()); CHECK(live_trace::tradingEnv() == "unknown"); // emit while disarmed is a no-op — must not throw or touch the publisher. diff --git a/tests/tickLine.cpp b/tests/tickLine.cpp new file mode 100644 index 0000000..e5ebefd --- /dev/null +++ b/tests/tickLine.cpp @@ -0,0 +1,50 @@ +// Backtesting Engine in C++ +// +// (c) 2026 Ryan McCaffery | https://mccaffers.com +// This code is licensed under MIT license (see LICENSE.txt for details) +// --------------------------------------- + +#include + +#include +#include +#include + +import tickLine; +import tickPacket; +import priceData; + +namespace { + +// 2024-06-26T00:00:00Z = 1719360000000000 us = 1719360000000000000 ns. +tick_packet::DecodedTick makeTick(const std::string& source) { + const auto ts = std::chrono::system_clock::time_point( + std::chrono::duration_cast( + std::chrono::microseconds(1'719'360'000'000'000LL))); + return {PriceData(/*ask=*/110001, /*bid=*/110000, ts, "EURUSD"), source}; +} + +} // namespace + +TEST_CASE("formatLine tags known sources", "[tickLine]") { + CHECK(tick_line::formatLine(makeTick("IG")) == + "EURUSD,source=IG ask=110001i,bid=110000i 1719360000000000000\n"); + CHECK(tick_line::formatLine(makeTick("MX")) == + "EURUSD,source=MX ask=110001i,bid=110000i 1719360000000000000\n"); +} + +TEST_CASE("formatLine omits the tag for an empty or unknown source", "[tickLine]") { + const std::string untagged = "EURUSD ask=110001i,bid=110000i 1719360000000000000\n"; + CHECK(tick_line::formatLine(makeTick("")) == untagged); + CHECK(tick_line::formatLine(makeTick("XX")) == untagged); + // Would need ILP escaping if it were ever written as a tag value. + CHECK(tick_line::formatLine(makeTick("I G,x=1")) == untagged); +} + +TEST_CASE("isKnownSource accepts only the producer feed ids", "[tickLine]") { + CHECK(tick_line::isKnownSource("IG")); + CHECK(tick_line::isKnownSource("MX")); + CHECK_FALSE(tick_line::isKnownSource("")); + CHECK_FALSE(tick_line::isKnownSource("ig")); + CHECK_FALSE(tick_line::isKnownSource("IGX")); +} diff --git a/tests/tickPacket.cpp b/tests/tickPacket.cpp index b913dbf..5a15245 100644 --- a/tests/tickPacket.cpp +++ b/tests/tickPacket.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include import tickPacket; @@ -19,11 +20,12 @@ import priceData; namespace { -// Build the 40-byte little-endian packet exactly as the C# sender would +// Build the 48-byte little-endian packet exactly as the C# sender would // (see the layout in tickPacket.cppm), so the test exercises the real contract. std::array makePacket(double bid, double ask, std::int64_t tsMicros, - const std::string& symbol) { + const std::string& symbol, + const std::string& source = "IG") { std::array packet{}; const auto put = [&](std::size_t offset, auto value) { @@ -37,6 +39,9 @@ std::array makePacket(double bid, double as for (std::size_t i = 0; i < symbol.size() && i < tick_packet::kSymbolSize; ++i) { packet[tick_packet::kSymbolOffset + i] = static_cast(symbol[i]); } + for (std::size_t i = 0; i < source.size() && i < tick_packet::kSourceSize; ++i) { + packet[tick_packet::kSourceOffset + i] = static_cast(source[i]); + } return packet; } @@ -54,12 +59,12 @@ TEST_CASE("decodeTick parses a packet into scaled PriceData", "[tickPacket]") { const auto tick = tick_packet::decodeTick(packet); REQUIRE(tick.has_value()); - CHECK(tick->symbol == "EURUSD"); + CHECK(tick->price.symbol == "EURUSD"); // EURUSD price multiplier is 100000: 1.10000 -> 110000, 1.10001 -> 110001. - CHECK(tick->bid == 110000); - CHECK(tick->ask == 110001); + CHECK(tick->price.bid == 110000); + CHECK(tick->price.ask == 110001); CHECK(std::chrono::duration_cast( - tick->timestamp.time_since_epoch()) + tick->price.timestamp.time_since_epoch()) .count() == tsMicros); } @@ -68,15 +73,15 @@ TEST_CASE("decodeTick scales by the per-symbol multiplier", "[tickPacket]") { const auto packet = makePacket(156.123, 156.125, kValidTs, "USDJPY"); const auto tick = tick_packet::decodeTick(packet); REQUIRE(tick.has_value()); - CHECK(tick->bid == 156123); - CHECK(tick->ask == 156125); + CHECK(tick->price.bid == 156123); + CHECK(tick->price.ask == 156125); } SECTION("index uses x100") { const auto packet = makePacket(5432.10, 5432.20, kValidTs, "USA500IDXUSD"); const auto tick = tick_packet::decodeTick(packet); REQUIRE(tick.has_value()); - CHECK(tick->bid == 543210); - CHECK(tick->ask == 543220); + CHECK(tick->price.bid == 543210); + CHECK(tick->price.ask == 543220); } } @@ -92,14 +97,16 @@ TEST_CASE("decodeTick rejects malformed input", "[tickPacket]") { } TEST_CASE("decodeTick golden vector pins the on-the-wire byte layout", "[tickPacket]") { - // The exact 40 bytes of the documented little-endian contract (the byte map + // The exact 48 bytes of the documented little-endian contract (the byte map // in tickPacket.cppm) for bid 1.10000 / ask 1.10001 / - // ts 2024-06-26T00:00:00Z (1719360000000000 us) / "EURUSD". Unlike the + // ts 2024-06-26T00:00:00Z (1719360000000000 us) / "EURUSD" / source "IG". + // The C# producer's test (vortex tests/TickSerializer/TickSerializerTests.cs) + // asserts TickSerializer emits these same bytes, so together the two tests + // pin the cross-language contract. Unlike the // makePacket-based tests, these literals are positional — they do NOT route // through kBidOffset/kAskOffset — so a future swap or shift of the decoder's // offset constants breaks this test even though the self-consistent tests - // stay green. (To turn this into a true cross-language check, regenerate - // these bytes from the real C# Serialize output for the same tick.) + // stay green. constexpr std::array golden{ std::byte{0x9A}, std::byte{0x99}, std::byte{0x99}, std::byte{0x99}, std::byte{0x99}, std::byte{0x99}, std::byte{0xF1}, std::byte{0x3F}, // bid 1.10000 @@ -111,16 +118,62 @@ TEST_CASE("decodeTick golden vector pins the on-the-wire byte layout", "[tickPac std::byte{0x53}, std::byte{0x44}, std::byte{0x00}, std::byte{0x00}, // "EURUSD" std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, + std::byte{0x49}, std::byte{0x47}, std::byte{0x00}, std::byte{0x00}, + std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, // "IG" }; const auto tick = tick_packet::decodeTick(golden); REQUIRE(tick.has_value()); - CHECK(tick->symbol == "EURUSD"); - CHECK(tick->bid == 110000); - CHECK(tick->ask == 110001); + CHECK(tick->price.symbol == "EURUSD"); + CHECK(tick->price.bid == 110000); + CHECK(tick->price.ask == 110001); CHECK(std::chrono::duration_cast( - tick->timestamp.time_since_epoch()) + tick->price.timestamp.time_since_epoch()) .count() == 1'719'360'000'000'000LL); + CHECK(tick->source == "IG"); +} + +TEST_CASE("decodeTick reads the feed source", "[tickPacket]") { + SECTION("IG") { + const auto tick = tick_packet::decodeTick(makePacket(1.1, 1.1, kValidTs, "EURUSD", "IG")); + REQUIRE(tick.has_value()); + CHECK(tick->source == tick_packet::kSourceIG); + } + SECTION("Massive") { + const auto tick = tick_packet::decodeTick(makePacket(1.1, 1.1, kValidTs, "EURUSD", "MX")); + REQUIRE(tick.has_value()); + CHECK(tick->source == tick_packet::kSourceMassive); + } + SECTION("empty source decodes as empty") { + const auto tick = tick_packet::decodeTick(makePacket(1.1, 1.1, kValidTs, "EURUSD", "")); + REQUIRE(tick.has_value()); + CHECK(tick->source.empty()); + } + SECTION("full-width source has no NUL and is taken whole") { + const auto tick = + tick_packet::decodeTick(makePacket(1.1, 1.1, kValidTs, "EURUSD", "ABCDEFGH")); + REQUIRE(tick.has_value()); + CHECK(tick->source == "ABCDEFGH"); + } + SECTION("source is trimmed at the first NUL") { + auto packet = makePacket(1.1, 1.1, kValidTs, "EURUSD", "MX"); + packet[tick_packet::kSourceOffset + 3] = std::byte{'Z'}; // after the NUL at +2 + const auto tick = tick_packet::decodeTick(packet); + REQUIRE(tick.has_value()); + CHECK(tick->source == "MX"); + } +} + +TEST_CASE("decodeTick rejects sizes other than 48 bytes", "[tickPacket]") { + const auto full = makePacket(1.10000, 1.10001, kValidTs, "EURUSD", "IG"); + // 40 = the retired pre-source layout, which is no longer accepted. + for (const std::size_t size : {39UZ, 40UZ, 41UZ, 47UZ}) { + CHECK_FALSE(tick_packet::decodeTick(std::span(full).first(size)) + .has_value()); + } + std::array tooLarge{}; + std::ranges::copy(full, tooLarge.begin()); + CHECK_FALSE(tick_packet::decodeTick(tooLarge).has_value()); } TEST_CASE("decodeTick drops corrupt ticks (hardening guards)", "[tickPacket]") {