File size: 7,125 Bytes
d592b34
 
 
 
 
 
 
 
 
 
 
 
e05e65b
d592b34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
language: en
license: mit
tags:
  - finance
  - trading
  - time-series
  - recurrent-neural-network
  - market-regime
  - pytorch
  - interpretable-ai
library_name: strata-market
pipeline_tag: time-series-forecasting
---

# StrataNet β€” Market Trading Neural Architecture

**StrataNet** is a novel recurrent neural network architecture purpose-built for
market trading. It is a direct competitor to LSTM/GRU/Transformer for financial
time-series, but with a fully **interpretable hidden state** and outputs designed
specifically for trading decision support.

> **Install:** `pip install strata-market`  
> **GitHub:** https://github.com/rafaelsistems/strata-market  
> **PyPI:** https://pypi.org/project/strata-market/

---

## What makes StrataNet different?

| | LSTM / GRU | Transformer | **StrataNet** |
|---|---|---|---|
| Domain | General | General | **Market trading only** |
| Hidden state | Opaque N-dim vector | Attention weights | **4-dim, fully interpretable** |
| State bounds | None | None | **Hard bounds per dimension** |
| Output | Price / probability | Token / probability | **LONG/SHORT/HOLD + regime** |
| Label source | Future price required | Manual labels | **Auto from STRATA teacher** |
| Parameters | Millions | Millions | **~5,000 (default)** |

### Interpretable Hidden State

Unlike LSTM/GRU where the hidden state is an opaque vector, StrataNet's hidden
state directly encodes market concepts β€” at every timestep, always:

```
h = [bias, momentum, trap_risk, uncertainty]

bias        ∈ [-1, 1]   directional conviction (negative=bearish, positive=bullish)
momentum    ∈ [ 0, 1]   structural energy from breakouts and volume
trap_risk   ∈ [ 0, 1]   adverse selection / liquidity trap pressure
uncertainty ∈ [ 0, 1]   volatility-driven ambiguity
```

---

## Pretrained Models

This repository contains pretrained weights trained on synthetic OHLCV data
with realistic volatility profiles matching each asset class.

| File | Asset | Profile | Params |
|---|---|---|---|
| `aapl_net.pt` | AAPL | Medium volatility (large-cap tech) | ~5,000 |
| `tsla_net.pt` | TSLA | High volatility (growth/momentum) | ~5,000 |
| `spy_net.pt`  | SPY  | Low volatility (broad market ETF) | ~5,000 |
| `nvda_net.pt` | NVDA | High volatility (semiconductor) | ~5,000 |
| `qqq_net.pt`  | QQQ  | Medium volatility (tech ETF) | ~5,000 |
| `btc_net.pt`  | BTC  | High volatility (crypto) | ~5,000 |

---

## Quick Start

### Load pretrained model

```python
from huggingface_hub import hf_hub_download
from strata import StrataNet

# Download pretrained weights
path  = hf_hub_download(repo_id="emylton/strata-net", filename="aapl_net.pt")
model = StrataNet.load(path)

# Predict from normalised OHLCV window (30 candles)
import torch
from strata.net_trainer import _normalise_window

candles = [{"open": ..., "high": ..., "low": ..., "close": ..., "volume": ...}]  # 30 candles
x       = torch.tensor(_normalise_window(candles), dtype=torch.float32)  # (30, 5)
result  = model.predict_action(x)

print(result)
# {
#   "action":     "LONG",
#   "confidence": 0.71,
#   "regime":     "TRENDING",
#   "state": {
#       "bias":        0.82,   # interpretable β€” always in [-1, 1]
#       "momentum":    0.45,   # interpretable β€” always in [0, 1]
#       "trap_risk":   0.18,   # interpretable β€” always in [0, 1]
#       "uncertainty": 0.31,   # interpretable β€” always in [0, 1]
#   }
# }
```

### Train your own model (no manual labeling needed)

