crca 1.4.0__py3-none-any.whl → 1.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- CRCA.py +172 -7
- MODEL_CARD.md +53 -0
- PKG-INFO +8 -2
- RELEASE_NOTES.md +17 -0
- STABILITY.md +19 -0
- architecture/hybrid/consistency_engine.py +362 -0
- architecture/hybrid/conversation_manager.py +421 -0
- architecture/hybrid/explanation_generator.py +452 -0
- architecture/hybrid/few_shot_learner.py +533 -0
- architecture/hybrid/graph_compressor.py +286 -0
- architecture/hybrid/hybrid_agent.py +4398 -0
- architecture/hybrid/language_compiler.py +623 -0
- architecture/hybrid/main,py +0 -0
- architecture/hybrid/reasoning_tracker.py +322 -0
- architecture/hybrid/self_verifier.py +524 -0
- architecture/hybrid/task_decomposer.py +567 -0
- architecture/hybrid/text_corrector.py +341 -0
- benchmark_results/crca_core_benchmarks.json +178 -0
- branches/crca_sd/crca_sd_realtime.py +6 -2
- branches/general_agent/__init__.py +102 -0
- branches/general_agent/general_agent.py +1400 -0
- branches/general_agent/personality.py +169 -0
- branches/general_agent/utils/__init__.py +19 -0
- branches/general_agent/utils/prompt_builder.py +170 -0
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/METADATA +8 -2
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/RECORD +303 -20
- crca_core/__init__.py +35 -0
- crca_core/benchmarks/__init__.py +14 -0
- crca_core/benchmarks/synthetic_scm.py +103 -0
- crca_core/core/__init__.py +23 -0
- crca_core/core/api.py +120 -0
- crca_core/core/estimate.py +208 -0
- crca_core/core/godclass.py +72 -0
- crca_core/core/intervention_design.py +174 -0
- crca_core/core/lifecycle.py +48 -0
- crca_core/discovery/__init__.py +9 -0
- crca_core/discovery/tabular.py +193 -0
- crca_core/identify/__init__.py +171 -0
- crca_core/identify/backdoor.py +39 -0
- crca_core/identify/frontdoor.py +48 -0
- crca_core/identify/graph.py +106 -0
- crca_core/identify/id_algorithm.py +43 -0
- crca_core/identify/iv.py +48 -0
- crca_core/models/__init__.py +67 -0
- crca_core/models/provenance.py +56 -0
- crca_core/models/refusal.py +39 -0
- crca_core/models/result.py +83 -0
- crca_core/models/spec.py +151 -0
- crca_core/models/validation.py +68 -0
- crca_core/scm/__init__.py +9 -0
- crca_core/scm/linear_gaussian.py +198 -0
- crca_core/timeseries/__init__.py +6 -0
- crca_core/timeseries/pcmci.py +181 -0
- crca_llm/__init__.py +12 -0
- crca_llm/client.py +85 -0
- crca_llm/coauthor.py +118 -0
- crca_llm/orchestrator.py +289 -0
- crca_llm/types.py +21 -0
- crca_reasoning/__init__.py +16 -0
- crca_reasoning/critique.py +54 -0
- crca_reasoning/godclass.py +206 -0
- crca_reasoning/memory.py +24 -0
- crca_reasoning/rationale.py +10 -0
- crca_reasoning/react_controller.py +81 -0
- crca_reasoning/tool_router.py +97 -0
- crca_reasoning/types.py +40 -0
- crca_sd/__init__.py +15 -0
- crca_sd/crca_sd_core.py +2 -0
- crca_sd/crca_sd_governance.py +2 -0
- crca_sd/crca_sd_mpc.py +2 -0
- crca_sd/crca_sd_realtime.py +2 -0
- crca_sd/crca_sd_tui.py +2 -0
- cuda-keyring_1.1-1_all.deb +0 -0
- cuda-keyring_1.1-1_all.deb.1 +0 -0
- docs/IMAGE_ANNOTATION_USAGE.md +539 -0
- docs/INSTALL_DEEPSPEED.md +125 -0
- docs/api/branches/crca-cg.md +19 -0
- docs/api/branches/crca-q.md +27 -0
- docs/api/branches/crca-sd.md +37 -0
- docs/api/branches/general-agent.md +24 -0
- docs/api/branches/overview.md +19 -0
- docs/api/crca/agent-methods.md +62 -0
- docs/api/crca/operations.md +79 -0
- docs/api/crca/overview.md +32 -0
- docs/api/image-annotation/engine.md +52 -0
- docs/api/image-annotation/overview.md +17 -0
- docs/api/schemas/annotation.md +34 -0
- docs/api/schemas/core-schemas.md +82 -0
- docs/api/schemas/overview.md +32 -0
- docs/api/schemas/policy.md +30 -0
- docs/api/utils/conversation.md +22 -0
- docs/api/utils/graph-reasoner.md +32 -0
- docs/api/utils/overview.md +21 -0
- docs/api/utils/router.md +19 -0
- docs/api/utils/utilities.md +97 -0
- docs/architecture/causal-graphs.md +41 -0
- docs/architecture/data-flow.md +29 -0
- docs/architecture/design-principles.md +33 -0
- docs/architecture/hybrid-agent/components.md +38 -0
- docs/architecture/hybrid-agent/consistency.md +26 -0
- docs/architecture/hybrid-agent/overview.md +44 -0
- docs/architecture/hybrid-agent/reasoning.md +22 -0
- docs/architecture/llm-integration.md +26 -0
- docs/architecture/modular-structure.md +37 -0
- docs/architecture/overview.md +69 -0
- docs/architecture/policy-engine-arch.md +29 -0
- docs/branches/crca-cg/corposwarm.md +39 -0
- docs/branches/crca-cg/esg-scoring.md +30 -0
- docs/branches/crca-cg/multi-agent.md +35 -0
- docs/branches/crca-cg/overview.md +40 -0
- docs/branches/crca-q/alternative-data.md +55 -0
- docs/branches/crca-q/architecture.md +71 -0
- docs/branches/crca-q/backtesting.md +45 -0
- docs/branches/crca-q/causal-engine.md +33 -0
- docs/branches/crca-q/execution.md +39 -0
- docs/branches/crca-q/market-data.md +60 -0
- docs/branches/crca-q/overview.md +58 -0
- docs/branches/crca-q/philosophy.md +60 -0
- docs/branches/crca-q/portfolio-optimization.md +66 -0
- docs/branches/crca-q/risk-management.md +102 -0
- docs/branches/crca-q/setup.md +65 -0
- docs/branches/crca-q/signal-generation.md +61 -0
- docs/branches/crca-q/signal-validation.md +43 -0
- docs/branches/crca-sd/core.md +84 -0
- docs/branches/crca-sd/governance.md +53 -0
- docs/branches/crca-sd/mpc-solver.md +65 -0
- docs/branches/crca-sd/overview.md +59 -0
- docs/branches/crca-sd/realtime.md +28 -0
- docs/branches/crca-sd/tui.md +20 -0
- docs/branches/general-agent/overview.md +37 -0
- docs/branches/general-agent/personality.md +36 -0
- docs/branches/general-agent/prompt-builder.md +30 -0
- docs/changelog/index.md +79 -0
- docs/contributing/code-style.md +69 -0
- docs/contributing/documentation.md +43 -0
- docs/contributing/overview.md +29 -0
- docs/contributing/testing.md +29 -0
- docs/core/crcagent/async-operations.md +65 -0
- docs/core/crcagent/automatic-extraction.md +107 -0
- docs/core/crcagent/batch-prediction.md +80 -0
- docs/core/crcagent/bayesian-inference.md +60 -0
- docs/core/crcagent/causal-graph.md +92 -0
- docs/core/crcagent/counterfactuals.md +96 -0
- docs/core/crcagent/deterministic-simulation.md +78 -0
- docs/core/crcagent/dual-mode-operation.md +82 -0
- docs/core/crcagent/initialization.md +88 -0
- docs/core/crcagent/optimization.md +65 -0
- docs/core/crcagent/overview.md +63 -0
- docs/core/crcagent/time-series.md +57 -0
- docs/core/schemas/annotation.md +30 -0
- docs/core/schemas/core-schemas.md +82 -0
- docs/core/schemas/overview.md +30 -0
- docs/core/schemas/policy.md +41 -0
- docs/core/templates/base-agent.md +31 -0
- docs/core/templates/feature-mixins.md +31 -0
- docs/core/templates/overview.md +29 -0
- docs/core/templates/templates-guide.md +75 -0
- docs/core/tools/mcp-client.md +34 -0
- docs/core/tools/overview.md +24 -0
- docs/core/utils/conversation.md +27 -0
- docs/core/utils/graph-reasoner.md +29 -0
- docs/core/utils/overview.md +27 -0
- docs/core/utils/router.md +27 -0
- docs/core/utils/utilities.md +97 -0
- docs/css/custom.css +84 -0
- docs/examples/basic-usage.md +57 -0
- docs/examples/general-agent/general-agent-examples.md +50 -0
- docs/examples/hybrid-agent/hybrid-agent-examples.md +56 -0
- docs/examples/image-annotation/image-annotation-examples.md +54 -0
- docs/examples/integration/integration-examples.md +58 -0
- docs/examples/overview.md +37 -0
- docs/examples/trading/trading-examples.md +46 -0
- docs/features/causal-reasoning/advanced-topics.md +101 -0
- docs/features/causal-reasoning/counterfactuals.md +43 -0
- docs/features/causal-reasoning/do-calculus.md +50 -0
- docs/features/causal-reasoning/overview.md +47 -0
- docs/features/causal-reasoning/structural-models.md +52 -0
- docs/features/hybrid-agent/advanced-components.md +55 -0
- docs/features/hybrid-agent/core-components.md +64 -0
- docs/features/hybrid-agent/overview.md +34 -0
- docs/features/image-annotation/engine.md +82 -0
- docs/features/image-annotation/features.md +113 -0
- docs/features/image-annotation/integration.md +75 -0
- docs/features/image-annotation/overview.md +53 -0
- docs/features/image-annotation/quickstart.md +73 -0
- docs/features/policy-engine/doctrine-ledger.md +105 -0
- docs/features/policy-engine/monitoring.md +44 -0
- docs/features/policy-engine/mpc-control.md +89 -0
- docs/features/policy-engine/overview.md +46 -0
- docs/getting-started/configuration.md +225 -0
- docs/getting-started/first-agent.md +164 -0
- docs/getting-started/installation.md +144 -0
- docs/getting-started/quickstart.md +137 -0
- docs/index.md +118 -0
- docs/js/mathjax.js +13 -0
- docs/lrm/discovery_proof_notes.md +25 -0
- docs/lrm/finetune_full.md +83 -0
- docs/lrm/math_appendix.md +120 -0
- docs/lrm/overview.md +32 -0
- docs/mkdocs.yml +238 -0
- docs/stylesheets/extra.css +21 -0
- docs_generated/crca_core/CounterfactualResult.md +12 -0
- docs_generated/crca_core/DiscoveryHypothesisResult.md +13 -0
- docs_generated/crca_core/DraftSpec.md +13 -0
- docs_generated/crca_core/EstimateResult.md +13 -0
- docs_generated/crca_core/IdentificationResult.md +17 -0
- docs_generated/crca_core/InterventionDesignResult.md +12 -0
- docs_generated/crca_core/LockedSpec.md +15 -0
- docs_generated/crca_core/RefusalResult.md +12 -0
- docs_generated/crca_core/ValidationReport.md +9 -0
- docs_generated/crca_core/index.md +13 -0
- examples/general_agent_example.py +277 -0
- examples/general_agent_quickstart.py +202 -0
- examples/general_agent_simple.py +92 -0
- examples/hybrid_agent_auto_extraction.py +84 -0
- examples/hybrid_agent_dictionary_demo.py +104 -0
- examples/hybrid_agent_enhanced.py +179 -0
- examples/hybrid_agent_general_knowledge.py +107 -0
- examples/image_annotation_quickstart.py +328 -0
- examples/test_hybrid_fixes.py +77 -0
- image_annotation/__init__.py +27 -0
- image_annotation/annotation_engine.py +2593 -0
- install_cuda_wsl2.sh +59 -0
- install_deepspeed.sh +56 -0
- install_deepspeed_simple.sh +87 -0
- mkdocs.yml +252 -0
- ollama/Modelfile +8 -0
- prompts/__init__.py +2 -1
- prompts/default_crca.py +9 -1
- prompts/general_agent.py +227 -0
- prompts/image_annotation.py +56 -0
- pyproject.toml +17 -2
- requirements-docs.txt +10 -0
- requirements.txt +21 -2
- schemas/__init__.py +26 -1
- schemas/annotation.py +222 -0
- schemas/conversation.py +193 -0
- schemas/hybrid.py +211 -0
- schemas/reasoning.py +276 -0
- schemas_export/crca_core/CounterfactualResult.schema.json +108 -0
- schemas_export/crca_core/DiscoveryHypothesisResult.schema.json +113 -0
- schemas_export/crca_core/DraftSpec.schema.json +635 -0
- schemas_export/crca_core/EstimateResult.schema.json +113 -0
- schemas_export/crca_core/IdentificationResult.schema.json +145 -0
- schemas_export/crca_core/InterventionDesignResult.schema.json +111 -0
- schemas_export/crca_core/LockedSpec.schema.json +646 -0
- schemas_export/crca_core/RefusalResult.schema.json +90 -0
- schemas_export/crca_core/ValidationReport.schema.json +62 -0
- scripts/build_lrm_dataset.py +80 -0
- scripts/export_crca_core_schemas.py +54 -0
- scripts/export_hf_lrm.py +37 -0
- scripts/export_ollama_gguf.py +45 -0
- scripts/generate_changelog.py +157 -0
- scripts/generate_crca_core_docs_from_schemas.py +86 -0
- scripts/run_crca_core_benchmarks.py +163 -0
- scripts/run_full_finetune.py +198 -0
- scripts/run_lrm_eval.py +31 -0
- templates/graph_management.py +29 -0
- tests/conftest.py +9 -0
- tests/test_core.py +2 -3
- tests/test_crca_core_discovery_tabular.py +15 -0
- tests/test_crca_core_estimate_dowhy.py +36 -0
- tests/test_crca_core_identify.py +18 -0
- tests/test_crca_core_intervention_design.py +36 -0
- tests/test_crca_core_linear_gaussian_scm.py +69 -0
- tests/test_crca_core_spec.py +25 -0
- tests/test_crca_core_timeseries_pcmci.py +15 -0
- tests/test_crca_llm_coauthor.py +12 -0
- tests/test_crca_llm_orchestrator.py +80 -0
- tests/test_hybrid_agent_llm_enhanced.py +556 -0
- tests/test_image_annotation_demo.py +376 -0
- tests/test_image_annotation_operational.py +408 -0
- tests/test_image_annotation_unit.py +551 -0
- tests/test_training_moe.py +13 -0
- training/__init__.py +42 -0
- training/datasets.py +140 -0
- training/deepspeed_zero2_0_5b.json +22 -0
- training/deepspeed_zero2_1_5b.json +22 -0
- training/deepspeed_zero3_0_5b.json +28 -0
- training/deepspeed_zero3_14b.json +28 -0
- training/deepspeed_zero3_h100_3gpu.json +20 -0
- training/deepspeed_zero3_offload.json +28 -0
- training/eval.py +92 -0
- training/finetune.py +516 -0
- training/public_datasets.py +89 -0
- training_data/react_train.jsonl +7473 -0
- utils/agent_discovery.py +311 -0
- utils/batch_processor.py +317 -0
- utils/conversation.py +78 -0
- utils/edit_distance.py +118 -0
- utils/formatter.py +33 -0
- utils/graph_reasoner.py +530 -0
- utils/rate_limiter.py +283 -0
- utils/router.py +2 -2
- utils/tool_discovery.py +307 -0
- webui/__init__.py +10 -0
- webui/app.py +229 -0
- webui/config.py +104 -0
- webui/static/css/style.css +332 -0
- webui/static/js/main.js +284 -0
- webui/templates/index.html +42 -0
- tests/test_crca_excel.py +0 -166
- tests/test_data_broker.py +0 -424
- tests/test_palantir.py +0 -349
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/WHEEL +0 -0
- {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Backtesting Engine
|
|
2
|
+
|
|
3
|
+
Backtesting engine for evaluating trading strategies.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Backtesting engine performs walk-forward analysis to evaluate strategy performance on historical data.
|
|
8
|
+
|
|
9
|
+
## Mathematical Foundation
|
|
10
|
+
|
|
11
|
+
### Walk-Forward Analysis
|
|
12
|
+
|
|
13
|
+
Rolling window approach:
|
|
14
|
+
|
|
15
|
+
- Train window: $[T-H, T]$
|
|
16
|
+
- Test window: $[T, T+h]$
|
|
17
|
+
- Step forward: $T \leftarrow T + h$
|
|
18
|
+
|
|
19
|
+
### Performance Metrics
|
|
20
|
+
|
|
21
|
+
**Sharpe Ratio**:
|
|
22
|
+
$$Sharpe = \frac{E[R] - R_f}{\sigma(R)}$$
|
|
23
|
+
|
|
24
|
+
**Sortino Ratio**:
|
|
25
|
+
$$Sortino = \frac{E[R] - R_f}{\sigma_{down}(R)}$$
|
|
26
|
+
|
|
27
|
+
**Max Drawdown**:
|
|
28
|
+
$$MDD = \max_t \frac{Peak_t - Value_t}{Peak_t}$$
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from branches.CRCA-Q import BacktestEngine
|
|
34
|
+
|
|
35
|
+
engine = BacktestEngine()
|
|
36
|
+
results = engine.backtest(
|
|
37
|
+
strategy=strategy,
|
|
38
|
+
data=historical_data,
|
|
39
|
+
initial_capital=100000
|
|
40
|
+
)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Next Steps
|
|
44
|
+
|
|
45
|
+
- [Setup](setup.md) - Setup and configuration
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Causal Reasoning Engine
|
|
2
|
+
|
|
3
|
+
The causal reasoning engine validates trading signals using causal inference.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Causal engine builds Structural Causal Models and validates signals for causal validity rather than just correlation.
|
|
8
|
+
|
|
9
|
+
## Mathematical Foundation
|
|
10
|
+
|
|
11
|
+
Causal engine builds SCM:
|
|
12
|
+
|
|
13
|
+
$$M_t = f_M(U_M, Vol_{t-1})$$
|
|
14
|
+
$$Vol_t = f_Vol(U_Vol, L_t)$$
|
|
15
|
+
$$L_t = f_L(U_L, Volume_t)$$
|
|
16
|
+
$$Price_t = f_Price(U_Price, M_t, Vol_t, L_t)$$
|
|
17
|
+
|
|
18
|
+
Signal validation checks:
|
|
19
|
+
|
|
20
|
+
$$P(Returns | do(Signal=s)) \neq P(Returns)$$
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from branches.CRCA-Q import CausalEngine
|
|
26
|
+
|
|
27
|
+
engine = CausalEngine()
|
|
28
|
+
validated = engine.validate_signal(signal, target="returns")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Next Steps
|
|
32
|
+
|
|
33
|
+
- [Signal Validation](signal-validation.md) - Signal validation
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Trade Execution
|
|
2
|
+
|
|
3
|
+
Trade execution system for CRCA-Q.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Execution engine places trades on exchanges, handling order placement, fills, and transaction costs.
|
|
8
|
+
|
|
9
|
+
## Mathematical Foundation
|
|
10
|
+
|
|
11
|
+
Execution accounts for:
|
|
12
|
+
|
|
13
|
+
**Transaction Costs**:
|
|
14
|
+
$$TC = Spread + Slippage + Fees$$
|
|
15
|
+
|
|
16
|
+
Where:
|
|
17
|
+
- $Spread = (Ask - Bid) / Mid$
|
|
18
|
+
- $Slippage = \alpha \cdot \sqrt{Size / Volume}$
|
|
19
|
+
- $Fees = Size \cdot FeeRate$
|
|
20
|
+
|
|
21
|
+
**Market Impact**:
|
|
22
|
+
$$Impact = 0.5 \cdot \sqrt{\frac{TradeValue}{DailyVolume}}$$
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from branches.CRCA-Q import ExecutionEngine
|
|
28
|
+
|
|
29
|
+
engine = ExecutionEngine(exchange="kraken")
|
|
30
|
+
result = engine.execute_trade(
|
|
31
|
+
signal='BUY',
|
|
32
|
+
size_fraction=0.1,
|
|
33
|
+
base_symbol='ETH'
|
|
34
|
+
)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Next Steps
|
|
38
|
+
|
|
39
|
+
- [Backtesting](backtesting.md) - Backtesting engine
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Market Data Client
|
|
2
|
+
|
|
3
|
+
MarketDataClient fetches and normalizes market data from multiple sources.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
MarketDataClient provides a unified interface for accessing market data from various sources, normalizing it to a consistent schema.
|
|
8
|
+
|
|
9
|
+
## Data Sources
|
|
10
|
+
|
|
11
|
+
- **Crypto**: CoinGecko API (free tier)
|
|
12
|
+
- **Stocks**: Yahoo Finance (via `yfinance`)
|
|
13
|
+
- **FX/Futures**: Yahoo Finance with symbol transformations
|
|
14
|
+
|
|
15
|
+
## Unified Schema
|
|
16
|
+
|
|
17
|
+
All data is normalized to:
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
{
|
|
21
|
+
'date': pd.Timestamp,
|
|
22
|
+
'price': float, # Close price
|
|
23
|
+
'volume': float, # Trading volume
|
|
24
|
+
'market_cap': float, # Market capitalization
|
|
25
|
+
'returns': float # Computed returns
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from branches.CRCA-Q import MarketDataClient
|
|
33
|
+
|
|
34
|
+
client = MarketDataClient()
|
|
35
|
+
|
|
36
|
+
# Fetch single asset
|
|
37
|
+
data = client.fetch_price_data("BTC", days=365)
|
|
38
|
+
|
|
39
|
+
# Fetch multiple assets
|
|
40
|
+
multi_data = client.fetch_multiple_assets(["BTC", "ETH"], days=365)
|
|
41
|
+
|
|
42
|
+
# Compute covariance
|
|
43
|
+
covariance = client.compute_multi_asset_covariance(multi_data)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Mathematical Foundation
|
|
47
|
+
|
|
48
|
+
Returns are computed as:
|
|
49
|
+
|
|
50
|
+
$$r_t = \frac{P_t - P_{t-1}}{P_{t-1}} = \frac{\Delta P_t}{P_{t-1}}$$
|
|
51
|
+
|
|
52
|
+
Covariance is estimated using EWMA:
|
|
53
|
+
|
|
54
|
+
$$\Sigma_t = (1-\alpha) \Sigma_{t-1} + \alpha r_t r_t'$$
|
|
55
|
+
|
|
56
|
+
Where $\alpha = 0.06$ (decay factor).
|
|
57
|
+
|
|
58
|
+
## Next Steps
|
|
59
|
+
|
|
60
|
+
- [Signal Generation](signal-generation.md) - Generate trading signals
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# CRCA-Q Overview
|
|
2
|
+
|
|
3
|
+
CRCA-Q is a quantitative trading system that integrates causal reasoning with traditional quantitative finance techniques.
|
|
4
|
+
|
|
5
|
+
## Executive Summary
|
|
6
|
+
|
|
7
|
+
CRCA-Q uses **causal inference** to understand *why* market movements occur, enabling more robust predictions that remain valid across different market regimes. Unlike correlation-based systems, CRCA-Q implements Structural Causal Models (SCMs) based on Judea Pearl's framework.
|
|
8
|
+
|
|
9
|
+
## Key Features
|
|
10
|
+
|
|
11
|
+
- **Causal Signal Validation**: Validates trading signals using causal reasoning
|
|
12
|
+
- **Portfolio Optimization**: CVaR-based portfolio optimization
|
|
13
|
+
- **Risk Management**: Multi-layer risk controls and circuit breakers
|
|
14
|
+
- **Alternative Data**: Integration with on-chain, social, and news data
|
|
15
|
+
- **Backtesting**: Walk-forward analysis and performance metrics
|
|
16
|
+
|
|
17
|
+
## Mathematical Foundation
|
|
18
|
+
|
|
19
|
+
CRCA-Q implements Structural Causal Models for market variables:
|
|
20
|
+
|
|
21
|
+
$$M_t = f_M(U_M, Vol_{t-1})$$
|
|
22
|
+
$$Vol_t = f_Vol(U_Vol, L_t)$$
|
|
23
|
+
$$L_t = f_L(U_L, Volume_t)$$
|
|
24
|
+
$$Price_t = f_Price(U_Price, M_t, Vol_t, L_t)$$
|
|
25
|
+
|
|
26
|
+
Where:
|
|
27
|
+
- $M_t$: Momentum
|
|
28
|
+
- $Vol_t$: Volatility
|
|
29
|
+
- $L_t$: Liquidity
|
|
30
|
+
- $Price_t$: Price
|
|
31
|
+
|
|
32
|
+
## Philosophy
|
|
33
|
+
|
|
34
|
+
### Causal vs. Correlational Trading
|
|
35
|
+
|
|
36
|
+
Traditional systems rely on correlations: "When X happens, Y tends to follow."
|
|
37
|
+
|
|
38
|
+
CRCA-Q uses causal reasoning: "X *causes* Y through mechanism Z."
|
|
39
|
+
|
|
40
|
+
This approach is more robust because:
|
|
41
|
+
- It explains *why* relationships exist
|
|
42
|
+
- It predicts behavior under interventions
|
|
43
|
+
- It remains valid across market regimes
|
|
44
|
+
|
|
45
|
+
## Documentation
|
|
46
|
+
|
|
47
|
+
- **[Philosophy](philosophy.md)**: Causal vs. correlational trading
|
|
48
|
+
- **[Architecture](architecture.md)**: System architecture
|
|
49
|
+
- **[Market Data](market-data.md)**: Market data client
|
|
50
|
+
- **[Signal Generation](signal-generation.md)**: Signal generation classes
|
|
51
|
+
- **[Portfolio Optimization](portfolio-optimization.md)**: Portfolio optimization
|
|
52
|
+
- **[Risk Management](risk-management.md)**: Risk management framework
|
|
53
|
+
- **[Setup](setup.md)**: Setup and configuration
|
|
54
|
+
|
|
55
|
+
## Next Steps
|
|
56
|
+
|
|
57
|
+
- [Philosophy](philosophy.md) - Understand the causal approach
|
|
58
|
+
- [Architecture](architecture.md) - System architecture
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Causal vs. Correlational Trading
|
|
2
|
+
|
|
3
|
+
CRCA-Q's fundamental philosophy is based on causal reasoning rather than correlation-based patterns.
|
|
4
|
+
|
|
5
|
+
## The Fundamental Problem
|
|
6
|
+
|
|
7
|
+
Traditional quantitative trading systems rely heavily on **correlational patterns**: "When X happens, Y tends to follow." This approach has critical limitations:
|
|
8
|
+
|
|
9
|
+
1. **Regime Dependency**: Correlations break down when market regimes change (e.g., bull vs. bear markets)
|
|
10
|
+
2. **Spurious Relationships**: Many correlations are coincidental, not causal
|
|
11
|
+
3. **Lack of Interpretability**: It's unclear *why* a signal works
|
|
12
|
+
4. **Overfitting Risk**: Complex models can memorize patterns without understanding mechanisms
|
|
13
|
+
|
|
14
|
+
## The Causal Solution
|
|
15
|
+
|
|
16
|
+
CRCA-Q implements **causal reasoning** based on Judea Pearl's framework:
|
|
17
|
+
|
|
18
|
+
- **Structural Causal Models (SCMs)**: Represent market variables as nodes in a DAG
|
|
19
|
+
- **Do-Calculus**: Reason about interventions using Pearl's do-operator
|
|
20
|
+
- **Counterfactual Reasoning**: Answer "what would have happened if" questions
|
|
21
|
+
- **Confounder Identification**: Distinguish direct causal effects from spurious correlations
|
|
22
|
+
|
|
23
|
+
## Mathematical Foundation
|
|
24
|
+
|
|
25
|
+
### Correlation vs. Causation
|
|
26
|
+
|
|
27
|
+
**Correlation**: $P(Y | X) \neq P(Y)$
|
|
28
|
+
|
|
29
|
+
**Causation**: $P(Y | do(X)) \neq P(Y)$
|
|
30
|
+
|
|
31
|
+
The key difference is the do-operator, which represents intervention rather than observation.
|
|
32
|
+
|
|
33
|
+
### Causal Effect
|
|
34
|
+
|
|
35
|
+
The causal effect of $X$ on $Y$ is:
|
|
36
|
+
|
|
37
|
+
$$E[Y | do(X=x)] = \sum_{z} E[Y | X=x, Z=z] P(Z=z)$$
|
|
38
|
+
|
|
39
|
+
Where $Z$ are confounders that need to be adjusted for.
|
|
40
|
+
|
|
41
|
+
## Example: Volume and Price
|
|
42
|
+
|
|
43
|
+
**Correlational view**: "High volume correlates with price increases"
|
|
44
|
+
|
|
45
|
+
**Causal view**: "High volume *causes* increased liquidity, which *causes* reduced price impact, which *causes* more efficient price discovery"
|
|
46
|
+
|
|
47
|
+
The causal view is more robust because it explains *why* the relationship exists and can predict behavior under interventions.
|
|
48
|
+
|
|
49
|
+
## Why This Matters
|
|
50
|
+
|
|
51
|
+
Causal models:
|
|
52
|
+
- Explain *why* relationships exist
|
|
53
|
+
- Predict behavior under interventions
|
|
54
|
+
- Remain valid across different market regimes
|
|
55
|
+
- Enable robust counterfactual analysis
|
|
56
|
+
|
|
57
|
+
## Next Steps
|
|
58
|
+
|
|
59
|
+
- [Architecture](architecture.md) - System architecture
|
|
60
|
+
- [Signal Generation](signal-generation.md) - Causal signal validation
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Portfolio Optimization
|
|
2
|
+
|
|
3
|
+
CRCA-Q uses Conditional Value at Risk (CVaR) optimization for portfolio allocation.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Portfolio optimization allocates capital based on expected returns, covariance, and risk constraints.
|
|
8
|
+
|
|
9
|
+
## Mathematical Foundation
|
|
10
|
+
|
|
11
|
+
### Optimization Problem
|
|
12
|
+
|
|
13
|
+
Maximize expected return subject to risk constraints:
|
|
14
|
+
|
|
15
|
+
$$\max_{\mathbf{w}} \boldsymbol{\mu}' \mathbf{w} - \lambda \mathbf{w}' \boldsymbol{\Sigma} \mathbf{w}$$
|
|
16
|
+
|
|
17
|
+
Subject to:
|
|
18
|
+
|
|
19
|
+
$$\sum_{i} |w_i| \leq L$$
|
|
20
|
+
$$w_i \geq -L, \quad w_i \leq L$$
|
|
21
|
+
$$\sum_{i \in T} |w_i| \leq L_T$$
|
|
22
|
+
|
|
23
|
+
Where:
|
|
24
|
+
- $\boldsymbol{\mu}$: Expected returns vector
|
|
25
|
+
- $\boldsymbol{\Sigma}$: Covariance matrix
|
|
26
|
+
- $\mathbf{w}$: Portfolio weights
|
|
27
|
+
- $\lambda$: Risk aversion parameter
|
|
28
|
+
- $L$: Maximum leverage
|
|
29
|
+
- $L_T$: Maximum exposure per asset type $T$
|
|
30
|
+
|
|
31
|
+
### CVaR Optimization
|
|
32
|
+
|
|
33
|
+
CVaR (Conditional Value at Risk) measures expected loss in worst-case scenarios:
|
|
34
|
+
|
|
35
|
+
$$\text{CVaR}_\alpha = E[Loss | Loss \geq \text{VaR}_\alpha]$$
|
|
36
|
+
|
|
37
|
+
Where $\text{VaR}_\alpha$ is the Value at Risk at confidence level $\alpha$.
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from branches.CRCA-Q import PortfolioOptimizer
|
|
43
|
+
|
|
44
|
+
optimizer = PortfolioOptimizer()
|
|
45
|
+
|
|
46
|
+
weights = optimizer.optimize(
|
|
47
|
+
expected_returns=mu,
|
|
48
|
+
covariance=sigma,
|
|
49
|
+
risk_aversion=1.0,
|
|
50
|
+
max_leverage=1.0
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Covariance Estimation
|
|
55
|
+
|
|
56
|
+
Covariance is estimated using EWMA:
|
|
57
|
+
|
|
58
|
+
$$\boldsymbol{\Sigma}_t = (1-\alpha) \boldsymbol{\Sigma}_{t-1} + \alpha \mathbf{r}_t \mathbf{r}_t'$$
|
|
59
|
+
|
|
60
|
+
With shrinkage for stability:
|
|
61
|
+
|
|
62
|
+
$$\boldsymbol{\Sigma}_{shrink} = \beta \frac{\text{tr}(\boldsymbol{\Sigma})}{n} \mathbf{I} + (1-\beta) \boldsymbol{\Sigma}$$
|
|
63
|
+
|
|
64
|
+
## Next Steps
|
|
65
|
+
|
|
66
|
+
- [Risk Management](risk-management.md) - Risk management framework
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Risk Management Framework
|
|
2
|
+
|
|
3
|
+
CRCA-Q implements comprehensive risk management with multiple layers of protection.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Risk management includes position limits, leverage constraints, correlation limits, volatility limits, drawdown limits, and circuit breakers.
|
|
8
|
+
|
|
9
|
+
## Risk Constraints
|
|
10
|
+
|
|
11
|
+
### Position Size Limits
|
|
12
|
+
|
|
13
|
+
Maximum position per asset:
|
|
14
|
+
|
|
15
|
+
$$|w_i| \leq w_{max}$$
|
|
16
|
+
|
|
17
|
+
Where $w_{max}$ is typically 15-20% of portfolio.
|
|
18
|
+
|
|
19
|
+
### Leverage Limits
|
|
20
|
+
|
|
21
|
+
Total leverage constraint:
|
|
22
|
+
|
|
23
|
+
$$\sum_i |w_i| \leq L_{max}$$
|
|
24
|
+
|
|
25
|
+
Where $L_{max}$ is maximum leverage (default: 1.0 for no leverage).
|
|
26
|
+
|
|
27
|
+
### Correlation Limits
|
|
28
|
+
|
|
29
|
+
Prevent over-concentration:
|
|
30
|
+
|
|
31
|
+
$$\rho_{i,j} \leq \rho_{max}$$
|
|
32
|
+
|
|
33
|
+
Where $\rho_{max}$ is typically 0.8.
|
|
34
|
+
|
|
35
|
+
### Volatility Limits
|
|
36
|
+
|
|
37
|
+
Portfolio volatility constraint:
|
|
38
|
+
|
|
39
|
+
$$\sqrt{\mathbf{w}' \boldsymbol{\Sigma} \mathbf{w}} \leq \sigma_{max}$$
|
|
40
|
+
|
|
41
|
+
Where $\sigma_{max}$ is maximum portfolio volatility (default: 20% annualized).
|
|
42
|
+
|
|
43
|
+
### Drawdown Limits
|
|
44
|
+
|
|
45
|
+
Maximum allowed drawdown:
|
|
46
|
+
|
|
47
|
+
$$\text{DD}_t = \frac{\text{Peak}_t - \text{Value}_t}{\text{Peak}_t} \leq \text{DD}_{max}$$
|
|
48
|
+
|
|
49
|
+
Where $\text{DD}_{max}$ is typically 15%.
|
|
50
|
+
|
|
51
|
+
## Circuit Breakers
|
|
52
|
+
|
|
53
|
+
### Daily Loss Limit
|
|
54
|
+
|
|
55
|
+
Maximum daily loss:
|
|
56
|
+
|
|
57
|
+
$$\text{Daily Loss}_t \leq \text{Loss}_{max}$$
|
|
58
|
+
|
|
59
|
+
Where $\text{Loss}_{max}$ is typically 5% of portfolio.
|
|
60
|
+
|
|
61
|
+
### Trade Frequency Limit
|
|
62
|
+
|
|
63
|
+
Maximum trades per day:
|
|
64
|
+
|
|
65
|
+
$$N_{trades} \leq N_{max}$$
|
|
66
|
+
|
|
67
|
+
Where $N_{max}$ is typically 50 trades/day.
|
|
68
|
+
|
|
69
|
+
### Kill Switch
|
|
70
|
+
|
|
71
|
+
Manual emergency stop via file check.
|
|
72
|
+
|
|
73
|
+
## Mathematical Foundation
|
|
74
|
+
|
|
75
|
+
Risk-adjusted returns use Sharpe ratio:
|
|
76
|
+
|
|
77
|
+
$$\text{Sharpe} = \frac{E[R] - R_f}{\sigma(R)}$$
|
|
78
|
+
|
|
79
|
+
Where $R_f$ is the risk-free rate.
|
|
80
|
+
|
|
81
|
+
## Usage
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from branches.CRCA-Q import RiskMonitor
|
|
85
|
+
|
|
86
|
+
monitor = RiskMonitor(
|
|
87
|
+
max_position_size=0.15,
|
|
88
|
+
max_leverage=1.0,
|
|
89
|
+
max_drawdown=0.15
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
is_valid, reason, adjusted_size = monitor.pre_trade_check(
|
|
93
|
+
signal='BUY',
|
|
94
|
+
position_size=0.20,
|
|
95
|
+
current_positions={},
|
|
96
|
+
portfolio_value=10000
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Next Steps
|
|
101
|
+
|
|
102
|
+
- [Setup](setup.md) - Setup and configuration
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# CRCA-Q Setup
|
|
2
|
+
|
|
3
|
+
Setup and configuration guide for CRCA-Q.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Install trading dependencies
|
|
9
|
+
pip install ccxt web3
|
|
10
|
+
|
|
11
|
+
# Or install all optional dependencies
|
|
12
|
+
pip install crca[trading]
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Environment Variables
|
|
16
|
+
|
|
17
|
+
Create `.env` file:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# Required for LLM
|
|
21
|
+
OPENAI_API_KEY=your_openai_key
|
|
22
|
+
|
|
23
|
+
# For live trading (Kraken example)
|
|
24
|
+
KRAKEN_API_KEY=your_kraken_key
|
|
25
|
+
KRAKEN_API_SECRET=your_kraken_secret
|
|
26
|
+
|
|
27
|
+
# For alternative data
|
|
28
|
+
TWITTER_BEARER_TOKEN=your_twitter_token
|
|
29
|
+
NEWSAPI_KEY=your_newsapi_key
|
|
30
|
+
ETHERSCAN_API_KEY=your_etherscan_key
|
|
31
|
+
THEGRAPH_API_KEY=your_thegraph_key
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Configuration
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
TRADING_CONFIG = {
|
|
38
|
+
'account_size': 10000,
|
|
39
|
+
'max_position_size': 0.3,
|
|
40
|
+
'max_position_hard_cap': 0.3,
|
|
41
|
+
'min_trade_value': 5.0,
|
|
42
|
+
'conservative_mode': True,
|
|
43
|
+
'stop_loss_pct': -10.0,
|
|
44
|
+
'stop_gain_pct': 20.0
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick Start
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from branches.CRCA-Q import QuantTradingAgent
|
|
52
|
+
|
|
53
|
+
agent = QuantTradingAgent(
|
|
54
|
+
account_size=10000,
|
|
55
|
+
dry_run=True # Start in demo mode
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
result = agent.run("BTC")
|
|
59
|
+
print(result)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Next Steps
|
|
63
|
+
|
|
64
|
+
- [Overview](overview.md) - Return to overview
|
|
65
|
+
- [Architecture](architecture.md) - System architecture
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Signal Generation
|
|
2
|
+
|
|
3
|
+
CRCA-Q generates hundreds of signals across multiple categories for trading decisions.
|
|
4
|
+
|
|
5
|
+
## Signal Categories
|
|
6
|
+
|
|
7
|
+
### Time-Series Signals
|
|
8
|
+
|
|
9
|
+
Technical indicators based on price/volume history:
|
|
10
|
+
|
|
11
|
+
- **Momentum**: $\log(P_t) - \log(P_{t-k})$ for lookback $k$
|
|
12
|
+
- **Reversal**: Negative of recent returns
|
|
13
|
+
- **MA Distance**: Distance from price to moving average
|
|
14
|
+
|
|
15
|
+
### Volatility Signals
|
|
16
|
+
|
|
17
|
+
Measure and predict volatility:
|
|
18
|
+
|
|
19
|
+
- **Realized Volatility**: $\sqrt{\frac{1}{n}\sum_{i=1}^n (r_i - \bar{r})^2}$
|
|
20
|
+
- **GARCH Volatility**: GARCH(1,1) model
|
|
21
|
+
- **Vol of Vol**: Volatility of volatility
|
|
22
|
+
|
|
23
|
+
### Liquidity Signals
|
|
24
|
+
|
|
25
|
+
Measure market depth:
|
|
26
|
+
|
|
27
|
+
- **Amihud Illiquidity**: $\frac{|r_t|}{V_t}$ (price impact per volume)
|
|
28
|
+
- **VPIN**: Volume-synchronized probability of informed trading
|
|
29
|
+
- **Turnover**: $\frac{V_t}{MC_t}$ (volume to market cap ratio)
|
|
30
|
+
|
|
31
|
+
## Mathematical Foundation
|
|
32
|
+
|
|
33
|
+
Signals are computed and then validated using causal reasoning. A signal $S$ is causally valid if:
|
|
34
|
+
|
|
35
|
+
$$P(Returns | do(S=s)) \neq P(Returns)$$
|
|
36
|
+
|
|
37
|
+
And the relationship is stable across regimes:
|
|
38
|
+
|
|
39
|
+
$$\text{Var}(P(Returns | do(S=s), Regime=r)) < \epsilon$$
|
|
40
|
+
|
|
41
|
+
## Signal Validation
|
|
42
|
+
|
|
43
|
+
Signals are validated using:
|
|
44
|
+
|
|
45
|
+
1. **Mutual Information**: Information-theoretic relationship
|
|
46
|
+
2. **Regime Invariance**: Stability across market regimes
|
|
47
|
+
3. **Structural Consistency**: Mapping to SCM structure
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from branches.CRCA-Q import QuantTradingAgent
|
|
53
|
+
|
|
54
|
+
agent = QuantTradingAgent()
|
|
55
|
+
signals = agent.compute_signals(data)
|
|
56
|
+
validated = agent.validate_signals(signals)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Next Steps
|
|
60
|
+
|
|
61
|
+
- [Portfolio Optimization](portfolio-optimization.md) - Optimize portfolio allocation
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Signal Validation
|
|
2
|
+
|
|
3
|
+
Signal validation ensures trading signals are causally valid, not just correlated.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Signal validation uses causal criteria to filter out spurious signals that may break down in different market regimes.
|
|
8
|
+
|
|
9
|
+
## Mathematical Foundation
|
|
10
|
+
|
|
11
|
+
Signals are validated using:
|
|
12
|
+
|
|
13
|
+
### Causal Score
|
|
14
|
+
|
|
15
|
+
$$Score(S) = w_1 \cdot MI(S, Y) + w_2 \cdot RI(S, Y) + w_3 \cdot SC(S)$$
|
|
16
|
+
|
|
17
|
+
Where:
|
|
18
|
+
- $MI$: Mutual information (40% weight)
|
|
19
|
+
- $RI$: Regime invariance (40% weight)
|
|
20
|
+
- $SC$: Structural consistency (20% weight)
|
|
21
|
+
|
|
22
|
+
### Regime Invariance
|
|
23
|
+
|
|
24
|
+
$$RI(S, Y) = 1 - \text{Var}(Corr(S, Y | Regime=r))$$
|
|
25
|
+
|
|
26
|
+
Causal signals should be stable across regimes.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from branches.CRCA-Q import SignalValidator
|
|
32
|
+
|
|
33
|
+
validator = SignalValidator()
|
|
34
|
+
score = validator.compute_causal_score(
|
|
35
|
+
signal_name="momentum",
|
|
36
|
+
signal_values=signals,
|
|
37
|
+
target=returns
|
|
38
|
+
)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Next Steps
|
|
42
|
+
|
|
43
|
+
- [Portfolio Optimization](portfolio-optimization.md) - Portfolio optimization
|