Metadata-Version: 2.4
Name: brain-login-and-sim
Version: 0.1.0
Summary: WorldQuant BRAIN client — login (biometric Scan + auto re-login), simulation, batch run, and auto-keeping promising alphas
Author-email: Tanakrit <9tanakrit.work@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/Tanakrit/brain-login-and-sim
Project-URL: Issues, https://github.com/Tanakrit/brain-login-and-sim/issues
Keywords: worldquant,brain,alpha,quant,simulation,trading
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Topic :: Office/Business :: Financial :: Investment
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.28
Requires-Dist: python-dotenv>=0.19
Provides-Extra: dev
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Dynamic: license-file

# brain-login-and-sim

ไคลเอนต์ Python สำหรับ **WorldQuant BRAIN** — login (จัดการ biometric "Scan" + auto re-login),
ส่ง simulation, รัน batch (สูงสุด 3 พร้อมกันตามลิมิต), และคัดเก็บ alpha ที่ผ่านเกณฑ์อัตโนมัติ

## ติดตั้ง

```bash
pip install brain-login-and-sim
```

## ตั้งค่า credentials

สร้างไฟล์ `.env` (ดู `.env.example`):

```
WQ_EMAIL=your-email@example.com
WQ_PASSWORD=your-password
# ส่งลิงก์ Scan เข้า Telegram (ทางเลือก แต่แนะนำ)
TELEGRAM_BOT_TOKEN=123456789:AAH...
TELEGRAM_CHAT_ID=123456789
```

## ใช้งาน

```python
from brain_login_and_sim import BrainClient, Simulator

# login — ถ้าระบบขอ biometric Scan จะส่งลิงก์เข้า Telegram แล้วรอจนสแกนเสร็จ
client = BrainClient(notify_telegram=True).authenticate()
print(client.whoami()["id"])

sim = Simulator(client)

# ยิง 1 ตัว
r = sim.simulate("ts_rank(close, 5)")
print(r["simulation"]["status"], r["alpha"]["is"]["sharpe"])

# รันหลายตัวจากไฟล์ JSONL คัดเก็บตัวที่ผ่านเกณฑ์อัตโนมัติ
sim.simulate_batch("alphas.jsonl", save_promising=True)
```

## ฟีเจอร์หลัก

- **Login + biometric Scan** — ส่งลิงก์สแกนเข้า Telegram/อีเมล แล้ว poll รอจนเสร็จ (ไม่ต้องเฝ้าจอ)
- **Auto re-login** — เมื่อ session หมดอายุ (WorldQuant ให้ ~4 ชม.) เจอ 401 จะ login ใหม่แล้วลองซ้ำให้เอง
- **คัดเก็บ alpha ดี** — เกณฑ์ `|Sharpe| ≥ 1` และ `|Fitness| ≥ 0.9` (ค่าสัมบูรณ์ จับ flip candidate ที่ negate แล้วดี)
- **Batch** — รันสูงสุด 3 ตัวพร้อมกัน (ลิมิต WorldQuant), error รายตัวไม่ล้มทั้ง batch
- **Resume** — จด checkpoint ด้วย hash รันไฟล์เดิมซ้ำจะข้ามตัวที่รันแล้ว รันต่อจากที่ค้าง
- **Input ยืดหยุ่น** — list ของสูตร, ไฟล์ `.txt` (1 สูตร/บรรทัด), หรือ `.jsonl` ที่มี field `expression` (+`hash`)

## รูปแบบ JSONL

```jsonl
{"hash": "f7b3...", "expression": "rank(reverse(ts_delta(anl4_cfo_median, 10)))", "status": "generated"}
```

ระบบดึง `expression` ไปรัน และถ้าผ่านเกณฑ์จะเก็บ `source_hash` ไว้อ้างกลับไฟล์ต้นฉบับใน `promising_alphas.jsonl`

## API ย่อ

`BrainClient`: `authenticate(on_biometric=None)`, `whoami()`, `get(path)`, `post(path)`, `auto_relogin`

`Simulator(client)`: `simulate(...)`, `simulate_batch(...)`, `is_promising(...)`, `save_if_promising(...)`,
`load_promising(path)`, `get_operators()`, `get_data_fields(...)`, `get_alpha(id)`

## ทดสอบ

```bash
python tests/test_full_loop.py   # mock API ไม่ต้องต่อเน็ต/รหัสผ่าน
```

## License

MIT