```python
from strata import StrataNet, StrataNetConfig, StrataNetTrainer, StrataNetDataset

# Your candles: list of OHLCV dicts, oldest β†’ newest
candles = [{"open": ..., "high": ..., "low": ..., "close": ..., "volume": ...}, ...]

# Labels auto-generated by STRATA rule-based teacher (no manual labeling)
dataset = StrataNetDataset.from_candles(candles, seq_len=30, asset="AAPL")
trainer = StrataNetTrainer(asset="AAPL")
model   = trainer.train(dataset, epochs=30)

model.save("my_model.pt")
```

### Fine-tune from pretrained

```python
from huggingface_hub import hf_hub_download
from strata import StrataNet, StrataNetTrainer, StrataNetDataset

# Load pretrained base
path  = hf_hub_download(repo_id="emylton/strata-net", filename="aapl_net.pt")
model = StrataNet.load(path)

# Fine-tune on your own data
dataset = StrataNetDataset.from_candles(my_candles, seq_len=30, asset="AAPL")
trainer = StrataNetTrainer(model=model, lr=1e-4)   # lower LR for fine-tuning
model   = trainer.train(dataset, epochs=10)
```

---

## Architecture

```
Input: OHLCV window  (B, T, 5)
         β”‚
         β–Ό
StrataEmbedding      learned OHLCV β†’ semantic features
         β”‚            Linear(5 β†’ embed_dim) + LayerNorm + GELU
         β–Ό
StrataCoreCell  Γ—T   custom GRU-like recurrent cell
         β”‚            β€” update gate z  (how much to update state)
         β”‚            β€” reset gate r   (how much past to forget)
         │            — candidate h̃   projected to 4-dim
         β”‚            β€” bounds enforced: tanh for bias, sigmoid for others
         β–Ό
StrataHead           hidden state β†’ outputs
         β”‚            Linear(4 β†’ head_dim) + GELU
         β”‚            β†’ action logits  (3: LONG/SHORT/HOLD)
         β”‚            β†’ confidence     (1: sigmoid)
         β”‚            β†’ regime logits  (4: TRENDING/RANGING/TRANSITIONING/CHOPPY)
         β–Ό
StrataNetOutput
```

---

## Training Methodology: Teacher-Student Distillation

StrataNet uses **knowledge distillation from a rule-based teacher** β€” no manual
price labeling required:

1. **Teacher**: STRATA rule-based state machine processes OHLCV windows and
   generates action labels (LONG/SHORT/HOLD) and regime labels using explicit,
   auditable rules.
2. **Student**: StrataNet learns to replicate the teacher's decisions while
   discovering its own compact representation in the 4-dim hidden state.
3. **Result**: A neural network that generalises the teacher's pattern recognition
   while being faster (single forward pass, no iterative state updates).

---

## Tick-Level Sensing (v2.6+)

If your data includes bid/ask prices, STRATA automatically upgrades from
**reactive** (volume-based) to **anticipatory** (spread-based) liquidity sensing:

```python
# Add bid/ask to candles β€” sense() auto-detects and uses spread
candle = {
    "open": 150.0, "high": 150.5, "low": 149.8, "close": 150.2,
    "volume": 1_000_000,
    "bid": 150.18, "ask": 150.22,    # spread = 0.04 (2.7 bps)
}
from strata import sense
signals = sense([...window..., candle])
# signals["source"] == "spread"      <- anticipatory (before price moves)
# signals["liquidity_above"] = 0.61  <- elevated: spread widened vs history
```

---

## Citation

If you use STRATA in research or production, please cite:

```bibtex
@software{strata2024,
  author    = {emylton},
  title     = {STRATA: A Stateful Market Trading Architecture},
  year      = {2024},
  url       = {https://github.com/rafaelsistems/strata-market},
  version   = {2.6.0},
}
```

---

## License

MIT License. See [LICENSE](https://github.com/rafaelsistems/strata-market/blob/main/LICENSE).