File size: 9,319 Bytes
ba26ea6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# BankNifty Strategy Engine — README

A **resumable**, **LLM-driven** intraday engine that digests sentiment, expert transcripts, technicals (RSI/MACD), and news to produce trade plans for **BankNifty** (and Nifty for context). The engine simulates executions on **1-minute** data, evaluates P&L, and keeps airtight checkpoints so you can **resume exactly where you left off** after interruptions.

---

## Table of contents

- [Features](#features)
- [Project structure](#project-structure)
- [Data inputs & expected columns](#data-inputs--expected-columns)
- [Installation](#installation)
- [Configuration (.env + config classes)](#configuration-env--config-classes)
- [Running](#running)
- [Resumable checkpoints](#resumable-checkpoints)
- [How the engine works (timeline)](#how-the-engine-works-timeline)
- [Trade simulation rules](#trade-simulation-rules)
- [LLM JSON schemas](#llm-json-schemas)
- [Outputs](#outputs)


---

## Features

- 🧠 **LLM-assisted** trade plans using structured JSON outputs (strict schema).
- 📰 **News-aware** decisions (hourly, last 15 mins at close).
- 📈 **Technicals**: RSI + MACD on hourly/daily series.
- 🧪 **1-minute backtest execution** with deterministic tiebreak rules.
- 🔁 **Resumable** runs via checkpoint (safe to kill & rerun).
-**Flip/No-trade exit enforcement**: if plan flips side or says “No trade” while holding, engine exits at market price.
- 🧠 **Memory** string summarizing the last completed trade gets fed back into prompts.
- 📦 **Excel & Parquet** logs for analysis.

---

## Project structure

```
banknifty_strategy/
├─ app/
│  ├─ __init__.py
│  ├─ engine.py              # Core loop (09:15 → … → 15:30)
│  ├─ models.py              # Pydantic v2 models
│  ├─ prompts.py             # Prompt templates: morning / intrahour / closing
│  ├─ simulator.py           # simulate_trade_from_signal, slice_intraday
│  ├─ dataio.py              # load_data() – reads/normalizes data frames
│  ├─ checkpoint.py          # CheckpointManager – resume & append parquet logs
│  ├─ logging_setup.py       # Rotating file/logger
│  ├─ config.py              # AppConfig, Paths, LLMConfig
│  ├─ llm.py                 # OpenAI client wrapper; strict JSON schema handling
│  ├─ news.py                # summaries_between() helpers
│  ├─ utils.py               # hour_passed(), hourly_ohlc_dict(), helpers
│  └─ writer.py              # to_excel_safely()
├─ scripts/
│  └─ run_backtest.py        # CLI entrypoint
├─ requirements.txt
└─ README.md
```

> **Note**: Keep `app/` a proper package (it must include `__init__.py`). Always run from the project root so imports like `from app.engine import Engine` work.

---

## Data inputs & expected columns

Your **loader** (`app/dataio.py`) should read and normalize sources. The engine expects these **canonical** frames and columns:

### 1. BankNifty hourly (`df_bn_hourly`)
- Columns: `datetime`, `open`, `high`, `low`, `close`, `RSI`, `MACD_Line`, `Signal_Line`
- Granularity: **hourly** (09:15, 10:15, …, 15:15, 15:30)
- Used for: 09:15 previous indicators, hourly OHLC dicts, close price lookup.

### 2. BankNifty 1-minute (`df_bn_1m`)
- Columns: `datetime`, `open`, `high`, `low`, `close`
- Granularity: **1 min**
- Used by: **simulate_trade_from_signal** execution windows.

### 3. Nifty daily or daily-like context (`df_nifty_daily`)
- Columns: `datetime`, `open`, `high`, `low`, `close`, `RSI`, `MACD_Line`, `Signal_Line`
- Used for: contextual morning prompt.

### 4. Sentiment predictions (`df_sentiment`)
- Columns: `predicted_for` (datetime), `proposed_sentiment`, `reasoning`

### 5. Expert transcript (`df_transcript`)
- Columns: `prediction_for` (datetime), `Transcript`
  - (If your raw file has `Prediction_for_date`, normalize to `prediction_for`.)

### 6. News with summaries (`df_news`)
- Columns: `datetime_ist` (datetime), `Article_summary` (string)

> Ensure all datetime columns are timezone-normalized (naive or same tz) and parsed.

---

## Installation

**Python 3.9+** recommended.

```bash
pip install -r requirements.txt
```
---

## Configuration (.env + config classes)

Create a **.env** in project root:

```env
OPENAI_API_KEY=EMPTY
OPENAI_BASE_URL=http://localhost:8000/v1
OPENAI_MODEL=Qwen/Qwen3-4B
```

Edit temperature and top_p values from:

- `app/config.py`

---

## Running

Use the provided script. **Minimal edits** added `--ckpt-dir` (defaults to `<out-dir>/checkpoint`).

```bash
# Always run from project root so "app" package is on sys.path
python -m scripts.run_backtest   --data-dir ./data   --out-dir ./result   --start "2023-12-29 15:15"   --end   "2024-05-01 09:15"
```


---

## Resumable checkpoints

The engine persists state and logs in **Parquet** inside `--ckpt-dir`:

```
<ckpt-dir>/
├─ checkpoint.json           # last_timestamp_processed, state, plans, memory_str
├─ trade_log.parquet
├─ stats_log.parquet
├─ expert_log.parquet
└─ summary_log.parquet
```

You can **kill** the process and **re-run with the same `--ckpt-dir`**. The engine:
- Reads `checkpoint.json`
- Skips timestamps already processed
- Continues from the next tick

Excel mirrors (`*.xlsx`) are written to `--out-dir` for human inspection.

---

## How the engine works (timeline)

At each timestamp `ts` in your **hourly** series:

1. **09:15 — Morning**
   - Gathers **Nifty/BankNifty** previous OHLC + indicators.
   - Pulls **sentiment** + **expert transcript**.
   - Calls LLM (schema **SummaryMorning**) → morning summary.
   - Calls LLM (schema **TradePlan**) → first plan of the day (but the actual open/close is derived dynamically from previous state; first day has no memory).

2. **10:15 — First intrahour**
   - **Simulate** 1-minute window from last slice start → 10:15 using current plan.
   - Log **state change** only if it changed by value (not identity).
   - If **exited** naturally (stop/target), update **memory_str**, **reset state**, move `last_slice_start`.
   - Pull last hour **news**, OHLC dict, current indicators.
   - Call LLM (**DecisionOutput** → `{summary_banknifty, trade}`). 
   - **Flip/No-trade exit enforcement**: if holding and LLM flips side or says “No trade” → **force flatten** at market price (hourly close).
   - Update logs.

3. **11:15 → 15:15 — Subsequent intrahours**
   - Same as 10:15 loop.

4. **15:30 — Close**
   - **Simulate last 15 minutes (15:15 → 15:30)** on 1-minute data.
   - Log state change once; if exited → update memory + reset.
   - LLM **close plan** (schema **TradePlan**).
   - If holding and plan flips/no-trade → **force flatten** at close.
   - Otherwise **carry overnight** (state remains open).
   - Save **checkpoint** after each timestamp.

---

## Trade simulation rules

`simulate_trade_from_signal(df, trade, dt_col, state, lookback_minutes)`:

- Trade schema (`TradePlan`):
  - `status`: `"Trade"` or `"No trade"`
  - `type`: `"long" | "short" | "none"`
  - `entry_at`, `target`, `stoploss`: numbers (positive; 0 if `No trade`)
- Entry is **limit-style**: if `entry_at` in `[low, high]` of a 1-min bar → entry fills at `entry_at`.
- Exit resolution when **both target & stoploss could be hit in same bar**: use **tiebreaker** (engine uses “stoploss_first”).
- P&L (`pnl_pct`): signed percentage vs entry (`long` positive if `exit > entry`; `short` inverted).
- **Flip/No-trade** handling in engine:
  - If **open_position** and LLM plan flips or says **No trade** at the tick → **force flatten** at minutes **close** (market price) and log memory.

---

## LLM JSON schemas

All Pydantic models enforce `extra="forbid"` so the model can’t invent fields.
The client (`app/llm.py`) **sanitizes** the schema name and **forces** `additionalProperties: false` at the **root and nested** objects, satisfying strict servers.

### SummaryMorning (example)
```python
class SummaryMorning(BaseModel):
    major_concern_nifty50: str
    trade_reasoning_nifty50: str
    trade_strategy_nifty50: str
    major_concern_banknifty: str
    trade_reasoning_banknifty: str
    trade_strategy_banknifty: str
    model_config = {"extra": "forbid"}
```
### TradePlan
```python
class TradePlan(BaseModel):
    status: Literal["No trade", "Trade"]
    brief_reason: str
    type: Literal["long", "short", "none"]
    entry_at: float
    target: float
    stoploss: float
    model_config = {"extra": "forbid"}
```
### DecisionOutput
```python
class SummaryBankNifty(BaseModel):
    major_concern: str
    sentiment: Literal["bullish", "bearish"]
    reasoning: str
    trade_strategy: str
    news_summary: str
    model_config = {"extra": "forbid"}

class DecisionOutput(BaseModel):
    summary_banknifty: SummaryBankNifty
    trade: TradePlan
    model_config = {"extra": "forbid"}
```

---

## Outputs

**Out dir (`--out-dir`)**:
- `stats_log.xlsx` – time series of state snapshots/closing stats (one row when state changes; final close row).
- `trade_log.xlsx` – model trade plans over time.
- `expert_log.xlsx` – morning summaries (one per day).
- `summary_log.xlsx` – per-hour summaries.

**Checkpoint dir (`--ckpt-dir` or `<out-dir>/checkpoint`)**:
- Parquets for each log, plus `checkpoint.json` (state & last timestamp).