Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Verify source release
on: [push, pull_request]
jobs:
verify:
runs-on: ubuntu-latest
container: ubuntu:26.04
steps:
- uses: actions/checkout@v4
- name: Install build and isolated-test dependencies
run: |
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y cmake ninja-build g++ pkg-config libpq-dev libcurl4-openssl-dev libssl-dev libxml2-dev nlohmann-json3-dev libcpp-httplib-dev python3 postgresql openssl nodejs
- name: Build and numerical checks
run: |
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build -j2
ctest --test-dir build --output-on-failure
node --check frontend/app.js
node --check frontend/release.js
node --check frontend/gateway-session.js
node tests/gateway_session_test.mjs
- name: Private database and synthetic Gateway integration
run: |
useradd -m reachtest
chown -R reachtest:reachtest "$GITHUB_WORKSPACE"
runuser -u reachtest -- python3 scripts/test-isolated.py
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ find_package(nlohmann_json 3.11 REQUIRED)
find_package(Threads REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(HTTPLIB REQUIRED IMPORTED_TARGET cpp-httplib>=0.18)
add_executable(reachterminal backend/main.cpp)
find_package(Python3 REQUIRED COMPONENTS Interpreter)
set(REACH_ASSET_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(REACH_ASSETS frontend/index.html frontend/app.js frontend/release.js frontend/gateway-session.js frontend/style.css db/001_initial.sql)
add_custom_command(OUTPUT "${REACH_ASSET_DIR}/embedded_assets.cpp" "${REACH_ASSET_DIR}/embedded_assets.hpp"
COMMAND Python3::Interpreter "${CMAKE_CURRENT_SOURCE_DIR}/scripts/embed_assets.py" --root "${CMAKE_CURRENT_SOURCE_DIR}" --output "${REACH_ASSET_DIR}"
DEPENDS scripts/embed_assets.py ${REACH_ASSETS} VERBATIM)
add_executable(reachterminal backend/main.cpp "${REACH_ASSET_DIR}/embedded_assets.cpp")
target_include_directories(reachterminal PRIVATE "${REACH_ASSET_DIR}")
target_compile_definitions(reachterminal PRIVATE REACH_ROOT="${CMAKE_SOURCE_DIR}")
target_link_libraries(reachterminal PRIVATE PostgreSQL::PostgreSQL CURL::libcurl OpenSSL::Crypto LibXml2::LibXml2 nlohmann_json::nlohmann_json PkgConfig::HTTPLIB Threads::Threads)
target_compile_options(reachterminal PRIVATE -Wall -Wextra -Wconversion)
enable_testing()
add_executable(core_tests tests/core_tests.cpp)
target_link_libraries(core_tests PRIVATE nlohmann_json::nlohmann_json OpenSSL::Crypto LibXml2::LibXml2 CURL::libcurl)
add_test(NAME core COMMAND core_tests)
add_executable(release_tests tests/release_tests.cpp)
target_link_libraries(release_tests PRIVATE nlohmann_json::nlohmann_json OpenSSL::Crypto LibXml2::LibXml2 CURL::libcurl)
add_test(NAME release COMMAND release_tests)
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ C++20 backend, PostgreSQL database. No broker order submission code.
第一版将基本面研究、策略核算、估值与风险放在同一个本地工作区。
软件不收取订阅费;第三方数据的费用、覆盖和使用权限仍由对应来源决定。

## IBKR read-only data queries

Client Portal Gateway connection settings, symbol/company search, contract details,
market snapshots and historical daily previews are available in Data Center.
Run `./scripts/gateway.sh`, sign in yourself, then use `./scripts/live.sh`.
See [IBKR setup, scope and verification](docs/ibkr.md). No generic API key is required.

## Real public data

Run `./scripts/live.sh` for the separate real-data workspace on **http://127.0.0.1:8788**.
Expand Down Expand Up @@ -98,6 +105,7 @@ its connection only inside the launched process; another terminal must set it ex
From the repository root, with the development database running:

```sh
export REACH_ENV=demo
export REACH_DATABASE_URL="host='$PWD/.local/socket' port=55433 dbname=reachterminal"
./build/reachterminal export > /private/location/workspace.json
```
Expand All @@ -118,7 +126,7 @@ See [data contract](docs/data-contract.md) and [architecture](docs/architecture.

**A working first release, not a replacement for a complete institutional data feed.**

- No TWS/Gateway live connection, real-time quote stream, broker margin engine,
- No TWS account stream, continuous real-time quote stream, broker margin engine,
derivatives accounting, options Greeks, automatic global filings parser, full-text
news feed or bundled global analyst-consensus history in this release.
- SEC mapping covers an explicit US-GAAP/DEI subset, not custom tags, segments or IFRS
Expand All @@ -141,14 +149,23 @@ See [data contract](docs/data-contract.md) and [architecture](docs/architecture.

```sh
ctest --test-dir build --output-on-failure
python3 tests/integration.py --url http://127.0.0.1:8787 --root .
python3 scripts/test-isolated.py
```

Run integration tests against a **fresh disposable database**; they intentionally import
synthetic records. See [verification](docs/verification.md) for measured coverage.
The isolated runner creates a fresh private PostgreSQL cluster, mock HTTPS Gateway and
configuration directory. Mutating tests refuse user/demo instances before any write.
Test runtimes cannot connect to real Gateway ports or external SEC/Alpha services. See [verification](docs/verification.md) for measured coverage.
Financial changes need independently computed expected values and regression cases.
Contributions for additional markets should include source terms, reproducible fixtures,
unit/period mapping and missing-data behavior. Keep provider adapters separate from accounting.

MIT applies to ReachTerminal code. Dependencies and external data retain their own licenses.
See [third-party notices](docs/third-party.md) and [roadmap](docs/roadmap.md).

## v0.1.0 扩展

新增衍生品假设对冲、IBKR 券商保证金监控、世界银行年度宏观和美联储官方新闻工作区。详见 [发布范围与验收](docs/release-0.1.0.md)。这些工作区保留数据来源和未知状态;欧式期权模型、券商保证金和现金股票账本各自计算。

## 本地内嵌发行版

前端和迁移 SQL 已内嵌到 C++ 可执行程序;不依赖运行目录里的前端资源。二进制打包与网关登录反馈见 [本地内嵌说明](docs/embedded-local.md)。
50 changes: 48 additions & 2 deletions backend/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <chrono>
#include <cmath>
#include <cstdint>
#include <ctime>
#include <deque>
#include <iomanip>
#include <map>
Expand Down Expand Up @@ -96,16 +97,28 @@ inline void source(const json &v) {
u.starts_with("demo:"),
"Source URL must use http(s), manual: or demo:");
}
inline std::string retrieval_date(int64_t ms) {
require(ms > 0 && ms <= 4102444800000LL, "Invalid retrieval epoch");
std::time_t t = ms / 1000;
std::tm tm{};
require(gmtime_r(&t, &tm) != nullptr, "Invalid retrieval timestamp");
std::ostringstream out;
out << std::put_time(&tm, "%Y-%m-%d");
return out.str();
}
inline const std::vector<std::string> collections = {
"instruments", "trades", "cashflows", "quotes", "fx", "facts", "bars",
"news", "notes", "reconciliations", "estimates", "raw_sources", "splits", "valuations"};
"instruments", "trades", "cashflows", "quotes", "fx",
"facts", "bars", "news", "notes", "reconciliations",
"estimates", "raw_sources", "splits", "valuations", "macro_observations",
"hedge_models"};
inline json empty_data() {
json j = json::object();
for (auto &k : collections)
j[k] = json::array();
return j;
}
inline json dcf(const json &v);
inline json hedge_book(const json &v);
inline void validate(const std::string &kind, const json &v) {
require(v.is_object(), "Record must be object");
id(str(v, "id"));
Expand Down Expand Up @@ -243,6 +256,37 @@ inline void validate(const std::string &kind, const json &v) {
source(v);
date(str(v, "date"));
require(v.contains("document"), "Raw document required");
} else if (kind == "macro_observations") {
date(str(v, "date"));
date(str(v, "period_end"));
source(v);
require(str(v, "period_end") <= str(v, "date"), "Macro period is in future");
require(std::regex_match(str(v, "country"), std::regex("[A-Z]{3}")),
"Invalid macro country");
auto indicator = str(v, "indicator");
require(indicator == "gdp" || indicator == "growth" || indicator == "inflation",
"Invalid macro indicator");
require(str(v, "frequency") == "annual" &&
str(v, "unit") == (indicator == "gdp" ? "current USD" : "percent"),
"Invalid macro unit/frequency");
require(v.contains("known_at") && v["known_at"].is_null(),
"Historical release date unavailable");
require(v.contains("retrieved_at_ms") && v["retrieved_at_ms"].is_number_integer() &&
v["retrieved_at_ms"].get<int64_t>() > 0,
"Invalid macro retrieval timestamp");
require(str(v, "date") == retrieval_date(v["retrieved_at_ms"].get<int64_t>()) &&
str(v, "date") <= today(),
"Macro visibility must match actual retrieval date");
require(v.contains("value") &&
(v["value"].is_null() ||
(v["value"].is_number() && std::isfinite(v["value"].get<double>()) &&
std::abs(v["value"].get<double>()) <= 1e18)),
"Invalid macro value");
} else if (kind == "hedge_models") {
date(str(v, "date"));
str(v, "title");
require(v.at("result") == hedge_book(v.at("assumptions")),
"Hedge result differs from assumptions");
} else if (kind == "valuations") {
id(str(v, "instrument"));
date(str(v, "date"));
Expand Down Expand Up @@ -783,3 +827,5 @@ inline json stress(const json &p, const json &input) {
{"model", p["scenario_model"]}};
}
} // namespace reach

#include "derivatives.hpp"
133 changes: 133 additions & 0 deletions backend/derivatives.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#pragma once
#include "core.hpp"
namespace reach {
inline double model_number(const json &v, const char *k, double low, double high) {
require(v.contains(k) && v[k].is_number(), std::string("Required model number: ") + k);
double n = v[k];
require(std::isfinite(n) && n >= low && n <= high, std::string("Model domain: ") + k);
return n;
}
inline double normal_cdf(double x) {
return .5 * std::erfc(-x / std::sqrt(2.));
}
inline json european_option(const json &v) {
double s = model_number(v, "spot", .000001, 1e9), k = model_number(v, "strike", .000001, 1e9),
t = model_number(v, "years", 0, 20), r = model_number(v, "rate", -.5, 1),
q = model_number(v, "yield", -.5, 1), vol = model_number(v, "volatility", .0001, 5);
auto right = str(v, "right");
require(right == "call" || right == "put", "European call/put only");
require(v.value("style", std::string("European")) == "European",
"American/exotic option pricing not supported");
if (t == 0)
return {{"price", std::max(0., right == "call" ? s - k : k - s)},
{"delta", nullptr},
{"gamma", nullptr},
{"vega_1pp", nullptr},
{"theta_day", nullptr},
{"rho_1pp", nullptr},
{"expired", true}};
double rt = std::sqrt(t), d1 = (std::log(s / k) + (r - q + vol * vol / 2) * t) / (vol * rt),
d2 = d1 - vol * rt, dq = std::exp(-q * t), dr = std::exp(-r * t),
pdf = std::exp(-d1 * d1 / 2) / std::sqrt(2 * std::acos(-1.)),
common = -s * dq * pdf * vol / (2 * rt);
bool call = right == "call";
double price = call ? s * dq * normal_cdf(d1) - k * dr * normal_cdf(d2)
: k * dr * normal_cdf(-d2) - s * dq * normal_cdf(-d1);
double theta = call ? common - r * k * dr * normal_cdf(d2) + q * s * dq * normal_cdf(d1)
: common + r * k * dr * normal_cdf(-d2) - q * s * dq * normal_cdf(-d1);
return {{"price", std::max(0., price)},
{"delta", call ? dq * normal_cdf(d1) : dq * (normal_cdf(d1) - 1)},
{"gamma", dq * pdf / (s * vol * rt)},
{"vega_1pp", s * dq * pdf * rt * .01},
{"theta_day", theta / 365.},
{"rho_1pp", (call ? k * t * dr * normal_cdf(d2) : -k * t * dr * normal_cdf(-d2)) * .01},
{"expired", false}};
}
inline json hedge_book(const json &v) {
currency(str(v, "currency"));
require(v.contains("positions") && v["positions"].is_array() && !v["positions"].empty() &&
v["positions"].size() <= 100,
"Hedge book needs 1-100 modeled positions");
json rows = json::array(), groups = json::object();
double value = 0, vega = 0, theta = 0;
std::set<std::string> ids;
for (auto &p : v["positions"]) {
id(str(p, "id"));
require(ids.insert(str(p, "id")).second, "Duplicate hedge position ID");
auto type = str(p, "type"), u = str(p, "underlying");
id(u);
require(!p.contains("currency") || p["currency"] == v["currency"],
"Cross-currency hedge book prohibited");
double s = model_number(p, "spot", .000001, 1e9),
qty = model_number(p, "quantity", -1e7, 1e7),
mult = model_number(p, "multiplier", .000001, 1e6);
require(qty != 0, "Zero hedge quantity");
require(type == "STK" || type == "OPT" || type == "FUT", "Unsupported hedge asset");
require(type != "STK" || mult == 1, "Stock multiplier must be one");
json m = type == "OPT" ? european_option(p)
: json{{"price", s}, {"delta", 1.}, {"gamma", 0.},
{"vega_1pp", 0.}, {"theta_day", 0.}, {"expired", false}};
require(!m["expired"].get<bool>(), "Initial options must have positive time to expiry");
double n = qty * mult, d = m["delta"].get<double>() * n, g = m["gamma"].get<double>() * n;
// Futures have zero funded market value at the scenario origin; P&L is price change.
double mv = type == "FUT" ? 0 : m["price"].get<double>() * n;
value += mv;
vega += m["vega_1pp"].get<double>() * n;
theta += m["theta_day"].get<double>() * n;
if (!groups.contains(u))
groups[u] = {{"delta_units", 0.}, {"delta_notional", 0.}, {"gamma_half_notional", 0.}};
groups[u]["delta_units"] = groups[u]["delta_units"].get<double>() + d;
groups[u]["delta_notional"] = groups[u]["delta_notional"].get<double>() + d * s;
groups[u]["gamma_half_notional"] =
groups[u]["gamma_half_notional"].get<double>() + .5 * g * s * s;
rows.push_back({{"id", p["id"]},
{"type", type},
{"underlying", u},
{"unit_model", m},
{"market_value", mv},
{"delta_units", d},
{"gamma_units", g},
{"vega_1pp", m["vega_1pp"].get<double>() * n}});
}
auto shocks = v.value("scenario", json{{"spot", -.1}, {"volatility", .05}, {"days", 7}});
double ds = model_number(shocks, "spot", -.99, 2),
dv = model_number(shocks, "volatility", -4.9, 4.9),
dd = model_number(shocks, "days", 0, 3650);
double pnl = 0;
json scenario_rows = json::array();
for (size_t i = 0; i < v["positions"].size(); ++i) {
auto p = v["positions"][i];
double n = p["quantity"].get<double>() * p["multiplier"].get<double>(),
s = p["spot"].get<double>();
double change = s * ds;
if (p["type"] == "OPT") {
p["spot"] = s * (1 + ds);
p["years"] = std::max(0., p["years"].get<double>() - dd / 365.);
p["volatility"] = p["volatility"].get<double>() + dv;
require(p["volatility"].get<double>() >= .0001 && p["volatility"].get<double>() <= 5,
"Scenario volatility outside model domain");
auto repriced = european_option(p);
change = repriced["price"].get<double>() - rows[i]["unit_model"]["price"].get<double>();
}
pnl += change * n;
scenario_rows.push_back({{"id", p["id"]}, {"pnl", change * n}});
}
for (auto &[u, g] : groups.items())
g["delta_neutral_stock_units"] = -g["delta_units"].get<double>();
require(std::isfinite(value) && std::isfinite(pnl) && std::isfinite(vega) &&
std::isfinite(theta),
"Hedge model overflow");
return {{"currency", v["currency"]},
{"market_value", value},
{"vega_1pp", vega},
{"theta_day", theta},
{"underlyings", groups},
{"positions", rows},
{"scenario_pnl", pnl},
{"scenario_rows", scenario_rows},
{"scenario", shocks},
{"basis", "Modeled single-currency hedge book; European Black-Scholes continuous "
"yield; futures are linear contract-price shocks with no basis/carry model. "
"Not a trade ledger, margin rule, or executable recommendation."}};
}
} // namespace reach
30 changes: 30 additions & 0 deletions backend/environment.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include "core.hpp"
namespace reach {
inline void workspace_environment(const std::string &mode, const std::string &database) {
require(mode == "user" || mode == "test" || mode == "demo", "Invalid REACH_ENV");
require(mode != "demo" || database == "reachterminal" ||
database.starts_with("reachterminal_demo_"),
"Demo runtime requires a demo database");
require(mode != "user" || (database != "reachterminal" && !database.starts_with("reachterminal_demo_")),
"User runtime cannot use the default demo database");
require((mode == "test") == database.starts_with("reachterminal_test_"),
"Test runtime requires a dedicated reachterminal_test_* database; user/demo cannot use "
"test databases");
}
inline void gateway_environment(const std::string &mode, int port) {
require(mode == "user" || mode == "test", "Gateway disabled in demo workspace");
require(mode == "test" ? port == 5001 : port != 5001,
"Mock Gateway is reserved on localhost:5001; real and test Gateways cannot be mixed");
}
inline void user_batch(const std::string &mode, const json &batch) {
if (mode != "user")
return;
for (auto it = batch.begin(); it != batch.end(); ++it)
if (it.value().is_array())
for (auto &r : it.value())
require(r.value("source", std::string()) != "Synthetic demo" &&
!r.value("id", std::string()).starts_with("demo:"),
"Synthetic demo data prohibited in user workspace");
}
} // namespace reach
Loading
Loading