38 Commits

Author SHA1 Message Date
SkywalkerJi
3a579bc39d docs: update PR templates to English-only 2026-01-13 12:49:12 +08:00
SkywalkerJi
b10b9ec1a7 docs: convert PR templates to English-only (#1331) 2026-01-12 22:06:17 -06:00
tinkle-community
c1def0e2c2 fix: change GAMMA-RAY risk level from ZERO to LOW 2026-01-13 10:36:27 +08:00
tinkle-community
705aa641b0 fix: backtest module PostgreSQL compatibility and bug fixes
- Fix PostgreSQL placeholder conversion (? to $1, $2...) in all SQL queries
- Fix int4 overflow for timestamp columns (ALTER to BIGINT)
- Fix notional calculation bug in position Close() using proportional entry
- Fix potential panic in DecisionTimestamp with bounds check
- Fix nil pointer dereference in sliceUpTo with defensive checks
- Fix race condition in releaseLock using sync.Once
- Fix UnrealizedPnLPct always 0 in convertPositions
- Improve Sharpe ratio calculation with proper negative return handling
2026-01-09 01:48:02 +08:00
tinkle-community
2f88205231 fix: chart container height using flexbox layout 2026-01-08 15:48:33 +08:00
tinkle-community
e92222950a fix: use completeRegistration for incomplete OTP setup in login flow
- LoginPage: call completeRegistration instead of verifyOTP when qrCodeURL exists
- This ensures otp_verified is set to true for users completing OTP setup
- Backend: reorder maxUsers check to allow existing incomplete users to continue
- Backend: return OTP info when login with incomplete OTP setup
2026-01-07 20:15:27 +08:00
tinkle-community
138943d6fb fix: update xyz dex order routing configuration 2026-01-07 02:31:52 +08:00
tinkle-community
b36ab27b65 feat: add pending orders (SL/TP) display on chart
- Add GetOpenOrders method to Trader interface
- Implement for Binance (legacy + Algo), Bybit, Hyperliquid
- Add stub implementations for OKX, Bitget, Aster, Lighter
- Add /api/open-orders endpoint
- Display price lines for SL (red) and TP (green) orders
- Refresh open orders every 60 seconds (separate from 5s kline refresh)
2026-01-07 00:50:29 +08:00
tinkle-community
5e65ae7077 fix: chart order markers not displaying due to timestamp format mismatch
- Fix milliseconds to seconds conversion in parseCustomTime (AdvancedChart & ChartWithOrders)
- Add GetTraderOrdersFiltered to filter orders at database level by symbol/status
- Increase order limit from 50 to 200 for more historical orders
- Group multiple orders at same candle time and show count (B3, S5, etc.)
- Buy markers shown below bar (green), sell markers above bar (red)
2026-01-06 21:08:42 +08:00
tinkle-community
c0c89d7534 docs: update Railway deploy button with official template URL 2026-01-06 19:07:25 +08:00
tinkle-community
3b2a3f4e76 chore: clean up Railway deployment - remove debug code 2026-01-06 18:58:27 +08:00
tinkle-community
c8458ec79c fix: align PORT defaults to 8080 for Railway 2026-01-06 18:53:27 +08:00
tinkle-community
aee096ab1e debug: test nginx startup and internal health check 2026-01-06 18:48:11 +08:00
tinkle-community
165c0b1b5d debug: add nginx config test and file check 2026-01-06 18:44:24 +08:00
tinkle-community
4c097f7190 fix: use heredoc for nginx config to avoid envsubst issues 2026-01-06 18:41:08 +08:00
tinkle-community
ea763a2471 fix: use port 8081 for backend to avoid conflict with nginx 2026-01-06 18:37:18 +08:00
tinkle-community
6e6bdf1e57 refactor: simplify Railway deployment using existing GHCR images
- Use multi-stage build from existing backend/frontend images
- Remove supervisord, use simple shell script
- Single process model: backend runs in background, nginx foreground
- Auto-generate encryption keys on startup
2026-01-06 18:31:39 +08:00
tinkle-community
f0b4913ad6 debug: add PORT environment variable debugging 2026-01-06 18:19:28 +08:00
tinkle-community
29cd79c626 fix: use Railway PORT env var for nginx 2026-01-06 18:07:11 +08:00
tinkle-community
7db37ade1c fix: auto-generate encryption keys in Railway startup script 2026-01-06 17:59:29 +08:00
tinkle-community
4804cfcb05 feat: add Railway one-click deployment support
- Add Dockerfile.railway for all-in-one container
- Add railway.toml configuration
- Add railway/nginx.conf and supervisord.conf
- Update README with Deploy on Railway button
- Update Chinese README with deployment instructions
2026-01-06 17:32:09 +08:00
tinkle-community
799d8b9c2e feat: migrate timestamps to int64 and security improvements
- Convert all time.Time fields to int64 Unix milliseconds (UTC)
- Add PostgreSQL migration to convert timestamp columns to bigint
- Reduce Binance sync window from 7 days to 24 hours
- Fix dashboard trader name visibility (add nofx-text-main color)
- Add position value column to history table
- Remove hardcoded API keys from test files
2026-01-06 15:56:07 +08:00
tinkle-community
5c4c9cdc99 fix: handle large Binance trade IDs in Go to avoid database CAST limitations 2026-01-06 10:43:21 +08:00
tinkle-community
8b86d4d85c docs: add prerequisites section and reorganize README structure across all languages 2026-01-06 08:16:00 +08:00
tinkle-community
962df5c3ed feat: add strategy description input field 2026-01-05 00:08:51 +08:00
tinkle-community
9f3de6e3c0 fix: resolve hyperliquid order execution approval issue 2026-01-04 22:27:15 +08:00
tinkle-community
5c9e134e99 fix: ensure all timestamps use UTC timezone
- Add NowFunc to GORM config for UTC auto-generated timestamps
- Add .UTC() to all time.UnixMilli() calls in trader files
- Add .UTC() to all time.Now() calls in store and api files
- Fix TypeScript unused imports in frontend
2026-01-04 20:03:56 +08:00
tinkle-community
50923f6a2e feat: add DeepVoidBackground and update UI theme across pages
- Add DeepVoidBackground component with animated gradient effects
- Apply nofx theme classes to StrategyStudioPage, AITradersPage, etc.
- Update styling for consistent dark theme with gold accents
- Add PageNotFound and TraderDashboardPage components
2026-01-04 17:49:59 +08:00
tinkle-community
bdfd8dc0d0 fix: auto-restart trader on config update and add scan interval debug logs
- RemoveTrader now stops running trader before removing from memory
- handleUpdateTrader auto-restarts trader if it was running before update
- Add debug logs to trace scan_interval_minutes through update/save/load flow
2026-01-04 01:27:30 +08:00
tinkle-community
0275e23b7e feat: unify NofxOS data provider and fix language consistency
- Add unified NofxOS API key configuration in IndicatorEditor
- Add language field to StrategyConfig for consistent prompt generation
- Auto-update prompt sections when interface language changes
- Remove scattered URL inputs from CoinSourceEditor and IndicatorEditor
- Create nofxos provider package with formatted data output
- Update kernel engine to use config-based language setting
2026-01-04 00:59:07 +08:00
tinkle-community
13fda47151 refactor: rename decision package to kernel 2026-01-03 14:25:40 +08:00
tinkle-community
d664dcca3d style: update CSS styles 2026-01-03 13:44:44 +08:00
tinkle-community
04141642a5 feat: improve landing page responsiveness and styling 2026-01-03 13:12:25 +08:00
tinkle-community
7f7c4ea2a7 fix: sanitize API error messages to prevent sensitive info exposure 2026-01-03 13:11:15 +08:00
tinkle-community
e07dc0de86 feat: add excluded coins filter for strategy
- Add excluded_coins field to CoinSourceConfig
- Filter excluded coins in GetCandidateCoins function
- Add excluded coins UI in CoinSourceEditor
2026-01-03 01:21:17 +08:00
tinkle-community
cc726adb57 feat: add strategy publish settings and reorder navigation
- Add is_public and config_visible fields to Strategy type
- Add PublishSettingsEditor component for strategy studio
- Enable GORM AutoMigrate to add new columns
- Reorder nav: Market → Config → Dashboard → Strategy → Leaderboard → Arena → Backtest → FAQ
- Rename Live to Leaderboard, Debate Arena to Arena
2026-01-03 00:52:11 +08:00
tinkle-community
7df8197542 fix: convert branch name for docker manifest tags 2026-01-01 23:34:43 +08:00
tinkle-community
60194306e1 feat: add stable release branch support
- Add release/stable branch to CI workflow
- Create docker-compose.stable.yml with :stable tag
- Create install-stable.sh for one-click deployment
- Add stable tag creation in manifest step
2026-01-01 23:27:53 +08:00
125 changed files with 11177 additions and 7251 deletions

View File

@@ -1,16 +1,16 @@
# PR 标题指南
# PR Title Guide
## 📋 概述
## 📋 Overview
我们使用 **Conventional Commits** 格式来保持 PR 标题的一致性,但这是**建议性的**,不会阻止你的 PR 被合并。
We use the **Conventional Commits** format to maintain consistency in PR titles, but this is **recommended**, not mandatory. It will not prevent your PR from being merged.
## ✅ 推荐格式
## ✅ Recommended Format
```
type(scope): description
```
### 示例
### Examples
```
feat(trader): add new trading strategy
@@ -22,63 +22,63 @@ ci(workflow): improve GitHub Actions
---
## 📖 详细说明
## 📖 Detailed Guide
### Type(类型)- 必需
### Type - Required
描述这次变更的类型:
Describes the type of change:
| Type | 说明 | 示例 |
|------|------|------|
| `feat` | 新功能 | `feat(trader): add stop-loss feature` |
| `fix` | Bug 修复 | `fix(api): handle null response` |
| `docs` | 文档变更 | `docs: update installation guide` |
| `style` | 代码格式(不影响代码运行) | `style: format code with prettier` |
| `refactor` | 重构(既不是新功能也不是修复) | `refactor(exchange): simplify connection logic` |
| `perf` | 性能优化 | `perf(ai): optimize prompt processing` |
| `test` | 添加或修改测试 | `test(trader): add unit tests` |
| `chore` | 构建过程或辅助工具的变动 | `chore: update dependencies` |
| `ci` | CI/CD 相关变更 | `ci: add test coverage report` |
| `security` | 安全相关修复 | `security: update vulnerable dependencies` |
| `build` | 构建系统或外部依赖项变更 | `build: upgrade webpack to v5` |
| Type | Description | Example |
|------|-------------|---------|
| `feat` | New feature | `feat(trader): add stop-loss feature` |
| `fix` | Bug fix | `fix(api): handle null response` |
| `docs` | Documentation change | `docs: update installation guide` |
| `style` | Code formatting (no functional change) | `style: format code with prettier` |
| `refactor` | Code refactoring (neither feature nor fix) | `refactor(exchange): simplify connection logic` |
| `perf` | Performance optimization | `perf(ai): optimize prompt processing` |
| `test` | Add or modify tests | `test(trader): add unit tests` |
| `chore` | Build process or auxiliary tool changes | `chore: update dependencies` |
| `ci` | CI/CD related changes | `ci: add test coverage report` |
| `security` | Security fixes | `security: update vulnerable dependencies` |
| `build` | Build system or external dependency changes | `build: upgrade webpack to v5` |
### Scope(范围)- 可选
### Scope - Optional
描述这次变更影响的范围:
Describes the area affected by the change:
| Scope | 说明 |
|-------|------|
| `exchange` | 交易所相关 |
| `trader` | 交易员/交易策略 |
| `ai` | AI 模型相关 |
| `api` | API 接口 |
| `ui` | 用户界面 |
| `frontend` | 前端代码 |
| `backend` | 后端代码 |
| `security` | 安全相关 |
| `deps` | 依赖项 |
| Scope | Description |
|-------|-------------|
| `exchange` | Exchange-related |
| `trader` | Trader/trading strategy |
| `ai` | AI model related |
| `api` | API interface |
| `ui` | User interface |
| `frontend` | Frontend code |
| `backend` | Backend code |
| `security` | Security related |
| `deps` | Dependencies |
| `workflow` | GitHub Actions workflows |
| `github` | GitHub 配置 |
| `github` | GitHub configuration |
| `actions` | GitHub Actions |
| `config` | 配置文件 |
| `docker` | Docker 相关 |
| `build` | 构建相关 |
| `release` | 发布相关 |
| `config` | Configuration files |
| `docker` | Docker related |
| `build` | Build related |
| `release` | Release related |
**注意:** 如果变更影响多个范围,可以省略 scope 或选择最主要的。
**Note:** If the change affects multiple scopes, you can omit the scope or choose the most relevant one.
### Description(描述)- 必需
### Description - Required
- 使用现在时态("add" 而不是 "added"
- 首字母小写
- 结尾不加句号
- 简洁明了地描述变更内容
- Use present tense ("add" not "added")
- Start with lowercase
- No period at the end
- Concisely describe what changed
---
## 🎯 完整示例
## 🎯 Complete Examples
### ✅ 好的 PR 标题
### ✅ Good PR Titles
```
feat(trader): add risk management system
@@ -94,38 +94,38 @@ security(api): fix SQL injection vulnerability
build(docker): optimize Docker image size
```
### ⚠️ 需要改进的标题
### ⚠️ Titles That Need Improvement
| 不好的标题 | 问题 | 改进后 |
|-----------|------|--------|
| `update code` | 太模糊 | `refactor(trader): simplify order execution logic` |
| `Fixed bug` | 首字母大写,不够具体 | `fix(api): handle edge case in login` |
| `Add new feature.` | 有句号,不够具体 | `feat(ui): add dark mode toggle` |
| `changes` | 完全不符合格式 | `chore: update dependencies` |
| `feat: Added new trading algo` | 时态错误 | `feat(trader): add new trading algorithm` |
| Poor Title | Issue | Improved |
|-----------|-------|----------|
| `update code` | Too vague | `refactor(trader): simplify order execution logic` |
| `Fixed bug` | Capitalized, not specific | `fix(api): handle edge case in login` |
| `Add new feature.` | Has period, not specific | `feat(ui): add dark mode toggle` |
| `changes` | Doesn't follow format | `chore: update dependencies` |
| `feat: Added new trading algo` | Wrong tense | `feat(trader): add new trading algorithm` |
---
## 🤖 自动检查行为
## 🤖 Automated Check Behavior
### 当 PR 标题不符合格式时
### When PR Title Doesn't Follow Format
1. **不会阻止合并**
- 检查会标记为"建议"
- PR 仍然可以被审查和合并
1. **Won't block merging**
- Check is marked as "advisory"
- PR can still be reviewed and merged
2. **会收到友好提示** 💬
- 机器人会在 PR 中留言
- 提供格式说明和示例
- 建议如何改进标题
2. **Provides friendly reminder** 💬
- Bot will comment on the PR
- Provides format guidance and examples
- Suggests how to improve the title
3. **可以随时更新** 🔄
- 更新 PR 标题后会重新检查
- 无需关闭和重新打开 PR
3. **Can be updated anytime** 🔄
- Re-checks after updating PR title
- No need to close and reopen PR
### 示例评论
### Example Comment
如果你的 PR 标题是 `update workflow`,你会收到这样的评论:
If your PR title is `update workflow`, you'll receive a comment like this:
```markdown
## ⚠️ PR Title Format Suggestion
@@ -157,11 +157,11 @@ Your PR can still be reviewed and merged.
---
## 🔧 配置详情
## 🔧 Configuration Details
### 支持的 Types
### Supported Types
`.github/workflows/pr-checks.yml` 中配置:
Configured in `.github/workflows/pr-checks.yml`:
```yaml
types: |
@@ -178,7 +178,7 @@ types: |
build
```
### 支持的 Scopes
### Supported Scopes
```yaml
scopes: |
@@ -200,38 +200,38 @@ scopes: |
release
```
### 添加新的 Scope
### Adding New Scopes
如果你需要添加新的 scope,请:
If you need to add a new scope:
1. `.github/workflows/pr-checks.yml``scopes` 部分添加
2. `.github/workflows/pr-checks-run.yml` 更新正则表达式(可选)
3. 更新本文档
1. Add it to the `scopes` section in `.github/workflows/pr-checks.yml`
2. Update the regex in `.github/workflows/pr-checks-run.yml` (optional)
3. Update this documentation
---
## 📚 为什么使用 Conventional Commits
## 📚 Why Use Conventional Commits?
### 优点
### Benefits
1. **自动化 Changelog** 📝
- 可以自动生成版本更新日志
- 清晰地分类各种变更
1. **Automated Changelog** 📝
- Automatically generate version changelogs
- Clearly categorize different types of changes
2. **语义化版本** 🔢
- `feat` → MINOR 版本(1.1.0
- `fix` → PATCH 版本(1.0.1
- `BREAKING CHANGE` → MAJOR 版本(2.0.0
2. **Semantic Versioning** 🔢
- `feat` → MINOR version (1.1.0)
- `fix` → PATCH version (1.0.1)
- `BREAKING CHANGE` → MAJOR version (2.0.0)
3. **更好的可读性** 👀
- 一眼看出 PR 的目的
- 更容易浏览 Git 历史
3. **Better Readability** 👀
- Understand PR purpose at a glance
- Easier to browse Git history
4. **团队协作** 🤝
- 统一的提交风格
- 降低沟通成本
4. **Team Collaboration** 🤝
- Unified commit style
- Reduces communication overhead
### 示例:自动生成的 Changelog
### Example: Auto-generated Changelog
```markdown
## v1.2.0 (2025-11-02)
@@ -250,9 +250,9 @@ scopes: |
---
## 🎓 学习资源
## 🎓 Learning Resources
- **Conventional Commits 官网:** https://www.conventionalcommits.org/
- **Conventional Commits:** https://www.conventionalcommits.org/
- **Angular Commit Guidelines:** https://github.com/angular/angular/blob/main/CONTRIBUTING.md#commit
- **Semantic Versioning:** https://semver.org/
@@ -260,33 +260,33 @@ scopes: |
## ❓ FAQ
### Q: 我必须遵循这个格式吗?
### Q: Must I follow this format?
**A:** 不必须。这是建议性的,不会阻止你的 PR 被合并。但遵循格式可以提高项目的可维护性。
**A:** No. This is recommended but not mandatory. It won't block your PR from being merged. However, following the format improves project maintainability.
### Q: 如果我忘记了怎么办?
### Q: What if I forget?
**A:** 机器人会在 PR 中提醒你,你可以随时更新标题。
**A:** The bot will remind you in the PR comments. You can update the title anytime.
### Q: 我可以在一个 PR 中做多种类型的变更吗?
### Q: Can I make multiple types of changes in one PR?
**A:** 可以,但建议:
- 选择最主要的类型
- 或者考虑拆分成多个 PR更易于审查
**A:** Yes, but it's recommended to:
- Choose the most significant type
- Or consider splitting into multiple PRs (easier to review)
### Q: Scope 可以省略吗?
### Q: Can I omit the scope?
**A:** 可以。`requireScope: false` 表示 scope 是可选的。
**A:** Yes. `requireScope: false` means scope is optional.
示例:`docs: update README` (没有 scope 也可以)
Example: `docs: update README` (no scope is fine)
### Q: 我想添加新的 type scope,怎么做?
### Q: How do I add a new type or scope?
**A:** 提一个 PR 修改 `.github/workflows/pr-checks.yml`,并在本文档中说明新增项的用途。
**A:** Submit a PR to modify `.github/workflows/pr-checks.yml` and document the purpose of the new item in this guide.
### Q: Breaking Changes 怎么表示?
### Q: How do I indicate Breaking Changes?
**A:** 在描述中添加 `BREAKING CHANGE:` 或在 type 后加 `!`
**A:** Add `BREAKING CHANGE:` in the description or add `!` after the type:
```
feat!: remove deprecated API
@@ -297,9 +297,9 @@ BREAKING CHANGE: The old /auth endpoint is removed
---
## 📊 统计
## 📊 Statistics
想看项目的 commit 类型分布?运行:
Want to see the commit type distribution in your project? Run:
```bash
git log --oneline --no-merges | \
@@ -309,14 +309,14 @@ git log --oneline --no-merges | \
---
## ✅ 快速检查清单
## ✅ Quick Checklist
在提交 PR 前,检查你的标题是否:
Before submitting a PR, check if your title:
- [ ] 包含有效的 typefeat, fix, docs 等)
- [ ] 使用小写字母开头
- [ ] 使用现在时态("add" 而不是 "added"
- [ ] 简洁明了(最好在 50 字符内)
- [ ] 准确描述了变更内容
- [ ] Contains a valid type (feat, fix, docs, etc.)
- [ ] Starts with lowercase
- [ ] Uses present tense ("add" not "added")
- [ ] Is concise (preferably under 50 characters)
- [ ] Accurately describes the change
**记住:** 这些都是建议,不是强制要求!
**Remember:** These are recommendations, not requirements!

View File

@@ -1,104 +1,100 @@
# Pull Request | PR 提交
# Pull Request
> **📋 选择专用模板 | Choose Specialized Template**
> **📋 Choose Specialized Template**
>
> 我们现在提供了针对不同类型PR的专用模板帮助你更快速地填写PR信息
> We now offer specialized templates for different types of PRs to help you fill out the information faster:
>
> - 🔧 **[Backend PR Template](./PULL_REQUEST_TEMPLATE/backend.md)** | 后端PR模板 - For Go/API/Trading changes
> - 🎨 **[Frontend PR Template](./PULL_REQUEST_TEMPLATE/frontend.md)** | 前端PR模板 - For UI/UX changes
> - 📝 **[Documentation PR Template](./PULL_REQUEST_TEMPLATE/docs.md)** | 文档PR模板 - For documentation updates
> - 📦 **[General PR Template](./PULL_REQUEST_TEMPLATE/general.md)** | 通用PR模板 - For mixed or other changes
> - 🔧 **[Backend PR Template](./PULL_REQUEST_TEMPLATE/backend.md)** - For Go/API/Trading changes
> - 🎨 **[Frontend PR Template](./PULL_REQUEST_TEMPLATE/frontend.md)** - For UI/UX changes
> - 📝 **[Documentation PR Template](./PULL_REQUEST_TEMPLATE/docs.md)** - For documentation updates
> - 📦 **[General PR Template](./PULL_REQUEST_TEMPLATE/general.md)** - For mixed or other changes
>
> **如何使用?| How to use?**
> - 创建PR时在URL中添加 `?template=backend.md` 或其他模板名称
> **How to use?**
> - When creating a PR, add `?template=backend.md` or other template name to the URL
> - 或者直接复制粘贴对应模板的内容
> - Or simply copy and paste the content from the corresponding template
---
> **💡 提示 Tip:** 推荐 PR 标题格式 `type(scope): description`
> 例如: `feat(trader): add new strategy` | `fix(api): resolve auth issue`
> **💡 Tip:** Recommended PR title format `type(scope): description`
> Example: `feat(trader): add new strategy` | `fix(api): resolve auth issue`
---
## 📝 Description | 描述
**English:** **中文:**
## 📝 Description
<!-- Describe your changes in detail -->
---
## 🎯 Type of Change | 变更类型
## 🎯 Type of Change
- [ ] 🐛 Bug fix | 修复 Bug
- [ ] ✨ New feature | 新功能
- [ ] 💥 Breaking change | 破坏性变更
- [ ] 📝 Documentation update | 文档更新
- [ ] 🎨 Code style update | 代码样式更新
- [ ] ♻️ Refactoring | 重构
- [ ] ⚡ Performance improvement | 性能优化
- [ ] ✅ Test update | 测试更新
- [ ] 🔧 Build/config change | 构建/配置变更
- [ ] 🔒 Security fix | 安全修复
- [ ] 🐛 Bug fix
- [ ] ✨ New feature
- [ ] 💥 Breaking change
- [ ] 📝 Documentation update
- [ ] 🎨 Code style update
- [ ] ♻️ Refactoring
- [ ] ⚡ Performance improvement
- [ ] ✅ Test update
- [ ] 🔧 Build/config change
- [ ] 🔒 Security fix
---
## 🔗 Related Issues | 相关 Issue
## 🔗 Related Issues
- Closes # | 关闭 #
- Related to # | 相关 #
- Closes #
- Related to #
---
## 📋 Changes Made | 具体变更
## 📋 Changes Made
**English:** **中文:**
<!-- List the specific changes made -->
-
-
---
## 🧪 Testing | 测试
## 🧪 Testing
- [ ] Tested locally | 本地测试通过
- [ ] Tests pass | 测试通过
- [ ] Verified no existing functionality broke | 确认没有破坏现有功能
- [ ] Tested locally
- [ ] Tests pass
- [ ] Verified no existing functionality broke
---
## ✅ Checklist | 检查清单
## ✅ Checklist
### Code Quality | 代码质量
- [ ] Code follows project style | 代码遵循项目风格
- [ ] Self-review completed | 已完成代码自查
- [ ] Comments added for complex logic | 已添加必要注释
### Code Quality
- [ ] Code follows project style
- [ ] Self-review completed
- [ ] Comments added for complex logic
### Documentation | 文档
- [ ] Updated relevant documentation | 已更新相关文档
### Documentation
- [ ] Updated relevant documentation
### Git
- [ ] Commits follow conventional format | 提交遵循 Conventional Commits 格式
- [ ] Rebased on latest `dev` branch | 已 rebase 到最新 `dev` 分支
- [ ] No merge conflicts | 无合并冲突
- [ ] Commits follow conventional format
- [ ] Rebased on latest `dev` branch
- [ ] No merge conflicts
---
## 📚 Additional Notes | 补充说明
## 📚 Additional Notes
**English:** **中文:**
<!-- Any additional information or context -->
---
**By submitting this PR, I confirm | 提交此 PR我确认**
**By submitting this PR, I confirm:**
- [ ] I have read the [Contributing Guidelines](../CONTRIBUTING.md) | 已阅读贡献指南
- [ ] I agree to the [Code of Conduct](../CODE_OF_CONDUCT.md) | 同意行为准则
- [ ] My contribution is licensed under AGPL-3.0 | 贡献遵循 AGPL-3.0 许可证
- [ ] I have read the [Contributing Guidelines](../CONTRIBUTING.md)
- [ ] I agree to the [Code of Conduct](../CODE_OF_CONDUCT.md)
- [ ] My contribution is licensed under AGPL-3.0
---
🌟 **Thank you for your contribution! | 感谢你的贡献!**
🌟 **Thank you for your contribution!**

View File

@@ -1,213 +1,177 @@
# PR Templates | PR 模板
# PR Templates
## 📋 模板概述 | Template Overview
## 📋 Template Overview
我们提供了4种针对不同类型PR的专用模板帮助贡献者快速填写PR信息
We offer 4 specialized templates for different types of PRs to help contributors quickly fill out PR information:
### 1. 🔧 Backend Template | 后端模板
**文件:** `backend.md`
### 1. 🔧 Backend Template
**File:** `backend.md`
**适用于 | Use for:**
- Go代码变更 | Go code changes
- API端点开发 | API endpoint development
- 交易逻辑实现 | Trading logic implementation
- 后端性能优化 | Backend performance optimization
- 数据库相关改动 | Database-related changes
**Use for:**
- Go code changes
- API endpoint development
- Trading logic implementation
- Backend performance optimization
- Database-related changes
**包含 | Includes:**
- Go测试环境配置 | Go test environment
- 安全考虑检查 | Security considerations
- 性能影响评估 | Performance impact assessment
- `go fmt``go build` 检查 | `go fmt` and `go build` checks
**Includes:**
- Go test environment
- Security considerations
- Performance impact assessment
- `go fmt` and `go build` checks
### 2. 🎨 Frontend Template | 前端模板
**文件:** `frontend.md`
### 2. 🎨 Frontend Template
**File:** `frontend.md`
**适用于 | Use for:**
- UI/UX变更 | UI/UX changes
- React/Vue组件开发 | React/Vue component development
- 前端样式更新 | Frontend styling updates
- 浏览器兼容性修复 | Browser compatibility fixes
- 前端性能优化 | Frontend performance optimization
**Use for:**
- UI/UX changes
- React/Vue component development
- Frontend styling updates
- Browser compatibility fixes
- Frontend performance optimization
**包含 | Includes:**
- 截图/演示要求 | Screenshots/demo requirements
- 浏览器测试清单 | Browser testing checklist
- 国际化检查 | Internationalization checks
- 响应式设计验证 | Responsive design verification
- `npm run lint` `npm run build` 检查 | Linting and build checks
**Includes:**
- Screenshots/demo requirements
- Browser testing checklist
- Internationalization checks
- Responsive design verification
- `npm run lint` and `npm run build` checks
### 3. 📝 Documentation Template | 文档模板
**文件:** `docs.md`
### 3. 📝 Documentation Template
**File:** `docs.md`
**适用于 | Use for:**
- README更新 | README updates
- API文档编写 | API documentation
- 教程和指南 | Tutorials and guides
- 代码注释改进 | Code comment improvements
- 翻译工作 | Translation work
**Use for:**
- README updates
- API documentation
- Tutorials and guides
- Code comment improvements
- Translation work
**包含 | Includes:**
- 文档类型分类 | Documentation type classification
- 内容质量检查 | Content quality checks
- 双语要求(中英文)| Bilingual requirements (EN/CN)
- 链接有效性验证 | Link validity verification
**Includes:**
- Documentation type classification
- Content quality checks
- Bilingual requirements (EN/CN)
- Link validity verification
### 4. 📦 General Template | 通用模板
**文件:** `general.md`
### 4. 📦 General Template
**File:** `general.md`
**适用于 | Use for:**
- 混合类型变更 | Mixed-type changes
- 跨多个领域的PR | Cross-domain PRs
- 构建配置变更 | Build configuration changes
- 依赖更新 | Dependency updates
- 不确定使用哪个模板时 | When unsure which template to use
**Use for:**
- Mixed-type changes
- Cross-domain PRs
- Build configuration changes
- Dependency updates
- When unsure which template to use
## 🤖 自动模板建议 | Automatic Template Suggestion
## 🤖 Automatic Template Suggestion
我们的GitHub Action会自动分析你的PR并建议最合适的模板
Our GitHub Action automatically analyzes your PR and suggests the most suitable template:
### 工作原理 | How it works:
### How it works:
1. **文件分析 | File Analysis**
- 检测PR中所有变更的文件类型
1. **File Analysis**
- Detects all changed file types in the PR
2. **智能判断 | Smart Detection**
- 如果 >50% 是 `.go` 文件 → 建议**后端模板**
2. **Smart Detection**
- If >50% are `.go` files → Suggests **Backend template**
- 如果 >50% 是 `.js/.ts/.tsx/.vue` 文件 → 建议**前端模板**
- If >50% are `.js/.ts/.tsx/.vue` files → Suggests **Frontend template**
- 如果 >70% 是 `.md` 文件 → 建议**文档模板**
- If >70% are `.md` files → Suggests **Documentation template**
3. **自动评论 | Auto-comment**
- 如果检测到你使用了默认模板,但应该用专用模板
3. **Auto-comment**
- If it detects you're using the default template but should use a specialized one
- 会自动添加友好的评论建议
- It will automatically add a friendly comment suggestion
4. **自动标签 | Auto-labeling**
- 自动添加对应的标签:`backend``frontend``documentation`
4. **Auto-labeling**
- Automatically adds corresponding labels: `backend`, `frontend`, `documentation`
## 📖 使用方法 | How to Use
## 📖 How to Use
### 方法1: URL参数推荐 | Method 1: URL Parameter (Recommended)
### Method 1: URL Parameter (Recommended)
创建PR时在URL末尾添加模板参数
When creating a PR, add the template parameter to the URL:
```
https://github.com/YOUR_ORG/nofx/compare/dev...YOUR_BRANCH?template=backend.md
```
替换 `backend.md` 为:
Replace `backend.md` with:
- `backend.md` - 后端模板 | Backend template
- `frontend.md` - 前端模板 | Frontend template
- `docs.md` - 文档模板 | Documentation template
- `general.md` - 通用模板 | General template
- `backend.md` - Backend template
- `frontend.md` - Frontend template
- `docs.md` - Documentation template
- `general.md` - General template
### 方法2: 手动选择 | Method 2: Manual Selection
### Method 2: Manual Selection
1. 创建PR时默认模板会显示
When creating a PR, the default template will be shown
1. When creating a PR, the default template will be shown
2. 根据顶部的指引链接,点击查看对应的模板
Follow the guidance links at the top to view the corresponding template
2. Follow the guidance links at the top to view the corresponding template
3. 复制模板内容到PR描述中
Copy the template content into the PR description
3. Copy the template content into the PR description
### 方法3: 跟随自动建议 | Method 3: Follow Auto-suggestion
### Method 3: Follow Auto-suggestion
1. 使用任何模板创建PR
Create a PR with any template
1. Create a PR with any template
2. GitHub Action会自动分析并评论建议
GitHub Action will automatically analyze and comment with a suggestion
2. GitHub Action will automatically analyze and comment with a suggestion
3. 根据建议更新PR描述
Update the PR description based on the suggestion
3. Update the PR description based on the suggestion
## 🎯 最佳实践 | Best Practices
## 🎯 Best Practices
1. **提前选择 | Choose in Advance**
- 在创建PR前确定变更类型
1. **Choose in Advance**
- Determine the change type before creating the PR
2. **完整填写 | Complete Filling**
- 不要跳过必填项(标记为 required
2. **Complete Filling**
- Don't skip required items
3. **保持简洁 | Keep it Concise**
- 描述清晰但简洁
3. **Keep it Concise**
- Keep descriptions clear but concise
4. **添加截图 | Add Screenshots**
- 对于UI变更务必添加截图
4. **Add Screenshots**
- For UI changes, always add screenshots
5. **测试证明 | Test Evidence**
- 提供测试通过的证据
5. **Test Evidence**
- Provide evidence that tests pass
## 🔧 自定义 | Customization
## 🔧 Customization
如果需要修改模板或自动检测逻辑:
If you need to modify templates or auto-detection logic:
1. **修改模板** | **Modify Templates**
- 编辑 `.github/PULL_REQUEST_TEMPLATE/*.md` 文件
1. **Modify Templates**
- Edit `.github/PULL_REQUEST_TEMPLATE/*.md` files
2. **调整检测阈值** | **Adjust Detection Threshold**
- 编辑 `.github/workflows/pr-template-suggester.yml`
2. **Adjust Detection Threshold**
- Edit `.github/workflows/pr-template-suggester.yml`
- 修改文件类型占比阈值当前50%后端50%前端70%文档)
- Modify file type percentage thresholds (current: 50% backend, 50% frontend, 70% docs)
3. **添加新模板** | **Add New Template**
-`PULL_REQUEST_TEMPLATE/` 目录创建新的 `.md` 文件
3. **Add New Template**
- Create a new `.md` file in the `PULL_REQUEST_TEMPLATE/` directory
- 更新工作流以支持新的文件类型检测
- Update the workflow to support new file type detection
## ❓ FAQ
**Q: 我的PR既有前端又有后端代码用哪个模板**
**Q: My PR has both frontend and backend code, which template should I use?**
A: 使用**通用模板**`general.md`),或选择主要变更类型的模板。
A: Use the **General template** (`general.md`), or choose the template for the primary change type.
---
**Q: 自动建议的模板不合适怎么办?**
**Q: What if the automatically suggested template is not suitable?**
A: 你可以忽略建议,继续使用当前模板。自动建议仅供参考。
A: You can ignore the suggestion and continue using the current template. Auto-suggestions are for reference only.
---
**Q: 可以不使用任何模板吗?**
**Q: Can I not use any template?**
A: 不推荐。模板帮助确保PR包含必要信息加快审查速度。
A: Not recommended. Templates help ensure PRs contain necessary information and speed up reviews.
---
**Q: 如何禁用自动模板建议?**
**Q: How to disable automatic template suggestions?**
A: 删除或禁用 `.github/workflows/pr-template-suggester.yml` 文件。
A: Delete or disable the `.github/workflows/pr-template-suggester.yml` file.
---
🌟 **感谢使用我们的PR模板系统| Thank you for using our PR template system!**
🌟 **Thank you for using our PR template system!**

View File

@@ -1,121 +1,116 @@
# Pull Request - Backend | 后端 PR
# Pull Request - Backend
> **💡 提示 Tip:** 推荐 PR 标题格式 `type(scope): description`
> 例如: `feat(trader): add new strategy` | `fix(api): resolve auth issue`
> **💡 Tip:** Recommended PR title format `type(scope): description`
> Example: `feat(trader): add new strategy` | `fix(api): resolve auth issue`
---
## 📝 Description | 描述
**English:** **中文:**
## 📝 Description
---
## 🎯 Type of Change | 变更类型
## 🎯 Type of Change
- [ ] 🐛 Bug fix | 修复 Bug
- [ ] ✨ New feature | 新功能
- [ ] 💥 Breaking change | 破坏性变更
- [ ] ♻️ Refactoring | 重构
- [ ] ⚡ Performance improvement | 性能优化
- [ ] 🔒 Security fix | 安全修复
- [ ] 🔧 Build/config change | 构建/配置变更
- [ ] 🐛 Bug fix
- [ ] ✨ New feature
- [ ] 💥 Breaking change
- [ ] ♻️ Refactoring
- [ ] ⚡ Performance improvement
- [ ] 🔒 Security fix
- [ ] 🔧 Build/config change
---
## 🔗 Related Issues | 相关 Issue
## 🔗 Related Issues
- Closes # | 关闭 #
- Related to # | 相关 #
- Closes #
- Related to #
---
## 📋 Changes Made | 具体变更
## 📋 Changes Made
**English:** **中文:**
-
-
---
## 🧪 Testing | 测试
## 🧪 Testing
### Test Environment | 测试环境
- **OS | 操作系统:**
- **Go Version | Go 版本:**
- **Exchange | 交易所:** [if applicable | 如适用]
### Test Environment
- **OS:**
- **Go Version:**
- **Exchange:** [if applicable]
### Manual Testing | 手动测试
- [ ] Tested locally | 本地测试通过
- [ ] Tested on testnet | 测试网测试通过(交易所集成相关)
- [ ] Unit tests pass | 单元测试通过
- [ ] Verified no existing functionality broke | 确认没有破坏现有功能
### Manual Testing
- [ ] Tested locally
- [ ] Tested on testnet (for exchange integration)
- [ ] Unit tests pass
- [ ] Verified no existing functionality broke
### Test Results | 测试结果
### Test Results
```
Test output here | 测试输出
Test output here
```
---
## 🔒 Security Considerations | 安全考虑
## 🔒 Security Considerations
- [ ] No API keys or secrets hardcoded | 没有硬编码 API 密钥
- [ ] User inputs properly validated | 用户输入已正确验证
- [ ] No SQL injection vulnerabilities | 无 SQL 注入漏洞
- [ ] Authentication/authorization properly handled | 认证/授权正确处理
- [ ] Sensitive data is encrypted | 敏感数据已加密
- [ ] N/A (not security-related) | 不适用
- [ ] No API keys or secrets hardcoded
- [ ] User inputs properly validated
- [ ] No SQL injection vulnerabilities
- [ ] Authentication/authorization properly handled
- [ ] Sensitive data is encrypted
- [ ] N/A (not security-related)
---
## ⚡ Performance Impact | 性能影响
## ⚡ Performance Impact
- [ ] No significant performance impact | 无显著性能影响
- [ ] Performance improved | 性能提升
- [ ] Performance may be impacted (explain below) | 性能可能受影响
- [ ] No significant performance impact
- [ ] Performance improved
- [ ] Performance may be impacted (explain below)
**If impacted, explain | 如果受影响,请说明:**
**If impacted, explain:**
---
## ✅ Checklist | 检查清单
## ✅ Checklist
### Code Quality | 代码质量
- [ ] Code follows project style | 代码遵循项目风格
- [ ] Self-review completed | 已完成代码自查
- [ ] Comments added for complex logic | 已添加必要注释
- [ ] Code compiles successfully | 代码编译成功 (`go build`)
- [ ] Ran `go fmt` | 已运行 `go fmt`
### Code Quality
- [ ] Code follows project style
- [ ] Self-review completed
- [ ] Comments added for complex logic
- [ ] Code compiles successfully (`go build`)
- [ ] Ran `go fmt`
### Documentation | 文档
- [ ] Updated relevant documentation | 已更新相关文档
- [ ] Added inline comments where necessary | 已添加必要的代码注释
- [ ] Updated API documentation (if applicable) | 已更新 API 文档
### Documentation
- [ ] Updated relevant documentation
- [ ] Added inline comments where necessary
- [ ] Updated API documentation (if applicable)
### Git
- [ ] Commits follow conventional format | 提交遵循 Conventional Commits 格式
- [ ] Rebased on latest `dev` branch | 已 rebase 到最新 `dev` 分支
- [ ] No merge conflicts | 无合并冲突
- [ ] Commits follow conventional format
- [ ] Rebased on latest `dev` branch
- [ ] No merge conflicts
---
## 📚 Additional Notes | 补充说明
**English:** **中文:**
## 📚 Additional Notes
---
**By submitting this PR, I confirm | 提交此 PR我确认**
**By submitting this PR, I confirm:**
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md) | 已阅读贡献指南
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md) | 同意行为准则
- [ ] My contribution is licensed under AGPL-3.0 | 贡献遵循 AGPL-3.0 许可证
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md)
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md)
- [ ] My contribution is licensed under AGPL-3.0
---
🌟 **Thank you for your contribution! | 感谢你的贡献!**
🌟 **Thank you for your contribution!**

View File

@@ -1,97 +1,91 @@
# Pull Request - Documentation | 文档 PR
# Pull Request - Documentation
> **💡 提示 Tip:** 推荐 PR 标题格式 `docs(scope): description`
> 例如: `docs(api): update trading endpoints` | `docs(readme): add setup guide`
> **💡 Tip:** Recommended PR title format `docs(scope): description`
> Example: `docs(api): update trading endpoints` | `docs(readme): add setup guide`
---
## 📝 Description | 描述
**English:** **中文:**
## 📝 Description
---
## 📚 Type of Documentation | 文档类型
## 📚 Type of Documentation
- [ ] 📖 README update | README 更新
- [ ] 📋 API documentation | API 文档
- [ ] 🎓 Tutorial/Guide | 教程/指南
- [ ] 📝 Code comments | 代码注释
- [ ] 🔧 Configuration docs | 配置文档
- [ ] 🐛 Fix typo/error | 修复拼写/错误
- [ ] 🌍 Translation | 翻译
- [ ] 📖 README update
- [ ] 📋 API documentation
- [ ] 🎓 Tutorial/Guide
- [ ] 📝 Code comments
- [ ] 🔧 Configuration docs
- [ ] 🐛 Fix typo/error
- [ ] 🌍 Translation
---
## 🔗 Related Issues | 相关 Issue
## 🔗 Related Issues
- Closes # | 关闭 #
- Related to # | 相关 #
- Closes #
- Related to #
---
## 📋 Changes Made | 具体变更
## 📋 Changes Made
**English:** **中文:**
-
-
---
## 📸 Screenshots (if applicable) | 截图(如适用)
## 📸 Screenshots (if applicable)
<!-- For documentation with images, diagrams, or UI examples -->
<!-- 用于包含图片、图表或 UI 示例的文档 -->
---
## 🌐 Internationalization | 国际化
## 🌐 Internationalization
- [ ] English version complete | 英文版本完整
- [ ] Chinese version complete | 中文版本完整
- [ ] Both versions are consistent | 两个版本内容一致
- [ ] N/A (only one language needed) | 不适用(只需要一种语言)
- [ ] English version complete
- [ ] Chinese version complete
- [ ] Both versions are consistent
- [ ] N/A (only one language needed)
---
## ✅ Checklist | 检查清单
## ✅ Checklist
### Content Quality | 内容质量
- [ ] Information is accurate and up-to-date | 信息准确且最新
- [ ] Language is clear and concise | 语言清晰简洁
- [ ] No spelling or grammar errors | 无拼写或语法错误
- [ ] Links are valid and working | 链接有效且可用
- [ ] Code examples are tested and working | 代码示例已测试且可用
- [ ] Formatting is consistent | 格式一致
### Content Quality
- [ ] Information is accurate and up-to-date
- [ ] Language is clear and concise
- [ ] No spelling or grammar errors
- [ ] Links are valid and working
- [ ] Code examples are tested and working
- [ ] Formatting is consistent
### Documentation Standards | 文档标准
- [ ] Follows project documentation style | 遵循项目文档风格
- [ ] Includes necessary examples | 包含必要的示例
- [ ] Technical terms are explained | 技术术语已解释
- [ ] Self-review completed | 已完成自查
### Documentation Standards
- [ ] Follows project documentation style
- [ ] Includes necessary examples
- [ ] Technical terms are explained
- [ ] Self-review completed
### Git
- [ ] Commits follow conventional format | 提交遵循 Conventional Commits 格式
- [ ] Rebased on latest `dev` branch | 已 rebase 到最新 `dev` 分支
- [ ] No merge conflicts | 无合并冲突
- [ ] Commits follow conventional format
- [ ] Rebased on latest `dev` branch
- [ ] No merge conflicts
---
## 📚 Additional Notes | 补充说明
**English:** **中文:**
## 📚 Additional Notes
---
**By submitting this PR, I confirm | 提交此 PR我确认**
**By submitting this PR, I confirm:**
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md) | 已阅读贡献指南
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md) | 同意行为准则
- [ ] My contribution is licensed under AGPL-3.0 | 贡献遵循 AGPL-3.0 许可证
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md)
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md)
- [ ] My contribution is licensed under AGPL-3.0
---
🌟 **Thank you for your contribution! | 感谢你的贡献!**
🌟 **Thank you for your contribution!**

View File

@@ -1,119 +1,113 @@
# Pull Request - Frontend | 前端 PR
# Pull Request - Frontend
> **💡 提示 Tip:** 推荐 PR 标题格式 `type(scope): description`
> 例如: `feat(ui): add dark mode toggle` | `fix(form): resolve validation bug`
> **💡 Tip:** Recommended PR title format `type(scope): description`
> Example: `feat(ui): add dark mode toggle` | `fix(form): resolve validation bug`
---
## 📝 Description | 描述
**English:** **中文:**
## 📝 Description
---
## 🎯 Type of Change | 变更类型
## 🎯 Type of Change
- [ ] 🐛 Bug fix | 修复 Bug
- [ ] ✨ New feature | 新功能
- [ ] 💥 Breaking change | 破坏性变更
- [ ] 🎨 Code style update | 代码样式更新
- [ ] ♻️ Refactoring | 重构
- [ ] ⚡ Performance improvement | 性能优化
- [ ] 🐛 Bug fix
- [ ] ✨ New feature
- [ ] 💥 Breaking change
- [ ] 🎨 Code style update
- [ ] ♻️ Refactoring
- [ ] ⚡ Performance improvement
---
## 🔗 Related Issues | 相关 Issue
## 🔗 Related Issues
- Closes # | 关闭 #
- Related to # | 相关 #
- Closes #
- Related to #
---
## 📋 Changes Made | 具体变更
## 📋 Changes Made
**English:** **中文:**
-
-
---
## 📸 Screenshots / Demo | 截图/演示
## 📸 Screenshots / Demo
<!-- For UI changes, include before/after screenshots or video demo -->
<!-- 对于 UI 变更,请包含变更前后的截图或视频演示 -->
**Before | 变更前:**
**Before:**
**After | 变更后:**
**After:**
---
## 🧪 Testing | 测试
## 🧪 Testing
### Test Environment | 测试环境
- **OS | 操作系统:**
- **Node Version | Node 版本:**
- **Browser(s) | 浏览器:**
### Test Environment
- **OS:**
- **Node Version:**
- **Browser(s):**
### Manual Testing | 手动测试
- [ ] Tested in development mode | 开发模式测试通过
- [ ] Tested production build | 生产构建测试通过
- [ ] Tested on multiple browsers | 多浏览器测试通过
- [ ] Tested responsive design | 响应式设计测试通过
- [ ] Verified no existing functionality broke | 确认没有破坏现有功能
### Manual Testing
- [ ] Tested in development mode
- [ ] Tested production build
- [ ] Tested on multiple browsers
- [ ] Tested responsive design
- [ ] Verified no existing functionality broke
---
## 🌐 Internationalization | 国际化
## 🌐 Internationalization
- [ ] All user-facing text supports i18n | 所有面向用户的文本支持国际化
- [ ] Both English and Chinese versions provided | 提供了中英文版本
- [ ] N/A | 不适用
- [ ] All user-facing text supports i18n
- [ ] Both English and Chinese versions provided
- [ ] N/A
---
## ✅ Checklist | 检查清单
## ✅ Checklist
### Code Quality | 代码质量
- [ ] Code follows project style | 代码遵循项目风格
- [ ] Self-review completed | 已完成代码自查
- [ ] Comments added for complex logic | 已添加必要注释
- [ ] Code builds successfully | 代码构建成功 (`npm run build`)
- [ ] Ran `npm run lint` | 已运行 `npm run lint`
- [ ] No console errors or warnings | 无控制台错误或警告
### Code Quality
- [ ] Code follows project style
- [ ] Self-review completed
- [ ] Comments added for complex logic
- [ ] Code builds successfully (`npm run build`)
- [ ] Ran `npm run lint`
- [ ] No console errors or warnings
### Testing | 测试
- [ ] Component tests added/updated | 已添加/更新组件测试
- [ ] Tests pass locally | 测试在本地通过
### Testing
- [ ] Component tests added/updated
- [ ] Tests pass locally
### Documentation | 文档
- [ ] Updated relevant documentation | 已更新相关文档
- [ ] Updated type definitions (TypeScript) | 已更新类型定义
- [ ] Added JSDoc comments where necessary | 已添加 JSDoc 注释
### Documentation
- [ ] Updated relevant documentation
- [ ] Updated type definitions (TypeScript)
- [ ] Added JSDoc comments where necessary
### Git
- [ ] Commits follow conventional format | 提交遵循 Conventional Commits 格式
- [ ] Rebased on latest `dev` branch | 已 rebase 到最新 `dev` 分支
- [ ] No merge conflicts | 无合并冲突
- [ ] Commits follow conventional format
- [ ] Rebased on latest `dev` branch
- [ ] No merge conflicts
---
## 📚 Additional Notes | 补充说明
**English:** **中文:**
## 📚 Additional Notes
---
**By submitting this PR, I confirm | 提交此 PR我确认**
**By submitting this PR, I confirm:**
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md) | 已阅读贡献指南
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md) | 同意行为准则
- [ ] My contribution is licensed under AGPL-3.0 | 贡献遵循 AGPL-3.0 许可证
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md)
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md)
- [ ] My contribution is licensed under AGPL-3.0
---
🌟 **Thank you for your contribution! | 感谢你的贡献!**
🌟 **Thank you for your contribution!**

View File

@@ -1,98 +1,93 @@
# Pull Request - General | 通用 PR
# Pull Request - General
> **💡 提示 Tip:** 推荐 PR 标题格式 `type(scope): description`
> 例如: `feat(trader): add new strategy` | `fix(api): resolve auth issue` | `docs(readme): update`
> **💡 Tip:** Recommended PR title format `type(scope): description`
> Example: `feat(trader): add new strategy` | `fix(api): resolve auth issue` | `docs(readme): update`
---
## 📝 Description | 描述
**English:** **中文:**
## 📝 Description
---
## 🎯 Type of Change | 变更类型
## 🎯 Type of Change
- [ ] 🐛 Bug fix | 修复 Bug
- [ ] ✨ New feature | 新功能
- [ ] 💥 Breaking change | 破坏性变更
- [ ] 📝 Documentation update | 文档更新
- [ ] 🎨 Code style update | 代码样式更新
- [ ] ♻️ Refactoring | 重构
- [ ] ⚡ Performance improvement | 性能优化
- [ ] ✅ Test update | 测试更新
- [ ] 🔧 Build/config change | 构建/配置变更
- [ ] 🔒 Security fix | 安全修复
- [ ] 🐛 Bug fix
- [ ] ✨ New feature
- [ ] 💥 Breaking change
- [ ] 📝 Documentation update
- [ ] 🎨 Code style update
- [ ] ♻️ Refactoring
- [ ] ⚡ Performance improvement
- [ ] ✅ Test update
- [ ] 🔧 Build/config change
- [ ] 🔒 Security fix
---
## 🔗 Related Issues | 相关 Issue
## 🔗 Related Issues
- Closes # | 关闭 #
- Related to # | 相关 #
- Closes #
- Related to #
---
## 📋 Changes Made | 具体变更
## 📋 Changes Made
**English:** **中文:**
-
-
---
## 🧪 Testing | 测试
## 🧪 Testing
- [ ] Tested locally | 本地测试通过
- [ ] Tests pass | 测试通过
- [ ] Verified no existing functionality broke | 确认没有破坏现有功能
- [ ] Tested locally
- [ ] Tests pass
- [ ] Verified no existing functionality broke
**Test details | 测试详情:**
**Test details:**
---
## ✅ Checklist | 检查清单
## ✅ Checklist
### Code Quality | 代码质量
- [ ] Code follows project style | 代码遵循项目风格
- [ ] Self-review completed | 已完成代码自查
- [ ] Comments added for complex logic | 已添加必要注释
- [ ] No new warnings or errors | 无新的警告或错误
### Code Quality
- [ ] Code follows project style
- [ ] Self-review completed
- [ ] Comments added for complex logic
- [ ] No new warnings or errors
### Documentation | 文档
- [ ] Updated relevant documentation | 已更新相关文档
- [ ] Added inline comments where necessary | 已添加必要的代码注释
### Documentation
- [ ] Updated relevant documentation
- [ ] Added inline comments where necessary
### Git
- [ ] Commits follow conventional format | 提交遵循 Conventional Commits 格式
- [ ] Rebased on latest `dev` branch | 已 rebase 到最新 `dev` 分支
- [ ] No merge conflicts | 无合并冲突
- [ ] Commits follow conventional format
- [ ] Rebased on latest `dev` branch
- [ ] No merge conflicts
---
## 🔒 Security (if applicable) | 安全(如适用)
## 🔒 Security (if applicable)
- [ ] No API keys or secrets hardcoded | 没有硬编码 API 密钥
- [ ] User inputs properly validated | 用户输入已正确验证
- [ ] N/A | 不适用
- [ ] No API keys or secrets hardcoded
- [ ] User inputs properly validated
- [ ] N/A
---
## 📚 Additional Notes | 补充说明
**English:** **中文:**
## 📚 Additional Notes
---
**By submitting this PR, I confirm | 提交此 PR我确认**
**By submitting this PR, I confirm:**
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md) | 已阅读贡献指南
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md) | 同意行为准则
- [ ] My contribution is licensed under AGPL-3.0 | 贡献遵循 AGPL-3.0 许可证
- [ ] I have read the [Contributing Guidelines](../../CONTRIBUTING.md)
- [ ] I agree to the [Code of Conduct](../../CODE_OF_CONDUCT.md)
- [ ] My contribution is licensed under AGPL-3.0
---
🌟 **Thank you for your contribution! | 感谢你的贡献!**
🌟 **Thank you for your contribution!**

View File

@@ -5,6 +5,7 @@ on:
branches:
- main
- dev
- release/stable
tags:
- 'v*'
pull_request:
@@ -152,12 +153,14 @@ jobs:
env:
IMAGE_BASE: ${{ needs.prepare.outputs.image_base }}
run: |
REF_NAME="${{ github.ref_name }}"
# Convert branch name: release/stable -> release-stable (matching Docker metadata action)
REF_NAME=$(echo "${{ github.ref_name }}" | sed 's/\//-/g')
GHCR_IMAGE="${{ env.REGISTRY_GHCR }}/${IMAGE_BASE}/nofx-${{ matrix.image_suffix }}"
echo "📦 Creating manifest for ${{ matrix.image_suffix }}"
echo "Repository: ${IMAGE_BASE}"
echo "Image: ${GHCR_IMAGE}"
echo "Ref name: ${REF_NAME}"
docker buildx imagetools create -t "${GHCR_IMAGE}:${REF_NAME}" \
"${GHCR_IMAGE}:${REF_NAME}-amd64" \
@@ -171,6 +174,14 @@ jobs:
echo "✅ Created latest tag (main branch only)"
fi
# release/stable branch gets the 'stable' tag
if [[ "${{ github.ref }}" == "refs/heads/release/stable" ]]; then
docker buildx imagetools create -t "${GHCR_IMAGE}:stable" \
"${GHCR_IMAGE}:${REF_NAME}-amd64" \
"${GHCR_IMAGE}:${REF_NAME}-arm64"
echo "✅ Created stable tag (release/stable branch)"
fi
if [[ -n "${{ secrets.DOCKERHUB_USERNAME }}" ]]; then
DOCKERHUB_IMAGE="${{ secrets.DOCKERHUB_USERNAME }}/nofx-${{ matrix.image_suffix }}"
docker buildx imagetools create -t "${DOCKERHUB_IMAGE}:${REF_NAME}" \

40
Dockerfile.railway Normal file
View File

@@ -0,0 +1,40 @@
# Railway All-in-One: 复用现有 GHCR 镜像
# 从现有镜像提取内容,合并到一个容器
# 从后端镜像提取二进制
FROM ghcr.io/nofxaios/nofx/nofx-backend:latest AS backend
# 从前端镜像提取静态文件
FROM ghcr.io/nofxaios/nofx/nofx-frontend:latest AS frontend
# 最终镜像
FROM alpine:latest
RUN apk add --no-cache ca-certificates tzdata sqlite nginx openssl gettext
# 复制后端二进制
COPY --from=backend /app/nofx /app/nofx
# 复制 TA-Lib 库
COPY --from=backend /usr/local/lib/libta_lib* /usr/local/lib/
RUN ldconfig /usr/local/lib 2>/dev/null || true
# 复制前端静态文件
COPY --from=frontend /usr/share/nginx/html /usr/share/nginx/html
WORKDIR /app
RUN mkdir -p /app/data
# 启动脚本(包含 nginx 配置生成)
COPY railway/start.sh /app/start.sh
RUN chmod +x /app/start.sh
ENV DB_PATH=/app/data/data.db
# Railway 会自动设置 PORT 环境变量
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:${PORT:-8080}/health || exit 1
CMD ["/app/start.sh"]

View File

@@ -50,40 +50,12 @@ Join our Telegram developer community: **[NOFX Developer Community](https://t.me
---
## Screenshots
## Before You Begin
### Config Page
| AI Models & Exchanges | Traders List |
|:---:|:---:|
| <img src="screenshots/config-ai-exchanges.png" width="400" alt="Config - AI Models & Exchanges"/> | <img src="screenshots/config-traders-list.png" width="400" alt="Config - Traders List"/> |
To use NOFX, you'll need:
### Competition & Backtest
| Competition Mode | Backtest Lab |
|:---:|:---:|
| <img src="screenshots/competition-page.png" width="400" alt="Competition Page"/> | <img src="screenshots/backtest-lab.png" width="400" alt="Backtest Lab"/> |
### Dashboard
| Overview | Market Chart |
|:---:|:---:|
| <img src="screenshots/dashboard-page.png" width="400" alt="Dashboard Overview"/> | <img src="screenshots/dashboard-market-chart.png" width="400" alt="Dashboard Market Chart"/> |
| Trading Stats | Position History |
|:---:|:---:|
| <img src="screenshots/dashboard-trading-stats.png" width="400" alt="Trading Stats"/> | <img src="screenshots/dashboard-position-history.png" width="400" alt="Position History"/> |
| Positions | Trader Details |
|:---:|:---:|
| <img src="screenshots/dashboard-positions.png" width="400" alt="Dashboard Positions"/> | <img src="screenshots/details-page.png" width="400" alt="Trader Details"/> |
### Strategy Studio
| Strategy Editor | Indicators Config |
|:---:|:---:|
| <img src="screenshots/strategy-studio.png" width="400" alt="Strategy Studio"/> | <img src="screenshots/strategy-indicators.png" width="400" alt="Strategy Indicators"/> |
### Debate Arena
| AI Debate Session | Create Debate |
|:---:|:---:|
| <img src="screenshots/debate-arena.png" width="400" alt="Debate Arena"/> | <img src="screenshots/debate-create.png" width="400" alt="Create Debate"/> |
1. **Exchange Account** - Register on any supported exchange and create API credentials with trading permissions
2. **AI Model API Key** - Get from any supported provider (DeepSeek recommended for cost-effectiveness)
---
@@ -122,9 +94,46 @@ Join our Telegram developer community: **[NOFX Developer Community](https://t.me
---
## Screenshots
### Config Page
| AI Models & Exchanges | Traders List |
|:---:|:---:|
| <img src="screenshots/config-ai-exchanges.png" width="400" alt="Config - AI Models & Exchanges"/> | <img src="screenshots/config-traders-list.png" width="400" alt="Config - Traders List"/> |
### Competition & Backtest
| Competition Mode | Backtest Lab |
|:---:|:---:|
| <img src="screenshots/competition-page.png" width="400" alt="Competition Page"/> | <img src="screenshots/backtest-lab.png" width="400" alt="Backtest Lab"/> |
### Dashboard
| Overview | Market Chart |
|:---:|:---:|
| <img src="screenshots/dashboard-page.png" width="400" alt="Dashboard Overview"/> | <img src="screenshots/dashboard-market-chart.png" width="400" alt="Dashboard Market Chart"/> |
| Trading Stats | Position History |
|:---:|:---:|
| <img src="screenshots/dashboard-trading-stats.png" width="400" alt="Trading Stats"/> | <img src="screenshots/dashboard-position-history.png" width="400" alt="Position History"/> |
| Positions | Trader Details |
|:---:|:---:|
| <img src="screenshots/dashboard-positions.png" width="400" alt="Dashboard Positions"/> | <img src="screenshots/details-page.png" width="400" alt="Trader Details"/> |
### Strategy Studio
| Strategy Editor | Indicators Config |
|:---:|:---:|
| <img src="screenshots/strategy-studio.png" width="400" alt="Strategy Studio"/> | <img src="screenshots/strategy-indicators.png" width="400" alt="Strategy Indicators"/> |
### Debate Arena
| AI Debate Session | Create Debate |
|:---:|:---:|
| <img src="screenshots/debate-arena.png" width="400" alt="Debate Arena"/> | <img src="screenshots/debate-create.png" width="400" alt="Create Debate"/> |
---
## Quick Start
### One-Click Install (Recommended)
### One-Click Install (Local/Server)
**Linux / macOS:**
```bash
@@ -133,6 +142,14 @@ curl -fsSL https://raw.githubusercontent.com/NoFxAiOS/nofx/main/install.sh | bas
That's it! Open **http://127.0.0.1:3000** in your browser.
### One-Click Cloud Deploy (Railway)
Deploy to Railway with one click - no server setup required:
[![Deploy on Railway](https://railway.com/button.svg)](https://railway.com/deploy/nofx?referralCode=nofx)
After deployment, Railway will provide a public URL to access your NOFX instance.
### Docker Compose (Manual)
```bash

View File

@@ -15,7 +15,7 @@ import (
"nofx/backtest"
"nofx/logger"
"nofx/market"
"nofx/provider"
"nofx/provider/nofxos"
"nofx/store"
"github.com/gin-gonic/gin"
@@ -60,7 +60,7 @@ func (s *Server) handleBacktestStart(c *gin.Context) {
var req backtestStartRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -78,23 +78,23 @@ func (s *Server) handleBacktestStart(c *gin.Context) {
if cfg.StrategyID != "" {
strategy, err := s.store.Strategy().Get(cfg.UserID, cfg.StrategyID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to load strategy: %v", err)})
SafeBadRequest(c, "Failed to load strategy")
return
}
if strategy == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("strategy not found: %s", cfg.StrategyID)})
SafeBadRequest(c, "Strategy not found")
return
}
var strategyConfig store.StrategyConfig
if err := json.Unmarshal([]byte(strategy.Config), &strategyConfig); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse strategy config: %v", err)})
SafeBadRequest(c, "Failed to parse strategy config")
return
}
cfg.SetLoadedStrategy(&strategyConfig)
logger.Infof("📊 Backtest using saved strategy: %s (%s)", strategy.Name, strategy.ID)
logger.Infof("📊 Strategy coin source: type=%s, use_coin_pool=%v, use_oi_top=%v, static_coins=%v",
logger.Infof("📊 Strategy coin source: type=%s, use_ai500=%v, use_oi_top=%v, static_coins=%v",
strategyConfig.CoinSource.SourceType,
strategyConfig.CoinSource.UseCoinPool,
strategyConfig.CoinSource.UseAI500,
strategyConfig.CoinSource.UseOITop,
strategyConfig.CoinSource.StaticCoins)
@@ -102,7 +102,7 @@ func (s *Server) handleBacktestStart(c *gin.Context) {
if len(cfg.Symbols) == 0 {
symbols, err := s.resolveStrategyCoins(&strategyConfig)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to resolve coins from strategy: %v", err)})
SafeBadRequest(c, "Failed to resolve coins from strategy")
return
}
cfg.Symbols = symbols
@@ -111,7 +111,7 @@ func (s *Server) handleBacktestStart(c *gin.Context) {
}
if err := s.hydrateBacktestAIConfig(&cfg); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Failed to configure AI model")
return
}
@@ -120,7 +120,7 @@ func (s *Server) handleBacktestStart(c *gin.Context) {
runner, err := s.backtestManager.Start(context.Background(), cfg)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeError(c, http.StatusBadRequest, "Failed to start backtest", err)
return
}
@@ -149,11 +149,11 @@ func (s *Server) handleBacktestControl(c *gin.Context, fn func(string) error) {
var req runIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
if req.RunID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "run_id is required"})
SafeBadRequest(c, "run_id is required")
return
}
@@ -162,7 +162,7 @@ func (s *Server) handleBacktestControl(c *gin.Context, fn func(string) error) {
}
if err := fn(req.RunID); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeError(c, http.StatusBadRequest, "Failed to execute backtest operation", err)
return
}
@@ -181,11 +181,11 @@ func (s *Server) handleBacktestLabel(c *gin.Context) {
}
var req labelRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
if strings.TrimSpace(req.RunID) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "run_id is required"})
SafeBadRequest(c, "run_id is required")
return
}
userID := normalizeUserID(c.GetString("user_id"))
@@ -194,7 +194,7 @@ func (s *Server) handleBacktestLabel(c *gin.Context) {
}
meta, err := s.backtestManager.UpdateLabel(req.RunID, req.Label)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "Update backtest label", err)
return
}
c.JSON(http.StatusOK, meta)
@@ -207,11 +207,11 @@ func (s *Server) handleBacktestDelete(c *gin.Context) {
}
var req runIDRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
if strings.TrimSpace(req.RunID) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "run_id is required"})
SafeBadRequest(c, "run_id is required")
return
}
userID := normalizeUserID(c.GetString("user_id"))
@@ -219,7 +219,7 @@ func (s *Server) handleBacktestDelete(c *gin.Context) {
return
}
if err := s.backtestManager.Delete(req.RunID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "Delete backtest run", err)
return
}
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
@@ -277,7 +277,7 @@ func (s *Server) handleBacktestRuns(c *gin.Context) {
metas, err := s.backtestManager.ListRuns()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "List backtest runs", err)
return
}
stateFilter := strings.ToLower(strings.TrimSpace(c.Query("state")))
@@ -349,7 +349,7 @@ func (s *Server) handleBacktestEquity(c *gin.Context) {
points, err := s.backtestManager.LoadEquity(runID, timeframe, limit)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeError(c, http.StatusBadRequest, "Failed to load equity data", err)
return
}
c.JSON(http.StatusOK, points)
@@ -375,7 +375,7 @@ func (s *Server) handleBacktestTrades(c *gin.Context) {
events, err := s.backtestManager.LoadTrades(runID, limit)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeError(c, http.StatusBadRequest, "Failed to load trades", err)
return
}
c.JSON(http.StatusOK, events)
@@ -404,7 +404,7 @@ func (s *Server) handleBacktestMetrics(c *gin.Context) {
c.JSON(http.StatusAccepted, gin.H{"error": "metrics not ready yet"})
return
}
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeError(c, http.StatusBadRequest, "Failed to load metrics", err)
return
}
c.JSON(http.StatusOK, metrics)
@@ -427,7 +427,7 @@ func (s *Server) handleBacktestTrace(c *gin.Context) {
cycle := queryInt(c, "cycle", 0)
record, err := s.backtestManager.GetTrace(runID, cycle)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trace record")
return
}
c.JSON(http.StatusOK, record)
@@ -461,7 +461,7 @@ func (s *Server) handleBacktestDecisions(c *gin.Context) {
records, err := backtest.LoadDecisionRecords(runID, limit, offset)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "Load decision records", err)
return
}
c.JSON(http.StatusOK, records)
@@ -483,7 +483,7 @@ func (s *Server) handleBacktestExport(c *gin.Context) {
}
path, err := s.backtestManager.ExportRun(runID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeError(c, http.StatusBadRequest, "Failed to export backtest", err)
return
}
defer os.Remove(path)
@@ -536,8 +536,7 @@ func (s *Server) handleBacktestKlines(c *gin.Context) {
klines, err := market.GetKlinesRange(symbol, timeframe, startTime, endTime)
if err != nil {
logger.Errorf("Failed to fetch klines for %s: %v", symbol, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to fetch klines: %v", err)})
SafeInternalError(c, "Fetch klines", err)
return
}
@@ -620,11 +619,11 @@ func writeBacktestAccessError(c *gin.Context, err error) bool {
}
switch {
case errors.Is(err, errBacktestForbidden):
c.JSON(http.StatusForbidden, gin.H{"error": "No permission to access this backtest task"})
SafeForbidden(c, "No permission to access this backtest task")
case errors.Is(err, os.ErrNotExist), errors.Is(err, sql.ErrNoRows):
c.JSON(http.StatusNotFound, gin.H{"error": "Backtest task does not exist"})
SafeNotFound(c, "Backtest task")
default:
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "Access backtest", err)
}
return true
}
@@ -639,21 +638,13 @@ func (s *Server) resolveStrategyCoins(strategyConfig *store.StrategyConfig) ([]s
var symbols []string
symbolSet := make(map[string]bool)
// Set custom API URLs if provided
if coinSource.CoinPoolAPIURL != "" {
provider.SetCoinPoolAPI(coinSource.CoinPoolAPIURL)
}
if coinSource.OITopAPIURL != "" {
provider.SetOITopAPI(coinSource.OITopAPIURL)
}
// Handle empty source_type - check flags for backward compatibility
sourceType := coinSource.SourceType
if sourceType == "" {
if coinSource.UseCoinPool && coinSource.UseOITop {
if coinSource.UseAI500 && coinSource.UseOITop {
sourceType = "mixed"
} else if coinSource.UseCoinPool {
sourceType = "coinpool"
} else if coinSource.UseAI500 {
sourceType = "ai500"
} else if coinSource.UseOITop {
sourceType = "oi_top"
} else if len(coinSource.StaticCoins) > 0 {
@@ -674,13 +665,13 @@ func (s *Server) resolveStrategyCoins(strategyConfig *store.StrategyConfig) ([]s
}
}
case "coinpool":
limit := coinSource.CoinPoolLimit
case "ai500":
limit := coinSource.AI500Limit
if limit <= 0 {
limit = 30
}
logger.Infof("📊 Fetching AI500 coins with limit=%d", limit)
coins, err := provider.GetTopRatedCoins(limit)
coins, err := nofxos.DefaultClient().GetTopRatedCoins(limit)
if err != nil {
return nil, fmt.Errorf("failed to get AI500 coins: %w", err)
}
@@ -694,7 +685,7 @@ func (s *Server) resolveStrategyCoins(strategyConfig *store.StrategyConfig) ([]s
}
case "oi_top":
coins, err := provider.GetOITopSymbols()
coins, err := nofxos.DefaultClient().GetOITopSymbols()
if err != nil {
return nil, fmt.Errorf("failed to get OI Top coins: %w", err)
}
@@ -714,13 +705,13 @@ func (s *Server) resolveStrategyCoins(strategyConfig *store.StrategyConfig) ([]s
}
case "mixed":
// Get from coin pool
if coinSource.UseCoinPool {
limit := coinSource.CoinPoolLimit
// Get from AI500
if coinSource.UseAI500 {
limit := coinSource.AI500Limit
if limit <= 0 {
limit = 30
}
coins, err := provider.GetTopRatedCoins(limit)
coins, err := nofxos.DefaultClient().GetTopRatedCoins(limit)
if err != nil {
logger.Warnf("Failed to get AI500 coins: %v", err)
} else {
@@ -736,7 +727,7 @@ func (s *Server) resolveStrategyCoins(strategyConfig *store.StrategyConfig) ([]s
// Get from OI Top
if coinSource.UseOITop {
coins, err := provider.GetOITopSymbols()
coins, err := nofxos.DefaultClient().GetOITopSymbols()
if err != nil {
logger.Warnf("Failed to get OI Top coins: %v", err)
} else {

View File

@@ -8,7 +8,7 @@ import (
"nofx/debate"
"nofx/logger"
"nofx/provider"
"nofx/provider/nofxos"
"nofx/store"
"github.com/gin-gonic/gin"
@@ -131,7 +131,7 @@ func (h *DebateHandler) HandleCreateDebate(c *gin.Context) {
var req CreateDebateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -158,35 +158,27 @@ func (h *DebateHandler) HandleCreateDebate(c *gin.Context) {
if len(coinSource.StaticCoins) > 0 {
req.Symbol = coinSource.StaticCoins[0]
}
case "coinpool":
// Fetch from coin pool API
if coinSource.CoinPoolAPIURL != "" {
provider.SetCoinPoolAPI(coinSource.CoinPoolAPIURL)
}
if coins, err := provider.GetTopRatedCoins(1); err == nil && len(coins) > 0 {
case "ai500":
// Fetch from AI500 API
if coins, err := nofxos.DefaultClient().GetTopRatedCoins(1); err == nil && len(coins) > 0 {
req.Symbol = coins[0]
logger.Infof("Fetched coin from pool API: %s", req.Symbol)
logger.Infof("Fetched coin from AI500 API: %s", req.Symbol)
}
case "oi_top":
// Fetch from OI top API
if coinSource.OITopAPIURL != "" {
provider.SetOITopAPI(coinSource.OITopAPIURL)
}
if coins, err := provider.GetOITopSymbols(); err == nil && len(coins) > 0 {
if coins, err := nofxos.DefaultClient().GetOITopSymbols(); err == nil && len(coins) > 0 {
req.Symbol = coins[0]
logger.Infof("Fetched coin from OI Top API: %s", req.Symbol)
}
case "mixed":
// Try coin pool first, then OI top
if coinSource.UseCoinPool && coinSource.CoinPoolAPIURL != "" {
provider.SetCoinPoolAPI(coinSource.CoinPoolAPIURL)
if coins, err := provider.GetTopRatedCoins(1); err == nil && len(coins) > 0 {
// Try AI500 first, then OI top
if coinSource.UseAI500 {
if coins, err := nofxos.DefaultClient().GetTopRatedCoins(1); err == nil && len(coins) > 0 {
req.Symbol = coins[0]
logger.Infof("Fetched coin from pool API (mixed): %s", req.Symbol)
logger.Infof("Fetched coin from AI500 API (mixed): %s", req.Symbol)
}
} else if coinSource.UseOITop && coinSource.OITopAPIURL != "" {
provider.SetOITopAPI(coinSource.OITopAPIURL)
if coins, err := provider.GetOITopSymbols(); err == nil && len(coins) > 0 {
} else if coinSource.UseOITop {
if coins, err := nofxos.DefaultClient().GetOITopSymbols(); err == nil && len(coins) > 0 {
req.Symbol = coins[0]
logger.Infof("Fetched coin from OI Top API (mixed): %s", req.Symbol)
}
@@ -292,7 +284,7 @@ func (h *DebateHandler) HandleStartDebate(c *gin.Context) {
// Start debate asynchronously
if err := h.engine.StartDebate(debateID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "Start debate", err)
return
}
@@ -316,7 +308,7 @@ func (h *DebateHandler) HandleCancelDebate(c *gin.Context) {
}
if err := h.engine.CancelDebate(debateID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "Cancel debate", err)
return
}
@@ -495,20 +487,20 @@ func (h *DebateHandler) HandleExecuteDebate(c *gin.Context) {
// Parse request
var req ExecuteDebateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
// Get trader executor
executor, err := h.traderManager.GetTraderExecutor(req.TraderID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("trader not available: %v", err)})
SafeError(c, http.StatusBadRequest, "Trader not available", err)
return
}
// Execute consensus
if err := h.engine.ExecuteConsensus(debateID, executor); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
SafeInternalError(c, "Execute consensus", err)
return
}
@@ -635,7 +627,9 @@ func (h *DebateHandler) broadcastConsensus(sessionID string, decision *store.Deb
}
func (h *DebateHandler) broadcastError(sessionID string, err error) {
// Sanitize error message before broadcasting to client
safeMsg := SanitizeError(err, "An error occurred during debate")
h.broadcast(sessionID, "error", map[string]interface{}{
"error": err.Error(),
"error": safeMsg,
})
}

95
api/errors.go Normal file
View File

@@ -0,0 +1,95 @@
package api
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"nofx/logger"
)
// SafeError returns a safe error message without exposing internal details
// It logs the actual error for debugging but returns a generic message to the client
func SafeError(c *gin.Context, statusCode int, publicMsg string, internalErr error) {
// Log the actual error internally
if internalErr != nil {
logger.Errorf("[API Error] %s: %v", publicMsg, internalErr)
}
c.JSON(statusCode, gin.H{"error": publicMsg})
}
// SafeInternalError logs internal error and returns a generic message
func SafeInternalError(c *gin.Context, operation string, err error) {
logger.Errorf("[Internal Error] %s: %v", operation, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": operation + " failed"})
}
// SafeBadRequest returns a safe bad request error
// For validation errors, we can be more specific since they're about user input
func SafeBadRequest(c *gin.Context, msg string) {
c.JSON(http.StatusBadRequest, gin.H{"error": msg})
}
// SafeNotFound returns a generic not found error
func SafeNotFound(c *gin.Context, resource string) {
c.JSON(http.StatusNotFound, gin.H{"error": resource + " not found"})
}
// SafeUnauthorized returns unauthorized error
func SafeUnauthorized(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
}
// SafeForbidden returns forbidden error
func SafeForbidden(c *gin.Context, msg string) {
c.JSON(http.StatusForbidden, gin.H{"error": msg})
}
// IsSensitiveError checks if an error message contains sensitive information
func IsSensitiveError(err error) bool {
if err == nil {
return false
}
errMsg := strings.ToLower(err.Error())
sensitivePatterns := []string{
// Database
"postgres", "mysql", "sqlite", "database", "sql",
"connection", "connect", "failed to connect",
// Network
"dial", "tcp", "udp", "socket", "timeout",
// Server info
"127.0.0.1", "localhost", "0.0.0.0",
// File system
"no such file", "permission denied", "open /",
// Credentials
"password", "user=", "host=", "port=",
// Internal
"panic", "runtime error", "stack trace",
}
for _, pattern := range sensitivePatterns {
if strings.Contains(errMsg, pattern) {
return true
}
}
// Check for IP addresses (simple pattern)
if strings.Contains(errMsg, ":") && (strings.Contains(errMsg, ".") || strings.Contains(errMsg, "::")) {
return true
}
return false
}
// SanitizeError returns the error message if safe, otherwise returns a generic message
func SanitizeError(err error, fallbackMsg string) string {
if err == nil {
return fallbackMsg
}
if IsSensitiveError(err) {
return fallbackMsg
}
return err.Error()
}

View File

@@ -202,6 +202,7 @@ func (s *Server) setupRoutes() {
protected.GET("/trades", s.handleTrades)
protected.GET("/orders", s.handleOrders) // Order list (all orders)
protected.GET("/orders/:id/fills", s.handleOrderFills) // Order fill details
protected.GET("/open-orders", s.handleOpenOrders) // Open orders from exchange (pending SL/TP)
protected.GET("/decisions", s.handleDecisions)
protected.GET("/decisions/latest", s.handleLatestDecisions)
protected.GET("/statistics", s.handleStatistics)
@@ -406,7 +407,7 @@ type CreateTraderRequest struct {
CustomPrompt string `json:"custom_prompt"`
OverrideBasePrompt bool `json:"override_base_prompt"`
SystemPromptTemplate string `json:"system_prompt_template"` // System prompt template name
UseCoinPool bool `json:"use_coin_pool"`
UseAI500 bool `json:"use_ai500"`
UseOITop bool `json:"use_oi_top"`
}
@@ -486,7 +487,7 @@ func (s *Server) handleCreateTrader(c *gin.Context) {
userID := c.GetString("user_id")
var req CreateTraderRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -666,7 +667,7 @@ func (s *Server) handleCreateTrader(c *gin.Context) {
BTCETHLeverage: btcEthLeverage,
AltcoinLeverage: altcoinLeverage,
TradingSymbols: req.TradingSymbols,
UseCoinPool: req.UseCoinPool,
UseAI500: req.UseAI500,
UseOITop: req.UseOITop,
CustomPrompt: req.CustomPrompt,
OverrideBasePrompt: req.OverrideBasePrompt,
@@ -682,7 +683,7 @@ func (s *Server) handleCreateTrader(c *gin.Context) {
err = s.store.Trader().Create(traderRecord)
if err != nil {
logger.Infof("❌ Failed to create trader: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create trader: %v", err)})
SafeInternalError(c, "Failed to create trader", err)
return
}
logger.Infof("🔧 DEBUG: CreateTrader succeeded")
@@ -732,7 +733,7 @@ func (s *Server) handleUpdateTrader(c *gin.Context) {
var req UpdateTraderRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -779,11 +780,13 @@ func (s *Server) handleUpdateTrader(c *gin.Context) {
// Set scan interval, allow updates
scanIntervalMinutes := req.ScanIntervalMinutes
logger.Infof("📊 Update trader scan_interval: req=%d, existing=%d", req.ScanIntervalMinutes, existingTrader.ScanIntervalMinutes)
if scanIntervalMinutes <= 0 {
scanIntervalMinutes = existingTrader.ScanIntervalMinutes // Keep original value
} else if scanIntervalMinutes < 3 {
scanIntervalMinutes = 3
}
logger.Infof("📊 Final scan_interval_minutes: %d", scanIntervalMinutes)
// Set system prompt template
systemPromptTemplate := req.SystemPromptTemplate
@@ -818,16 +821,26 @@ func (s *Server) handleUpdateTrader(c *gin.Context) {
IsRunning: existingTrader.IsRunning, // Keep original value
}
// Check if trader was running before update (we'll restart it after)
wasRunning := false
if existingMemTrader, memErr := s.traderManager.GetTrader(traderID); memErr == nil {
status := existingMemTrader.GetStatus()
if running, ok := status["is_running"].(bool); ok && running {
wasRunning = true
logger.Infof("🔄 Trader %s was running, will restart with new config after update", traderID)
}
}
// Update database
logger.Infof("🔄 Updating trader: ID=%s, Name=%s, AIModelID=%s, StrategyID=%s, req.StrategyID=%s",
traderRecord.ID, traderRecord.Name, traderRecord.AIModelID, traderRecord.StrategyID, req.StrategyID)
logger.Infof("🔄 Updating trader: ID=%s, Name=%s, AIModelID=%s, StrategyID=%s, ScanInterval=%d min",
traderRecord.ID, traderRecord.Name, traderRecord.AIModelID, traderRecord.StrategyID, scanIntervalMinutes)
err = s.store.Trader().Update(traderRecord)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update trader: %v", err)})
SafeInternalError(c, "Failed to update trader", err)
return
}
// Remove old trader from memory first to ensure fresh config is loaded
// Remove old trader from memory first (this also stops if running)
s.traderManager.RemoveTrader(traderID)
// Reload traders into memory with fresh config
@@ -836,6 +849,18 @@ func (s *Server) handleUpdateTrader(c *gin.Context) {
logger.Infof("⚠️ Failed to reload user traders into memory: %v", err)
}
// If trader was running before, restart it with new config
if wasRunning {
if reloadedTrader, getErr := s.traderManager.GetTrader(traderID); getErr == nil {
go func() {
logger.Infof("▶️ Restarting trader %s with new config...", traderID)
if runErr := reloadedTrader.Run(); runErr != nil {
logger.Infof("❌ Trader %s runtime error: %v", traderID, runErr)
}
}()
}
}
logger.Infof("✓ Trader updated successfully: %s (model: %s, exchange: %s, strategy: %s)", req.Name, req.AIModelID, req.ExchangeID, strategyID)
c.JSON(http.StatusOK, gin.H{
@@ -854,7 +879,7 @@ func (s *Server) handleDeleteTrader(c *gin.Context) {
// Delete from database
err := s.store.Trader().Delete(userID, traderID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete trader: %v", err)})
SafeInternalError(c, "Failed to delete trader", err)
return
}
@@ -1012,14 +1037,14 @@ func (s *Server) handleUpdateTraderPrompt(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
// Update database
err := s.store.Trader().UpdateCustomPrompt(userID, traderID, req.CustomPrompt, req.OverrideBasePrompt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update custom prompt: %v", err)})
SafeInternalError(c, "Failed to update custom prompt", err)
return
}
@@ -1044,14 +1069,14 @@ func (s *Server) handleToggleCompetition(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
// Update database
err := s.store.Trader().UpdateShowInCompetition(userID, traderID, req.ShowInCompetition)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update competition visibility: %v", err)})
SafeInternalError(c, "Update competition visibility", err)
return
}
@@ -1150,7 +1175,7 @@ func (s *Server) handleSyncBalance(c *gin.Context) {
if createErr != nil {
logger.Infof("⚠️ Failed to create temporary trader: %v", createErr)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to connect to exchange: %v", createErr)})
SafeInternalError(c, "Failed to connect to exchange", createErr)
return
}
@@ -1158,7 +1183,7 @@ func (s *Server) handleSyncBalance(c *gin.Context) {
balanceInfo, balanceErr := tempTrader.GetBalance()
if balanceErr != nil {
logger.Infof("⚠️ Failed to query exchange balance: %v", balanceErr)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to query balance: %v", balanceErr)})
SafeInternalError(c, "Failed to query balance", balanceErr)
return
}
@@ -1302,7 +1327,7 @@ func (s *Server) handleClosePosition(c *gin.Context) {
if createErr != nil {
logger.Infof("⚠️ Failed to create temporary trader: %v", createErr)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to connect to exchange: %v", createErr)})
SafeInternalError(c, "Failed to connect to exchange", createErr)
return
}
@@ -1344,7 +1369,7 @@ func (s *Server) handleClosePosition(c *gin.Context) {
if closeErr != nil {
logger.Infof("❌ Close position failed: symbol=%s, side=%s, error=%v", req.Symbol, req.Side, closeErr)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to close position: %v", closeErr)})
SafeInternalError(c, "Failed to close position", closeErr)
return
}
@@ -1428,9 +1453,9 @@ func (s *Server) recordClosePositionOrder(traderID, exchangeID, exchangeType, sy
FilledQuantity: quantity,
AvgFillPrice: exitPrice,
Commission: fee,
FilledAt: time.Now(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
FilledAt: time.Now().UTC().UnixMilli(),
CreatedAt: time.Now().UTC().UnixMilli(),
UpdatedAt: time.Now().UTC().UnixMilli(),
}
if err := s.store.Order().CreateOrder(orderRecord); err != nil {
@@ -1458,7 +1483,7 @@ func (s *Server) recordClosePositionOrder(traderID, exchangeID, exchangeType, sy
CommissionAsset: "USDT",
RealizedPnL: 0,
IsMaker: false,
CreatedAt: time.Now(),
CreatedAt: time.Now().UTC().UnixMilli(),
}
if err := s.store.Order().CreateFill(fillRecord); err != nil {
@@ -1533,7 +1558,7 @@ func (s *Server) pollAndUpdateOrderStatus(orderRecordID int64, traderID, exchang
CommissionAsset: "USDT",
RealizedPnL: 0,
IsMaker: false,
CreatedAt: time.Now(),
CreatedAt: time.Now().UTC().UnixMilli(),
}
if err := s.store.Order().CreateFill(fillRecord); err != nil {
@@ -1582,7 +1607,7 @@ func (s *Server) handleGetModelConfigs(c *gin.Context) {
models, err := s.store.AIModel().List(userID)
if err != nil {
logger.Infof("❌ Failed to get AI model configs: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to get AI model configs: %v", err)})
SafeInternalError(c, "Failed to get AI model configs", err)
return
}
@@ -1684,7 +1709,7 @@ func (s *Server) handleUpdateModelConfigs(c *gin.Context) {
for modelID, modelData := range req.Models {
err := s.store.AIModel().Update(userID, modelID, modelData.Enabled, modelData.APIKey, modelData.CustomAPIURL, modelData.CustomModelName)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update model %s: %v", modelID, err)})
SafeInternalError(c, fmt.Sprintf("Update model %s", modelID), err)
return
}
}
@@ -1706,8 +1731,7 @@ func (s *Server) handleGetExchangeConfigs(c *gin.Context) {
logger.Infof("🔍 Querying exchange configs for user %s", userID)
exchanges, err := s.store.Exchange().List(userID)
if err != nil {
logger.Infof("❌ Failed to get exchange configs: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to get exchange configs: %v", err)})
SafeInternalError(c, "Failed to get exchange configs", err)
return
}
@@ -1805,7 +1829,7 @@ func (s *Server) handleUpdateExchangeConfigs(c *gin.Context) {
for exchangeID, exchangeData := range req.Exchanges {
err := s.store.Exchange().Update(userID, exchangeID, exchangeData.Enabled, exchangeData.APIKey, exchangeData.SecretKey, exchangeData.Passphrase, exchangeData.Testnet, exchangeData.HyperliquidWalletAddr, exchangeData.AsterUser, exchangeData.AsterSigner, exchangeData.AsterPrivateKey, exchangeData.LighterWalletAddr, exchangeData.LighterPrivateKey, exchangeData.LighterAPIKeyPrivateKey, exchangeData.LighterAPIKeyIndex)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update exchange %s: %v", exchangeID, err)})
SafeInternalError(c, fmt.Sprintf("Update exchange %s", exchangeID), err)
return
}
}
@@ -1910,7 +1934,7 @@ func (s *Server) handleCreateExchange(c *gin.Context) {
)
if err != nil {
logger.Infof("❌ Failed to create exchange account: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create exchange account: %v", err)})
SafeInternalError(c, "Failed to create exchange account", err)
return
}
@@ -1953,7 +1977,7 @@ func (s *Server) handleDeleteExchange(c *gin.Context) {
err = s.store.Exchange().Delete(userID, exchangeID)
if err != nil {
logger.Infof("❌ Failed to delete exchange account: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete exchange account: %v", err)})
SafeInternalError(c, "Failed to delete exchange account", err)
return
}
@@ -1966,7 +1990,7 @@ func (s *Server) handleTraderList(c *gin.Context) {
userID := c.GetString("user_id")
traders, err := s.store.Trader().List(userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to get trader list: %v", err)})
SafeInternalError(c, "Failed to get trader list", err)
return
}
@@ -2019,7 +2043,7 @@ func (s *Server) handleGetTraderConfig(c *gin.Context) {
fullCfg, err := s.store.Trader().GetFullConfig(userID, traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Failed to get trader config: %v", err)})
SafeNotFound(c, "Trader config")
return
}
traderConfig := fullCfg.Trader
@@ -2050,7 +2074,7 @@ func (s *Server) handleGetTraderConfig(c *gin.Context) {
"custom_prompt": traderConfig.CustomPrompt,
"override_base_prompt": traderConfig.OverrideBasePrompt,
"is_cross_margin": traderConfig.IsCrossMargin,
"use_coin_pool": traderConfig.UseCoinPool,
"use_ai500": traderConfig.UseAI500,
"use_oi_top": traderConfig.UseOITop,
"is_running": isRunning,
}
@@ -2062,13 +2086,13 @@ func (s *Server) handleGetTraderConfig(c *gin.Context) {
func (s *Server) handleStatus(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
@@ -2080,23 +2104,20 @@ func (s *Server) handleStatus(c *gin.Context) {
func (s *Server) handleAccount(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
logger.Infof("📊 Received account info request [%s]", trader.GetName())
account, err := trader.GetAccountInfo()
if err != nil {
logger.Infof("❌ Failed to get account info [%s]: %v", trader.GetName(), err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get account info: %v", err),
})
SafeInternalError(c, "Get account info", err)
return
}
@@ -2113,21 +2134,19 @@ func (s *Server) handleAccount(c *gin.Context) {
func (s *Server) handlePositions(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
positions, err := trader.GetPositions()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get position list: %v", err),
})
SafeInternalError(c, "Get positions", err)
return
}
@@ -2138,13 +2157,13 @@ func (s *Server) handlePositions(c *gin.Context) {
func (s *Server) handlePositionHistory(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
@@ -2165,9 +2184,7 @@ func (s *Server) handlePositionHistory(c *gin.Context) {
// Get closed positions
positions, err := store.Position().GetClosedPositions(trader.GetID(), limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get position history: %v", err),
})
SafeInternalError(c, "Get position history", err)
return
}
@@ -2192,13 +2209,13 @@ func (s *Server) handlePositionHistory(c *gin.Context) {
func (s *Server) handleTrades(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
@@ -2224,9 +2241,7 @@ func (s *Server) handleTrades(c *gin.Context) {
allTrades, err := store.Position().GetRecentTrades(trader.GetID(), limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get trades: %v", err),
})
SafeInternalError(c, "Get trades", err)
return
}
@@ -2249,13 +2264,13 @@ func (s *Server) handleTrades(c *gin.Context) {
func (s *Server) handleOrders(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
@@ -2280,30 +2295,14 @@ func (s *Server) handleOrders(c *gin.Context) {
return
}
// Get all orders for this trader
allOrders, err := store.Order().GetTraderOrders(trader.GetID(), limit)
// Get orders with filters applied at database level
orders, err := store.Order().GetTraderOrdersFiltered(trader.GetID(), symbol, statusFilter, limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get orders: %v", err),
})
SafeInternalError(c, "Get orders", err)
return
}
// Filter by symbol and status if specified
result := make([]interface{}, 0)
for _, order := range allOrders {
// Filter by symbol
if symbol != "" && order.Symbol != symbol {
continue
}
// Filter by status
if statusFilter != "" && order.Status != statusFilter {
continue
}
result = append(result, order)
}
c.JSON(http.StatusOK, result)
c.JSON(http.StatusOK, orders)
}
// handleOrderFills Order fill details (all fills for a specific order)
@@ -2317,13 +2316,13 @@ func (s *Server) handleOrderFills(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
@@ -2336,15 +2335,47 @@ func (s *Server) handleOrderFills(c *gin.Context) {
// Get fills for this order
fills, err := store.Order().GetOrderFills(orderID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get order fills: %v", err),
})
SafeInternalError(c, "Get order fills", err)
return
}
c.JSON(http.StatusOK, fills)
}
// handleOpenOrders Get open orders (pending SL/TP) from exchange
func (s *Server) handleOpenOrders(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
SafeNotFound(c, "Trader")
return
}
// Get symbol parameter (required for exchange query)
symbol := c.Query("symbol")
if symbol == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "symbol parameter is required"})
return
}
// Normalize symbol
symbol = market.Normalize(symbol)
// Get open orders from exchange
openOrders, err := trader.GetOpenOrders(symbol)
if err != nil {
SafeInternalError(c, "Get open orders", err)
return
}
c.JSON(http.StatusOK, openOrders)
}
// handleKlines K-line data (supports multiple exchanges via coinank)
func (s *Server) handleKlines(c *gin.Context) {
// Get query parameters
@@ -2376,30 +2407,21 @@ func (s *Server) handleKlines(c *gin.Context) {
// US Stocks via Alpaca
klines, err = s.getKlinesFromAlpaca(symbol, interval, limit)
if err != nil {
logger.Errorf("❌ Alpaca API failed for %s: %v", symbol, err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get klines from Alpaca: %v", err),
})
SafeInternalError(c, "Get klines from Alpaca", err)
return
}
case "forex", "metals":
// Forex and Metals via Twelve Data
klines, err = s.getKlinesFromTwelveData(symbol, interval, limit)
if err != nil {
logger.Errorf("❌ TwelveData API failed for %s: %v", symbol, err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get klines from TwelveData: %v", err),
})
SafeInternalError(c, "Get klines from TwelveData", err)
return
}
case "hyperliquid", "hyperliquid-xyz", "xyz":
// Hyperliquid native API - supports both crypto perps and stock perps (xyz dex)
klines, err = s.getKlinesFromHyperliquid(symbol, interval, limit)
if err != nil {
logger.Errorf("❌ Hyperliquid API failed for %s: %v", symbol, err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get klines from Hyperliquid: %v", err),
})
SafeInternalError(c, "Get klines from Hyperliquid", err)
return
}
default:
@@ -2407,10 +2429,7 @@ func (s *Server) handleKlines(c *gin.Context) {
symbol = market.Normalize(symbol)
klines, err = s.getKlinesFromCoinank(symbol, interval, exchange, limit)
if err != nil {
logger.Errorf("❌ CoinAnk API failed for %s on %s: %v", symbol, exchange, err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get klines from CoinAnk: %v", err),
})
SafeInternalError(c, "Get klines from CoinAnk", err)
return
}
}
@@ -2728,22 +2747,20 @@ func (s *Server) handleSymbols(c *gin.Context) {
func (s *Server) handleDecisions(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
// Get all historical decision records (unlimited)
records, err := trader.GetStore().Decision().GetLatestRecords(trader.GetID(), 10000)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get decision log: %v", err),
})
SafeInternalError(c, "Get decision log", err)
return
}
@@ -2754,13 +2771,13 @@ func (s *Server) handleDecisions(c *gin.Context) {
func (s *Server) handleLatestDecisions(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
@@ -2777,9 +2794,7 @@ func (s *Server) handleLatestDecisions(c *gin.Context) {
records, err := trader.GetStore().Decision().GetLatestRecords(trader.GetID(), limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get decision log: %v", err),
})
SafeInternalError(c, "Get decision log", err)
return
}
@@ -2796,21 +2811,19 @@ func (s *Server) handleLatestDecisions(c *gin.Context) {
func (s *Server) handleStatistics(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
trader, err := s.traderManager.GetTrader(traderID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
SafeNotFound(c, "Trader")
return
}
stats, err := trader.GetStore().Decision().GetStatistics(trader.GetID())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get statistics: %v", err),
})
SafeInternalError(c, "Get statistics", err)
return
}
@@ -2829,9 +2842,7 @@ func (s *Server) handleCompetition(c *gin.Context) {
competition, err := s.traderManager.GetCompetitionData()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get competition data: %v", err),
})
SafeInternalError(c, "Get competition data", err)
return
}
@@ -2843,7 +2854,7 @@ func (s *Server) handleCompetition(c *gin.Context) {
func (s *Server) handleEquityHistory(c *gin.Context) {
_, traderID, err := s.getTraderFromQuery(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid trader ID")
return
}
@@ -2851,9 +2862,7 @@ func (s *Server) handleEquityHistory(c *gin.Context) {
// Every 3 minutes per cycle: 10000 records = about 20 days of data
snapshots, err := s.store.Equity().GetLatest(traderID, 10000)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get historical data: %v", err),
})
SafeInternalError(c, "Get historical data", err)
return
}
@@ -2931,7 +2940,8 @@ func (s *Server) authMiddleware() gin.HandlerFunc {
// Validate JWT token
claims, err := auth.ValidateJWT(tokenString)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token: " + err.Error()})
logger.Errorf("[Auth] Invalid token: %v", err)
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid or expired token"})
c.Abort()
return
}
@@ -2979,7 +2989,44 @@ func (s *Server) handleRegister(c *gin.Context) {
return
}
// Check max users limit
var req struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required,min=6"`
}
if err := c.ShouldBindJSON(&req); err != nil {
SafeBadRequest(c, "Invalid request parameters")
return
}
// Check if email already exists (must check before maxUsers to allow incomplete OTP users)
existingUser, err := s.store.User().GetByEmail(req.Email)
if err == nil {
// User exists, check OTP verification status
if !existingUser.OTPVerified {
// OTP not verified, verify password first for security
if !auth.CheckPassword(req.Password, existingUser.PasswordHash) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Email or password incorrect"})
return
}
// Password correct, allow user to continue OTP setup
// Return existing OTP information
qrCodeURL := auth.GetOTPQRCodeURL(existingUser.OTPSecret, req.Email)
c.JSON(http.StatusOK, gin.H{
"user_id": existingUser.ID,
"email": existingUser.Email,
"otp_secret": existingUser.OTPSecret,
"qr_code_url": qrCodeURL,
"message": "Incomplete registration detected, please continue OTP setup",
})
return
}
// OTP already verified, reject duplicate registration
c.JSON(http.StatusConflict, gin.H{"error": "Email already registered"})
return
}
// Check max users limit (only for new users)
maxUsers := config.Get().MaxUsers
if maxUsers > 0 {
userCount, err := s.store.User().Count()
@@ -2993,23 +3040,6 @@ func (s *Server) handleRegister(c *gin.Context) {
}
}
var req struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required,min=6"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Check if email already exists
_, err := s.store.User().GetByEmail(req.Email)
if err == nil {
c.JSON(http.StatusConflict, gin.H{"error": "Email already registered"})
return
}
// Generate password hash
passwordHash, err := auth.HashPassword(req.Password)
if err != nil {
@@ -3036,7 +3066,7 @@ func (s *Server) handleRegister(c *gin.Context) {
err = s.store.User().Create(user)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user: " + err.Error()})
SafeInternalError(c, "Failed to create user", err)
return
}
@@ -3059,14 +3089,14 @@ func (s *Server) handleCompleteRegistration(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
// Get user information
user, err := s.store.User().GetByID(req.UserID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "User does not exist"})
SafeNotFound(c, "User")
return
}
@@ -3112,7 +3142,7 @@ func (s *Server) handleLogin(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -3131,10 +3161,15 @@ func (s *Server) handleLogin(c *gin.Context) {
// Check if OTP is verified
if !user.OTPVerified {
c.JSON(http.StatusUnauthorized, gin.H{
"error": "Account has not completed OTP setup",
// Return OTP info so user can complete setup
qrCodeURL := auth.GetOTPQRCodeURL(user.OTPSecret, user.Email)
c.JSON(http.StatusOK, gin.H{
"user_id": user.ID,
"email": user.Email,
"otp_secret": user.OTPSecret,
"qr_code_url": qrCodeURL,
"requires_otp_setup": true,
"message": "Please complete OTP setup first",
})
return
}
@@ -3156,14 +3191,14 @@ func (s *Server) handleVerifyOTP(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
// Get user information
user, err := s.store.User().GetByID(req.UserID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "User does not exist"})
SafeNotFound(c, "User")
return
}
@@ -3197,7 +3232,7 @@ func (s *Server) handleResetPassword(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -3326,9 +3361,7 @@ func (s *Server) handlePublicTraderList(c *gin.Context) {
// Get trader information from all users
competition, err := s.traderManager.GetCompetitionData()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get trader list: %v", err),
})
SafeInternalError(c, "Get trader list", err)
return
}
@@ -3371,9 +3404,7 @@ func (s *Server) handlePublicTraderList(c *gin.Context) {
func (s *Server) handlePublicCompetition(c *gin.Context) {
competition, err := s.traderManager.GetCompetitionData()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get competition data: %v", err),
})
SafeInternalError(c, "Get competition data", err)
return
}
@@ -3384,9 +3415,7 @@ func (s *Server) handlePublicCompetition(c *gin.Context) {
func (s *Server) handleTopTraders(c *gin.Context) {
topTraders, err := s.traderManager.GetTopTradersData()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get top 10 trader data: %v", err),
})
SafeInternalError(c, "Get top traders data", err)
return
}
@@ -3409,9 +3438,7 @@ func (s *Server) handleEquityHistoryBatch(c *gin.Context) {
// If no trader_ids specified, return historical data for top 5
topTraders, err := s.traderManager.GetTopTradersData()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": fmt.Sprintf("Failed to get top 5 traders: %v", err),
})
SafeInternalError(c, "Get top traders", err)
return
}
@@ -3506,7 +3533,8 @@ func (s *Server) getEquityHistoryForTraders(traderIDs []string, hours int) map[s
snapshots, err = s.store.Equity().GetLatest(traderID, 500)
}
if err != nil {
errors[traderID] = fmt.Sprintf("Failed to get historical data: %v", err)
logger.Errorf("[API] Failed to get equity history for %s: %v", traderID, err)
errors[traderID] = "Failed to get historical data"
continue
}

View File

@@ -4,11 +4,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"nofx/decision"
"nofx/kernel"
"nofx/logger"
"nofx/market"
"nofx/mcp"
"nofx/store"
"strings"
"time"
"github.com/gin-gonic/gin"
@@ -19,11 +19,11 @@ import (
func validateStrategyConfig(config *store.StrategyConfig) []string {
var warnings []string
// Validate quant data URL if enabled
if config.Indicators.EnableQuantData && config.Indicators.QuantDataAPIURL != "" {
if !strings.Contains(config.Indicators.QuantDataAPIURL, "{symbol}") {
warnings = append(warnings, "Quant data URL does not contain {symbol} placeholder. The same data will be used for all coins, which may not be correct.")
}
// Validate NofxOS API key if any NofxOS feature is enabled
if (config.Indicators.EnableQuantData || config.Indicators.EnableOIRanking ||
config.Indicators.EnableNetFlowRanking || config.Indicators.EnablePriceRanking) &&
config.Indicators.NofxOSAPIKey == "" {
warnings = append(warnings, "NofxOS API key is not configured. NofxOS data sources may not work properly.")
}
return warnings
@@ -33,7 +33,7 @@ func validateStrategyConfig(config *store.StrategyConfig) []string {
func (s *Server) handlePublicStrategies(c *gin.Context) {
strategies, err := s.store.Strategy().ListPublic()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get public strategies: " + err.Error()})
SafeInternalError(c, "Failed to get public strategies", err)
return
}
@@ -76,7 +76,7 @@ func (s *Server) handleGetStrategies(c *gin.Context) {
strategies, err := s.store.Strategy().List(userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get strategy list: " + err.Error()})
SafeInternalError(c, "Failed to get strategy list", err)
return
}
@@ -151,14 +151,14 @@ func (s *Server) handleCreateStrategy(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request parameters: " + err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
// Serialize configuration
configJSON, err := json.Marshal(req.Config)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to serialize configuration"})
SafeInternalError(c, "Serialize configuration", err)
return
}
@@ -173,7 +173,7 @@ func (s *Server) handleCreateStrategy(c *gin.Context) {
}
if err := s.store.Strategy().Create(strategy); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create strategy: " + err.Error()})
SafeInternalError(c, "Failed to create strategy", err)
return
}
@@ -221,14 +221,14 @@ func (s *Server) handleUpdateStrategy(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request parameters: " + err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
// Serialize configuration
configJSON, err := json.Marshal(req.Config)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to serialize configuration"})
SafeInternalError(c, "Serialize configuration", err)
return
}
@@ -243,7 +243,7 @@ func (s *Server) handleUpdateStrategy(c *gin.Context) {
}
if err := s.store.Strategy().Update(strategy); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update strategy: " + err.Error()})
SafeInternalError(c, "Failed to update strategy", err)
return
}
@@ -269,7 +269,7 @@ func (s *Server) handleDeleteStrategy(c *gin.Context) {
}
if err := s.store.Strategy().Delete(userID, strategyID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete strategy: " + err.Error()})
SafeInternalError(c, "Failed to delete strategy", err)
return
}
@@ -287,7 +287,7 @@ func (s *Server) handleActivateStrategy(c *gin.Context) {
}
if err := s.store.Strategy().SetActive(userID, strategyID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to activate strategy: " + err.Error()})
SafeInternalError(c, "Failed to activate strategy", err)
return
}
@@ -309,13 +309,13 @@ func (s *Server) handleDuplicateStrategy(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request parameters: " + err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
newID := uuid.New().String()
if err := s.store.Strategy().Duplicate(userID, sourceID, newID, req.Name); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to duplicate strategy: " + err.Error()})
SafeInternalError(c, "Failed to duplicate strategy", err)
return
}
@@ -383,7 +383,7 @@ func (s *Server) handlePreviewPrompt(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request parameters: " + err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -396,7 +396,7 @@ func (s *Server) handlePreviewPrompt(c *gin.Context) {
}
// Create strategy engine to build prompt
engine := decision.NewStrategyEngine(&req.Config)
engine := kernel.NewStrategyEngine(&req.Config)
// Build system prompt (using built-in method from strategy engine)
systemPrompt := engine.BuildSystemPrompt(
@@ -433,7 +433,7 @@ func (s *Server) handleStrategyTestRun(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request parameters: " + err.Error()})
SafeBadRequest(c, "Invalid request parameters")
return
}
@@ -442,13 +442,14 @@ func (s *Server) handleStrategyTestRun(c *gin.Context) {
}
// Create strategy engine to build prompt
engine := decision.NewStrategyEngine(&req.Config)
engine := kernel.NewStrategyEngine(&req.Config)
// Get candidate coins
candidates, err := engine.GetCandidateCoins()
if err != nil {
logger.Errorf("[API Error] Failed to get candidate coins: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to get candidate coins: " + err.Error(),
"error": "Failed to get candidate coins",
"ai_response": "",
})
return
@@ -502,12 +503,18 @@ func (s *Server) handleStrategyTestRun(c *gin.Context) {
// Fetch OI ranking data (market-wide position changes)
oiRankingData := engine.FetchOIRankingData()
// Fetch NetFlow ranking data (market-wide fund flow)
netFlowRankingData := engine.FetchNetFlowRankingData()
// Fetch Price ranking data (market-wide gainers/losers)
priceRankingData := engine.FetchPriceRankingData()
// Build real context (for generating User Prompt)
testContext := &decision.Context{
testContext := &kernel.Context{
CurrentTime: time.Now().UTC().Format("2006-01-02 15:04:05 UTC"),
RuntimeMinutes: 0,
CallCount: 1,
Account: decision.AccountInfo{
Account: kernel.AccountInfo{
TotalEquity: 1000.0,
AvailableBalance: 1000.0,
UnrealizedPnL: 0,
@@ -517,12 +524,14 @@ func (s *Server) handleStrategyTestRun(c *gin.Context) {
MarginUsedPct: 0,
PositionCount: 0,
},
Positions: []decision.PositionInfo{},
CandidateCoins: candidates,
PromptVariant: req.PromptVariant,
MarketDataMap: marketDataMap,
QuantDataMap: quantDataMap,
OIRankingData: oiRankingData,
Positions: []kernel.PositionInfo{},
CandidateCoins: candidates,
PromptVariant: req.PromptVariant,
MarketDataMap: marketDataMap,
QuantDataMap: quantDataMap,
OIRankingData: oiRankingData,
NetFlowRankingData: netFlowRankingData,
PriceRankingData: priceRankingData,
}
// Build System Prompt

View File

@@ -122,10 +122,10 @@ func (acc *BacktestAccount) Close(symbol, side string, quantity float64, price f
}
execPrice := applySlippage(price, acc.slippageRate, side, false)
notional := execPrice * quantity
closingFee := notional * acc.feeRate
closeNotional := execPrice * quantity // Notional at close price (for fee calculation)
closingFee := closeNotional * acc.feeRate
// Calculate proportional opening fee for the quantity being closed
// Calculate proportional values based on the portion being closed
closePortion := quantity / pos.Quantity
openingFeePortion := pos.AccumulatedFee * closePortion
totalFee := closingFee + openingFeePortion
@@ -133,13 +133,17 @@ func (acc *BacktestAccount) Close(symbol, side string, quantity float64, price f
realized := realizedPnL(pos, quantity, execPrice)
marginPortion := pos.Margin * closePortion
// BUG FIX: Calculate notional portion based on ENTRY price, not close price
// pos.Notional tracks the total notional at entry, so we must subtract proportionally
entryNotionalPortion := pos.Notional * closePortion
// Note: Opening fee was already deducted from cash when opening, so we only deduct closing fee here
acc.cash += marginPortion + realized - closingFee
// But for realized P&L tracking, we include both fees
acc.realizedPnL += realized - totalFee
pos.Quantity -= quantity
pos.Notional -= notional
pos.Notional -= entryNotionalPortion // FIX: Use entry notional portion, not close notional
pos.Margin -= marginPortion
pos.AccumulatedFee -= openingFeePortion // Reduce tracked opening fee

View File

@@ -9,7 +9,7 @@ import (
"path/filepath"
"sync"
"nofx/decision"
"nofx/kernel"
"nofx/market"
)
@@ -17,7 +17,7 @@ type cachedDecision struct {
Key string `json:"key"`
PromptVariant string `json:"prompt_variant"`
Timestamp int64 `json:"ts"`
Decision *decision.FullDecision `json:"decision"`
Decision *kernel.FullDecision `json:"decision"`
}
// AICache persists AI decisions for repeated backtesting or replay.
@@ -67,7 +67,7 @@ func (c *AICache) Path() string {
return c.path
}
func (c *AICache) Get(key string) (*decision.FullDecision, bool) {
func (c *AICache) Get(key string) (*kernel.FullDecision, bool) {
if c == nil || key == "" {
return nil, false
}
@@ -80,7 +80,7 @@ func (c *AICache) Get(key string) (*decision.FullDecision, bool) {
return cloneDecision(entry.Decision), true
}
func (c *AICache) Put(key string, variant string, ts int64, decision *decision.FullDecision) error {
func (c *AICache) Put(key string, variant string, ts int64, decision *kernel.FullDecision) error {
if c == nil || key == "" || decision == nil {
return nil
}
@@ -109,7 +109,7 @@ func (c *AICache) save() error {
return writeFileAtomic(c.path, data, 0o644)
}
func cloneDecision(src *decision.FullDecision) *decision.FullDecision {
func cloneDecision(src *kernel.FullDecision) *kernel.FullDecision {
if src == nil {
return nil
}
@@ -117,14 +117,14 @@ func cloneDecision(src *decision.FullDecision) *decision.FullDecision {
if err != nil {
return nil
}
var dst decision.FullDecision
var dst kernel.FullDecision
if err := json.Unmarshal(data, &dst); err != nil {
return nil
}
return &dst
}
func computeCacheKey(ctx *decision.Context, variant string, ts int64) (string, error) {
func computeCacheKey(ctx *kernel.Context, variant string, ts int64) (string, error) {
if ctx == nil {
return "", fmt.Errorf("context is nil")
}
@@ -132,9 +132,9 @@ func computeCacheKey(ctx *decision.Context, variant string, ts int64) (string, e
Variant string `json:"variant"`
Timestamp int64 `json:"ts"`
CurrentTime string `json:"current_time"`
Account decision.AccountInfo `json:"account"`
Positions []decision.PositionInfo `json:"positions"`
CandidateCoins []decision.CandidateCoin `json:"candidate_coins"`
Account kernel.AccountInfo `json:"account"`
Positions []kernel.PositionInfo `json:"positions"`
CandidateCoins []kernel.CandidateCoin `json:"candidate_coins"`
MarketData map[string]market.Data `json:"market"`
MarginUsedPct float64 `json:"margin_used_pct"`
Runtime int `json:"runtime_minutes"`

View File

@@ -199,7 +199,7 @@ func (cfg *BacktestConfig) ToStrategyConfig() *store.StrategyConfig {
if len(cfg.Symbols) > 0 {
result.CoinSource.SourceType = "static"
result.CoinSource.StaticCoins = cfg.Symbols
result.CoinSource.UseCoinPool = false
result.CoinSource.UseAI500 = false
result.CoinSource.UseOITop = false
}
@@ -241,12 +241,12 @@ func (cfg *BacktestConfig) ToStrategyConfig() *store.StrategyConfig {
return &store.StrategyConfig{
CoinSource: store.CoinSourceConfig{
SourceType: "static",
StaticCoins: cfg.Symbols,
UseCoinPool: false,
CoinPoolLimit: len(cfg.Symbols),
UseOITop: false,
OITopLimit: 0,
SourceType: "static",
StaticCoins: cfg.Symbols,
UseAI500: false,
AI500Limit: len(cfg.Symbols),
UseOITop: false,
OITopLimit: 0,
},
Indicators: store.IndicatorConfig{
Klines: store.KlineConfig{

View File

@@ -124,11 +124,23 @@ func (df *DataFeed) DecisionBarCount() int {
}
func (df *DataFeed) DecisionTimestamp(index int) int64 {
// Bounds check to prevent panic
if index < 0 || index >= len(df.decisionTimes) {
return 0
}
return df.decisionTimes[index]
}
func (df *DataFeed) sliceUpTo(symbol, tf string, ts int64) []market.Kline {
series := df.symbolSeries[symbol].byTF[tf]
// Nil checks to prevent panic
ss, ok := df.symbolSeries[symbol]
if !ok || ss == nil {
return nil
}
series, ok := ss.byTF[tf]
if !ok || series == nil {
return nil
}
idx := sort.Search(len(series.closeTimes), func(i int) bool {
return series.closeTimes[i] > ts
})

View File

@@ -91,8 +91,13 @@ func maxDrawdown(points []EquityPoint, state *BacktestState) float64 {
return maxDD
}
// sharpeRatio calculates the Sharpe ratio from equity points.
// Uses sample standard deviation (n-1) and annualizes assuming ~252 trading days.
// Returns math.NaN() for edge cases (insufficient data, zero variance).
func sharpeRatio(points []EquityPoint) float64 {
if len(points) < 2 {
// Need at least 10 data points for meaningful Sharpe calculation
const minDataPoints = 10
if len(points) < minDataPoints {
return 0
}
@@ -108,34 +113,42 @@ func sharpeRatio(points []EquityPoint) float64 {
returns = append(returns, ret)
prev = curr
}
if len(returns) == 0 {
if len(returns) < minDataPoints-1 {
return 0
}
// Calculate mean return
mean := 0.0
for _, r := range returns {
mean += r
}
mean /= float64(len(returns))
// Calculate sample variance (using n-1 for unbiased estimator)
variance := 0.0
for _, r := range returns {
diff := r - mean
variance += diff * diff
}
variance /= float64(len(returns))
if len(returns) > 1 {
variance /= float64(len(returns) - 1)
}
std := math.Sqrt(variance)
if std == 0 {
if mean > 0 {
return 999
}
if mean < 0 {
return -999
}
if std < 1e-10 {
// Zero or near-zero volatility - return 0 instead of infinity/NaN
return 0
}
return mean / std
// Calculate Sharpe ratio (assuming risk-free rate = 0 for crypto)
// Annualize by multiplying by sqrt(periods per year)
// Assuming each equity point represents ~1 hour, we have ~8760 periods/year
// For conservative estimate, use sqrt(252) as if daily returns
periodsPerYear := 252.0
annualizationFactor := math.Sqrt(periodsPerYear)
sharpe := (mean / std) * annualizationFactor
return sharpe
}
func fillTradeMetrics(metrics *Metrics, events []TradeEvent) {
@@ -189,7 +202,8 @@ func fillTradeMetrics(metrics *Metrics, events []TradeEvent) {
if totalLossAmount > 0 {
metrics.ProfitFactor = totalWinAmount / totalLossAmount
} else if totalWinAmount > 0 {
metrics.ProfitFactor = 999
// No losses but have wins - use a high but reasonable cap
metrics.ProfitFactor = 100.0
}
bestSymbol := ""

View File

@@ -2,15 +2,39 @@ package backtest
import (
"database/sql"
"fmt"
"strings"
)
var persistenceDB *sql.DB
var dbIsPostgres bool
// UseDatabase enables database-backed persistence for all backtest storage operations.
// If isPostgres is true, queries will use $1, $2... placeholders instead of ?
func UseDatabase(db *sql.DB) {
persistenceDB = db
}
// UseDatabaseWithType enables database-backed persistence with explicit type.
func UseDatabaseWithType(db *sql.DB, isPostgres bool) {
persistenceDB = db
dbIsPostgres = isPostgres
}
func usingDB() bool {
return persistenceDB != nil
}
// convertQuery converts ? placeholders to $1, $2, etc. for PostgreSQL
func convertQuery(query string) string {
if !dbIsPostgres {
return query
}
result := query
index := 1
for strings.Contains(result, "?") {
result = strings.Replace(result, "?", fmt.Sprintf("$%d", index), 1)
index++
}
return result
}

View File

@@ -73,12 +73,12 @@ func enforceRetentionDB(maxRuns int) {
RunStateFailed,
RunStateLiquidated,
}
query := `
query := convertQuery(`
SELECT run_id FROM backtest_runs
WHERE state IN (?, ?, ?, ?)
ORDER BY updated_at DESC
OFFSET ?
`
`)
rows, err := persistenceDB.Query(query,
finalStates[0], finalStates[1], finalStates[2], finalStates[3], maxRuns)
if err != nil {

View File

@@ -13,7 +13,7 @@ import (
"sync"
"time"
"nofx/decision"
"nofx/kernel"
"nofx/market"
"nofx/mcp"
"nofx/store"
@@ -34,7 +34,7 @@ type Runner struct {
cfg BacktestConfig
feed *DataFeed
account *BacktestAccount
strategyEngine *decision.StrategyEngine
strategyEngine *kernel.StrategyEngine
decisionLogDir string
mcpClient mcp.AIClient
@@ -60,8 +60,9 @@ type Runner struct {
aiCache *AICache
cachePath string
lockInfo *RunLockInfo
lockStop chan struct{}
lockInfo *RunLockInfo
lockStop chan struct{}
lockStopOnce sync.Once // Ensures lockStop is closed only once
}
// NewRunner constructs a backtest runner.
@@ -118,7 +119,7 @@ func NewRunner(cfg BacktestConfig, mcpClient mcp.AIClient) (*Runner, error) {
// Create strategy engine from backtest config for unified prompt generation
strategyConfig := cfg.ToStrategyConfig()
strategyEngine := decision.NewStrategyEngine(strategyConfig)
strategyEngine := kernel.NewStrategyEngine(strategyConfig)
r := &Runner{
cfg: cfg,
@@ -175,10 +176,12 @@ func (r *Runner) lockHeartbeatLoop() {
}
func (r *Runner) releaseLock() {
if r.lockStop != nil {
close(r.lockStop)
r.lockStop = nil
}
// Use sync.Once to ensure channel is closed exactly once, preventing panic on double-close
r.lockStopOnce.Do(func() {
if r.lockStop != nil {
close(r.lockStop)
}
})
if err := deleteRunLock(r.cfg.RunID); err != nil {
logger.Infof("failed to release lock for %s: %v", r.cfg.RunID, err)
}
@@ -297,15 +300,18 @@ func (r *Runner) stepOnce() error {
if shouldDecide {
ctx, rec, err := r.buildDecisionContext(ts, marketData, multiTF, priceMap, callCount)
if err != nil {
rec.Success = false
rec.ErrorMessage = fmt.Sprintf("failed to build trading context: %v", err)
_ = r.logDecision(rec)
// Defensive nil check to prevent panic if buildDecisionContext returns error with nil record
if rec != nil {
rec.Success = false
rec.ErrorMessage = fmt.Sprintf("failed to build trading context: %v", err)
_ = r.logDecision(rec)
}
return err
}
record = rec
var (
fullDecision *decision.FullDecision
fullDecision *kernel.FullDecision
fromCache bool
cacheKey string
)
@@ -470,7 +476,7 @@ func (r *Runner) stepOnce() error {
return nil
}
func (r *Runner) buildDecisionContext(ts int64, marketData map[string]*market.Data, multiTF map[string]map[string]*market.Data, priceMap map[string]float64, callCount int) (*decision.Context, *store.DecisionRecord, error) {
func (r *Runner) buildDecisionContext(ts int64, marketData map[string]*market.Data, multiTF map[string]map[string]*market.Data, priceMap map[string]float64, callCount int) (*kernel.Context, *store.DecisionRecord, error) {
equity, unrealized, _ := r.account.TotalEquity(priceMap)
available := r.account.Cash()
marginUsed := r.totalMarginUsed()
@@ -479,7 +485,7 @@ func (r *Runner) buildDecisionContext(ts int64, marketData map[string]*market.Da
marginPct = (marginUsed / equity) * 100
}
accountInfo := decision.AccountInfo{
accountInfo := kernel.AccountInfo{
TotalEquity: equity,
AvailableBalance: available,
TotalPnL: equity - r.account.InitialBalance(),
@@ -495,14 +501,14 @@ func (r *Runner) buildDecisionContext(ts int64, marketData map[string]*market.Da
candidateCoins, err := r.strategyEngine.GetCandidateCoins()
if err != nil {
// Fallback to simple list if strategy engine fails
candidateCoins = make([]decision.CandidateCoin, 0, len(r.cfg.Symbols))
candidateCoins = make([]kernel.CandidateCoin, 0, len(r.cfg.Symbols))
for _, sym := range r.cfg.Symbols {
candidateCoins = append(candidateCoins, decision.CandidateCoin{Symbol: sym, Sources: []string{"backtest"}})
candidateCoins = append(candidateCoins, kernel.CandidateCoin{Symbol: sym, Sources: []string{"backtest"}})
}
}
runtime := int((ts - int64(r.cfg.StartTS*1000)) / 60000)
ctx := &decision.Context{
ctx := &kernel.Context{
CurrentTime: time.UnixMilli(ts).UTC().Format("2006-01-02 15:04:05 UTC"),
RuntimeMinutes: runtime,
CallCount: callCount,
@@ -519,7 +525,7 @@ func (r *Runner) buildDecisionContext(ts int64, marketData map[string]*market.Da
// Fetch quantitative data if enabled in strategy (uses current data as approximation)
strategyConfig := r.strategyEngine.GetConfig()
if strategyConfig.Indicators.EnableQuantData && strategyConfig.Indicators.QuantDataAPIURL != "" {
if strategyConfig.Indicators.EnableQuantData {
// Collect symbols to query (candidate coins + position coins)
symbolSet := make(map[string]bool)
for _, sym := range r.cfg.Symbols {
@@ -547,6 +553,24 @@ func (r *Runner) buildDecisionContext(ts int64, marketData map[string]*market.Da
}
}
// Fetch NetFlow ranking data if enabled in strategy
if strategyConfig.Indicators.EnableNetFlowRanking {
ctx.NetFlowRankingData = r.strategyEngine.FetchNetFlowRankingData()
if ctx.NetFlowRankingData != nil {
logger.Infof("💰 Backtest: NetFlow ranking data ready: inst_in=%d, inst_out=%d",
len(ctx.NetFlowRankingData.InstitutionFutureTop), len(ctx.NetFlowRankingData.InstitutionFutureLow))
}
}
// Fetch Price ranking data if enabled in strategy
if strategyConfig.Indicators.EnablePriceRanking {
ctx.PriceRankingData = r.strategyEngine.FetchPriceRankingData()
if ctx.PriceRankingData != nil {
logger.Infof("📈 Backtest: Price ranking data ready for %d durations",
len(ctx.PriceRankingData.Durations))
}
}
record := &store.DecisionRecord{
AccountState: store.AccountSnapshot{
TotalBalance: accountInfo.TotalEquity,
@@ -566,7 +590,7 @@ func (r *Runner) buildDecisionContext(ts int64, marketData map[string]*market.Da
return ctx, record, nil
}
func (r *Runner) fillDecisionRecord(record *store.DecisionRecord, full *decision.FullDecision) {
func (r *Runner) fillDecisionRecord(record *store.DecisionRecord, full *kernel.FullDecision) {
record.InputPrompt = full.UserPrompt
record.CoTTrace = full.CoTTrace
if len(full.Decisions) > 0 {
@@ -576,12 +600,12 @@ func (r *Runner) fillDecisionRecord(record *store.DecisionRecord, full *decision
}
}
func (r *Runner) invokeAIWithRetry(ctx *decision.Context) (*decision.FullDecision, error) {
func (r *Runner) invokeAIWithRetry(ctx *kernel.Context) (*kernel.FullDecision, error) {
var lastErr error
for attempt := 0; attempt < aiDecisionMaxRetries; attempt++ {
// Use GetFullDecisionWithStrategy with the pre-configured strategy engine
// This ensures backtest uses the same unified prompt generation as live trading
fd, err := decision.GetFullDecisionWithStrategy(
fd, err := kernel.GetFullDecisionWithStrategy(
ctx,
r.mcpClient,
r.strategyEngine,
@@ -597,8 +621,12 @@ func (r *Runner) invokeAIWithRetry(ctx *decision.Context) (*decision.FullDecisio
return nil, lastErr
}
func (r *Runner) executeDecision(dec decision.Decision, priceMap map[string]float64, ts int64, cycle int) (store.DecisionAction, []TradeEvent, string, error) {
func (r *Runner) executeDecision(dec kernel.Decision, priceMap map[string]float64, ts int64, cycle int) (store.DecisionAction, []TradeEvent, string, error) {
symbol := dec.Symbol
if symbol == "" {
return store.DecisionAction{}, nil, "", fmt.Errorf("empty symbol in decision")
}
usedLeverage := r.resolveLeverage(dec.Leverage, symbol)
actionRecord := store.DecisionAction{
Action: dec.Action,
@@ -607,9 +635,13 @@ func (r *Runner) executeDecision(dec decision.Decision, priceMap map[string]floa
Timestamp: time.UnixMilli(ts).UTC(),
}
basePrice := priceMap[symbol]
if basePrice <= 0 {
return actionRecord, nil, "", fmt.Errorf("price unavailable for %s", symbol)
if priceMap == nil {
return actionRecord, nil, "", fmt.Errorf("priceMap is nil")
}
basePrice, ok := priceMap[symbol]
if !ok || basePrice <= 0 {
return actionRecord, nil, "", fmt.Errorf("price unavailable for %s (found=%v, price=%.4f)", symbol, ok, basePrice)
}
fillPrice := r.executionPrice(symbol, basePrice, ts)
@@ -739,7 +771,10 @@ func (r *Runner) executeDecision(dec decision.Decision, priceMap map[string]floa
}
}
func (r *Runner) determineQuantity(dec decision.Decision, price float64) float64 {
// MinPositionSizeUSD is the minimum position size in USD to avoid dust positions
const MinPositionSizeUSD = 10.0
func (r *Runner) determineQuantity(dec kernel.Decision, price float64) float64 {
snapshot := r.snapshotState()
equity := snapshot.Equity
if equity <= 0 {
@@ -770,6 +805,13 @@ func (r *Runner) determineQuantity(dec decision.Decision, price float64) float64
sizeUSD = maxPositionValue
}
// Reject positions below minimum size to avoid dust positions
if sizeUSD < MinPositionSizeUSD {
logger.Infof("📊 Backtest: rejecting position size %.2f USD (below minimum %.2f USD)",
sizeUSD, MinPositionSizeUSD)
return 0
}
qty := sizeUSD / price
if qty < 0 {
qty = 0
@@ -777,7 +819,7 @@ func (r *Runner) determineQuantity(dec decision.Decision, price float64) float64
return qty
}
func (r *Runner) determineCloseQuantity(symbol, side string, dec decision.Decision) float64 {
func (r *Runner) determineCloseQuantity(symbol, side string, dec kernel.Decision) float64 {
for _, pos := range r.account.Positions() {
if pos.Symbol == strings.ToUpper(symbol) && pos.Side == side {
return pos.Quantity
@@ -787,20 +829,37 @@ func (r *Runner) determineCloseQuantity(symbol, side string, dec decision.Decisi
}
func (r *Runner) resolveLeverage(requested int, symbol string) int {
if requested > 0 {
return requested
}
sym := strings.ToUpper(symbol)
if sym == "BTCUSDT" || sym == "ETHUSDT" {
if r.cfg.Leverage.BTCETHLeverage > 0 {
return r.cfg.Leverage.BTCETHLeverage
isBTCETH := sym == "BTCUSDT" || sym == "ETHUSDT"
// Determine configured max leverage for this symbol type
var maxLeverage int
if isBTCETH {
maxLeverage = r.cfg.Leverage.BTCETHLeverage
if maxLeverage <= 0 {
maxLeverage = 10 // Default max for BTC/ETH
}
} else {
if r.cfg.Leverage.AltcoinLeverage > 0 {
return r.cfg.Leverage.AltcoinLeverage
maxLeverage = r.cfg.Leverage.AltcoinLeverage
if maxLeverage <= 0 {
maxLeverage = 5 // Default max for altcoins
}
}
return 5
// Use requested leverage if provided, otherwise use max as default
leverage := requested
if leverage <= 0 {
leverage = maxLeverage
}
// Enforce max leverage limit
if leverage > maxLeverage {
logger.Infof("📊 Backtest: capping leverage from %dx to %dx for %s",
leverage, maxLeverage, symbol)
leverage = maxLeverage
}
return leverage
}
func (r *Runner) remainingPosition(symbol, side string) float64 {
@@ -831,20 +890,26 @@ func (r *Runner) snapshotPositions(priceMap map[string]float64) []store.Position
return list
}
func (r *Runner) convertPositions(priceMap map[string]float64) []decision.PositionInfo {
func (r *Runner) convertPositions(priceMap map[string]float64) []kernel.PositionInfo {
positions := r.account.Positions()
list := make([]decision.PositionInfo, 0, len(positions))
list := make([]kernel.PositionInfo, 0, len(positions))
for _, pos := range positions {
price := priceMap[pos.Symbol]
list = append(list, decision.PositionInfo{
pnl := unrealizedPnL(pos, price)
// Calculate P&L percentage based on entry notional (position cost)
pnlPct := 0.0
if pos.Notional > 0 {
pnlPct = (pnl / pos.Notional) * 100
}
list = append(list, kernel.PositionInfo{
Symbol: pos.Symbol,
Side: pos.Side,
EntryPrice: pos.EntryPrice,
MarkPrice: price,
Quantity: pos.Quantity,
Leverage: pos.Leverage,
UnrealizedPnL: unrealizedPnL(pos, price),
UnrealizedPnLPct: 0,
UnrealizedPnL: pnl,
UnrealizedPnLPct: pnlPct,
LiquidationPrice: pos.LiquidationPrice,
MarginUsed: pos.Margin,
UpdateTime: time.Now().UnixMilli(),
@@ -1416,7 +1481,7 @@ func snapshotsToMap(snaps []PositionSnapshot) map[string]PositionSnapshot {
return positions
}
func sortDecisionsByPriority(decisions []decision.Decision) []decision.Decision {
func sortDecisionsByPriority(decisions []kernel.Decision) []kernel.Decision {
if len(decisions) <= 1 {
return decisions
}
@@ -1434,7 +1499,7 @@ func sortDecisionsByPriority(decisions []decision.Decision) []decision.Decision
}
}
result := make([]decision.Decision, len(decisions))
result := make([]kernel.Decision, len(decisions))
copy(result, decisions)
sort.Slice(result, func(i, j int) bool {

View File

@@ -17,17 +17,17 @@ func saveCheckpointDB(runID string, ckpt *Checkpoint) error {
if err != nil {
return err
}
_, err = persistenceDB.Exec(`
_, err = persistenceDB.Exec(convertQuery(`
INSERT INTO backtest_checkpoints (run_id, payload, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(run_id) DO UPDATE SET payload=excluded.payload, updated_at=CURRENT_TIMESTAMP
`, runID, data)
`), runID, data)
return err
}
func loadCheckpointDB(runID string) (*Checkpoint, error) {
var payload []byte
err := persistenceDB.QueryRow(`SELECT payload FROM backtest_checkpoints WHERE run_id = ?`, runID).Scan(&payload)
err := persistenceDB.QueryRow(convertQuery(`SELECT payload FROM backtest_checkpoints WHERE run_id = ?`), runID).Scan(&payload)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, os.ErrNotExist
@@ -57,25 +57,25 @@ func saveConfigDB(runID string, cfg *BacktestConfig) error {
if userID == "" {
userID = "default"
}
_, err = persistenceDB.Exec(`
_, err = persistenceDB.Exec(convertQuery(`
INSERT INTO backtest_runs (run_id, user_id, config_json, prompt_template, custom_prompt, override_prompt, ai_provider, ai_model, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(run_id) DO NOTHING
`, runID, userID, data, template, cfg.CustomPrompt, cfg.OverrideBasePrompt, cfg.AICfg.Provider, cfg.AICfg.Model, now, now)
`), runID, userID, data, template, cfg.CustomPrompt, cfg.OverrideBasePrompt, cfg.AICfg.Provider, cfg.AICfg.Model, now, now)
if err != nil {
return err
}
_, err = persistenceDB.Exec(`
_, err = persistenceDB.Exec(convertQuery(`
UPDATE backtest_runs
SET user_id = ?, config_json = ?, prompt_template = ?, custom_prompt = ?, override_prompt = ?, ai_provider = ?, ai_model = ?, updated_at = CURRENT_TIMESTAMP
WHERE run_id = ?
`, userID, data, template, cfg.CustomPrompt, cfg.OverrideBasePrompt, cfg.AICfg.Provider, cfg.AICfg.Model, runID)
`), userID, data, template, cfg.CustomPrompt, cfg.OverrideBasePrompt, cfg.AICfg.Provider, cfg.AICfg.Model, runID)
return err
}
func loadConfigDB(runID string) (*BacktestConfig, error) {
var payload []byte
err := persistenceDB.QueryRow(`SELECT config_json FROM backtest_runs WHERE run_id = ?`, runID).Scan(&payload)
err := persistenceDB.QueryRow(convertQuery(`SELECT config_json FROM backtest_runs WHERE run_id = ?`), runID).Scan(&payload)
if err != nil {
return nil, err
}
@@ -96,18 +96,18 @@ func saveRunMetadataDB(meta *RunMetadata) error {
if userID == "" {
userID = "default"
}
if _, err := persistenceDB.Exec(`
if _, err := persistenceDB.Exec(convertQuery(`
INSERT INTO backtest_runs (run_id, user_id, label, last_error, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(run_id) DO NOTHING
`, meta.RunID, userID, meta.Label, meta.LastError, created, updated); err != nil {
`), meta.RunID, userID, meta.Label, meta.LastError, created, updated); err != nil {
return err
}
_, err := persistenceDB.Exec(`
_, err := persistenceDB.Exec(convertQuery(`
UPDATE backtest_runs
SET user_id = ?, state = ?, symbol_count = ?, decision_tf = ?, processed_bars = ?, progress_pct = ?, equity_last = ?, max_drawdown_pct = ?, liquidated = ?, liquidation_note = ?, label = ?, last_error = ?, updated_at = ?
WHERE run_id = ?
`, userID, string(meta.State), meta.Summary.SymbolCount, meta.Summary.DecisionTF, meta.Summary.ProcessedBars, meta.Summary.ProgressPct, meta.Summary.EquityLast, meta.Summary.MaxDrawdownPct, meta.Summary.Liquidated, meta.Summary.LiquidationNote, meta.Label, meta.LastError, updated, meta.RunID)
`), userID, string(meta.State), meta.Summary.SymbolCount, meta.Summary.DecisionTF, meta.Summary.ProcessedBars, meta.Summary.ProgressPct, meta.Summary.EquityLast, meta.Summary.MaxDrawdownPct, meta.Summary.Liquidated, meta.Summary.LiquidationNote, meta.Label, meta.LastError, updated, meta.RunID)
return err
}
@@ -128,10 +128,10 @@ func loadRunMetadataDB(runID string) (*RunMetadata, error) {
createdISO string
updatedISO string
)
err := persistenceDB.QueryRow(`
err := persistenceDB.QueryRow(convertQuery(`
SELECT user_id, state, label, last_error, symbol_count, decision_tf, processed_bars, progress_pct, equity_last, max_drawdown_pct, liquidated, liquidation_note, created_at, updated_at
FROM backtest_runs WHERE run_id = ?
`, runID).Scan(&userID, &state, &label, &lastErr, &symbolCount, &decisionTF, &processedBars, &progressPct, &equityLast, &maxDD, &liquidated, &liquidationNote, &createdISO, &updatedISO)
`), runID).Scan(&userID, &state, &label, &lastErr, &symbolCount, &decisionTF, &processedBars, &progressPct, &equityLast, &maxDD, &liquidated, &liquidationNote, &createdISO, &updatedISO)
if err != nil {
return nil, err
}
@@ -183,18 +183,18 @@ func loadRunIDsDB() ([]string, error) {
}
func appendEquityPointDB(runID string, point EquityPoint) error {
_, err := persistenceDB.Exec(`
_, err := persistenceDB.Exec(convertQuery(`
INSERT INTO backtest_equity (run_id, ts, equity, available, pnl, pnl_pct, dd_pct, cycle)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, runID, point.Timestamp, point.Equity, point.Available, point.PnL, point.PnLPct, point.DrawdownPct, point.Cycle)
`), runID, point.Timestamp, point.Equity, point.Available, point.PnL, point.PnLPct, point.DrawdownPct, point.Cycle)
return err
}
func loadEquityPointsDB(runID string) ([]EquityPoint, error) {
rows, err := persistenceDB.Query(`
rows, err := persistenceDB.Query(convertQuery(`
SELECT ts, equity, available, pnl, pnl_pct, dd_pct, cycle
FROM backtest_equity WHERE run_id = ? ORDER BY ts ASC
`, runID)
`), runID)
if err != nil {
return nil, err
}
@@ -211,18 +211,18 @@ func loadEquityPointsDB(runID string) ([]EquityPoint, error) {
}
func appendTradeEventDB(runID string, event TradeEvent) error {
_, err := persistenceDB.Exec(`
_, err := persistenceDB.Exec(convertQuery(`
INSERT INTO backtest_trades (run_id, ts, symbol, action, side, qty, price, fee, slippage, order_value, realized_pnl, leverage, cycle, position_after, liquidation, note)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, runID, event.Timestamp, event.Symbol, event.Action, event.Side, event.Quantity, event.Price, event.Fee, event.Slippage, event.OrderValue, event.RealizedPnL, event.Leverage, event.Cycle, event.PositionAfter, event.LiquidationFlag, event.Note)
`), runID, event.Timestamp, event.Symbol, event.Action, event.Side, event.Quantity, event.Price, event.Fee, event.Slippage, event.OrderValue, event.RealizedPnL, event.Leverage, event.Cycle, event.PositionAfter, event.LiquidationFlag, event.Note)
return err
}
func loadTradeEventsDB(runID string) ([]TradeEvent, error) {
rows, err := persistenceDB.Query(`
rows, err := persistenceDB.Query(convertQuery(`
SELECT ts, symbol, action, side, qty, price, fee, slippage, order_value, realized_pnl, leverage, cycle, position_after, liquidation, note
FROM backtest_trades WHERE run_id = ? ORDER BY ts ASC
`, runID)
`), runID)
if err != nil {
return nil, err
}
@@ -243,17 +243,17 @@ func saveMetricsDB(runID string, metrics *Metrics) error {
if err != nil {
return err
}
_, err = persistenceDB.Exec(`
_, err = persistenceDB.Exec(convertQuery(`
INSERT INTO backtest_metrics (run_id, payload, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(run_id) DO UPDATE SET payload=excluded.payload, updated_at=CURRENT_TIMESTAMP
`, runID, data)
`), runID, data)
return err
}
func loadMetricsDB(runID string) (*Metrics, error) {
var payload []byte
err := persistenceDB.QueryRow(`SELECT payload FROM backtest_metrics WHERE run_id = ?`, runID).Scan(&payload)
err := persistenceDB.QueryRow(convertQuery(`SELECT payload FROM backtest_metrics WHERE run_id = ?`), runID).Scan(&payload)
if err != nil {
return nil, err
}
@@ -265,22 +265,21 @@ func loadMetricsDB(runID string) (*Metrics, error) {
}
func saveProgressDB(runID string, payload progressPayload) error {
_, err := persistenceDB.Exec(`
_, err := persistenceDB.Exec(convertQuery(`
UPDATE backtest_runs
SET progress_pct = ?, equity_last = ?, processed_bars = ?, liquidated = ?, updated_at = ?
WHERE run_id = ?
`, payload.ProgressPct, payload.Equity, payload.BarIndex, payload.Liquidated, payload.UpdatedAtISO, runID)
`), payload.ProgressPct, payload.Equity, payload.BarIndex, payload.Liquidated, payload.UpdatedAtISO, runID)
return err
}
func loadDecisionTraceDB(runID string, cycle int) (*store.DecisionRecord, error) {
query := `SELECT payload FROM backtest_decisions WHERE run_id = ?`
var rows *sql.Rows
var err error
if cycle > 0 {
rows, err = persistenceDB.Query(query+` AND cycle = ? ORDER BY created_at DESC LIMIT 1`, runID, cycle)
rows, err = persistenceDB.Query(convertQuery(`SELECT payload FROM backtest_decisions WHERE run_id = ? AND cycle = ? ORDER BY created_at DESC LIMIT 1`), runID, cycle)
} else {
rows, err = persistenceDB.Query(query+` ORDER BY created_at DESC LIMIT 1`, runID)
rows, err = persistenceDB.Query(convertQuery(`SELECT payload FROM backtest_decisions WHERE run_id = ? ORDER BY created_at DESC LIMIT 1`), runID)
}
if err != nil {
return nil, err
@@ -308,20 +307,20 @@ func saveDecisionRecordDB(runID string, record *store.DecisionRecord) error {
if err != nil {
return err
}
_, err = persistenceDB.Exec(`
_, err = persistenceDB.Exec(convertQuery(`
INSERT INTO backtest_decisions (run_id, cycle, payload)
VALUES (?, ?, ?)
`, runID, record.CycleNumber, data)
`), runID, record.CycleNumber, data)
return err
}
func loadDecisionRecordsDB(runID string, limit, offset int) ([]*store.DecisionRecord, error) {
rows, err := persistenceDB.Query(`
rows, err := persistenceDB.Query(convertQuery(`
SELECT payload FROM backtest_decisions
WHERE run_id = ?
ORDER BY id DESC
LIMIT ? OFFSET ?
`, runID, limit, offset)
`), runID, limit, offset)
if err != nil {
return nil, err
}
@@ -428,10 +427,10 @@ func writeJSONLinesToZip[T any](z *zip.Writer, name string, items []T) error {
}
func writeDecisionLogsToZip(z *zip.Writer, runID string) error {
rows, err := persistenceDB.Query(`
rows, err := persistenceDB.Query(convertQuery(`
SELECT id, cycle, payload FROM backtest_decisions
WHERE run_id = ? ORDER BY id ASC
`, runID)
`), runID)
if err != nil {
return err
}
@@ -494,6 +493,6 @@ func listIndexEntriesDB() ([]RunIndexEntry, error) {
}
func deleteRunDB(runID string) error {
_, err := persistenceDB.Exec(`DELETE FROM backtest_runs WHERE run_id = ?`, runID)
_, err := persistenceDB.Exec(convertQuery(`DELETE FROM backtest_runs WHERE run_id = ?`), runID)
return err
}

View File

@@ -9,7 +9,7 @@ import (
"sync"
"time"
"nofx/decision"
"nofx/kernel"
"nofx/logger"
"nofx/market"
"nofx/mcp"
@@ -18,7 +18,7 @@ import (
// TraderExecutor interface for executing trades
type TraderExecutor interface {
ExecuteDecision(decision *decision.Decision) error
ExecuteDecision(decision *kernel.Decision) error
GetBalance() (map[string]interface{}, error)
}
@@ -166,7 +166,7 @@ func (e *DebateEngine) runDebate(session *store.DebateSessionWithDetails, strate
}()
// Create strategy engine for building context
strategyEngine := decision.NewStrategyEngine(strategyConfig)
strategyEngine := kernel.NewStrategyEngine(strategyConfig)
// Build market context using strategy config
ctx, err := e.buildMarketContext(session, strategyEngine)
@@ -289,7 +289,7 @@ func (e *DebateEngine) runDebate(session *store.DebateSessionWithDetails, strate
}
// buildMarketContext builds the market context using strategy engine
func (e *DebateEngine) buildMarketContext(session *store.DebateSessionWithDetails, strategyEngine *decision.StrategyEngine) (*decision.Context, error) {
func (e *DebateEngine) buildMarketContext(session *store.DebateSessionWithDetails, strategyEngine *kernel.StrategyEngine) (*kernel.Context, error) {
config := strategyEngine.GetConfig()
// Get candidate coins
@@ -335,12 +335,18 @@ func (e *DebateEngine) buildMarketContext(session *store.DebateSessionWithDetail
// Fetch OI ranking data (market-wide position changes)
oiRankingData := strategyEngine.FetchOIRankingData()
// Fetch NetFlow ranking data (market-wide fund flow)
netFlowRankingData := strategyEngine.FetchNetFlowRankingData()
// Fetch Price ranking data (market-wide gainers/losers)
priceRankingData := strategyEngine.FetchPriceRankingData()
// Build context
ctx := &decision.Context{
ctx := &kernel.Context{
CurrentTime: time.Now().UTC().Format("2006-01-02 15:04:05 UTC"),
RuntimeMinutes: 0,
CallCount: 1,
Account: decision.AccountInfo{
Account: kernel.AccountInfo{
TotalEquity: 1000.0, // Simulated for debate
AvailableBalance: 1000.0,
UnrealizedPnL: 0,
@@ -350,12 +356,14 @@ func (e *DebateEngine) buildMarketContext(session *store.DebateSessionWithDetail
MarginUsedPct: 0,
PositionCount: 0,
},
Positions: []decision.PositionInfo{},
CandidateCoins: candidates,
PromptVariant: session.PromptVariant,
MarketDataMap: marketDataMap,
QuantDataMap: quantDataMap,
OIRankingData: oiRankingData,
Positions: []kernel.PositionInfo{},
CandidateCoins: candidates,
PromptVariant: session.PromptVariant,
MarketDataMap: marketDataMap,
QuantDataMap: quantDataMap,
OIRankingData: oiRankingData,
NetFlowRankingData: netFlowRankingData,
PriceRankingData: priceRankingData,
}
return ctx, nil
@@ -539,7 +547,7 @@ func (e *DebateEngine) getParticipantResponse(
}
// collectVotes collects final votes from all participants
func (e *DebateEngine) collectVotes(session *store.DebateSessionWithDetails, strategyEngine *decision.StrategyEngine, allMessages []*store.DebateMessage) ([]*store.DebateVote, error) {
func (e *DebateEngine) collectVotes(session *store.DebateSessionWithDetails, strategyEngine *kernel.StrategyEngine, allMessages []*store.DebateMessage) ([]*store.DebateVote, error) {
var votes []*store.DebateVote
// Build voting context
@@ -1009,7 +1017,7 @@ func (e *DebateEngine) ExecuteConsensus(sessionID string, executor TraderExecuto
}
// Create decision
tradeDecision := &decision.Decision{
tradeDecision := &kernel.Decision{
Symbol: session.Symbol,
Action: action,
Leverage: session.FinalDecision.Leverage,

48
docker-compose.stable.yml Normal file
View File

@@ -0,0 +1,48 @@
# NOFX Stable Release Deployment
# Production-ready stable version
services:
nofx:
image: ghcr.io/nofxaios/nofx/nofx-backend:stable
container_name: nofx-trading
restart: unless-stopped
stop_grace_period: 30s
ports:
- "${NOFX_BACKEND_PORT:-8080}:8080"
volumes:
- ./data:/app/data
- /etc/localtime:/etc/localtime:ro
env_file:
- .env
environment:
- TZ=${TZ:-Asia/Shanghai}
- AI_MAX_TOKENS=8000
networks:
- nofx-network
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/api/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
nofx-frontend:
image: ghcr.io/nofxaios/nofx/nofx-frontend:stable
container_name: nofx-frontend
restart: unless-stopped
ports:
- "${NOFX_FRONTEND_PORT:-3000}:80"
networks:
- nofx-network
depends_on:
- nofx
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 5s
networks:
nofx-network:
driver: bridge

852
docs/api/API_REFERENCE.md Normal file
View File

@@ -0,0 +1,852 @@
# CryptoMaster API 接口文档
## 概述
### 基础信息
- **Base URL**: `https://nofxos.ai`
- **响应格式**: JSON
- **缓存时间**: 15秒所有数据接口
- **限流**: 每个IP每秒最多30次请求
### 认证方式
所有数据接口需要认证,支持两种方式:
#### 方式1: Query参数推荐
```
GET /api/ai500/list?auth=your_api_key
```
#### 方式2: Authorization Header
```
GET /api/ai500/list
Authorization: Bearer your_api_key
```
### 响应格式
**成功响应:**
```json
{
"success": true,
"data": { ... }
}
```
**错误响应:**
```json
{
"success": false,
"error": "错误信息"
}
```
---
## 重要:数值格式说明
### 百分比字段格式
不同接口的百分比字段使用不同的格式,请注意区分:
| 字段名 | 格式 | 示例 | 说明 |
|--------|------|------|------|
| `price_delta` (涨跌幅榜/币种详情) | **小数** | `0.05` = 5% | 需要 ×100 转换为百分比 |
| `oi_delta_percent` | **已×100** | `5.0` = 5% | 直接使用,无需转换 |
| `price_delta_percent` (OI接口) | **已×100** | `5.0` = 5% | 直接使用,无需转换 |
| `increase_percent` (AI500) | **已×100** | `7.14` = 7.14% | 直接使用,无需转换 |
### 金额字段
| 字段名 | 单位 | 说明 |
|--------|------|------|
| `oi_delta_value` | USDT | 持仓价值变化 |
| `amount` / `future_flow` / `spot_flow` | USDT | 资金流量 |
| `price` | USDT | 当前价格 |
### 持仓量字段
| 字段名 | 单位 | 说明 |
|--------|------|------|
| `oi_delta` | 张/个 | 持仓量变化 |
| `current_oi` / `oi` | 张/个 | 当前持仓量 |
| `net_long` / `net_short` | 张/个 | 净多头/空头持仓 |
---
## 时间范围参数说明
所有接口支持的 `duration` 参数值:
| 参数值 | 说明 | 备注 |
|--------|------|------|
| `1m` | 1分钟 | |
| `5m` | 5分钟 | |
| `15m` | 15分钟 | |
| `30m` | 30分钟 | |
| `1h` | 1小时 | 默认值 |
| `4h` | 4小时 | |
| `8h` | 8小时 | |
| `12h` | 12小时 | |
| `24h` / `1d` | 24小时 | 两种写法均可 |
| `2d` | 2天 | |
| `3d` | 3天 | |
| `5d` | 5天 | |
| `7d` | 7天 | |
---
## 1. AI500 智能评分接口
AI500 是基于多维度量化指标的智能评分系统,用于筛选具有上涨潜力的币种。
### 1.1 获取AI500推荐币种列表
获取经过严格筛选的优质币种列表。
**请求**
```
GET /api/ai500/list
```
**过滤条件**
- AI评分 > 70
- 币安OI持仓价值 > 15M USDT
- 现价 > 上榜起始价格(只返回上涨中的币种)
- 资金没有持续流出1h/4h/12h/24h不能全为负
**响应示例**
```json
{
"success": true,
"data": {
"count": 5,
"coins": [
{
"pair": "BTCUSDT",
"score": 85.234,
"start_time": 1704067200,
"start_price": 42000.5,
"last_score": 83.5,
"max_score": 87.2,
"max_price": 45000.0,
"increase_percent": 7.14
}
]
}
}
```
**字段说明**
| 字段 | 类型 | 说明 |
|------|------|------|
| `pair` | string | 交易对名称,如 BTCUSDT |
| `score` | float | 当前AI评分0-100 |
| `start_time` | int64 | 上榜时间戳Unix秒 |
| `start_price` | float | 上榜时价格USDT |
| `last_score` | float | 上次记录的评分 |
| `max_score` | float | 在榜期间最高评分 |
| `max_price` | float | 在榜期间最高价格USDT |
| `increase_percent` | float | 最大涨幅百分比(**已×100**7.14 = 7.14% |
---
### 1.2 获取单个币种AI500信息
**请求**
```
GET /api/ai500/:symbol
```
**路径参数**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `symbol` | string | 是 | 币种符号,支持 `BTCUSDT``BTC` 格式 |
**示例**
```
GET /api/ai500/BTC
GET /api/ai500/ETHUSDT
```
**响应示例**
```json
{
"success": true,
"data": {
"info": {
"pair": "BTCUSDT",
"score": 85.234,
"start_time": 1704067200,
"start_price": 42000.5,
"last_score": 83.5,
"max_score": 87.2,
"max_price": 45000.0,
"increase_percent": 7.14
},
"current_price": 44500.0,
"score": 85.234
}
}
```
---
### 1.3 获取AI500统计信息
获取AI500整体统计数据。
**请求**
```
GET /api/ai500/stats
```
**响应示例**
```json
{
"success": true,
"data": {
"statistics": {
"total_count": 50,
"average_score": 72.5,
"max_score": 95.2,
"min_score": 55.3,
"average_increase": 12.5
},
"top_coins": [...],
"bottom_coins": [...]
}
}
```
---
## 2. 持仓量(OI)排行接口
监控各币种的合约持仓量变化,用于判断市场资金动向。
### 2.1 获取OI增加排行榜
返回持仓价值增加最多的币种排行。
**请求**
```
GET /api/oi/top-ranking
```
**查询参数**
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `limit` | int | 20 | 返回数量最大100 |
| `duration` | string | `1h` | 时间范围,见[时间范围参数](#时间范围参数说明) |
**示例**
```
GET /api/oi/top-ranking?limit=50&duration=4h
```
**响应示例**
```json
{
"success": true,
"data": {
"count": 20,
"exchange": "binance",
"time_range": "4小时",
"time_range_param": "4h",
"rank_type": "top",
"limit": 50,
"positions": [
{
"rank": 1,
"symbol": "BTCUSDT",
"price": 44500.0,
"oi_delta": 1500.5,
"oi_delta_value": 65000000,
"oi_delta_percent": 2.5,
"current_oi": 62000,
"price_delta_percent": 1.2,
"net_long": 35000,
"net_short": 27000
}
]
}
}
```
**字段说明**
| 字段 | 类型 | 格式 | 说明 |
|------|------|------|------|
| `rank` | int | - | 排名 |
| `symbol` | string | - | 交易对名称 |
| `price` | float | USDT | 当前价格 |
| `oi_delta` | float | 张/个 | 持仓量变化 |
| `oi_delta_value` | float | USDT | 持仓价值变化(**排序依据** |
| `oi_delta_percent` | float | **已×100** | 持仓量变化百分比2.5 = 2.5% |
| `current_oi` | float | 张/个 | 当前持仓量 |
| `price_delta_percent` | float | **已×100** | 价格变化百分比1.2 = 1.2% |
| `net_long` | float | 张/个 | 净多头持仓 |
| `net_short` | float | 张/个 | 净空头持仓 |
---
### 2.2 获取OI减少排行榜
返回持仓价值减少最多的币种排行。
**请求**
```
GET /api/oi/low-ranking
```
**查询参数**
同 [OI增加排行榜](#21-获取oi增加排行榜)
**示例**
```
GET /api/oi/low-ranking?limit=30&duration=24h
```
---
### 2.3 获取OI Top20向后兼容
**请求**
```
GET /api/oi/top
```
固定返回1小时内OI增加最多的Top20用于向后兼容。
---
## 3. 资金流量(NetFlow)排行接口
监控机构和散户的资金流向。
### 3.1 获取资金流入排行榜
**请求**
```
GET /api/netflow/top-ranking
```
**查询参数**
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `limit` | int | 20 | 返回数量最大100 |
| `duration` | string | `1h` | 时间范围,见[时间范围参数](#时间范围参数说明) |
| `type` | string | `institution` | 资金类型:`institution`(机构), `personal`(散户) |
| `trade` | string | `future` | 交易类型:`future`(合约), `spot`(现货) |
**示例**
```
GET /api/netflow/top-ranking?limit=30&duration=4h&type=institution&trade=future
```
**响应示例**
```json
{
"success": true,
"data": {
"count": 30,
"type": "institution",
"trade": "合约",
"time_range": "4h",
"rank_type": "top",
"limit": 30,
"netflows": [
{
"rank": 1,
"symbol": "BTCUSDT",
"amount": 15000000.5,
"price": 44500.0
}
]
}
}
```
**字段说明**
| 字段 | 类型 | 格式 | 说明 |
|------|------|------|------|
| `rank` | int | - | 排名 |
| `symbol` | string | - | 交易对名称 |
| `amount` | float | USDT | 资金流量,**正数=流入,负数=流出** |
| `price` | float | USDT | 当前价格 |
---
### 3.2 获取资金流出排行榜
**请求**
```
GET /api/netflow/low-ranking
```
**查询参数**
同 [资金流入排行榜](#31-获取资金流入排行榜)
**示例**
```
GET /api/netflow/low-ranking?limit=20&duration=1h&type=personal&trade=spot
```
---
### 3.3 获取资金流入Top20向后兼容
**请求**
```
GET /api/netflow/top
```
固定返回1小时内机构合约资金流入最多的Top20。
---
## 4. 涨跌幅榜接口
### 4.1 获取涨跌幅榜
同时返回涨幅榜(top)和跌幅榜(low),支持多个时间周期同时查询。
**请求**
```
GET /api/price/ranking
```
**查询参数**
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `duration` | string | `1h` | 时间范围,可多选逗号分隔:`1h,4h,24h` |
| `limit` | int | 20 | 每个榜单返回数量最大100 |
| `exchange` | string | `binance` | 交易所 |
**示例**
```
GET /api/price/ranking?duration=1h,4h,24h&limit=20
```
**响应示例**
```json
{
"success": true,
"data": {
"durations": ["1h", "4h", "24h"],
"limit": 20,
"data": {
"1h": {
"top": [
{
"pair": "MOGUSDT",
"symbol": "MOG",
"price_delta": 0.0723,
"price": 0.00123,
"future_flow": 201500,
"spot_flow": 0,
"oi": 15000000,
"oi_delta": 500000,
"oi_delta_value": 615
}
],
"low": [
{
"pair": "XYZUSDT",
"symbol": "XYZ",
"price_delta": -0.0512,
"price": 1.234,
"future_flow": -50000,
"spot_flow": -10000,
"oi": 8000000,
"oi_delta": -200000,
"oi_delta_value": -246800
}
]
},
"4h": { ... },
"24h": { ... }
}
}
}
```
**字段说明**
| 字段 | 类型 | 格式 | 说明 |
|------|------|------|------|
| `pair` | string | - | 完整交易对名称,如 BTCUSDT |
| `symbol` | string | - | 币种符号去除USDT如 BTC |
| `price_delta` | float | **小数** | 价格变动比例,**0.0723 = 7.23%**×100显示 |
| `price` | float | USDT | 当前价格 |
| `future_flow` | float | USDT | 合约资金流量,正数=流入 |
| `spot_flow` | float | USDT | 现货资金流量,正数=流入 |
| `oi` | float | 张/个 | 当前持仓量 |
| `oi_delta` | float | 张/个 | 持仓变化量 |
| `oi_delta_value` | float | USDT | 持仓变化价值 |
> **注意**`price_delta` 使用小数格式,与 OI 接口的 `price_delta_percent` 不同!
---
## 5. 币种详情接口
### 5.1 获取单币种完整数据
获取指定币种的所有统计信息,一次调用获取全部数据。
**请求**
```
GET /api/coin/:symbol
```
**路径参数**
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `symbol` | string | 是 | 币种符号,支持 `BTC``BTCUSDT` 格式 |
**查询参数**
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `include` | string | `netflow,oi,price,ai500` | 包含的数据类型,逗号分隔 |
**include 参数选项**
| 值 | 说明 |
|------|------|
| `netflow` | 资金流量数据(机构/散户,合约/现货) |
| `oi` | 持仓量数据(币安/Bybit |
| `price` | 价格变化数据 |
| `ai500` | AI500评分 |
**示例**
```
GET /api/coin/BTC?include=netflow,oi,price,ai500
GET /api/coin/ETHUSDT?include=netflow,oi
```
**响应示例**
```json
{
"success": true,
"data": {
"symbol": "BTCUSDT",
"price": 44500.0,
"ai500": {
"score": 85.234,
"is_active": true,
"start_time": 1704067200,
"start_price": 42000.5,
"increase_percent": 5.95
},
"netflow": {
"institution": {
"future": {
"1m": 50000,
"5m": 200000,
"15m": 500000,
"30m": 800000,
"1h": 1500000,
"4h": 5000000,
"8h": 8000000,
"12h": 10000000,
"24h": 15000000,
"2d": 25000000,
"3d": 35000000,
"5d": 50000000,
"7d": 75000000
},
"spot": { ... }
},
"personal": {
"future": { ... },
"spot": { ... }
}
},
"oi": {
"binance": {
"current_oi": 62000,
"net_long": 35000,
"net_short": 27000,
"delta": {
"1m": {
"oi_delta": 50,
"oi_delta_value": 2225000,
"oi_delta_percent": 0.08
},
"5m": { ... },
"1h": { ... },
"4h": { ... },
"24h": { ... }
}
},
"bybit": { ... }
},
"price_change": {
"1m": 0.001,
"5m": 0.005,
"15m": 0.008,
"30m": 0.012,
"1h": 0.015,
"4h": 0.025,
"8h": 0.035,
"12h": 0.042,
"24h": 0.055,
"2d": 0.08,
"3d": 0.12,
"5d": 0.18,
"7d": 0.25
}
}
}
```
**字段说明**
**price_change 对象**
| 字段 | 类型 | 格式 | 说明 |
|------|------|------|------|
| `{duration}` | float | **小数** | 价格变化比例,**0.015 = 1.5%**×100显示 |
**netflow 对象**
| 路径 | 类型 | 格式 | 说明 |
|------|------|------|------|
| `institution.future.{duration}` | float | USDT | 机构合约资金流量 |
| `institution.spot.{duration}` | float | USDT | 机构现货资金流量 |
| `personal.future.{duration}` | float | USDT | 散户合约资金流量 |
| `personal.spot.{duration}` | float | USDT | 散户现货资金流量 |
**oi 对象**
| 路径 | 类型 | 格式 | 说明 |
|------|------|------|------|
| `binance.current_oi` | float | 张/个 | 币安当前持仓量 |
| `binance.net_long` | float | 张/个 | 币安净多头 |
| `binance.net_short` | float | 张/个 | 币安净空头 |
| `binance.delta.{duration}.oi_delta` | float | 张/个 | 持仓量变化 |
| `binance.delta.{duration}.oi_delta_value` | float | USDT | 持仓价值变化 |
| `binance.delta.{duration}.oi_delta_percent` | float | **已×100** | 持仓变化百分比0.08 = 0.08% |
| `bybit.*` | - | - | Bybit数据结构同上 |
**ai500 对象**
| 字段 | 类型 | 格式 | 说明 |
|------|------|------|------|
| `score` | float | 0-100 | AI综合评分 |
| `is_active` | bool | - | 是否为活跃高分币种 |
| `start_time` | int64 | Unix秒 | 上榜时间 |
| `start_price` | float | USDT | 上榜时价格 |
| `increase_percent` | float | **已×100** | 最大涨幅5.95 = 5.95% |
---
## 错误码说明
| HTTP状态码 | 说明 | 常见原因 |
|------------|------|----------|
| 200 | 成功 | - |
| 400 | 请求参数错误 | 参数格式不正确、缺少必填参数 |
| 401 | 未授权 | 缺少认证信息或API Key无效 |
| 404 | 资源不存在 | 币种不存在或未被追踪 |
| 429 | 请求过于频繁 | 超过限流阈值30次/秒) |
| 500 | 服务器内部错误 | 服务端异常 |
**错误响应示例**
```json
{
"success": false,
"error": "unauthorized"
}
```
---
## 使用示例
### cURL 示例
```bash
# 方式1: Query参数认证
curl "https://nofxos.ai/api/ai500/list?auth=your_api_key"
# 方式2: Header认证
curl "https://nofxos.ai/api/ai500/list" \
-H "Authorization: Bearer your_api_key"
# 获取1小时涨跌幅榜
curl "https://nofxos.ai/api/price/ranking?duration=1h&limit=20&auth=your_api_key"
# 获取多个时间周期涨跌幅榜
curl "https://nofxos.ai/api/price/ranking?duration=1h,4h,24h&limit=10&auth=your_api_key"
# 获取BTC详细数据
curl "https://nofxos.ai/api/coin/BTC?auth=your_api_key"
# 只获取BTC的资金流和OI数据
curl "https://nofxos.ai/api/coin/BTC?include=netflow,oi&auth=your_api_key"
# 获取4小时OI增加排行Top50
curl "https://nofxos.ai/api/oi/top-ranking?duration=4h&limit=50&auth=your_api_key"
# 获取24小时OI减少排行Top30
curl "https://nofxos.ai/api/oi/low-ranking?duration=24h&limit=30&auth=your_api_key"
# 获取机构合约资金流入排行
curl "https://nofxos.ai/api/netflow/top-ranking?type=institution&trade=future&duration=1h&auth=your_api_key"
# 获取散户现货资金流出排行
curl "https://nofxos.ai/api/netflow/low-ranking?type=personal&trade=spot&duration=4h&auth=your_api_key"
```
### Python 示例
```python
import requests
BASE_URL = "https://nofxos.ai"
API_KEY = "your_api_key"
# 方式1: Query参数认证
def get_with_query_auth(endpoint, params=None):
if params is None:
params = {}
params["auth"] = API_KEY
response = requests.get(f"{BASE_URL}{endpoint}", params=params)
return response.json()
# 方式2: Header认证
def get_with_header_auth(endpoint, params=None):
headers = {"Authorization": f"Bearer {API_KEY}"}
response = requests.get(f"{BASE_URL}{endpoint}", params=params, headers=headers)
return response.json()
# 获取AI500列表
def get_ai500_list():
return get_with_query_auth("/api/ai500/list")
# 获取涨跌幅榜
def get_price_ranking(durations="1h,4h,24h", limit=20):
return get_with_query_auth("/api/price/ranking", {
"duration": durations,
"limit": limit
})
# 获取币种详情
def get_coin_stats(symbol, include="netflow,oi,price,ai500"):
return get_with_query_auth(f"/api/coin/{symbol}", {
"include": include
})
# 获取OI排行
def get_oi_ranking(rank_type="top", duration="1h", limit=20):
endpoint = f"/api/oi/{rank_type}-ranking"
return get_with_query_auth(endpoint, {
"duration": duration,
"limit": limit
})
# 获取资金流排行
def get_netflow_ranking(rank_type="top", duration="1h", limit=20,
flow_type="institution", trade="future"):
endpoint = f"/api/netflow/{rank_type}-ranking"
return get_with_query_auth(endpoint, {
"duration": duration,
"limit": limit,
"type": flow_type,
"trade": trade
})
# 使用示例
if __name__ == "__main__":
# 获取AI500推荐币种
ai500 = get_ai500_list()
print(f"AI500推荐币种数量: {ai500['data']['count']}")
# 获取1小时涨幅榜前10
ranking = get_price_ranking("1h", 10)
for coin in ranking['data']['data']['1h']['top'][:3]:
# 注意: price_delta 是小数需要×100
pct = coin['price_delta'] * 100
print(f"{coin['symbol']}: {pct:.2f}%")
# 获取BTC详情
btc = get_coin_stats("BTC")
# 注意: price_change 是小数
print(f"BTC 1小时涨跌: {btc['data']['price_change']['1h'] * 100:.2f}%")
# 获取4小时OI增加Top20
oi = get_oi_ranking("top", "4h", 20)
for pos in oi['data']['positions'][:3]:
# 注意: oi_delta_percent 已×100
print(f"{pos['symbol']}: OI变化 {pos['oi_delta_percent']:.2f}%")
```
### JavaScript/TypeScript 示例
```typescript
const BASE_URL = "https://nofxos.ai";
const API_KEY = "your_api_key";
// 通用请求函数
async function apiRequest<T>(endpoint: string, params: Record<string, any> = {}): Promise<T> {
const url = new URL(`${BASE_URL}${endpoint}`);
params.auth = API_KEY;
Object.entries(params).forEach(([key, value]) => {
url.searchParams.append(key, String(value));
});
const response = await fetch(url.toString());
return response.json();
}
// 获取涨跌幅榜
interface PriceRankingItem {
pair: string;
symbol: string;
price_delta: number; // 小数格式0.05 = 5%
price: number;
future_flow: number;
spot_flow: number;
}
async function getPriceRanking(durations = "1h", limit = 20) {
const data = await apiRequest<any>("/api/price/ranking", { duration: durations, limit });
return data;
}
// 使用示例
async function main() {
const ranking = await getPriceRanking("1h,4h", 10);
for (const coin of ranking.data.data["1h"].top) {
// 转换为百分比显示
const pctChange = (coin.price_delta * 100).toFixed(2);
console.log(`${coin.symbol}: ${pctChange}%`);
}
}
```
---
## 常见问题
### Q: 为什么有些百分比字段格式不同?
A: 这是历史原因造成的:
- **OI接口**的 `oi_delta_percent``price_delta_percent` 是**已乘100**的格式5.0 = 5%
- **涨跌幅榜和币种详情**的 `price_delta` / `price_change` 是**小数**格式0.05 = 5%
建议在前端显示时统一处理。
### Q: duration 参数支持哪些值?
A: 支持以下值:`1m`, `5m`, `15m`, `30m`, `1h`, `4h`, `8h`, `12h`, `24h`(或`1d`), `2d`, `3d`, `5d`, `7d`
### Q: 如何判断资金是流入还是流出?
A: `amount``future_flow``spot_flow` 等字段:
- **正数** = 资金流入
- **负数** = 资金流出
### Q: API缓存时间是多久
A: 所有数据接口缓存15秒相同请求在15秒内返回缓存数据。
### Q: 限流规则是什么?
A: 每个IP每秒最多30次请求超过会返回 429 错误。

View File

@@ -1,350 +0,0 @@
# 币种综合数据接口文档
## 接口概述
该接口提供单个币种的综合数据查询,一次请求即可获取资金净流入、持仓变化、价格变化等多维度数据。
## 请求信息
### 接口地址
```
GET /api/coin/{symbol}
```
### 完整示例
```
http://nofxaios.com:30006/api/coin/PIPPINUSDT?include=netflow,oi,price&auth=cm_568c67eae410d912c54c
```
### 请求参数
| 参数 | 位置 | 类型 | 必填 | 说明 |
|-----|------|------|-----|------|
| symbol | path | string | 是 | 币种符号,如 `PIPPINUSDT``ETH`会自动补全USDT后缀 |
| include | query | string | 否 | 返回数据类型,逗号分隔。可选值:`netflow,oi,price`。默认返回全部 |
| auth | query | string | 是 | 认证密钥 |
### include 参数说明
| 值 | 说明 |
|---|------|
| netflow | 资金净流入数据(机构/散户、合约/现货) |
| oi | 持仓数据币安、Bybit |
| price | 价格变化百分比 |
---
## 返回数据
### 完整响应示例
```json
{
"code": 0,
"data": {
"symbol": "PIPPINUSDT",
"price": 0.085,
"netflow": {
"institution": {
"future": {
"1m": 120000,
"5m": 580000,
"15m": 1200000,
"30m": 2500000,
"1h": 5800000,
"4h": 12000000,
"8h": 25000000,
"12h": 38000000,
"24h": 65000000,
"2d": 120000000,
"3d": 180000000
},
"spot": {
"1m": 50000,
"5m": 280000,
"15m": 600000,
"30m": 1200000,
"1h": 2800000,
"4h": 6000000,
"8h": 12000000,
"12h": 18000000,
"24h": 32000000,
"2d": 60000000,
"3d": 90000000
}
},
"personal": {
"future": {
"1m": -80000,
"5m": -350000,
"15m": -800000,
"30m": -1500000,
"1h": -3200000,
"4h": -8000000,
"8h": -15000000,
"12h": -22000000,
"24h": -40000000,
"2d": -75000000,
"3d": -110000000
},
"spot": {
"1m": -30000,
"5m": -150000,
"15m": -400000,
"30m": -800000,
"1h": -1800000,
"4h": -4000000,
"8h": -8000000,
"12h": -12000000,
"24h": -22000000,
"2d": -40000000,
"3d": -60000000
}
}
},
"oi": {
"binance": {
"current_oi": 85000,
"net_long": 48000,
"net_short": 37000,
"delta": {
"1m": {
"oi_delta": 150,
"oi_delta_value": 14550000,
"oi_delta_percent": 0.18
},
"5m": {
"oi_delta": 680,
"oi_delta_value": 65960000,
"oi_delta_percent": 0.8
},
"1h": {
"oi_delta": 2500,
"oi_delta_value": 242500000,
"oi_delta_percent": 2.94
},
"4h": {
"oi_delta": 5200,
"oi_delta_value": 504400000,
"oi_delta_percent": 6.12
},
"24h": {
"oi_delta": 8500,
"oi_delta_value": 824500000,
"oi_delta_percent": 10.0
}
}
},
"bybit": {
"current_oi": 42000,
"net_long": 24000,
"net_short": 18000,
"delta": {
"1h": {
"oi_delta": 1200,
"oi_delta_value": 116400000,
"oi_delta_percent": 2.86
}
}
}
},
"price_change": {
"1m": 0.05,
"5m": 0.18,
"15m": 0.35,
"30m": 0.62,
"1h": 1.25,
"4h": 2.80,
"8h": 3.50,
"12h": 2.95,
"24h": 4.80,
"2d": 6.50,
"3d": 8.20
}
}
}
```
---
## 字段详细说明
### 基础字段
| 字段 | 类型 | 说明 |
|-----|------|------|
| symbol | string | 币种交易对,如 `PIPPINUSDT` |
| price | float | 当前期货价格单位USDT |
---
### netflow - 资金净流入
资金净流入数据,**正数表示资金流入,负数表示资金流出**,单位为 USDT。
#### 数据结构
```
netflow
├── institution # 机构资金
│ ├── future # 合约市场
│ └── spot # 现货市场
└── personal # 散户资金
├── future # 合约市场
└── spot # 现货市场
```
#### 分类说明
| 字段 | 说明 |
|-----|------|
| institution.future | 机构在合约市场的资金净流入 |
| institution.spot | 机构在现货市场的资金净流入 |
| personal.future | 散户在合约市场的资金净流入 |
| personal.spot | 散户在现货市场的资金净流入 |
#### 时间周期
| 字段 | 说明 |
|-----|------|
| 1m | 最近 1 分钟 |
| 5m | 最近 5 分钟 |
| 15m | 最近 15 分钟 |
| 30m | 最近 30 分钟 |
| 1h | 最近 1 小时 |
| 4h | 最近 4 小时 |
| 8h | 最近 8 小时 |
| 12h | 最近 12 小时 |
| 24h | 最近 24 小时 |
| 2d | 最近 2 天 |
| 3d | 最近 3 天 |
#### 使用建议
- **机构资金流入 + 散户资金流出** = 典型的主力吸筹信号
- **机构资金流出 + 散户资金流入** = 典型的主力出货信号
- 关注 **合约与现货的资金流向是否一致**,判断市场情绪
---
### oi - 持仓数据
持仓量Open Interest数据来源于币安和 Bybit 交易所。
#### 字段说明
| 字段 | 类型 | 说明 |
|-----|------|------|
| current_oi | float | 当前总持仓量(单位:币) |
| net_long | float | 净多头持仓量 |
| net_short | float | 净空头持仓量 |
| delta | object | 各时间周期的持仓变化 |
#### delta 子字段
| 字段 | 类型 | 说明 |
|-----|------|------|
| oi_delta | float | 持仓量变化(单位:币) |
| oi_delta_value | float | 持仓价值变化单位USDT |
| oi_delta_percent | float | 持仓量变化百分比(% |
#### 使用建议
- **持仓量增加 + 价格上涨** = 多头主导,趋势可能延续
- **持仓量增加 + 价格下跌** = 空头主导,下跌趋势可能延续
- **持仓量减少 + 价格变化** = 平仓为主,趋势可能反转
- **net_long > net_short** = 市场整体偏多
---
### price_change - 价格变化
各时间周期的价格涨跌幅,**单位为百分比(%**,正数表示上涨,负数表示下跌。
| 字段 | 说明 |
|-----|------|
| 1m | 最近 1 分钟涨跌幅 |
| 5m | 最近 5 分钟涨跌幅 |
| 15m | 最近 15 分钟涨跌幅 |
| 30m | 最近 30 分钟涨跌幅 |
| 1h | 最近 1 小时涨跌幅 |
| 4h | 最近 4 小时涨跌幅 |
| 8h | 最近 8 小时涨跌幅 |
| 12h | 最近 12 小时涨跌幅 |
| 24h | 最近 24 小时涨跌幅 |
| 2d | 最近 2 天涨跌幅 |
| 3d | 最近 3 天涨跌幅 |
---
## 错误响应
| code | 说明 |
|------|------|
| 0 | 成功 |
| 400 | 参数错误(如缺少 symbol |
| 401 | 认证失败auth 无效) |
| 500 | 服务器内部错误 |
错误响应示例:
```json
{
"code": 400,
"message": "symbol parameter is required"
}
```
---
## 调用示例
### cURL
```bash
curl -X GET "http://nofxaios.com:30006/api/coin/PIPPINUSDT?include=netflow,oi,price&auth=cm_568c67eae410d912c54c"
```
### Python
```python
import requests
url = "http://nofxaios.com:30006/api/coin/PIPPINUSDT"
params = {
"include": "netflow,oi,price",
"auth": "cm_568c67eae410d912c54c"
}
response = requests.get(url, params=params)
data = response.json()
print(f"当前价格: {data['data']['price']}")
print(f"1小时机构合约净流入: {data['data']['netflow']['institution']['future']['1h']}")
print(f"24小时价格涨跌幅: {data['data']['price_change']['24h']}%")
```
### JavaScript
```javascript
const url = 'http://nofxaios.com:30006/api/coin/PIPPINUSDT?include=netflow,oi,price&auth=cm_568c67eae410d912c54c';
fetch(url)
.then(response => response.json())
.then(data => {
console.log('当前价格:', data.data.price);
console.log('1小时机构合约净流入:', data.data.netflow.institution.future['1h']);
console.log('24小时价格涨跌幅:', data.data.price_change['24h'], '%');
});
```
---
## 注意事项
1. **symbol 参数**:支持带或不带 `USDT` 后缀,如 `PIPPIN``PIPPINUSDT` 等效
2. **include 参数**:可按需选择返回数据,减少不必要的数据传输
3. **数据更新频率**:数据实时更新,建议轮询间隔不低于 1 秒
4. **资金流向解读**:机构与散户的资金流向通常呈相反趋势,可作为市场情绪判断依据

View File

@@ -1,254 +0,0 @@
# OI 持仓数据接口文档
## 接口概述
该接口提供币安交易所的合约持仓量Open Interest排行数据支持查询持仓增加和减少排行榜。
## 接口列表
| 接口 | 说明 |
|-----|------|
| `/api/oi/top` | 持仓增加排行 Top20固定参数向后兼容 |
| `/api/oi/top-ranking` | 持仓增加排行(支持自定义参数) |
| `/api/oi/low-ranking` | 持仓减少排行(支持自定义参数) |
---
## 1. 持仓增加排行 Top20
### 请求
```
GET /api/oi/top
```
### 完整示例
```
http://nofxaios.com:30006/api/oi/top?auth=cm_568c67eae410d912c54c
```
### 参数
| 参数 | 类型 | 必填 | 说明 |
|-----|------|-----|------|
| auth | string | 是 | 认证密钥 |
### 说明
固定返回 1 小时内持仓价值增加最多的前 20 个币种,向后兼容接口。
---
## 2. 持仓增加排行(自定义参数)
### 请求
```
GET /api/oi/top-ranking
```
### 完整示例
```
http://nofxaios.com:30006/api/oi/top-ranking?limit=50&duration=4h&auth=cm_568c67eae410d912c54c
```
### 参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|-----|------|-----|-------|------|
| limit | int | 否 | 20 | 获取数量,范围 1-100 |
| duration | string | 否 | 1h | 时间范围 |
| auth | string | 是 | - | 认证密钥 |
---
## 3. 持仓减少排行
### 请求
```
GET /api/oi/low-ranking
```
### 完整示例
```
http://nofxaios.com:30006/api/oi/low-ranking?limit=30&duration=24h&auth=cm_568c67eae410d912c54c
```
### 参数
同持仓增加排行接口。
---
## duration 时间范围参数
| 值 | 说明 |
|---|------|
| 1m | 1 分钟 |
| 5m | 5 分钟 |
| 15m | 15 分钟 |
| 30m | 30 分钟 |
| 1h | 1 小时(默认) |
| 4h | 4 小时 |
| 8h | 8 小时 |
| 12h | 12 小时 |
| 24h | 24 小时 |
| 1d | 1 天(同 24h |
| 2d | 2 天 |
| 3d | 3 天 |
---
## 返回数据
### 响应示例
```json
{
"code": 0,
"data": {
"count": 20,
"exchange": "binance",
"time_range": "4小时",
"time_range_param": "4h",
"rank_type": "top",
"limit": 20,
"positions": [
{
"rank": 1,
"symbol": "BTCUSDT",
"oi_delta": 1500.5,
"oi_delta_value": 145500000,
"oi_delta_percent": 3.52,
"current_oi": 44000,
"price_delta_percent": 2.15,
"net_long": 26000,
"net_short": 18000
},
{
"rank": 2,
"symbol": "ETHUSDT",
"oi_delta": 25000,
"oi_delta_value": 87500000,
"oi_delta_percent": 2.85,
"current_oi": 900000,
"price_delta_percent": 1.80,
"net_long": 520000,
"net_short": 380000
}
]
}
}
```
### 字段说明
#### 外层字段
| 字段 | 类型 | 说明 |
|-----|------|------|
| count | int | 返回的币种数量 |
| exchange | string | 交易所,固定为 `binance` |
| time_range | string | 时间范围显示名称 |
| time_range_param | string | 时间范围参数值 |
| rank_type | string | 排行类型:`top` 增加 / `low` 减少 |
| limit | int | 请求的数量限制 |
| positions | array | 持仓数据列表 |
#### positions 数组字段
| 字段 | 类型 | 说明 |
|-----|------|------|
| rank | int | 排名 |
| symbol | string | 币种交易对,如 `BTCUSDT` |
| oi_delta | float | 持仓量变化(单位:币) |
| oi_delta_value | float | 持仓价值变化单位USDT**排序依据** |
| oi_delta_percent | float | 持仓量变化百分比(% |
| current_oi | float | 当前持仓量(单位:币) |
| price_delta_percent | float | 价格变化百分比(% |
| net_long | float | 净多头持仓量 |
| net_short | float | 净空头持仓量 |
---
## 数据解读
### 持仓量与价格的关系
| 持仓变化 | 价格变化 | 市场含义 |
|---------|---------|---------|
| 增加 | 上涨 | 多头主导,上涨趋势可能延续 |
| 增加 | 下跌 | 空头主导,下跌趋势可能延续 |
| 减少 | 上涨 | 空头平仓,可能是反弹 |
| 减少 | 下跌 | 多头平仓,可能是回调 |
### 多空比例
- `net_long > net_short`:市场整体偏多
- `net_long < net_short`:市场整体偏空
---
## 调用示例
### cURL
```bash
curl -X GET "http://nofxaios.com:30006/api/oi/top-ranking?limit=50&duration=4h&auth=cm_568c67eae410d912c54c"
```
### Python
```python
import requests
url = "http://nofxaios.com:30006/api/oi/top-ranking"
params = {
"limit": 50,
"duration": "4h",
"auth": "cm_568c67eae410d912c54c"
}
response = requests.get(url, params=params)
data = response.json()
for pos in data['data']['positions']:
print(f"#{pos['rank']} {pos['symbol']}: 持仓价值变化 ${pos['oi_delta_value']:,.0f}")
```
### JavaScript
```javascript
const url = 'http://nofxaios.com:30006/api/oi/top-ranking?limit=50&duration=4h&auth=cm_568c67eae410d912c54c';
fetch(url)
.then(response => response.json())
.then(data => {
data.data.positions.forEach(pos => {
console.log(`#${pos.rank} ${pos.symbol}: 持仓价值变化 $${pos.oi_delta_value.toLocaleString()}`);
});
});
```
---
## 错误响应
| code | 说明 |
|------|------|
| 0 | 成功 |
| 401 | 认证失败auth 无效) |
| 500 | 服务器内部错误 |
---
## 注意事项
1. 数据来源为币安交易所
2. 排行依据为 `oi_delta_value`(持仓价值变化),非持仓量变化
3. 数据缓存 2 秒,高频请求会命中缓存
4. `limit` 最大值为 100

View File

@@ -112,7 +112,7 @@ func (e *StrategyEngine) getCoinPoolCoins(limit int) []CandidateCoin {
}
```
- **API:** `config.CoinSource.CoinPoolAPIURL` (默认: `http://nofxaios.com:30006/api/ai500/list`)
- **API:** `config.CoinSource.CoinPoolAPIURL` (默认: `https://nofxos.ai/api/ai500/list`)
- **用途:** 获取 AI 评分最高的 N 个币种
- **标签:** `["ai500"]`

View File

@@ -30,6 +30,50 @@ Telegram 開発者コミュニティに参加: **[NOFX 開発者コミュニテ
---
## 始める前に
NOFXを使用するには以下が必要です:
1. **取引所アカウント** - サポートされている取引所に登録し、取引権限付きのAPI認証情報を作成
2. **AI モデル API キー** - サポートされているプロバイダーから取得コスト効率の良いDeepSeekを推奨
---
## サポート取引所
### CEX (中央集権型取引所)
| 取引所 | ステータス | 登録 (手数料割引) |
|----------|--------|-------------------------|
| **Binance** | ✅ サポート | [登録](https://www.binance.com/join?ref=NOFXENG) |
| **Bybit** | ✅ サポート | [登録](https://partner.bybit.com/b/83856) |
| **OKX** | ✅ サポート | [登録](https://www.okx.com/join/1865360) |
| **Bitget** | ✅ サポート | [登録](https://www.bitget.com/referral/register?from=referral&clacCode=c8a43172) |
### Perp-DEX (分散型永久先物取引所)
| 取引所 | ステータス | 登録 (手数料割引) |
|----------|--------|-------------------------|
| **Hyperliquid** | ✅ サポート | [登録](https://app.hyperliquid.xyz/join/AITRADING) |
| **Aster DEX** | ✅ サポート | [登録](https://www.asterdex.com/en/referral/fdfc0e) |
| **Lighter** | ✅ サポート | [登録](https://app.lighter.xyz/?referral=68151432) |
---
## サポート AI モデル
| AI モデル | ステータス | API キー取得 |
|----------|--------|-------------|
| **DeepSeek** | ✅ サポート | [API キー取得](https://platform.deepseek.com) |
| **Qwen** | ✅ サポート | [API キー取得](https://dashscope.console.aliyun.com) |
| **OpenAI (GPT)** | ✅ サポート | [API キー取得](https://platform.openai.com) |
| **Claude** | ✅ サポート | [API キー取得](https://console.anthropic.com) |
| **Gemini** | ✅ サポート | [API キー取得](https://aistudio.google.com) |
| **Grok** | ✅ サポート | [API キー取得](https://console.x.ai) |
| **Kimi** | ✅ サポート | [API キー取得](https://platform.moonshot.cn) |
---
## クイックスタート
### オプション 1: Docker デプロイ(推奨)

View File

@@ -30,6 +30,50 @@ Telegram 개발자 커뮤니티 참여: **[NOFX 개발자 커뮤니티](https://
---
## 시작하기 전에
NOFX를 사용하려면 다음이 필요합니다:
1. **거래소 계정** - 지원되는 거래소에 등록하고 거래 권한이 있는 API 자격 증명 생성
2. **AI 모델 API 키** - 지원되는 제공업체에서 획득 (비용 효율성을 위해 DeepSeek 권장)
---
## 지원 거래소
### CEX (중앙화 거래소)
| 거래소 | 상태 | 등록 (수수료 할인) |
|----------|--------|-------------------------|
| **Binance** | ✅ 지원 | [등록](https://www.binance.com/join?ref=NOFXENG) |
| **Bybit** | ✅ 지원 | [등록](https://partner.bybit.com/b/83856) |
| **OKX** | ✅ 지원 | [등록](https://www.okx.com/join/1865360) |
| **Bitget** | ✅ 지원 | [등록](https://www.bitget.com/referral/register?from=referral&clacCode=c8a43172) |
### Perp-DEX (탈중앙화 영구 선물 거래소)
| 거래소 | 상태 | 등록 (수수료 할인) |
|----------|--------|-------------------------|
| **Hyperliquid** | ✅ 지원 | [등록](https://app.hyperliquid.xyz/join/AITRADING) |
| **Aster DEX** | ✅ 지원 | [등록](https://www.asterdex.com/en/referral/fdfc0e) |
| **Lighter** | ✅ 지원 | [등록](https://app.lighter.xyz/?referral=68151432) |
---
## 지원 AI 모델
| AI 모델 | 상태 | API 키 받기 |
|----------|--------|-------------|
| **DeepSeek** | ✅ 지원 | [API 키 받기](https://platform.deepseek.com) |
| **Qwen** | ✅ 지원 | [API 키 받기](https://dashscope.console.aliyun.com) |
| **OpenAI (GPT)** | ✅ 지원 | [API 키 받기](https://platform.openai.com) |
| **Claude** | ✅ 지원 | [API 키 받기](https://console.anthropic.com) |
| **Gemini** | ✅ 지원 | [API 키 받기](https://aistudio.google.com) |
| **Grok** | ✅ 지원 | [API 키 받기](https://console.x.ai) |
| **Kimi** | ✅ 지원 | [API 키 받기](https://platform.moonshot.cn) |
---
## 빠른 시작
### 옵션 1: Docker 배포 (권장)

View File

@@ -30,6 +30,50 @@
---
## Перед началом
Для использования NOFX вам понадобится:
1. **Аккаунт биржи** - Зарегистрируйтесь на поддерживаемой бирже и создайте API ключи с правами торговли
2. **API ключ AI модели** - Получите от любого поддерживаемого провайдера (рекомендуется DeepSeek для экономии)
---
## Поддерживаемые биржи
### CEX (Централизованные биржи)
| Биржа | Статус | Регистрация (скидка) |
|----------|--------|-------------------------|
| **Binance** | ✅ Поддерживается | [Регистрация](https://www.binance.com/join?ref=NOFXENG) |
| **Bybit** | ✅ Поддерживается | [Регистрация](https://partner.bybit.com/b/83856) |
| **OKX** | ✅ Поддерживается | [Регистрация](https://www.okx.com/join/1865360) |
| **Bitget** | ✅ Поддерживается | [Регистрация](https://www.bitget.com/referral/register?from=referral&clacCode=c8a43172) |
### Perp-DEX (Децентрализованные биржи)
| Биржа | Статус | Регистрация (скидка) |
|----------|--------|-------------------------|
| **Hyperliquid** | ✅ Поддерживается | [Регистрация](https://app.hyperliquid.xyz/join/AITRADING) |
| **Aster DEX** | ✅ Поддерживается | [Регистрация](https://www.asterdex.com/en/referral/fdfc0e) |
| **Lighter** | ✅ Поддерживается | [Регистрация](https://app.lighter.xyz/?referral=68151432) |
---
## Поддерживаемые AI модели
| AI Модель | Статус | Получить API ключ |
|----------|--------|-------------|
| **DeepSeek** | ✅ Поддерживается | [Получить](https://platform.deepseek.com) |
| **Qwen** | ✅ Поддерживается | [Получить](https://dashscope.console.aliyun.com) |
| **OpenAI (GPT)** | ✅ Поддерживается | [Получить](https://platform.openai.com) |
| **Claude** | ✅ Поддерживается | [Получить](https://console.anthropic.com) |
| **Gemini** | ✅ Поддерживается | [Получить](https://aistudio.google.com) |
| **Grok** | ✅ Поддерживается | [Получить](https://console.x.ai) |
| **Kimi** | ✅ Поддерживается | [Получить](https://platform.moonshot.cn) |
---
## Быстрый старт
### Вариант 1: Docker развёртывание (рекомендуется)

View File

@@ -30,6 +30,50 @@
---
## Перед початком
Для використання NOFX вам знадобиться:
1. **Акаунт біржі** - Зареєструйтесь на підтримуваній біржі та створіть API ключі з правами торгівлі
2. **API ключ AI моделі** - Отримайте від будь-якого підтримуваного провайдера (рекомендується DeepSeek для економії)
---
## Підтримувані біржі
### CEX (Централізовані біржі)
| Біржа | Статус | Реєстрація (знижка) |
|----------|--------|-------------------------|
| **Binance** | ✅ Підтримується | [Реєстрація](https://www.binance.com/join?ref=NOFXENG) |
| **Bybit** | ✅ Підтримується | [Реєстрація](https://partner.bybit.com/b/83856) |
| **OKX** | ✅ Підтримується | [Реєстрація](https://www.okx.com/join/1865360) |
| **Bitget** | ✅ Підтримується | [Реєстрація](https://www.bitget.com/referral/register?from=referral&clacCode=c8a43172) |
### Perp-DEX (Децентралізовані біржі)
| Біржа | Статус | Реєстрація (знижка) |
|----------|--------|-------------------------|
| **Hyperliquid** | ✅ Підтримується | [Реєстрація](https://app.hyperliquid.xyz/join/AITRADING) |
| **Aster DEX** | ✅ Підтримується | [Реєстрація](https://www.asterdex.com/en/referral/fdfc0e) |
| **Lighter** | ✅ Підтримується | [Реєстрація](https://app.lighter.xyz/?referral=68151432) |
---
## Підтримувані AI моделі
| AI Модель | Статус | Отримати API ключ |
|----------|--------|-------------|
| **DeepSeek** | ✅ Підтримується | [Отримати](https://platform.deepseek.com) |
| **Qwen** | ✅ Підтримується | [Отримати](https://dashscope.console.aliyun.com) |
| **OpenAI (GPT)** | ✅ Підтримується | [Отримати](https://platform.openai.com) |
| **Claude** | ✅ Підтримується | [Отримати](https://console.anthropic.com) |
| **Gemini** | ✅ Підтримується | [Отримати](https://aistudio.google.com) |
| **Grok** | ✅ Підтримується | [Отримати](https://console.x.ai) |
| **Kimi** | ✅ Підтримується | [Отримати](https://platform.moonshot.cn) |
---
## Швидкий старт
### Варіант 1: Docker розгортання (рекомендовано)

View File

@@ -30,6 +30,50 @@ Tham gia cộng đồng Telegram: **[NOFX Developer Community](https://t.me/nofx
---
## Trước Khi Bắt Đầu
Để sử dụng NOFX, bạn cần:
1. **Tài khoản sàn giao dịch** - Đăng ký trên sàn được hỗ trợ và tạo API key với quyền giao dịch
2. **API Key mô hình AI** - Lấy từ nhà cung cấp được hỗ trợ (khuyến nghị DeepSeek để tiết kiệm chi phí)
---
## Sàn Giao Dịch Được Hỗ Trợ
### CEX (Sàn Tập Trung)
| Sàn | Trạng thái | Đăng ký (Giảm phí) |
|----------|--------|-------------------------|
| **Binance** | ✅ Hỗ trợ | [Đăng ký](https://www.binance.com/join?ref=NOFXENG) |
| **Bybit** | ✅ Hỗ trợ | [Đăng ký](https://partner.bybit.com/b/83856) |
| **OKX** | ✅ Hỗ trợ | [Đăng ký](https://www.okx.com/join/1865360) |
| **Bitget** | ✅ Hỗ trợ | [Đăng ký](https://www.bitget.com/referral/register?from=referral&clacCode=c8a43172) |
### Perp-DEX (Sàn Phi Tập Trung)
| Sàn | Trạng thái | Đăng ký (Giảm phí) |
|----------|--------|-------------------------|
| **Hyperliquid** | ✅ Hỗ trợ | [Đăng ký](https://app.hyperliquid.xyz/join/AITRADING) |
| **Aster DEX** | ✅ Hỗ trợ | [Đăng ký](https://www.asterdex.com/en/referral/fdfc0e) |
| **Lighter** | ✅ Hỗ trợ | [Đăng ký](https://app.lighter.xyz/?referral=68151432) |
---
## Mô Hình AI Được Hỗ Trợ
| Mô hình AI | Trạng thái | Lấy API Key |
|----------|--------|-------------|
| **DeepSeek** | ✅ Hỗ trợ | [Lấy API Key](https://platform.deepseek.com) |
| **Qwen** | ✅ Hỗ trợ | [Lấy API Key](https://dashscope.console.aliyun.com) |
| **OpenAI (GPT)** | ✅ Hỗ trợ | [Lấy API Key](https://platform.openai.com) |
| **Claude** | ✅ Hỗ trợ | [Lấy API Key](https://console.anthropic.com) |
| **Gemini** | ✅ Hỗ trợ | [Lấy API Key](https://aistudio.google.com) |
| **Grok** | ✅ Hỗ trợ | [Lấy API Key](https://console.x.ai) |
| **Kimi** | ✅ Hỗ trợ | [Lấy API Key](https://platform.moonshot.cn) |
---
## Bắt Đầu Nhanh
### Tùy chọn 1: Triển khai Docker (Khuyến nghị)

View File

@@ -42,19 +42,12 @@
---
## 截图
## 开始之前
### 竞赛模式 - 实时 AI 对战
![竞赛页面](../../../screenshots/competition-page.png)
*多 AI 排行榜,实时性能对比*
使用 NOFX 你需要准备:
### 仪表板 - 市场图表视图
![仪表板市场图表](../../../screenshots/dashboard-market-chart.png)
*专业交易仪表板TradingView 风格图表*
### 策略工作室
![策略工作室](../../../screenshots/strategy-studio.png)
*多数据源策略配置与 AI 测试*
1. **交易所账户** - 在任意支持的交易所注册并创建具有交易权限的 API 凭证
2. **AI 模型 API Key** - 从任意支持的提供商获取(推荐 DeepSeek性价比最高
---
@@ -67,6 +60,7 @@
| **Binance** | ✅ 已支持 | [注册](https://www.binance.com/join?ref=NOFXENG) |
| **Bybit** | ✅ 已支持 | [注册](https://partner.bybit.com/b/83856) |
| **OKX** | ✅ 已支持 | [注册](https://www.okx.com/join/1865360) |
| **Bitget** | ✅ 已支持 | [注册](https://www.bitget.com/referral/register?from=referral&clacCode=c8a43172) |
### Perp-DEX (去中心化永续交易所)
@@ -92,9 +86,25 @@
---
## 截图
### 竞赛模式 - 实时 AI 对战
![竞赛页面](../../../screenshots/competition-page.png)
*多 AI 排行榜,实时性能对比*
### 仪表板 - 市场图表视图
![仪表板市场图表](../../../screenshots/dashboard-market-chart.png)
*专业交易仪表板TradingView 风格图表*
### 策略工作室
![策略工作室](../../../screenshots/strategy-studio.png)
*多数据源策略配置与 AI 测试*
---
## 快速开始
### 一键安装 (推荐)
### 一键安装 (本地/服务器)
**Linux / macOS:**
```bash
@@ -103,6 +113,14 @@ curl -fsSL https://raw.githubusercontent.com/NoFxAiOS/nofx/main/install.sh | bas
完成!打开浏览器访问 **http://127.0.0.1:3000**
### 一键云部署 (Railway)
一键部署到 Railway - 无需自己搭建服务器:
[![Deploy on Railway](https://railway.com/button.svg)](https://railway.com/deploy/nofx?referralCode=nofx)
部署后Railway 会提供一个公网 URL 访问你的 NOFX 实例。
### Docker Compose (手动)
```bash

106
install-stable.sh Executable file
View File

@@ -0,0 +1,106 @@
#!/bin/bash
#
# NOFX Stable Release Installation Script
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/NoFxAiOS/nofx/release/stable/install-stable.sh | bash
#
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
INSTALL_DIR="${1:-$HOME/nofx}"
COMPOSE_FILE="docker-compose.stable.yml"
GITHUB_RAW="https://raw.githubusercontent.com/NoFxAiOS/nofx/release/stable"
echo -e "${BLUE}"
echo "╔════════════════════════════════════════════════════════════╗"
echo "║ NOFX Stable Release ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
check_docker() {
if ! command -v docker &> /dev/null; then
echo -e "${RED}Error: Docker is not installed.${NC}"
exit 1
fi
if ! docker info &> /dev/null; then
echo -e "${RED}Error: Docker daemon is not running.${NC}"
exit 1
fi
if docker compose version &> /dev/null; then
COMPOSE_CMD="docker compose"
elif command -v docker-compose &> /dev/null; then
COMPOSE_CMD="docker-compose"
else
echo -e "${RED}Error: Docker Compose is not available.${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker ready${NC}"
}
setup_directory() {
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"
echo -e "${GREEN}✓ Directory: $INSTALL_DIR${NC}"
}
download_files() {
curl -fsSL "$GITHUB_RAW/$COMPOSE_FILE" -o docker-compose.yml
echo -e "${GREEN}✓ Config downloaded${NC}"
}
generate_env() {
if [ -f ".env" ]; then
echo -e "${GREEN}✓ .env exists${NC}"
return
fi
JWT_SECRET=$(openssl rand -base64 32)
DATA_ENCRYPTION_KEY=$(openssl rand -base64 32)
RSA_PRIVATE_KEY=$(openssl genrsa 2048 2>/dev/null | tr '\n' '\\' | sed 's/\\/\\n/g' | sed 's/\\n$//')
cat > .env << EOF
NOFX_BACKEND_PORT=8080
NOFX_FRONTEND_PORT=3000
TZ=Asia/Shanghai
JWT_SECRET=${JWT_SECRET}
DATA_ENCRYPTION_KEY=${DATA_ENCRYPTION_KEY}
RSA_PRIVATE_KEY=${RSA_PRIVATE_KEY}
EOF
echo -e "${GREEN}✓ Keys generated${NC}"
}
start_services() {
$COMPOSE_CMD pull
$COMPOSE_CMD up -d
echo -e "${GREEN}✓ Services started${NC}"
}
get_server_ip() {
local ip=$(curl -s --max-time 3 ifconfig.me 2>/dev/null || echo "")
echo "${ip:-127.0.0.1}"
}
print_success() {
local IP=$(get_server_ip)
echo ""
echo -e "${GREEN}Installation Complete!${NC}"
echo -e " Web: http://${IP}:3000"
echo -e " API: http://${IP}:8080"
echo ""
}
main() {
check_docker
setup_directory
download_files
generate_env
start_services
print_success
}
main

View File

@@ -1,4 +1,4 @@
package decision
package kernel
import (
"encoding/json"
@@ -8,7 +8,7 @@ import (
"nofx/logger"
"nofx/market"
"nofx/mcp"
"nofx/provider"
"nofx/provider/nofxos"
"nofx/security"
"nofx/store"
"regexp"
@@ -119,8 +119,10 @@ type Context struct {
MultiTFMarket map[string]map[string]*market.Data `json:"-"`
OITopDataMap map[string]*OITopData `json:"-"`
QuantDataMap map[string]*QuantData `json:"-"`
OIRankingData *provider.OIRankingData `json:"-"` // Market-wide OI ranking data
BTCETHLeverage int `json:"-"`
OIRankingData *nofxos.OIRankingData `json:"-"` // Market-wide OI ranking data
NetFlowRankingData *nofxos.NetFlowRankingData `json:"-"` // Market-wide fund flow ranking data
PriceRankingData *nofxos.PriceRankingData `json:"-"` // Market-wide price gainers/losers
BTCETHLeverage int `json:"-"`
AltcoinLeverage int `json:"-"`
Timeframes []string `json:"-"`
}
@@ -189,12 +191,23 @@ type OIDeltaData struct {
// StrategyEngine strategy execution engine
type StrategyEngine struct {
config *store.StrategyConfig
config *store.StrategyConfig
nofxosClient *nofxos.Client
}
// NewStrategyEngine creates strategy execution engine
func NewStrategyEngine(config *store.StrategyConfig) *StrategyEngine {
return &StrategyEngine{config: config}
// Create NofxOS client with API key from config
apiKey := config.Indicators.NofxOSAPIKey
if apiKey == "" {
apiKey = nofxos.DefaultAuthKey
}
client := nofxos.NewClient(nofxos.DefaultBaseURL, apiKey)
return &StrategyEngine{
config: config,
nofxosClient: client,
}
}
// GetRiskControlConfig gets risk control configuration
@@ -202,6 +215,19 @@ func (e *StrategyEngine) GetRiskControlConfig() store.RiskControlConfig {
return e.config.RiskControl
}
// GetLanguage returns the language from config or falls back to auto-detection
func (e *StrategyEngine) GetLanguage() Language {
switch e.config.Language {
case "zh":
return LangChinese
case "en":
return LangEnglish
default:
// Fall back to auto-detection from prompt content for backward compatibility
return detectLanguage(e.config.PromptSections.RoleDefinition)
}
}
// GetConfig gets complete strategy configuration
func (e *StrategyEngine) GetConfig() *store.StrategyConfig {
return e.config
@@ -239,7 +265,7 @@ func GetFullDecisionWithStrategy(ctx *Context, mcpClient mcp.AIClient, engine *S
// Ensure OITopDataMap is initialized
if ctx.OITopDataMap == nil {
ctx.OITopDataMap = make(map[string]*OITopData)
oiPositions, err := provider.GetOITopPositions()
oiPositions, err := engine.nofxosClient.GetOITopPositions()
if err == nil {
for _, pos := range oiPositions {
ctx.OITopDataMap[pos.Symbol] = &OITopData{
@@ -385,13 +411,6 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
coinSource := e.config.CoinSource
if coinSource.CoinPoolAPIURL != "" {
provider.SetCoinPoolAPI(coinSource.CoinPoolAPIURL)
}
if coinSource.OITopAPIURL != "" {
provider.SetOITopAPI(coinSource.OITopAPIURL)
}
switch coinSource.SourceType {
case "static":
for _, symbol := range coinSource.StaticCoins {
@@ -401,12 +420,13 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
Sources: []string{"static"},
})
}
return candidates, nil
case "coinpool":
// 检查 use_coin_pool 标志,如果为 false 则回退到静态币种
if !coinSource.UseCoinPool {
logger.Infof("⚠️ source_type is 'coinpool' but use_coin_pool is false, falling back to static coins")
return e.filterExcludedCoins(candidates), nil
case "ai500":
// 检查 use_ai500 标志,如果为 false 则回退到静态币种
if !coinSource.UseAI500 {
logger.Infof("⚠️ source_type is 'ai500' but use_ai500 is false, falling back to static coins")
for _, symbol := range coinSource.StaticCoins {
symbol = market.Normalize(symbol)
candidates = append(candidates, CandidateCoin{
@@ -414,9 +434,13 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
Sources: []string{"static"},
})
}
return candidates, nil
return e.filterExcludedCoins(candidates), nil
}
return e.getCoinPoolCoins(coinSource.CoinPoolLimit)
coins, err := e.getAI500Coins(coinSource.AI500Limit)
if err != nil {
return nil, err
}
return e.filterExcludedCoins(coins), nil
case "oi_top":
// 检查 use_oi_top 标志,如果为 false 则回退到静态币种
@@ -429,15 +453,19 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
Sources: []string{"static"},
})
}
return candidates, nil
return e.filterExcludedCoins(candidates), nil
}
return e.getOITopCoins(coinSource.OITopLimit)
coins, err := e.getOITopCoins(coinSource.OITopLimit)
if err != nil {
return nil, err
}
return e.filterExcludedCoins(coins), nil
case "mixed":
if coinSource.UseCoinPool {
poolCoins, err := e.getCoinPoolCoins(coinSource.CoinPoolLimit)
if coinSource.UseAI500 {
poolCoins, err := e.getAI500Coins(coinSource.AI500Limit)
if err != nil {
logger.Infof("⚠️ Failed to get AI500 coin pool: %v", err)
logger.Infof("⚠️ Failed to get AI500 coins: %v", err)
} else {
for _, coin := range poolCoins {
symbolSources[coin.Symbol] = append(symbolSources[coin.Symbol], "ai500")
@@ -471,19 +499,45 @@ func (e *StrategyEngine) GetCandidateCoins() ([]CandidateCoin, error) {
Sources: sources,
})
}
return candidates, nil
return e.filterExcludedCoins(candidates), nil
default:
return nil, fmt.Errorf("unknown coin source type: %s", coinSource.SourceType)
}
}
func (e *StrategyEngine) getCoinPoolCoins(limit int) ([]CandidateCoin, error) {
// filterExcludedCoins removes excluded coins from the candidates list
func (e *StrategyEngine) filterExcludedCoins(candidates []CandidateCoin) []CandidateCoin {
if len(e.config.CoinSource.ExcludedCoins) == 0 {
return candidates
}
// Build excluded set for O(1) lookup
excluded := make(map[string]bool)
for _, coin := range e.config.CoinSource.ExcludedCoins {
normalized := market.Normalize(coin)
excluded[normalized] = true
}
// Filter out excluded coins
filtered := make([]CandidateCoin, 0, len(candidates))
for _, c := range candidates {
if !excluded[c.Symbol] {
filtered = append(filtered, c)
} else {
logger.Infof("🚫 Excluded coin: %s", c.Symbol)
}
}
return filtered
}
func (e *StrategyEngine) getAI500Coins(limit int) ([]CandidateCoin, error) {
if limit <= 0 {
limit = 30
}
symbols, err := provider.GetTopRatedCoins(limit)
symbols, err := e.nofxosClient.GetTopRatedCoins(limit)
if err != nil {
return nil, err
}
@@ -503,7 +557,7 @@ func (e *StrategyEngine) getOITopCoins(limit int) ([]CandidateCoin, error) {
limit = 20
}
positions, err := provider.GetOITopPositions()
positions, err := e.nofxosClient.GetOITopPositions()
if err != nil {
return nil, err
}
@@ -610,50 +664,82 @@ func extractJSONPath(data interface{}, path string) interface{} {
// FetchQuantData fetches quantitative data for a single coin
func (e *StrategyEngine) FetchQuantData(symbol string) (*QuantData, error) {
if !e.config.Indicators.EnableQuantData || e.config.Indicators.QuantDataAPIURL == "" {
if !e.config.Indicators.EnableQuantData {
return nil, nil
}
apiURL := e.config.Indicators.QuantDataAPIURL
url := strings.Replace(apiURL, "{symbol}", symbol, -1)
// Use nofxos client with unified API key
include := "oi,price"
if e.config.Indicators.EnableQuantNetflow {
include = "netflow,oi,price"
}
// SSRF Protection: Validate URL before making request
resp, err := security.SafeGet(url, 10*time.Second)
nofxosData, err := e.nofxosClient.GetCoinData(symbol, include)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP status code: %d", resp.StatusCode)
return nil, fmt.Errorf("failed to fetch quant data: %w", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
if nofxosData == nil {
return nil, nil
}
var apiResp struct {
Code int `json:"code"`
Data *QuantData `json:"data"`
// Convert nofxos.QuantData to kernel.QuantData
quantData := &QuantData{
Symbol: nofxosData.Symbol,
Price: nofxosData.Price,
PriceChange: nofxosData.PriceChange,
}
if err := json.Unmarshal(body, &apiResp); err != nil {
return nil, fmt.Errorf("failed to parse JSON: %w", err)
// Convert OI data
if nofxosData.OI != nil {
quantData.OI = make(map[string]*OIData)
for exchange, oiData := range nofxosData.OI {
if oiData != nil {
kData := &OIData{
CurrentOI: oiData.CurrentOI,
}
if oiData.Delta != nil {
kData.Delta = make(map[string]*OIDeltaData)
for dur, delta := range oiData.Delta {
if delta != nil {
kData.Delta[dur] = &OIDeltaData{
OIDelta: delta.OIDelta,
OIDeltaValue: delta.OIDeltaValue,
OIDeltaPercent: delta.OIDeltaPercent,
}
}
}
}
quantData.OI[exchange] = kData
}
}
}
if apiResp.Code != 0 {
return nil, fmt.Errorf("API returned error code: %d", apiResp.Code)
// Convert Netflow data
if nofxosData.Netflow != nil {
quantData.Netflow = &NetflowData{}
if nofxosData.Netflow.Institution != nil {
quantData.Netflow.Institution = &FlowTypeData{
Future: nofxosData.Netflow.Institution.Future,
Spot: nofxosData.Netflow.Institution.Spot,
}
}
if nofxosData.Netflow.Personal != nil {
quantData.Netflow.Personal = &FlowTypeData{
Future: nofxosData.Netflow.Personal.Future,
Spot: nofxosData.Netflow.Personal.Spot,
}
}
}
return apiResp.Data, nil
return quantData, nil
}
// FetchQuantDataBatch batch fetches quantitative data
func (e *StrategyEngine) FetchQuantDataBatch(symbols []string) map[string]*QuantData {
result := make(map[string]*QuantData)
if !e.config.Indicators.EnableQuantData || e.config.Indicators.QuantDataAPIURL == "" {
if !e.config.Indicators.EnableQuantData {
return result
}
@@ -672,28 +758,12 @@ func (e *StrategyEngine) FetchQuantDataBatch(symbols []string) map[string]*Quant
}
// FetchOIRankingData fetches market-wide OI ranking data
func (e *StrategyEngine) FetchOIRankingData() *provider.OIRankingData {
func (e *StrategyEngine) FetchOIRankingData() *nofxos.OIRankingData {
indicators := e.config.Indicators
if !indicators.EnableOIRanking {
return nil
}
baseURL := indicators.OIRankingAPIURL
if baseURL == "" {
baseURL = "http://nofxaios.com:30006"
}
// Get auth key from existing API URL or use default
authKey := "cm_568c67eae410d912c54c"
if indicators.QuantDataAPIURL != "" {
if idx := strings.Index(indicators.QuantDataAPIURL, "auth="); idx != -1 {
authKey = indicators.QuantDataAPIURL[idx+5:]
if ampIdx := strings.Index(authKey, "&"); ampIdx != -1 {
authKey = authKey[:ampIdx]
}
}
}
duration := indicators.OIRankingDuration
if duration == "" {
duration = "1h"
@@ -706,7 +776,7 @@ func (e *StrategyEngine) FetchOIRankingData() *provider.OIRankingData {
logger.Infof("📊 Fetching OI ranking data (duration: %s, limit: %d)", duration, limit)
data, err := provider.GetOIRankingData(baseURL, authKey, duration, limit)
data, err := e.nofxosClient.GetOIRanking(duration, limit)
if err != nil {
logger.Warnf("⚠️ Failed to fetch OI ranking data: %v", err)
return nil
@@ -718,6 +788,68 @@ func (e *StrategyEngine) FetchOIRankingData() *provider.OIRankingData {
return data
}
// FetchNetFlowRankingData fetches market-wide NetFlow ranking data
func (e *StrategyEngine) FetchNetFlowRankingData() *nofxos.NetFlowRankingData {
indicators := e.config.Indicators
if !indicators.EnableNetFlowRanking {
return nil
}
duration := indicators.NetFlowRankingDuration
if duration == "" {
duration = "1h"
}
limit := indicators.NetFlowRankingLimit
if limit <= 0 {
limit = 10
}
logger.Infof("💰 Fetching NetFlow ranking data (duration: %s, limit: %d)", duration, limit)
data, err := e.nofxosClient.GetNetFlowRanking(duration, limit)
if err != nil {
logger.Warnf("⚠️ Failed to fetch NetFlow ranking data: %v", err)
return nil
}
logger.Infof("✓ NetFlow ranking data ready: inst_in=%d, inst_out=%d, retail_in=%d, retail_out=%d",
len(data.InstitutionFutureTop), len(data.InstitutionFutureLow),
len(data.PersonalFutureTop), len(data.PersonalFutureLow))
return data
}
// FetchPriceRankingData fetches market-wide price ranking data (gainers/losers)
func (e *StrategyEngine) FetchPriceRankingData() *nofxos.PriceRankingData {
indicators := e.config.Indicators
if !indicators.EnablePriceRanking {
return nil
}
durations := indicators.PriceRankingDuration
if durations == "" {
durations = "1h"
}
limit := indicators.PriceRankingLimit
if limit <= 0 {
limit = 10
}
logger.Infof("📈 Fetching Price ranking data (durations: %s, limit: %d)", durations, limit)
data, err := e.nofxosClient.GetPriceRanking(durations, limit)
if err != nil {
logger.Warnf("⚠️ Failed to fetch Price ranking data: %v", err)
return nil
}
logger.Infof("✓ Price ranking data ready for %d durations", len(data.Durations))
return data
}
// ============================================================================
// Prompt Building - System Prompt
// ============================================================================
@@ -729,7 +861,7 @@ func (e *StrategyEngine) BuildSystemPrompt(accountEquity float64, variant string
promptSections := e.config.PromptSections
// 0. Data Dictionary & Schema (ensure AI understands all fields)
lang := detectLanguage(promptSections.RoleDefinition)
lang := e.GetLanguage()
schemaPrompt := GetSchemaPrompt(lang)
sb.WriteString(schemaPrompt)
sb.WriteString("\n\n")
@@ -920,7 +1052,7 @@ func (e *StrategyEngine) writeAvailableIndicators(sb *strings.Builder) {
sb.WriteString("- Funding rate\n")
}
if len(e.config.CoinSource.StaticCoins) > 0 || e.config.CoinSource.UseCoinPool || e.config.CoinSource.UseOITop {
if len(e.config.CoinSource.StaticCoins) > 0 || e.config.CoinSource.UseAI500 || e.config.CoinSource.UseOITop {
sb.WriteString("- AI500 / OI_Top filter tags (if available)\n")
}
@@ -976,8 +1108,8 @@ func (e *StrategyEngine) BuildUserPrompt(ctx *Context) string {
// Historical trading statistics (helps AI understand past performance)
if ctx.TradingStats != nil && ctx.TradingStats.TotalTrades > 0 {
// Detect language from strategy config
lang := detectLanguage(e.config.PromptSections.RoleDefinition)
// Get language from strategy config
lang := e.GetLanguage()
// Win/Loss ratio
var winLossRatio float64
@@ -1081,9 +1213,25 @@ func (e *StrategyEngine) BuildUserPrompt(ctx *Context) string {
}
sb.WriteString("\n")
// Get language for market data formatting
nofxosLang := nofxos.LangEnglish
if e.GetLanguage() == LangChinese {
nofxosLang = nofxos.LangChinese
}
// OI Ranking data (market-wide open interest changes)
if ctx.OIRankingData != nil {
sb.WriteString(provider.FormatOIRankingForAI(ctx.OIRankingData))
sb.WriteString(nofxos.FormatOIRankingForAI(ctx.OIRankingData, nofxosLang))
}
// NetFlow Ranking data (market-wide fund flow)
if ctx.NetFlowRankingData != nil {
sb.WriteString(nofxos.FormatNetFlowRankingForAI(ctx.NetFlowRankingData, nofxosLang))
}
// Price Ranking data (market-wide gainers/losers)
if ctx.PriceRankingData != nil {
sb.WriteString(nofxos.FormatPriceRankingForAI(ctx.PriceRankingData, nofxosLang))
}
sb.WriteString("---\n\n")

View File

@@ -1,8 +1,9 @@
package decision
package kernel
import (
"fmt"
"nofx/market"
"nofx/provider/nofxos"
"sort"
"strings"
"time"
@@ -89,11 +90,11 @@ func formatContextData(ctx *Context, lang Language) string {
// 7. OI排名数据如果有
if ctx.OIRankingData != nil {
nofxosLang := nofxos.LangEnglish
if lang == LangChinese {
sb.WriteString(formatOIRankingZH(ctx.OIRankingData))
} else {
sb.WriteString(formatOIRankingEN(ctx.OIRankingData))
nofxosLang = nofxos.LangChinese
}
sb.WriteString(nofxos.FormatOIRankingForAI(ctx.OIRankingData, nofxosLang))
}
return sb.String()
@@ -354,11 +355,6 @@ func formatKlineDataZH(symbol string, tfData map[string]*market.TimeframeSeriesD
return sb.String()
}
// formatOIRankingZH 格式化OI排名数据中文
func formatOIRankingZH(oiData interface{}) string {
// TODO: 根据实际OIRankingData结构实现
return "## 市场持仓量排名\n\n(数据加载中...)\n\n"
}
// getOIInterpretationZH 获取OI变化解读中文
func getOIInterpretationZH(oiChange, priceChange string) string {
@@ -624,10 +620,6 @@ func formatKlineDataEN(symbol string, tfData map[string]*market.TimeframeSeriesD
return sb.String()
}
// formatOIRankingEN 格式化OI排名数据英文
func formatOIRankingEN(oiData interface{}) string {
return "## Market-wide OI Ranking\n\n(Loading data...)\n\n"
}
// getOIInterpretationEN 获取OI变化解读英文
func getOIInterpretationEN(oiChange, priceChange string) string {

View File

@@ -1,4 +1,4 @@
package decision
package kernel
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package decision
package kernel
import (
"strings"

View File

@@ -1,6 +1,4 @@
package decision
import "fmt"
package kernel
// ============================================================================
// Trading Data Schema - 交易数据字典
@@ -481,18 +479,6 @@ func getSchemaPromptZH() string {
prompt += formatFieldDefZH(key, field)
}
// 交易规则
prompt += "\n## ⚖️ 交易规则\n\n"
prompt += "### 风险管理\n"
for name, rule := range TradingRules.RiskManagement {
prompt += "- **" + name + "**: " + rule.DescZH + "\n 理由:" + rule.ReasonZH + "\n"
}
prompt += "\n### 出场信号\n"
for name, rule := range TradingRules.ExitSignals {
prompt += "- **" + name + "**: " + rule.DescZH + "\n 理由:" + rule.ReasonZH + "\n"
}
// OI解读
prompt += "\n## 💹 持仓量(OI)变化解读\n\n"
prompt += "- **OI增加 + 价格上涨**: " + OIInterpretation.OIUp_PriceUp.ZH + "\n"
@@ -500,14 +486,6 @@ func getSchemaPromptZH() string {
prompt += "- **OI减少 + 价格上涨**: " + OIInterpretation.OIDown_PriceUp.ZH + "\n"
prompt += "- **OI减少 + 价格下跌**: " + OIInterpretation.OIDown_PriceDown.ZH + "\n"
// 常见错误
prompt += "\n## ⚠️ 常见错误(请避免)\n\n"
for i, mistake := range CommonMistakes {
prompt += fmt.Sprintf("**错误%d**: %s\n", i+1, mistake.ErrorZH)
prompt += "- 错误示例:" + mistake.ExampleZH + "\n"
prompt += "- 正确做法:" + mistake.CorrectZH + "\n\n"
}
return prompt
}
@@ -540,18 +518,6 @@ func getSchemaPromptEN() string {
prompt += formatFieldDefEN(key, field)
}
// Trading Rules
prompt += "\n## ⚖️ Trading Rules\n\n"
prompt += "### Risk Management\n"
for name, rule := range TradingRules.RiskManagement {
prompt += "- **" + name + "**: " + rule.DescEN + "\n Reason: " + rule.ReasonEN + "\n"
}
prompt += "\n### Exit Signals\n"
for name, rule := range TradingRules.ExitSignals {
prompt += "- **" + name + "**: " + rule.DescEN + "\n Reason: " + rule.ReasonEN + "\n"
}
// OI Interpretation
prompt += "\n## 💹 Open Interest (OI) Change Interpretation\n\n"
prompt += "- **OI Up + Price Up**: " + OIInterpretation.OIUp_PriceUp.EN + "\n"
@@ -559,14 +525,6 @@ func getSchemaPromptEN() string {
prompt += "- **OI Down + Price Up**: " + OIInterpretation.OIDown_PriceUp.EN + "\n"
prompt += "- **OI Down + Price Down**: " + OIInterpretation.OIDown_PriceDown.EN + "\n"
// Common Mistakes
prompt += "\n## ⚠️ Common Mistakes to Avoid\n\n"
for i, mistake := range CommonMistakes {
prompt += fmt.Sprintf("**Mistake %d**: %s\n", i+1, mistake.ErrorEN)
prompt += "- Bad Example: " + mistake.ExampleEN + "\n"
prompt += "- Correct Approach: " + mistake.CorrectEN + "\n\n"
}
return prompt
}

View File

@@ -1,4 +1,4 @@
package decision
package kernel
import (
"strings"
@@ -147,10 +147,7 @@ func TestGetSchemaPrompt(t *testing.T) {
"交易指标",
"持仓指标",
"市场数据",
"交易规则",
"风险管理",
"持仓量(OI)变化解读",
"常见错误",
}
for _, keyword := range mustContain {
@@ -174,10 +171,7 @@ func TestGetSchemaPrompt(t *testing.T) {
"Trade Metrics",
"Position Metrics",
"Market Data",
"Trading Rules",
"Risk Management",
"Open Interest",
"Common Mistakes",
}
for _, keyword := range mustContain {

View File

@@ -1,4 +1,4 @@
package decision
package kernel
import (
"testing"

View File

@@ -78,7 +78,7 @@ func main() {
logger.Fatalf("❌ Failed to initialize database: %v", err)
}
defer st.Close()
backtest.UseDatabase(st.DB())
backtest.UseDatabaseWithType(st.DB(), st.DBType() == store.DBTypePostgres)
// Initialize installation ID for experience improvement (anonymous statistics)
initInstallationID(st)

View File

@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"nofx/debate"
"nofx/decision"
"nofx/kernel"
"nofx/logger"
"nofx/store"
"nofx/trader"
@@ -19,7 +19,7 @@ type TraderExecutorAdapter struct {
}
// ExecuteDecision executes a trading decision
func (a *TraderExecutorAdapter) ExecuteDecision(d *decision.Decision) error {
func (a *TraderExecutorAdapter) ExecuteDecision(d *kernel.Decision) error {
return a.autoTrader.ExecuteDecision(d)
}
@@ -410,11 +410,18 @@ func (tm *TraderManager) GetTopTradersData() (map[string]interface{}, error) {
// RemoveTrader removes a trader from memory (does not affect database)
// Used to force reload when updating trader configuration
// If the trader is running, it will be stopped first
func (tm *TraderManager) RemoveTrader(traderID string) {
tm.mu.Lock()
defer tm.mu.Unlock()
if _, exists := tm.traders[traderID]; exists {
if t, exists := tm.traders[traderID]; exists {
// Stop the trader if it's running (this ensures the goroutine exits)
status := t.GetStatus()
if isRunning, ok := status["is_running"].(bool); ok && isRunning {
logger.Infof("⏹ Stopping trader %s before removing from memory...", traderID)
t.Stop()
}
delete(tm.traders, traderID)
logger.Infof("✓ Trader %s removed from memory", traderID)
}
@@ -606,7 +613,7 @@ func (tm *TraderManager) LoadTradersFromStore(st *store.Store) error {
continue
}
// Add to TraderManager (coinPoolURL/oiTopURL already obtained from strategy config)
// Add to TraderManager (ai500APIURL/oiTopAPIURL already obtained from strategy config)
err = tm.addTraderFromStore(traderCfg, aiModelCfg, exchangeCfg, st)
if err != nil {
logger.Infof("❌ Failed to add trader %s: %v", traderCfg.Name, err)
@@ -641,7 +648,7 @@ func (tm *TraderManager) addTraderFromStore(traderCfg *store.Trader, aiModelCfg
return fmt.Errorf("trader %s has no strategy configured", traderCfg.Name)
}
// Build AutoTraderConfig (coinPoolURL/oiTopURL obtained from strategy config, used in StrategyEngine)
// Build AutoTraderConfig (ai500APIURL/oiTopAPIURL obtained from strategy config, used in StrategyEngine)
traderConfig := trader.AutoTraderConfig{
ID: traderCfg.ID,
Name: traderCfg.Name,
@@ -664,6 +671,9 @@ func (tm *TraderManager) addTraderFromStore(traderCfg *store.Trader, aiModelCfg
StrategyConfig: strategyConfig,
}
logger.Infof("📊 Loading trader %s: ScanIntervalMinutes=%d (from DB), ScanInterval=%v",
traderCfg.Name, traderCfg.ScanIntervalMinutes, traderConfig.ScanInterval)
// Set API keys based on exchange type (convert EncryptedString to string)
switch exchangeCfg.ExchangeType {
case "binance":

View File

@@ -1,593 +0,0 @@
package provider
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"nofx/security"
"strings"
"time"
)
// AI500Config AI500 data provider configuration
type AI500Config struct {
APIURL string
Timeout time.Duration
}
var ai500Config = AI500Config{
APIURL: "",
Timeout: 30 * time.Second,
}
// CoinData coin information
type CoinData struct {
Pair string `json:"pair"` // Trading pair symbol (e.g.: BTCUSDT)
Score float64 `json:"score"` // Current score
StartTime int64 `json:"start_time"` // Start time (Unix timestamp)
StartPrice float64 `json:"start_price"` // Start price
LastScore float64 `json:"last_score"` // Latest score
MaxScore float64 `json:"max_score"` // Highest score
MaxPrice float64 `json:"max_price"` // Highest price
IncreasePercent float64 `json:"increase_percent"` // Increase percentage
IsAvailable bool `json:"-"` // Whether tradable (internal use)
}
// AI500APIResponse raw data structure returned by AI500 API
type AI500APIResponse struct {
Success bool `json:"success"`
Data struct {
Coins []CoinData `json:"coins"`
Count int `json:"count"`
} `json:"data"`
}
// SetAI500API sets AI500 data provider API
func SetAI500API(apiURL string) {
ai500Config.APIURL = apiURL
}
// SetOITopAPI sets OI Top API
func SetOITopAPI(apiURL string) {
oiTopConfig.APIURL = apiURL
}
// GetAI500Data retrieves AI500 coin list (with retry mechanism)
func GetAI500Data() ([]CoinData, error) {
// Check if API URL is configured
if strings.TrimSpace(ai500Config.APIURL) == "" {
return nil, fmt.Errorf("AI500 API URL not configured")
}
maxRetries := 3
var lastErr error
// Try to fetch from API
for attempt := 1; attempt <= maxRetries; attempt++ {
if attempt > 1 {
log.Printf("⚠️ Retry attempt %d of %d to fetch AI500 data...", attempt, maxRetries)
time.Sleep(2 * time.Second)
}
coins, err := fetchAI500()
if err == nil {
if attempt > 1 {
log.Printf("✓ Retry attempt %d succeeded", attempt)
}
return coins, nil
}
lastErr = err
log.Printf("❌ Request attempt %d failed: %v", attempt, err)
}
return nil, fmt.Errorf("all API requests failed: %w", lastErr)
}
// fetchAI500 actually executes AI500 request
func fetchAI500() ([]CoinData, error) {
log.Printf("🔄 Requesting AI500 data...")
// SSRF Protection: Validate URL before making request
resp, err := security.SafeGet(ai500Config.APIURL, ai500Config.Timeout)
if err != nil {
return nil, fmt.Errorf("failed to request AI500 API: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned error (status %d): %s", resp.StatusCode, string(body))
}
// Parse API response
var response AI500APIResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("JSON parsing failed: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("API returned failure status")
}
if len(response.Data.Coins) == 0 {
return nil, fmt.Errorf("coin list is empty")
}
// Set IsAvailable flag
coins := response.Data.Coins
for i := range coins {
coins[i].IsAvailable = true
}
log.Printf("✓ Successfully fetched %d coins", len(coins))
return coins, nil
}
// GetAvailableCoins retrieves available coin list (filters out unavailable ones)
func GetAvailableCoins() ([]string, error) {
coins, err := GetAI500Data()
if err != nil {
return nil, err
}
var symbols []string
for _, coin := range coins {
if coin.IsAvailable {
symbol := normalizeSymbol(coin.Pair)
symbols = append(symbols, symbol)
}
}
if len(symbols) == 0 {
return nil, fmt.Errorf("no available coins")
}
return symbols, nil
}
// GetTopRatedCoins retrieves top N coins by score (sorted by score descending)
func GetTopRatedCoins(limit int) ([]string, error) {
coins, err := GetAI500Data()
if err != nil {
return nil, err
}
// Filter available coins
var availableCoins []CoinData
for _, coin := range coins {
if coin.IsAvailable {
availableCoins = append(availableCoins, coin)
}
}
if len(availableCoins) == 0 {
return nil, fmt.Errorf("no available coins")
}
// Sort by Score descending (bubble sort)
for i := 0; i < len(availableCoins); i++ {
for j := i + 1; j < len(availableCoins); j++ {
if availableCoins[i].Score < availableCoins[j].Score {
availableCoins[i], availableCoins[j] = availableCoins[j], availableCoins[i]
}
}
}
// Take top N
maxCount := limit
if len(availableCoins) < maxCount {
maxCount = len(availableCoins)
}
var symbols []string
for i := 0; i < maxCount; i++ {
symbol := normalizeSymbol(availableCoins[i].Pair)
symbols = append(symbols, symbol)
}
return symbols, nil
}
// normalizeSymbol normalizes coin symbol
func normalizeSymbol(symbol string) string {
symbol = trimSpaces(symbol)
symbol = toUpper(symbol)
if !endsWith(symbol, "USDT") {
symbol = symbol + "USDT"
}
return symbol
}
// Helper functions
func trimSpaces(s string) string {
result := ""
for i := 0; i < len(s); i++ {
if s[i] != ' ' {
result += string(s[i])
}
}
return result
}
func toUpper(s string) string {
result := ""
for i := 0; i < len(s); i++ {
c := s[i]
if c >= 'a' && c <= 'z' {
c = c - 'a' + 'A'
}
result += string(c)
}
return result
}
func endsWith(s, suffix string) bool {
if len(s) < len(suffix) {
return false
}
return s[len(s)-len(suffix):] == suffix
}
// ========== OI Top (Open Interest Growth Top 20) Data ==========
// OIPosition open interest data
type OIPosition struct {
Symbol string `json:"symbol"`
Rank int `json:"rank"`
CurrentOI float64 `json:"current_oi"`
OIDelta float64 `json:"oi_delta"`
OIDeltaPercent float64 `json:"oi_delta_percent"`
OIDeltaValue float64 `json:"oi_delta_value"`
PriceDeltaPercent float64 `json:"price_delta_percent"`
NetLong float64 `json:"net_long"`
NetShort float64 `json:"net_short"`
}
// OITopAPIResponse data structure returned by OI Top API
type OITopAPIResponse struct {
Code int `json:"code"`
Data struct {
Positions []OIPosition `json:"positions"`
Count int `json:"count"`
Exchange string `json:"exchange"`
TimeRange string `json:"time_range"`
TimeRangeParam string `json:"time_range_param"`
RankType string `json:"rank_type"`
Limit int `json:"limit"`
} `json:"data"`
}
var oiTopConfig = struct {
APIURL string
Timeout time.Duration
}{
APIURL: "",
Timeout: 30 * time.Second,
}
// GetOITopPositions retrieves OI Top 20 data (with retry)
func GetOITopPositions() ([]OIPosition, error) {
if strings.TrimSpace(oiTopConfig.APIURL) == "" {
log.Printf("⚠️ OI Top API URL not configured, skipping OI Top data fetch")
return []OIPosition{}, nil
}
maxRetries := 3
var lastErr error
for attempt := 1; attempt <= maxRetries; attempt++ {
if attempt > 1 {
log.Printf("⚠️ Retry attempt %d of %d to fetch OI Top data...", attempt, maxRetries)
time.Sleep(2 * time.Second)
}
positions, err := fetchOITop()
if err == nil {
if attempt > 1 {
log.Printf("✓ Retry attempt %d succeeded", attempt)
}
return positions, nil
}
lastErr = err
log.Printf("❌ OI Top request attempt %d failed: %v", attempt, err)
}
log.Printf("⚠️ All OI Top API requests failed (last error: %v), skipping OI Top data", lastErr)
return []OIPosition{}, nil
}
// fetchOITop actually executes OI Top request
func fetchOITop() ([]OIPosition, error) {
log.Printf("🔄 Requesting OI Top data...")
// SSRF Protection: Validate URL before making request
resp, err := security.SafeGet(oiTopConfig.APIURL, oiTopConfig.Timeout)
if err != nil {
return nil, fmt.Errorf("failed to request OI Top API: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read OI Top response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("OI Top API returned error (status %d): %s", resp.StatusCode, string(body))
}
var response OITopAPIResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("OI Top JSON parsing failed: %w", err)
}
if response.Code != 0 {
return nil, fmt.Errorf("OI Top API returned error code: %d", response.Code)
}
if len(response.Data.Positions) == 0 {
return nil, fmt.Errorf("OI Top position list is empty")
}
log.Printf("✓ Successfully fetched %d OI Top coins (time range: %s, type: %s)",
len(response.Data.Positions), response.Data.TimeRange, response.Data.RankType)
return response.Data.Positions, nil
}
// GetOITopSymbols retrieves OI Top coin symbol list
func GetOITopSymbols() ([]string, error) {
positions, err := GetOITopPositions()
if err != nil {
return nil, err
}
var symbols []string
for _, pos := range positions {
symbol := normalizeSymbol(pos.Symbol)
symbols = append(symbols, symbol)
}
return symbols, nil
}
// MergedData merged data (AI500 + OI Top)
type MergedData struct {
AI500Coins []CoinData
OITopCoins []OIPosition
AllSymbols []string
SymbolSources map[string][]string
}
// OIRankingData OI ranking data for debate (includes both top and low)
type OIRankingData struct {
TimeRange string `json:"time_range"`
Duration string `json:"duration"`
TopPositions []OIPosition `json:"top_positions"`
LowPositions []OIPosition `json:"low_positions"`
FetchedAt time.Time `json:"fetched_at"`
}
// GetOIRankingData retrieves OI ranking data (both top increase and low decrease)
func GetOIRankingData(baseURL, authKey string, duration string, limit int) (*OIRankingData, error) {
if baseURL == "" || authKey == "" {
return nil, fmt.Errorf("OI API URL or auth key not configured")
}
if duration == "" {
duration = "1h"
}
if limit <= 0 {
limit = 20
}
result := &OIRankingData{
Duration: duration,
FetchedAt: time.Now(),
}
// Fetch top ranking
topURL := fmt.Sprintf("%s/api/oi/top-ranking?limit=%d&duration=%s&auth=%s", baseURL, limit, duration, authKey)
topPositions, timeRange, err := fetchOIRanking(topURL)
if err != nil {
log.Printf("⚠️ Failed to fetch OI top ranking: %v", err)
} else {
result.TopPositions = topPositions
result.TimeRange = timeRange
}
// Fetch low ranking
lowURL := fmt.Sprintf("%s/api/oi/low-ranking?limit=%d&duration=%s&auth=%s", baseURL, limit, duration, authKey)
lowPositions, _, err := fetchOIRanking(lowURL)
if err != nil {
log.Printf("⚠️ Failed to fetch OI low ranking: %v", err)
} else {
result.LowPositions = lowPositions
}
log.Printf("✓ Fetched OI ranking data: %d top, %d low (duration: %s)",
len(result.TopPositions), len(result.LowPositions), duration)
return result, nil
}
// fetchOIRanking fetches OI ranking from a single endpoint
func fetchOIRanking(url string) ([]OIPosition, string, error) {
// SSRF Protection: Validate URL before making request
resp, err := security.SafeGet(url, 30*time.Second)
if err != nil {
return nil, "", fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, "", fmt.Errorf("failed to read response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, "", fmt.Errorf("API returned error (status %d): %s", resp.StatusCode, string(body))
}
var response OITopAPIResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, "", fmt.Errorf("JSON parsing failed: %w", err)
}
if response.Code != 0 {
return nil, "", fmt.Errorf("API returned error code: %d", response.Code)
}
return response.Data.Positions, response.Data.TimeRange, nil
}
// FormatOIRankingForAI formats OI ranking data for AI consumption
func FormatOIRankingForAI(data *OIRankingData) string {
if data == nil {
return ""
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## 📊 市场持仓量变化数据 (Open Interest Changes in %s / %s)\n\n", data.TimeRange, data.Duration))
if len(data.TopPositions) > 0 {
sb.WriteString("### 🔺 持仓量增加排行 (OI Increase Ranking)\n")
sb.WriteString("市场资金正在流入以下币种,可能表示趋势延续或新仓位建立:\n\n")
sb.WriteString("| 排名 | 币种 | 持仓变化值(USDT) | 变化幅度 | 价格变化 |\n")
sb.WriteString("|------|------|------------------|----------|----------|\n")
for _, pos := range data.TopPositions {
sb.WriteString(fmt.Sprintf("| #%d | %s | %s | %+.2f%% | %+.2f%% |\n",
pos.Rank,
pos.Symbol,
formatOIValue(pos.OIDeltaValue),
pos.OIDeltaPercent,
pos.PriceDeltaPercent,
))
}
sb.WriteString("\n")
sb.WriteString("**解读**: 持仓增加 + 价格上涨 = 多头主导; 持仓增加 + 价格下跌 = 空头主导\n\n")
}
if len(data.LowPositions) > 0 {
sb.WriteString("### 🔻 持仓量减少排行 (OI Decrease Ranking)\n")
sb.WriteString("市场资金正在流出以下币种,可能表示趋势反转或仓位平仓:\n\n")
sb.WriteString("| 排名 | 币种 | 持仓变化值(USDT) | 变化幅度 | 价格变化 |\n")
sb.WriteString("|------|------|------------------|----------|----------|\n")
for _, pos := range data.LowPositions {
sb.WriteString(fmt.Sprintf("| #%d | %s | %s | %+.2f%% | %+.2f%% |\n",
pos.Rank,
pos.Symbol,
formatOIValue(pos.OIDeltaValue),
pos.OIDeltaPercent,
pos.PriceDeltaPercent,
))
}
sb.WriteString("\n")
sb.WriteString("**解读**: 持仓减少 + 价格上涨 = 空头平仓(反弹); 持仓减少 + 价格下跌 = 多头平仓(回调)\n\n")
}
return sb.String()
}
// formatOIValue formats OI value for display
func formatOIValue(v float64) string {
sign := ""
if v >= 0 {
sign = "+"
}
absV := v
if absV < 0 {
absV = -absV
}
if absV >= 1e9 {
return fmt.Sprintf("%s%.2fB", sign, v/1e9)
} else if absV >= 1e6 {
return fmt.Sprintf("%s%.2fM", sign, v/1e6)
} else if absV >= 1e3 {
return fmt.Sprintf("%s%.2fK", sign, v/1e3)
}
return fmt.Sprintf("%s%.2f", sign, v)
}
// GetMergedData retrieves merged data (AI500 + OI Top, deduplicated)
func GetMergedData(ai500Limit int) (*MergedData, error) {
ai500TopSymbols, err := GetTopRatedCoins(ai500Limit)
if err != nil {
log.Printf("⚠️ Failed to get AI500 data: %v", err)
ai500TopSymbols = []string{}
}
oiTopSymbols, err := GetOITopSymbols()
if err != nil {
log.Printf("⚠️ Failed to get OI Top data: %v", err)
oiTopSymbols = []string{}
}
symbolSet := make(map[string]bool)
symbolSources := make(map[string][]string)
for _, symbol := range ai500TopSymbols {
symbolSet[symbol] = true
symbolSources[symbol] = append(symbolSources[symbol], "ai500")
}
for _, symbol := range oiTopSymbols {
if !symbolSet[symbol] {
symbolSet[symbol] = true
}
symbolSources[symbol] = append(symbolSources[symbol], "oi_top")
}
var allSymbols []string
for symbol := range symbolSet {
allSymbols = append(allSymbols, symbol)
}
ai500Coins, _ := GetAI500Data()
oiTopPositions, _ := GetOITopPositions()
merged := &MergedData{
AI500Coins: ai500Coins,
OITopCoins: oiTopPositions,
AllSymbols: allSymbols,
SymbolSources: symbolSources,
}
log.Printf("📊 Data merge complete: AI500=%d, OI_Top=%d, Total(deduplicated)=%d",
len(ai500TopSymbols), len(oiTopSymbols), len(allSymbols))
return merged, nil
}
// ========== Backward Compatibility Aliases ==========
// Deprecated: Use SetAI500API instead
func SetCoinPoolAPI(apiURL string) {
SetAI500API(apiURL)
}
// Deprecated: Use GetAI500Data instead
func GetCoinPool() ([]CoinData, error) {
return GetAI500Data()
}
// Deprecated: Use MergedData instead
type MergedCoinPool = MergedData
// Deprecated: Use GetMergedData instead
func GetMergedCoinPool(ai500Limit int) (*MergedData, error) {
return GetMergedData(ai500Limit)
}
// Deprecated: Use CoinData instead
type CoinInfo = CoinData

163
provider/nofxos/ai500.go Normal file
View File

@@ -0,0 +1,163 @@
package nofxos
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
// CoinData represents AI500 coin information
type CoinData struct {
Pair string `json:"pair"` // Trading pair symbol (e.g.: BTCUSDT)
Score float64 `json:"score"` // Current AI score (0-100)
StartTime int64 `json:"start_time"` // Start time (Unix timestamp)
StartPrice float64 `json:"start_price"` // Start price
LastScore float64 `json:"last_score"` // Latest score
MaxScore float64 `json:"max_score"` // Highest score
MaxPrice float64 `json:"max_price"` // Highest price
IncreasePercent float64 `json:"increase_percent"` // Increase percentage (already x100)
IsAvailable bool `json:"-"` // Whether tradable (internal use)
}
// AI500Response is the API response structure
type AI500Response struct {
Success bool `json:"success"`
Data struct {
Coins []CoinData `json:"coins"`
Count int `json:"count"`
} `json:"data"`
}
// GetAI500List retrieves AI500 coin list with retry mechanism
func (c *Client) GetAI500List() ([]CoinData, error) {
maxRetries := 3
var lastErr error
for attempt := 1; attempt <= maxRetries; attempt++ {
if attempt > 1 {
log.Printf("⚠️ Retry attempt %d of %d to fetch AI500 data...", attempt, maxRetries)
time.Sleep(2 * time.Second)
}
coins, err := c.fetchAI500()
if err == nil {
if attempt > 1 {
log.Printf("✓ Retry attempt %d succeeded", attempt)
}
return coins, nil
}
lastErr = err
log.Printf("❌ AI500 request attempt %d failed: %v", attempt, err)
}
return nil, fmt.Errorf("all AI500 API requests failed: %w", lastErr)
}
func (c *Client) fetchAI500() ([]CoinData, error) {
log.Printf("🔄 Requesting AI500 data from %s...", c.GetBaseURL())
body, err := c.doRequest("/api/ai500/list")
if err != nil {
return nil, fmt.Errorf("failed to request AI500 API: %w", err)
}
var response AI500Response
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("JSON parsing failed: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("API returned failure status")
}
if len(response.Data.Coins) == 0 {
return nil, fmt.Errorf("coin list is empty")
}
// Set IsAvailable flag
coins := response.Data.Coins
for i := range coins {
coins[i].IsAvailable = true
}
log.Printf("✓ Successfully fetched %d AI500 coins", len(coins))
return coins, nil
}
// GetTopRatedCoins retrieves top N coins by score (sorted descending)
func (c *Client) GetTopRatedCoins(limit int) ([]string, error) {
coins, err := c.GetAI500List()
if err != nil {
return nil, err
}
// Filter available coins
var availableCoins []CoinData
for _, coin := range coins {
if coin.IsAvailable {
availableCoins = append(availableCoins, coin)
}
}
if len(availableCoins) == 0 {
return nil, fmt.Errorf("no available coins")
}
// Sort by Score descending (bubble sort)
for i := 0; i < len(availableCoins); i++ {
for j := i + 1; j < len(availableCoins); j++ {
if availableCoins[i].Score < availableCoins[j].Score {
availableCoins[i], availableCoins[j] = availableCoins[j], availableCoins[i]
}
}
}
// Take top N
maxCount := limit
if len(availableCoins) < maxCount {
maxCount = len(availableCoins)
}
var symbols []string
for i := 0; i < maxCount; i++ {
symbol := NormalizeSymbol(availableCoins[i].Pair)
symbols = append(symbols, symbol)
}
return symbols, nil
}
// GetAvailableCoins retrieves all available coin symbols
func (c *Client) GetAvailableCoins() ([]string, error) {
coins, err := c.GetAI500List()
if err != nil {
return nil, err
}
var symbols []string
for _, coin := range coins {
if coin.IsAvailable {
symbol := NormalizeSymbol(coin.Pair)
symbols = append(symbols, symbol)
}
}
if len(symbols) == 0 {
return nil, fmt.Errorf("no available coins")
}
return symbols, nil
}
// NormalizeSymbol normalizes coin symbol to XXXUSDT format
func NormalizeSymbol(symbol string) string {
symbol = strings.TrimSpace(symbol)
symbol = strings.ToUpper(symbol)
if !strings.HasSuffix(symbol, "USDT") {
symbol = symbol + "USDT"
}
return symbol
}

146
provider/nofxos/client.go Normal file
View File

@@ -0,0 +1,146 @@
// Package nofxos provides data access to the NofxOS API (https://nofxos.ai)
// for quantitative trading data including AI500 scores, OI rankings,
// fund flow (NetFlow), price rankings, and coin details.
package nofxos
import (
"io/ioutil"
"net/http"
"nofx/security"
"strings"
"sync"
"time"
)
// Default configuration
const (
DefaultBaseURL = "https://nofxos.ai"
DefaultTimeout = 30 * time.Second
DefaultAuthKey = "cm_568c67eae410d912c54c"
)
// Client is the NofxOS API client
type Client struct {
BaseURL string
AuthKey string
Timeout time.Duration
mu sync.RWMutex
}
var (
defaultClient *Client
clientOnce sync.Once
)
// DefaultClient returns the singleton default client
func DefaultClient() *Client {
clientOnce.Do(func() {
defaultClient = &Client{
BaseURL: DefaultBaseURL,
AuthKey: DefaultAuthKey,
Timeout: DefaultTimeout,
}
})
return defaultClient
}
// NewClient creates a new NofxOS API client
func NewClient(baseURL, authKey string) *Client {
if baseURL == "" {
baseURL = DefaultBaseURL
}
if authKey == "" {
authKey = DefaultAuthKey
}
return &Client{
BaseURL: baseURL,
AuthKey: authKey,
Timeout: DefaultTimeout,
}
}
// SetConfig updates client configuration
func (c *Client) SetConfig(baseURL, authKey string) {
c.mu.Lock()
defer c.mu.Unlock()
if baseURL != "" {
c.BaseURL = baseURL
}
if authKey != "" {
c.AuthKey = authKey
}
}
// GetBaseURL returns the current base URL
func (c *Client) GetBaseURL() string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.BaseURL
}
// GetAuthKey returns the current auth key
func (c *Client) GetAuthKey() string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.AuthKey
}
// doRequest performs an HTTP GET request with authentication
func (c *Client) doRequest(endpoint string) ([]byte, error) {
c.mu.RLock()
baseURL := c.BaseURL
authKey := c.AuthKey
timeout := c.Timeout
c.mu.RUnlock()
url := baseURL + endpoint
if !strings.Contains(url, "auth=") {
if strings.Contains(url, "?") {
url += "&auth=" + authKey
} else {
url += "?auth=" + authKey
}
}
resp, err := security.SafeGet(url, timeout)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return body, &APIError{
StatusCode: resp.StatusCode,
Message: string(body),
}
}
return body, nil
}
// APIError represents an API error response
type APIError struct {
StatusCode int
Message string
}
func (e *APIError) Error() string {
return e.Message
}
// ExtractAuthKey extracts auth key from a URL string
func ExtractAuthKey(url string) string {
if idx := strings.Index(url, "auth="); idx != -1 {
authKey := url[idx+5:]
if ampIdx := strings.Index(authKey, "&"); ampIdx != -1 {
authKey = authKey[:ampIdx]
}
return authKey
}
return ""
}

216
provider/nofxos/coin.go Normal file
View File

@@ -0,0 +1,216 @@
package nofxos
import (
"encoding/json"
"fmt"
"log"
"strings"
)
// QuantData represents quantitative data for a single coin
type QuantData struct {
Symbol string `json:"symbol"`
Price float64 `json:"price"`
Netflow *NetflowData `json:"netflow,omitempty"`
OI map[string]*OIData `json:"oi,omitempty"` // keyed by exchange: "binance", "bybit"
PriceChange map[string]float64 `json:"price_change,omitempty"` // keyed by duration: "1h", "4h", etc.
}
// NetflowData contains fund flow data
type NetflowData struct {
Institution *FlowTypeData `json:"institution,omitempty"`
Personal *FlowTypeData `json:"personal,omitempty"`
}
// FlowTypeData contains flow data by trade type
type FlowTypeData struct {
Future map[string]float64 `json:"future,omitempty"` // keyed by duration
Spot map[string]float64 `json:"spot,omitempty"` // keyed by duration
}
// OIData contains open interest data for an exchange
type OIData struct {
CurrentOI float64 `json:"current_oi"`
NetLong float64 `json:"net_long"`
NetShort float64 `json:"net_short"`
Delta map[string]*OIDeltaData `json:"delta,omitempty"` // keyed by duration
}
// OIDeltaData contains OI change data
type OIDeltaData struct {
OIDelta float64 `json:"oi_delta"`
OIDeltaValue float64 `json:"oi_delta_value"`
OIDeltaPercent float64 `json:"oi_delta_percent"` // Already x100
}
// CoinResponse is the API response structure for coin details
type CoinResponse struct {
Success bool `json:"success"`
Code int `json:"code"`
Data *QuantData `json:"data"`
}
// GetCoinData retrieves quantitative data for a single coin
func (c *Client) GetCoinData(symbol string, include string) (*QuantData, error) {
if symbol == "" {
return nil, fmt.Errorf("symbol is required")
}
if include == "" {
include = "netflow,oi,price"
}
// Normalize symbol (remove USDT suffix for API call if needed)
symbol = strings.TrimSuffix(strings.ToUpper(symbol), "USDT")
endpoint := fmt.Sprintf("/api/coin/%s?include=%s", symbol, include)
body, err := c.doRequest(endpoint)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
var response CoinResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("JSON parsing failed: %w", err)
}
// Check for success (support both success field and code field)
if !response.Success && response.Code != 0 {
return nil, fmt.Errorf("API returned error code: %d", response.Code)
}
return response.Data, nil
}
// GetCoinDataBatch retrieves quantitative data for multiple coins
func (c *Client) GetCoinDataBatch(symbols []string, include string) map[string]*QuantData {
result := make(map[string]*QuantData)
for _, symbol := range symbols {
data, err := c.GetCoinData(symbol, include)
if err != nil {
log.Printf("⚠️ Failed to fetch coin data for %s: %v", symbol, err)
continue
}
if data != nil {
// Use normalized symbol as key
normalizedSymbol := NormalizeSymbol(symbol)
result[normalizedSymbol] = data
}
}
return result
}
// FormatQuantDataForAI formats single coin quant data for AI consumption
func FormatQuantDataForAI(symbol string, data *QuantData, lang Language) string {
if data == nil {
return ""
}
if lang == LangChinese {
return formatQuantDataZH(symbol, data)
}
return formatQuantDataEN(symbol, data)
}
func formatQuantDataZH(symbol string, data *QuantData) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("### %s 量化数据\n", symbol))
sb.WriteString(fmt.Sprintf("价格: $%.4f\n\n", data.Price))
if len(data.PriceChange) > 0 {
sb.WriteString("**价格变化**:\n")
durations := []string{"1h", "4h", "8h", "12h", "24h"}
for _, d := range durations {
if change, ok := data.PriceChange[d]; ok {
sb.WriteString(fmt.Sprintf("- %s: %+.2f%%\n", d, change*100))
}
}
sb.WriteString("\n")
}
if len(data.OI) > 0 {
for exchange, oiData := range data.OI {
if oiData != nil {
sb.WriteString(fmt.Sprintf("**%s持仓**:\n", strings.ToUpper(exchange)))
sb.WriteString(fmt.Sprintf("- OI: %.2f\n", oiData.CurrentOI))
if oiData.NetLong > 0 || oiData.NetShort > 0 {
sb.WriteString(fmt.Sprintf("- 多头: %.2f, 空头: %.2f\n", oiData.NetLong, oiData.NetShort))
}
if oiData.Delta != nil {
if delta, ok := oiData.Delta["1h"]; ok && delta != nil {
sb.WriteString(fmt.Sprintf("- 1h变化: %s (%.2f%%)\n",
formatValue(delta.OIDeltaValue), delta.OIDeltaPercent))
}
}
sb.WriteString("\n")
}
}
}
if data.Netflow != nil && data.Netflow.Institution != nil && data.Netflow.Institution.Future != nil {
sb.WriteString("**机构资金流**:\n")
durations := []string{"1h", "4h", "24h"}
for _, d := range durations {
if flow, ok := data.Netflow.Institution.Future[d]; ok {
sb.WriteString(fmt.Sprintf("- %s: %s\n", d, formatValue(flow)))
}
}
sb.WriteString("\n")
}
return sb.String()
}
func formatQuantDataEN(symbol string, data *QuantData) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("### %s Quant Data\n", symbol))
sb.WriteString(fmt.Sprintf("Price: $%.4f\n\n", data.Price))
if len(data.PriceChange) > 0 {
sb.WriteString("**Price Change**:\n")
durations := []string{"1h", "4h", "8h", "12h", "24h"}
for _, d := range durations {
if change, ok := data.PriceChange[d]; ok {
sb.WriteString(fmt.Sprintf("- %s: %+.2f%%\n", d, change*100))
}
}
sb.WriteString("\n")
}
if len(data.OI) > 0 {
for exchange, oiData := range data.OI {
if oiData != nil {
sb.WriteString(fmt.Sprintf("**%s OI**:\n", strings.ToUpper(exchange)))
sb.WriteString(fmt.Sprintf("- Current OI: %.2f\n", oiData.CurrentOI))
if oiData.NetLong > 0 || oiData.NetShort > 0 {
sb.WriteString(fmt.Sprintf("- Net Long: %.2f, Net Short: %.2f\n", oiData.NetLong, oiData.NetShort))
}
if oiData.Delta != nil {
if delta, ok := oiData.Delta["1h"]; ok && delta != nil {
sb.WriteString(fmt.Sprintf("- 1h Change: %s (%.2f%%)\n",
formatValue(delta.OIDeltaValue), delta.OIDeltaPercent))
}
}
sb.WriteString("\n")
}
}
}
if data.Netflow != nil && data.Netflow.Institution != nil && data.Netflow.Institution.Future != nil {
sb.WriteString("**Institution Fund Flow**:\n")
durations := []string{"1h", "4h", "24h"}
for _, d := range durations {
if flow, ok := data.Netflow.Institution.Future[d]; ok {
sb.WriteString(fmt.Sprintf("- %s: %s\n", d, formatValue(flow)))
}
}
sb.WriteString("\n")
}
return sb.String()
}

263
provider/nofxos/netflow.go Normal file
View File

@@ -0,0 +1,263 @@
package nofxos
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
// NetFlowPosition represents fund flow data for a single coin
type NetFlowPosition struct {
Rank int `json:"rank"`
Symbol string `json:"symbol"`
Amount float64 `json:"amount"` // Fund flow amount in USDT (positive=inflow, negative=outflow)
Price float64 `json:"price"`
}
// NetFlowResponse is the API response structure
type NetFlowResponse struct {
Success bool `json:"success"`
Data struct {
Netflows []NetFlowPosition `json:"netflows"`
Count int `json:"count"`
Type string `json:"type"` // institution or personal
Trade string `json:"trade"` // 合约 or 现货
TimeRange string `json:"time_range"`
RankType string `json:"rank_type"` // top or low
Limit int `json:"limit"`
} `json:"data"`
}
// NetFlowRankingData contains institution and personal fund flow rankings
type NetFlowRankingData struct {
Duration string `json:"duration"`
TimeRange string `json:"time_range"`
InstitutionFutureTop []NetFlowPosition `json:"institution_future_top"`
InstitutionFutureLow []NetFlowPosition `json:"institution_future_low"`
PersonalFutureTop []NetFlowPosition `json:"personal_future_top"`
PersonalFutureLow []NetFlowPosition `json:"personal_future_low"`
FetchedAt time.Time `json:"fetched_at"`
}
// GetNetFlowRanking retrieves NetFlow ranking data (institution/personal, top/low)
func (c *Client) GetNetFlowRanking(duration string, limit int) (*NetFlowRankingData, error) {
if duration == "" {
duration = "1h"
}
if limit <= 0 {
limit = 10
}
result := &NetFlowRankingData{
Duration: duration,
FetchedAt: time.Now(),
}
// Fetch institution futures top (inflow)
positions, timeRange, err := c.fetchNetFlowRanking("top", duration, limit, "institution", "future")
if err != nil {
log.Printf("⚠️ Failed to fetch institution future inflow ranking: %v", err)
} else {
result.InstitutionFutureTop = positions
result.TimeRange = timeRange
}
// Fetch institution futures low (outflow)
positions, _, err = c.fetchNetFlowRanking("low", duration, limit, "institution", "future")
if err != nil {
log.Printf("⚠️ Failed to fetch institution future outflow ranking: %v", err)
} else {
result.InstitutionFutureLow = positions
}
// Fetch personal futures top (retail inflow)
positions, _, err = c.fetchNetFlowRanking("top", duration, limit, "personal", "future")
if err != nil {
log.Printf("⚠️ Failed to fetch personal future inflow ranking: %v", err)
} else {
result.PersonalFutureTop = positions
}
// Fetch personal futures low (retail outflow)
positions, _, err = c.fetchNetFlowRanking("low", duration, limit, "personal", "future")
if err != nil {
log.Printf("⚠️ Failed to fetch personal future outflow ranking: %v", err)
} else {
result.PersonalFutureLow = positions
}
log.Printf("✓ Fetched NetFlow ranking data: inst_in=%d, inst_out=%d, retail_in=%d, retail_out=%d (duration: %s)",
len(result.InstitutionFutureTop), len(result.InstitutionFutureLow),
len(result.PersonalFutureTop), len(result.PersonalFutureLow), duration)
return result, nil
}
func (c *Client) fetchNetFlowRanking(rankType, duration string, limit int, flowType, trade string) ([]NetFlowPosition, string, error) {
endpoint := fmt.Sprintf("/api/netflow/%s-ranking?limit=%d&duration=%s&type=%s&trade=%s",
rankType, limit, duration, flowType, trade)
body, err := c.doRequest(endpoint)
if err != nil {
return nil, "", fmt.Errorf("request failed: %w", err)
}
var response NetFlowResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, "", fmt.Errorf("JSON parsing failed: %w", err)
}
if !response.Success {
return nil, "", fmt.Errorf("API returned failure status")
}
return response.Data.Netflows, response.Data.TimeRange, nil
}
// FormatNetFlowRankingForAI formats NetFlow ranking data for AI consumption
func FormatNetFlowRankingForAI(data *NetFlowRankingData, lang Language) string {
if data == nil {
return ""
}
if lang == LangChinese {
return formatNetFlowRankingZH(data)
}
return formatNetFlowRankingEN(data)
}
func formatNetFlowRankingZH(data *NetFlowRankingData) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## 资金流向排行 (%s)\n\n", data.Duration))
// Institution inflow
if len(data.InstitutionFutureTop) > 0 {
sb.WriteString("### 机构资金流入榜\n")
sb.WriteString("Smart Money买入信号:\n\n")
sb.WriteString("| 排名 | 币种 | 流入金额(USDT) | 价格 |\n")
sb.WriteString("|------|------|----------------|------|\n")
for _, pos := range data.InstitutionFutureTop {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | $%.4f |\n",
pos.Rank, pos.Symbol, formatValue(pos.Amount), pos.Price))
}
sb.WriteString("\n")
}
// Institution outflow
if len(data.InstitutionFutureLow) > 0 {
sb.WriteString("### 机构资金流出榜\n")
sb.WriteString("Smart Money卖出信号:\n\n")
sb.WriteString("| 排名 | 币种 | 流出金额(USDT) | 价格 |\n")
sb.WriteString("|------|------|----------------|------|\n")
for _, pos := range data.InstitutionFutureLow {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | $%.4f |\n",
pos.Rank, pos.Symbol, formatValue(pos.Amount), pos.Price))
}
sb.WriteString("\n")
}
// Retail flow summary
if len(data.PersonalFutureTop) > 0 || len(data.PersonalFutureLow) > 0 {
sb.WriteString("### 散户资金动向\n")
if len(data.PersonalFutureTop) > 0 {
sb.WriteString("散户买入: ")
for i, pos := range data.PersonalFutureTop {
if i >= 3 {
break
}
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%s(%s)", pos.Symbol, formatValue(pos.Amount)))
}
sb.WriteString("\n")
}
if len(data.PersonalFutureLow) > 0 {
sb.WriteString("散户卖出: ")
for i, pos := range data.PersonalFutureLow {
if i >= 3 {
break
}
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%s(%s)", pos.Symbol, formatValue(pos.Amount)))
}
sb.WriteString("\n")
}
sb.WriteString("\n")
}
sb.WriteString("**解读**: 机构买入+散户卖出=强烈看多 | 机构卖出+散户买入=强烈看空\n\n")
return sb.String()
}
func formatNetFlowRankingEN(data *NetFlowRankingData) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## Fund Flow Ranking (%s)\n\n", data.Duration))
// Institution inflow
if len(data.InstitutionFutureTop) > 0 {
sb.WriteString("### Institution Inflow\n")
sb.WriteString("Smart Money buying signals:\n\n")
sb.WriteString("| Rank | Symbol | Inflow (USDT) | Price |\n")
sb.WriteString("|------|--------|---------------|-------|\n")
for _, pos := range data.InstitutionFutureTop {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | $%.4f |\n",
pos.Rank, pos.Symbol, formatValue(pos.Amount), pos.Price))
}
sb.WriteString("\n")
}
// Institution outflow
if len(data.InstitutionFutureLow) > 0 {
sb.WriteString("### Institution Outflow\n")
sb.WriteString("Smart Money selling signals:\n\n")
sb.WriteString("| Rank | Symbol | Outflow (USDT) | Price |\n")
sb.WriteString("|------|--------|----------------|-------|\n")
for _, pos := range data.InstitutionFutureLow {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | $%.4f |\n",
pos.Rank, pos.Symbol, formatValue(pos.Amount), pos.Price))
}
sb.WriteString("\n")
}
// Retail flow summary
if len(data.PersonalFutureTop) > 0 || len(data.PersonalFutureLow) > 0 {
sb.WriteString("### Retail Flow\n")
if len(data.PersonalFutureTop) > 0 {
sb.WriteString("Retail buying: ")
for i, pos := range data.PersonalFutureTop {
if i >= 3 {
break
}
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%s(%s)", pos.Symbol, formatValue(pos.Amount)))
}
sb.WriteString("\n")
}
if len(data.PersonalFutureLow) > 0 {
sb.WriteString("Retail selling: ")
for i, pos := range data.PersonalFutureLow {
if i >= 3 {
break
}
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("%s(%s)", pos.Symbol, formatValue(pos.Amount)))
}
sb.WriteString("\n")
}
sb.WriteString("\n")
}
sb.WriteString("**Key**: Institution buy + Retail sell = Strong bullish | Institution sell + Retail buy = Strong bearish\n\n")
return sb.String()
}

212
provider/nofxos/oi.go Normal file
View File

@@ -0,0 +1,212 @@
package nofxos
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
// OIPosition represents open interest data for a single coin
type OIPosition struct {
Symbol string `json:"symbol"`
Rank int `json:"rank"`
Price float64 `json:"price"`
CurrentOI float64 `json:"current_oi"`
OIDelta float64 `json:"oi_delta"`
OIDeltaPercent float64 `json:"oi_delta_percent"` // Already x100 (5.0 = 5%)
OIDeltaValue float64 `json:"oi_delta_value"` // USDT value
PriceDeltaPercent float64 `json:"price_delta_percent"` // Already x100 (5.0 = 5%)
NetLong float64 `json:"net_long"`
NetShort float64 `json:"net_short"`
}
// OIRankingResponse is the API response structure for OI ranking
type OIRankingResponse struct {
Success bool `json:"success"`
Code int `json:"code"`
Data struct {
Positions []OIPosition `json:"positions"`
Count int `json:"count"`
Exchange string `json:"exchange"`
TimeRange string `json:"time_range"`
TimeRangeParam string `json:"time_range_param"`
RankType string `json:"rank_type"`
Limit int `json:"limit"`
} `json:"data"`
}
// OIRankingData contains both top and low OI rankings
type OIRankingData struct {
TimeRange string `json:"time_range"`
Duration string `json:"duration"`
TopPositions []OIPosition `json:"top_positions"`
LowPositions []OIPosition `json:"low_positions"`
FetchedAt time.Time `json:"fetched_at"`
}
// GetOIRanking retrieves OI ranking data (both top increase and low decrease)
func (c *Client) GetOIRanking(duration string, limit int) (*OIRankingData, error) {
if duration == "" {
duration = "1h"
}
if limit <= 0 {
limit = 20
}
result := &OIRankingData{
Duration: duration,
FetchedAt: time.Now(),
}
// Fetch top ranking (OI increase)
topPositions, timeRange, err := c.fetchOIRanking("top", duration, limit)
if err != nil {
log.Printf("⚠️ Failed to fetch OI top ranking: %v", err)
} else {
result.TopPositions = topPositions
result.TimeRange = timeRange
}
// Fetch low ranking (OI decrease)
lowPositions, _, err := c.fetchOIRanking("low", duration, limit)
if err != nil {
log.Printf("⚠️ Failed to fetch OI low ranking: %v", err)
} else {
result.LowPositions = lowPositions
}
log.Printf("✓ Fetched OI ranking data: %d top, %d low (duration: %s)",
len(result.TopPositions), len(result.LowPositions), duration)
return result, nil
}
func (c *Client) fetchOIRanking(rankType, duration string, limit int) ([]OIPosition, string, error) {
endpoint := fmt.Sprintf("/api/oi/%s-ranking?limit=%d&duration=%s", rankType, limit, duration)
body, err := c.doRequest(endpoint)
if err != nil {
return nil, "", fmt.Errorf("request failed: %w", err)
}
var response OIRankingResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, "", fmt.Errorf("JSON parsing failed: %w", err)
}
// Check for success (support both success field and code field)
if !response.Success && response.Code != 0 {
return nil, "", fmt.Errorf("API returned error code: %d", response.Code)
}
return response.Data.Positions, response.Data.TimeRange, nil
}
// GetOITopPositions retrieves top OI increase positions (legacy compatibility)
func (c *Client) GetOITopPositions() ([]OIPosition, error) {
data, err := c.GetOIRanking("1h", 20)
if err != nil {
return nil, err
}
return data.TopPositions, nil
}
// GetOITopSymbols retrieves OI top coin symbol list
func (c *Client) GetOITopSymbols() ([]string, error) {
positions, err := c.GetOITopPositions()
if err != nil {
return nil, err
}
var symbols []string
for _, pos := range positions {
symbol := NormalizeSymbol(pos.Symbol)
symbols = append(symbols, symbol)
}
return symbols, nil
}
// FormatOIRankingForAI formats OI ranking data for AI consumption
func FormatOIRankingForAI(data *OIRankingData, lang Language) string {
if data == nil {
return ""
}
if lang == LangChinese {
return formatOIRankingZH(data)
}
return formatOIRankingEN(data)
}
func formatOIRankingZH(data *OIRankingData) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## 持仓量变化排行 (%s)\n\n", data.Duration))
if len(data.TopPositions) > 0 {
sb.WriteString("### 持仓增加榜\n")
sb.WriteString("资金流入,趋势延续或新仓建立信号:\n\n")
sb.WriteString("| 排名 | 币种 | 持仓变化(USDT) | OI变化% | 价格变化% |\n")
sb.WriteString("|------|------|----------------|---------|----------|\n")
for _, pos := range data.TopPositions {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | %+.2f%% | %+.2f%% |\n",
pos.Rank, pos.Symbol, formatValue(pos.OIDeltaValue),
pos.OIDeltaPercent, pos.PriceDeltaPercent))
}
sb.WriteString("\n")
}
if len(data.LowPositions) > 0 {
sb.WriteString("### 持仓减少榜\n")
sb.WriteString("资金流出,趋势反转或仓位平仓信号:\n\n")
sb.WriteString("| 排名 | 币种 | 持仓变化(USDT) | OI变化% | 价格变化% |\n")
sb.WriteString("|------|------|----------------|---------|----------|\n")
for _, pos := range data.LowPositions {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | %+.2f%% | %+.2f%% |\n",
pos.Rank, pos.Symbol, formatValue(pos.OIDeltaValue),
pos.OIDeltaPercent, pos.PriceDeltaPercent))
}
sb.WriteString("\n")
}
sb.WriteString("**解读**: OI增+价涨=多头主导 | OI增+价跌=空头主导 | OI减+价涨=空头平仓 | OI减+价跌=多头平仓\n\n")
return sb.String()
}
func formatOIRankingEN(data *OIRankingData) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## Open Interest Changes (%s)\n\n", data.Duration))
if len(data.TopPositions) > 0 {
sb.WriteString("### OI Increase Ranking\n")
sb.WriteString("Capital inflow signals - trend continuation or new positions:\n\n")
sb.WriteString("| Rank | Symbol | OI Change (USDT) | OI Change % | Price Change % |\n")
sb.WriteString("|------|--------|------------------|-------------|----------------|\n")
for _, pos := range data.TopPositions {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | %+.2f%% | %+.2f%% |\n",
pos.Rank, pos.Symbol, formatValue(pos.OIDeltaValue),
pos.OIDeltaPercent, pos.PriceDeltaPercent))
}
sb.WriteString("\n")
}
if len(data.LowPositions) > 0 {
sb.WriteString("### OI Decrease Ranking\n")
sb.WriteString("Capital outflow signals - trend reversal or position closing:\n\n")
sb.WriteString("| Rank | Symbol | OI Change (USDT) | OI Change % | Price Change % |\n")
sb.WriteString("|------|--------|------------------|-------------|----------------|\n")
for _, pos := range data.LowPositions {
sb.WriteString(fmt.Sprintf("| %d | %s | %s | %+.2f%% | %+.2f%% |\n",
pos.Rank, pos.Symbol, formatValue(pos.OIDeltaValue),
pos.OIDeltaPercent, pos.PriceDeltaPercent))
}
sb.WriteString("\n")
}
sb.WriteString("**Key**: OI up + Price up = Bulls dominant | OI up + Price down = Bears dominant | OI down + Price up = Short covering | OI down + Price down = Long liquidation\n\n")
return sb.String()
}

182
provider/nofxos/price.go Normal file
View File

@@ -0,0 +1,182 @@
package nofxos
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
)
// PriceRankingItem represents single coin price ranking data
type PriceRankingItem struct {
Pair string `json:"pair"`
Symbol string `json:"symbol"`
PriceDelta float64 `json:"price_delta"` // Decimal format: 0.0723 = 7.23%
Price float64 `json:"price"`
FutureFlow float64 `json:"future_flow"`
SpotFlow float64 `json:"spot_flow"`
OI float64 `json:"oi"`
OIDelta float64 `json:"oi_delta"`
OIDeltaValue float64 `json:"oi_delta_value"`
}
// PriceRankingDuration contains top gainers and losers for a single duration
type PriceRankingDuration struct {
Top []PriceRankingItem `json:"top"`
Low []PriceRankingItem `json:"low"`
}
// PriceRankingResponse is the API response structure
type PriceRankingResponse struct {
Success bool `json:"success"`
Data struct {
Durations []string `json:"durations"`
Limit int `json:"limit"`
Data map[string]PriceRankingDuration `json:"data"`
} `json:"data"`
}
// PriceRankingData contains price ranking data for multiple durations
type PriceRankingData struct {
Durations map[string]*PriceRankingDuration `json:"durations"`
FetchedAt time.Time `json:"fetched_at"`
}
// GetPriceRanking retrieves price ranking data (gainers/losers)
func (c *Client) GetPriceRanking(durations string, limit int) (*PriceRankingData, error) {
if durations == "" {
durations = "1h"
}
if limit <= 0 {
limit = 10
}
endpoint := fmt.Sprintf("/api/price/ranking?duration=%s&limit=%d", durations, limit)
body, err := c.doRequest(endpoint)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
var response PriceRankingResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("JSON parsing failed: %w", err)
}
if !response.Success {
return nil, fmt.Errorf("API returned failure status")
}
result := &PriceRankingData{
Durations: make(map[string]*PriceRankingDuration),
FetchedAt: time.Now(),
}
for duration, data := range response.Data.Data {
d := data // Create a copy to avoid pointer issues
result.Durations[duration] = &d
}
log.Printf("✓ Fetched Price ranking data for %d durations", len(result.Durations))
return result, nil
}
// FormatPriceRankingForAI formats Price ranking data for AI consumption
func FormatPriceRankingForAI(data *PriceRankingData, lang Language) string {
if data == nil || len(data.Durations) == 0 {
return ""
}
if lang == LangChinese {
return formatPriceRankingZH(data)
}
return formatPriceRankingEN(data)
}
func formatPriceRankingZH(data *PriceRankingData) string {
var sb strings.Builder
sb.WriteString("## 涨跌幅排行\n\n")
durationOrder := []string{"1h", "4h", "24h"}
for _, duration := range durationOrder {
durationData, exists := data.Durations[duration]
if !exists || durationData == nil {
continue
}
sb.WriteString(fmt.Sprintf("### %s 涨跌幅\n\n", duration))
if len(durationData.Top) > 0 {
sb.WriteString("**涨幅榜**\n")
sb.WriteString("| 币种 | 涨幅 | 价格 | 资金流 | OI变化 |\n")
sb.WriteString("|------|------|------|--------|--------|\n")
for _, item := range durationData.Top {
sb.WriteString(fmt.Sprintf("| %s | %+.2f%% | $%.4f | %s | %s |\n",
item.Symbol, item.PriceDelta*100, item.Price,
formatValue(item.FutureFlow), formatValue(item.OIDeltaValue)))
}
sb.WriteString("\n")
}
if len(durationData.Low) > 0 {
sb.WriteString("**跌幅榜**\n")
sb.WriteString("| 币种 | 跌幅 | 价格 | 资金流 | OI变化 |\n")
sb.WriteString("|------|------|------|--------|--------|\n")
for _, item := range durationData.Low {
sb.WriteString(fmt.Sprintf("| %s | %.2f%% | $%.4f | %s | %s |\n",
item.Symbol, item.PriceDelta*100, item.Price,
formatValue(item.FutureFlow), formatValue(item.OIDeltaValue)))
}
sb.WriteString("\n")
}
}
sb.WriteString("**解读**: 涨幅大+资金流入+OI增加=强势上涨 | 跌幅大+资金流出+OI减少=弱势下跌\n\n")
return sb.String()
}
func formatPriceRankingEN(data *PriceRankingData) string {
var sb strings.Builder
sb.WriteString("## Price Gainers/Losers\n\n")
durationOrder := []string{"1h", "4h", "24h"}
for _, duration := range durationOrder {
durationData, exists := data.Durations[duration]
if !exists || durationData == nil {
continue
}
sb.WriteString(fmt.Sprintf("### %s Price Change\n\n", duration))
if len(durationData.Top) > 0 {
sb.WriteString("**Top Gainers**\n")
sb.WriteString("| Symbol | Change | Price | Fund Flow | OI Change |\n")
sb.WriteString("|--------|--------|-------|-----------|----------|\n")
for _, item := range durationData.Top {
sb.WriteString(fmt.Sprintf("| %s | %+.2f%% | $%.4f | %s | %s |\n",
item.Symbol, item.PriceDelta*100, item.Price,
formatValue(item.FutureFlow), formatValue(item.OIDeltaValue)))
}
sb.WriteString("\n")
}
if len(durationData.Low) > 0 {
sb.WriteString("**Top Losers**\n")
sb.WriteString("| Symbol | Change | Price | Fund Flow | OI Change |\n")
sb.WriteString("|--------|--------|-------|-----------|----------|\n")
for _, item := range durationData.Low {
sb.WriteString(fmt.Sprintf("| %s | %.2f%% | $%.4f | %s | %s |\n",
item.Symbol, item.PriceDelta*100, item.Price,
formatValue(item.FutureFlow), formatValue(item.OIDeltaValue)))
}
sb.WriteString("\n")
}
}
sb.WriteString("**Key**: Big gain + Fund inflow + OI increase = Strong bullish | Big loss + Fund outflow + OI decrease = Strong bearish\n\n")
return sb.String()
}

31
provider/nofxos/util.go Normal file
View File

@@ -0,0 +1,31 @@
package nofxos
import "fmt"
// Language represents the language for formatting output
type Language string
const (
LangChinese Language = "zh-CN"
LangEnglish Language = "en-US"
)
// formatValue formats a numeric value with sign and appropriate suffix
func formatValue(v float64) string {
sign := "+"
if v < 0 {
sign = ""
}
absV := v
if absV < 0 {
absV = -absV
}
if absV >= 1e9 {
return fmt.Sprintf("%s%.2fB", sign, v/1e9)
} else if absV >= 1e6 {
return fmt.Sprintf("%s%.2fM", sign, v/1e6)
} else if absV >= 1e3 {
return fmt.Sprintf("%s%.2fK", sign, v/1e3)
}
return fmt.Sprintf("%s%.2f", sign, v)
}

8
railway.toml Normal file
View File

@@ -0,0 +1,8 @@
[build]
dockerfilePath = "Dockerfile.railway"
[deploy]
healthcheckPath = "/health"
healthcheckTimeout = 60
restartPolicyType = "ON_FAILURE"
restartPolicyMaxRetries = 3

57
railway/start.sh Normal file
View File

@@ -0,0 +1,57 @@
#!/bin/sh
set -e
# Railway 会设置 PORT 环境变量
export PORT=${PORT:-8080}
echo "🚀 Starting NOFX on port $PORT..."
# 生成加密密钥(如果没有设置)
if [ -z "$RSA_PRIVATE_KEY" ]; then
export RSA_PRIVATE_KEY=$(openssl genrsa 2048 2>/dev/null)
fi
if [ -z "$DATA_ENCRYPTION_KEY" ]; then
export DATA_ENCRYPTION_KEY=$(openssl rand -base64 32)
fi
# 生成 nginx 配置
cat > /etc/nginx/http.d/default.conf << NGINX_EOF
server {
listen $PORT;
server_name _;
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_types text/plain text/css application/json application/javascript;
location / {
try_files \$uri \$uri/ /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:8081/api/;
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
location /health {
return 200 'OK';
add_header Content-Type text/plain;
}
}
NGINX_EOF
# 启动后端(端口 8081
API_SERVER_PORT=8081 /app/nofx &
sleep 2
# 启动 nginx后台
nginx
echo "✅ NOFX started successfully"
# 保持容器运行
tail -f /dev/null

View File

@@ -7,6 +7,7 @@ import (
"nofx/store"
"os"
"path/filepath"
"time"
)
func main() {
@@ -83,7 +84,7 @@ func main() {
filledOrders++
// 检查 filled_at
if !order.FilledAt.IsZero() {
if order.FilledAt > 0 {
withFilledAt++
} else {
missingFilledAt++
@@ -119,8 +120,8 @@ func main() {
}
filledAtStr := "N/A"
if !order.FilledAt.IsZero() {
filledAtStr = order.FilledAt.Format("01-02 15:04")
if order.FilledAt > 0 {
filledAtStr = time.UnixMilli(order.FilledAt).Format("01-02 15:04")
}
fmt.Printf("%-15s %-10s %-10s %-15.2f %-10s %s\n",

View File

@@ -149,7 +149,7 @@ func (s *AIModelStore) Update(userID, id string, enabled bool, apiKey, customAPI
"enabled": enabled,
"custom_api_url": customAPIURL,
"custom_model_name": customModelName,
"updated_at": time.Now(),
"updated_at": time.Now().UTC(),
}
// If apiKey is not empty, update it (encryption handled by crypto.EncryptedString)
if apiKey != "" {
@@ -167,7 +167,7 @@ func (s *AIModelStore) Update(userID, id string, enabled bool, apiKey, customAPI
"enabled": enabled,
"custom_api_url": customAPIURL,
"custom_model_name": customModelName,
"updated_at": time.Now(),
"updated_at": time.Now().UTC(),
}
if apiKey != "" {
updates["api_key"] = crypto.EncryptedString(apiKey)

View File

@@ -147,7 +147,7 @@ func (BacktestCheckpoint) TableName() string {
type BacktestEquity struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
RunID string `gorm:"column:run_id;not null;index:idx_backtest_equity_run_ts"`
TS int64 `gorm:"column:ts;not null;index:idx_backtest_equity_run_ts"`
TS int64 `gorm:"column:ts;type:bigint;not null;index:idx_backtest_equity_run_ts"`
Equity float64 `gorm:"column:equity;not null"`
Available float64 `gorm:"column:available;not null"`
PnL float64 `gorm:"column:pnl;not null"`
@@ -164,7 +164,7 @@ func (BacktestEquity) TableName() string {
type BacktestTrade struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
RunID string `gorm:"column:run_id;not null;index:idx_backtest_trades_run_ts"`
TS int64 `gorm:"column:ts;not null;index:idx_backtest_trades_run_ts"`
TS int64 `gorm:"column:ts;type:bigint;not null;index:idx_backtest_trades_run_ts"`
Symbol string `gorm:"column:symbol;not null"`
Action string `gorm:"column:action;not null"`
Side string `gorm:"column:side;default:''"`
@@ -217,7 +217,10 @@ func (s *BacktestStore) initTables() error {
s.db.Raw(`SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'backtest_runs'`).Scan(&tableExists)
if tableExists > 0 {
// Tables exist - just ensure indexes exist
// Tables exist - fix column types and ensure indexes exist
// Fix ts column type from INTEGER to BIGINT (timestamps in milliseconds exceed int4 max)
s.db.Exec(`ALTER TABLE backtest_equity ALTER COLUMN ts TYPE BIGINT`)
s.db.Exec(`ALTER TABLE backtest_trades ALTER COLUMN ts TYPE BIGINT`)
s.db.Exec(`CREATE INDEX IF NOT EXISTS idx_backtest_equity_run_ts ON backtest_equity(run_id, ts)`)
s.db.Exec(`CREATE INDEX IF NOT EXISTS idx_backtest_trades_run_ts ON backtest_trades(run_id, ts)`)
s.db.Exec(`CREATE INDEX IF NOT EXISTS idx_backtest_decisions_run_cycle ON backtest_decisions(run_id, cycle)`)

View File

@@ -236,7 +236,7 @@ func (s *ExchangeStore) Update(userID, id string, enabled bool, apiKey, secretKe
"aster_signer": asterSigner,
"lighter_wallet_addr": lighterWalletAddr,
"lighter_api_key_index": lighterApiKeyIndex,
"updated_at": time.Now(),
"updated_at": time.Now().UTC(),
}
// Only update encrypted fields if not empty
@@ -275,7 +275,7 @@ func (s *ExchangeStore) UpdateAccountName(userID, id, accountName string) error
Where("id = ? AND user_id = ?", id, userID).
Updates(map[string]interface{}{
"account_name": accountName,
"updated_at": time.Now(),
"updated_at": time.Now().UTC(),
})
if result.Error != nil {
return result.Error

View File

@@ -2,6 +2,7 @@ package store
import (
"fmt"
"time"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
@@ -21,6 +22,10 @@ func DB() *gorm.DB {
func InitGorm(dbPath string) (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
// Use UTC for all auto-generated timestamps (autoCreateTime, autoUpdateTime)
NowFunc: func() time.Time {
return time.Now().UTC()
},
})
if err != nil {
return nil, fmt.Errorf("failed to open SQLite database: %w", err)
@@ -53,6 +58,10 @@ func InitGormPostgres(host string, port int, user, password, dbname, sslmode str
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
// Use UTC for all auto-generated timestamps (autoCreateTime, autoUpdateTime)
NowFunc: func() time.Time {
return time.Now().UTC()
},
})
if err != nil {
return nil, fmt.Errorf("failed to open PostgreSQL database: %w", err)

View File

@@ -2,43 +2,44 @@ package store
import (
"fmt"
"strings"
"strconv"
"time"
"gorm.io/gorm"
)
// TraderOrder order record
// All time fields use int64 millisecond timestamps (UTC) to avoid timezone issues
type TraderOrder struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
TraderID string `gorm:"column:trader_id;not null;index:idx_orders_trader_id" json:"trader_id"`
ExchangeID string `gorm:"column:exchange_id;not null;default:''" json:"exchange_id"`
ExchangeType string `gorm:"column:exchange_type;not null;default:''" json:"exchange_type"`
ExchangeOrderID string `gorm:"column:exchange_order_id;not null;uniqueIndex:idx_orders_exchange_unique,priority:2" json:"exchange_order_id"`
ClientOrderID string `gorm:"column:client_order_id;default:''" json:"client_order_id"`
Symbol string `gorm:"column:symbol;not null;index:idx_orders_symbol" json:"symbol"`
Side string `gorm:"column:side;not null" json:"side"`
PositionSide string `gorm:"column:position_side;default:''" json:"position_side"`
Type string `gorm:"column:type;not null" json:"type"`
TimeInForce string `gorm:"column:time_in_force;default:GTC" json:"time_in_force"`
Quantity float64 `gorm:"column:quantity;not null" json:"quantity"`
Price float64 `gorm:"column:price;default:0" json:"price"`
StopPrice float64 `gorm:"column:stop_price;default:0" json:"stop_price"`
Status string `gorm:"column:status;not null;default:NEW;index:idx_orders_status" json:"status"`
FilledQuantity float64 `gorm:"column:filled_quantity;default:0" json:"filled_quantity"`
AvgFillPrice float64 `gorm:"column:avg_fill_price;default:0" json:"avg_fill_price"`
Commission float64 `gorm:"column:commission;default:0" json:"commission"`
CommissionAsset string `gorm:"column:commission_asset;default:USDT" json:"commission_asset"`
Leverage int `gorm:"column:leverage;default:1" json:"leverage"`
ReduceOnly bool `gorm:"column:reduce_only;default:false" json:"reduce_only"`
ClosePosition bool `gorm:"column:close_position;default:false" json:"close_position"`
WorkingType string `gorm:"column:working_type;default:CONTRACT_PRICE" json:"working_type"`
PriceProtect bool `gorm:"column:price_protect;default:false" json:"price_protect"`
OrderAction string `gorm:"column:order_action;default:''" json:"order_action"`
RelatedPositionID int64 `gorm:"column:related_position_id;default:0" json:"related_position_id"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime" json:"updated_at"`
FilledAt time.Time `gorm:"column:filled_at" json:"filled_at"`
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
TraderID string `gorm:"column:trader_id;not null;index:idx_orders_trader_id" json:"trader_id"`
ExchangeID string `gorm:"column:exchange_id;not null;default:''" json:"exchange_id"`
ExchangeType string `gorm:"column:exchange_type;not null;default:''" json:"exchange_type"`
ExchangeOrderID string `gorm:"column:exchange_order_id;not null;uniqueIndex:idx_orders_exchange_unique,priority:2" json:"exchange_order_id"`
ClientOrderID string `gorm:"column:client_order_id;default:''" json:"client_order_id"`
Symbol string `gorm:"column:symbol;not null;index:idx_orders_symbol" json:"symbol"`
Side string `gorm:"column:side;not null" json:"side"`
PositionSide string `gorm:"column:position_side;default:''" json:"position_side"`
Type string `gorm:"column:type;not null" json:"type"`
TimeInForce string `gorm:"column:time_in_force;default:GTC" json:"time_in_force"`
Quantity float64 `gorm:"column:quantity;not null" json:"quantity"`
Price float64 `gorm:"column:price;default:0" json:"price"`
StopPrice float64 `gorm:"column:stop_price;default:0" json:"stop_price"`
Status string `gorm:"column:status;not null;default:NEW;index:idx_orders_status" json:"status"`
FilledQuantity float64 `gorm:"column:filled_quantity;default:0" json:"filled_quantity"`
AvgFillPrice float64 `gorm:"column:avg_fill_price;default:0" json:"avg_fill_price"`
Commission float64 `gorm:"column:commission;default:0" json:"commission"`
CommissionAsset string `gorm:"column:commission_asset;default:USDT" json:"commission_asset"`
Leverage int `gorm:"column:leverage;default:1" json:"leverage"`
ReduceOnly bool `gorm:"column:reduce_only;default:false" json:"reduce_only"`
ClosePosition bool `gorm:"column:close_position;default:false" json:"close_position"`
WorkingType string `gorm:"column:working_type;default:CONTRACT_PRICE" json:"working_type"`
PriceProtect bool `gorm:"column:price_protect;default:false" json:"price_protect"`
OrderAction string `gorm:"column:order_action;default:''" json:"order_action"`
RelatedPositionID int64 `gorm:"column:related_position_id;default:0" json:"related_position_id"`
CreatedAt int64 `gorm:"column:created_at" json:"created_at"` // Unix milliseconds UTC
UpdatedAt int64 `gorm:"column:updated_at" json:"updated_at"` // Unix milliseconds UTC
FilledAt int64 `gorm:"column:filled_at" json:"filled_at"` // Unix milliseconds UTC
}
// TableName returns the table name for TraderOrder
@@ -47,24 +48,25 @@ func (TraderOrder) TableName() string {
}
// TraderFill trade record
// All time fields use int64 millisecond timestamps (UTC) to avoid timezone issues
type TraderFill struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
TraderID string `gorm:"column:trader_id;not null;index:idx_fills_trader_id" json:"trader_id"`
ExchangeID string `gorm:"column:exchange_id;not null;default:''" json:"exchange_id"`
ExchangeType string `gorm:"column:exchange_type;not null;default:''" json:"exchange_type"`
OrderID int64 `gorm:"column:order_id;not null;index:idx_fills_order_id" json:"order_id"`
ExchangeOrderID string `gorm:"column:exchange_order_id;not null" json:"exchange_order_id"`
ExchangeTradeID string `gorm:"column:exchange_trade_id;not null;uniqueIndex:idx_fills_exchange_unique,priority:2" json:"exchange_trade_id"`
Symbol string `gorm:"column:symbol;not null" json:"symbol"`
Side string `gorm:"column:side;not null" json:"side"`
Price float64 `gorm:"column:price;not null" json:"price"`
Quantity float64 `gorm:"column:quantity;not null" json:"quantity"`
QuoteQuantity float64 `gorm:"column:quote_quantity;not null" json:"quote_quantity"`
Commission float64 `gorm:"column:commission;not null" json:"commission"`
CommissionAsset string `gorm:"column:commission_asset;not null" json:"commission_asset"`
RealizedPnL float64 `gorm:"column:realized_pnl;default:0" json:"realized_pnl"`
IsMaker bool `gorm:"column:is_maker;default:false" json:"is_maker"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
TraderID string `gorm:"column:trader_id;not null;index:idx_fills_trader_id" json:"trader_id"`
ExchangeID string `gorm:"column:exchange_id;not null;default:''" json:"exchange_id"`
ExchangeType string `gorm:"column:exchange_type;not null;default:''" json:"exchange_type"`
OrderID int64 `gorm:"column:order_id;not null;index:idx_fills_order_id" json:"order_id"`
ExchangeOrderID string `gorm:"column:exchange_order_id;not null" json:"exchange_order_id"`
ExchangeTradeID string `gorm:"column:exchange_trade_id;not null;uniqueIndex:idx_fills_exchange_unique,priority:2" json:"exchange_trade_id"`
Symbol string `gorm:"column:symbol;not null" json:"symbol"`
Side string `gorm:"column:side;not null" json:"side"`
Price float64 `gorm:"column:price;not null" json:"price"`
Quantity float64 `gorm:"column:quantity;not null" json:"quantity"`
QuoteQuantity float64 `gorm:"column:quote_quantity;not null" json:"quote_quantity"`
Commission float64 `gorm:"column:commission;not null" json:"commission"`
CommissionAsset string `gorm:"column:commission_asset;not null" json:"commission_asset"`
RealizedPnL float64 `gorm:"column:realized_pnl;default:0" json:"realized_pnl"`
IsMaker bool `gorm:"column:is_maker;default:false" json:"is_maker"`
CreatedAt int64 `gorm:"column:created_at" json:"created_at"` // Unix milliseconds UTC
}
// TableName returns the table name for TraderFill
@@ -105,6 +107,23 @@ func (s *OrderStore) InitTables() error {
s.db.Exec(fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT false", c.table, c.col))
}
// Migrate timestamp columns to bigint (Unix milliseconds UTC)
// Check if column is still timestamp type before migrating
timestampColumns := []struct{ table, col string }{
{"trader_orders", "created_at"},
{"trader_orders", "updated_at"},
{"trader_orders", "filled_at"},
{"trader_fills", "created_at"},
}
for _, c := range timestampColumns {
var dataType string
s.db.Raw(`SELECT data_type FROM information_schema.columns WHERE table_name = ? AND column_name = ?`, c.table, c.col).Scan(&dataType)
if dataType == "timestamp with time zone" || dataType == "timestamp without time zone" {
// Convert timestamp to Unix milliseconds (bigint)
s.db.Exec(fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s TYPE BIGINT USING EXTRACT(EPOCH FROM %s) * 1000`, c.table, c.col, c.col))
}
}
// Ensure indexes exist
s.db.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_orders_exchange_unique ON trader_orders(exchange_id, exchange_order_id)`)
s.db.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_fills_exchange_unique ON trader_fills(exchange_id, exchange_trade_id)`)
@@ -153,10 +172,11 @@ func (s *OrderStore) UpdateOrderStatus(id int64, status string, filledQty, avgPr
"filled_quantity": filledQty,
"avg_fill_price": avgPrice,
"commission": commission,
"updated_at": time.Now().UTC().UnixMilli(),
}
if status == "FILLED" {
updates["filled_at"] = time.Now()
updates["filled_at"] = time.Now().UTC().UnixMilli()
}
return s.db.Model(&TraderOrder{}).Where("id = ?", id).Updates(updates).Error
@@ -217,6 +237,27 @@ func (s *OrderStore) GetTraderOrders(traderID string, limit int) ([]*TraderOrder
return orders, nil
}
// GetTraderOrdersFiltered gets trader's order list with optional symbol and status filters
func (s *OrderStore) GetTraderOrdersFiltered(traderID string, symbol string, status string, limit int) ([]*TraderOrder, error) {
var orders []*TraderOrder
query := s.db.Where("trader_id = ?", traderID)
if symbol != "" {
query = query.Where("symbol = ?", symbol)
}
if status != "" {
query = query.Where("status = ?", status)
}
err := query.Order("created_at DESC").
Limit(limit).
Find(&orders).Error
if err != nil {
return nil, fmt.Errorf("failed to query orders: %w", err)
}
return orders, nil
}
// GetOrderFills gets order's fill records
func (s *OrderStore) GetOrderFills(orderID int64) ([]*TraderFill, error) {
var fills []*TraderFill
@@ -324,29 +365,59 @@ func (s *OrderStore) GetDuplicateFillsCount() (int, error) {
// GetMaxTradeIDsByExchange returns max trade ID for each symbol for a given exchange
func (s *OrderStore) GetMaxTradeIDsByExchange(exchangeID string) (map[string]int64, error) {
type symbolMaxID struct {
Symbol string
MaxTradeID int64
type symbolTradeID struct {
Symbol string
ExchangeTradeID string
}
var results []symbolMaxID
var results []symbolTradeID
// Query all trade IDs grouped by symbol, find max in Go to avoid database-specific CAST issues
// (PostgreSQL INTEGER is 32-bit, can't handle Binance trade IDs > 2.1B)
err := s.db.Model(&TraderFill{}).
Select("symbol, MAX(CAST(exchange_trade_id AS INTEGER)) as max_trade_id").
Select("symbol, exchange_trade_id").
Where("exchange_id = ? AND exchange_trade_id != ''", exchangeID).
Group("symbol").
Find(&results).Error
if err != nil {
// If CAST fails (non-numeric trade IDs), fallback to string comparison
if strings.Contains(err.Error(), "CAST") || strings.Contains(err.Error(), "invalid") {
return make(map[string]int64), nil
}
return nil, fmt.Errorf("failed to query max trade IDs: %w", err)
return nil, fmt.Errorf("failed to query trade IDs: %w", err)
}
// Find max trade ID per symbol in Go (handles 64-bit integers properly)
result := make(map[string]int64)
for _, r := range results {
result[r.Symbol] = r.MaxTradeID
tradeID, err := strconv.ParseInt(r.ExchangeTradeID, 10, 64)
if err != nil {
continue // Skip non-numeric trade IDs
}
if tradeID > result[r.Symbol] {
result[r.Symbol] = tradeID
}
}
return result, nil
}
// GetLastFillTimeByExchange returns the most recent fill time (Unix ms) for a given exchange
// Used to recover sync state after service restart
func (s *OrderStore) GetLastFillTimeByExchange(exchangeID string) (int64, error) {
var fill TraderFill
err := s.db.Where("exchange_id = ?", exchangeID).
Order("created_at DESC").
First(&fill).Error
if err != nil {
return 0, err
}
return fill.CreatedAt, nil
}
// GetRecentFillSymbolsByExchange returns distinct symbols with fills since given time (Unix ms)
func (s *OrderStore) GetRecentFillSymbolsByExchange(exchangeID string, sinceMs int64) ([]string, error) {
var symbols []string
err := s.db.Model(&TraderFill{}).
Select("DISTINCT symbol").
Where("exchange_id = ? AND created_at >= ?", exchangeID, sinceMs).
Pluck("symbol", &symbols).Error
if err != nil {
return nil, err
}
return symbols, nil
}

View File

@@ -25,30 +25,31 @@ type TraderStats struct {
}
// TraderPosition position record
// All time fields use int64 millisecond timestamps (UTC) to avoid timezone issues
type TraderPosition struct {
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
TraderID string `gorm:"column:trader_id;not null;index:idx_positions_trader" json:"trader_id"`
ExchangeID string `gorm:"column:exchange_id;not null;default:'';index:idx_positions_exchange" json:"exchange_id"`
ExchangeType string `gorm:"column:exchange_type;not null;default:''" json:"exchange_type"`
ExchangePositionID string `gorm:"column:exchange_position_id;not null;default:''" json:"exchange_position_id"`
Symbol string `gorm:"column:symbol;not null" json:"symbol"`
Side string `gorm:"column:side;not null" json:"side"`
EntryQuantity float64 `gorm:"column:entry_quantity;default:0" json:"entry_quantity"`
Quantity float64 `gorm:"column:quantity;not null" json:"quantity"`
EntryPrice float64 `gorm:"column:entry_price;not null" json:"entry_price"`
EntryOrderID string `gorm:"column:entry_order_id;default:''" json:"entry_order_id"`
EntryTime time.Time `gorm:"column:entry_time;not null;index:idx_positions_entry" json:"entry_time"`
ExitPrice float64 `gorm:"column:exit_price;default:0" json:"exit_price"`
ExitOrderID string `gorm:"column:exit_order_id;default:''" json:"exit_order_id"`
ExitTime *time.Time `gorm:"column:exit_time;index:idx_positions_exit" json:"exit_time"`
RealizedPnL float64 `gorm:"column:realized_pnl;default:0" json:"realized_pnl"`
Fee float64 `gorm:"column:fee;default:0" json:"fee"`
Leverage int `gorm:"column:leverage;default:1" json:"leverage"`
Status string `gorm:"column:status;default:OPEN;index:idx_positions_status" json:"status"`
CloseReason string `gorm:"column:close_reason;default:''" json:"close_reason"`
Source string `gorm:"column:source;default:system" json:"source"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime" json:"updated_at"`
ID int64 `gorm:"primaryKey;autoIncrement" json:"id"`
TraderID string `gorm:"column:trader_id;not null;index:idx_positions_trader" json:"trader_id"`
ExchangeID string `gorm:"column:exchange_id;not null;default:'';index:idx_positions_exchange" json:"exchange_id"`
ExchangeType string `gorm:"column:exchange_type;not null;default:''" json:"exchange_type"`
ExchangePositionID string `gorm:"column:exchange_position_id;not null;default:''" json:"exchange_position_id"`
Symbol string `gorm:"column:symbol;not null" json:"symbol"`
Side string `gorm:"column:side;not null" json:"side"`
EntryQuantity float64 `gorm:"column:entry_quantity;default:0" json:"entry_quantity"`
Quantity float64 `gorm:"column:quantity;not null" json:"quantity"`
EntryPrice float64 `gorm:"column:entry_price;not null" json:"entry_price"`
EntryOrderID string `gorm:"column:entry_order_id;default:''" json:"entry_order_id"`
EntryTime int64 `gorm:"column:entry_time;not null;index:idx_positions_entry" json:"entry_time"` // Unix milliseconds UTC
ExitPrice float64 `gorm:"column:exit_price;default:0" json:"exit_price"`
ExitOrderID string `gorm:"column:exit_order_id;default:''" json:"exit_order_id"`
ExitTime int64 `gorm:"column:exit_time;index:idx_positions_exit" json:"exit_time"` // Unix milliseconds UTC, 0 means not set
RealizedPnL float64 `gorm:"column:realized_pnl;default:0" json:"realized_pnl"`
Fee float64 `gorm:"column:fee;default:0" json:"fee"`
Leverage int `gorm:"column:leverage;default:1" json:"leverage"`
Status string `gorm:"column:status;default:OPEN;index:idx_positions_status" json:"status"`
CloseReason string `gorm:"column:close_reason;default:''" json:"close_reason"`
Source string `gorm:"column:source;default:system" json:"source"`
CreatedAt int64 `gorm:"column:created_at" json:"created_at"` // Unix milliseconds UTC
UpdatedAt int64 `gorm:"column:updated_at" json:"updated_at"` // Unix milliseconds UTC
}
// TableName returns the table name
@@ -78,6 +79,18 @@ func (s *PositionStore) InitTables() error {
var tableExists int64
s.db.Raw(`SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'trader_positions'`).Scan(&tableExists)
if tableExists > 0 {
// Migrate timestamp columns to bigint (Unix milliseconds UTC)
// Check if column is still timestamp type before migrating
timestampColumns := []string{"entry_time", "exit_time", "created_at", "updated_at"}
for _, col := range timestampColumns {
var dataType string
s.db.Raw(`SELECT data_type FROM information_schema.columns WHERE table_name = 'trader_positions' AND column_name = ?`, col).Scan(&dataType)
if dataType == "timestamp with time zone" || dataType == "timestamp without time zone" {
// Convert timestamp to Unix milliseconds (bigint)
s.db.Exec(fmt.Sprintf(`ALTER TABLE trader_positions ALTER COLUMN %s TYPE BIGINT USING EXTRACT(EPOCH FROM %s) * 1000`, col, col))
}
}
// Just ensure index exists
s.db.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_positions_exchange_pos_unique ON trader_positions(exchange_id, exchange_position_id) WHERE exchange_position_id != ''`)
return nil
@@ -115,15 +128,16 @@ func (s *PositionStore) Create(pos *TraderPosition) error {
// ClosePosition closes position
func (s *PositionStore) ClosePosition(id int64, exitPrice float64, exitOrderID string, realizedPnL float64, fee float64, closeReason string) error {
now := time.Now()
nowMs := time.Now().UTC().UnixMilli()
return s.db.Model(&TraderPosition{}).Where("id = ?", id).Updates(map[string]interface{}{
"exit_price": exitPrice,
"exit_order_id": exitOrderID,
"exit_time": now,
"exit_time": nowMs,
"realized_pnl": realizedPnL,
"fee": fee,
"status": "CLOSED",
"close_reason": closeReason,
"updated_at": nowMs,
}).Error
}
@@ -190,7 +204,8 @@ func (s *PositionStore) UpdatePositionExchangeInfo(id int64, exchangeID, exchang
}
// ClosePositionFully marks position as fully closed
func (s *PositionStore) ClosePositionFully(id int64, exitPrice float64, exitOrderID string, exitTime time.Time, totalRealizedPnL float64, totalFee float64, closeReason string) error {
// exitTimeMs is Unix milliseconds UTC
func (s *PositionStore) ClosePositionFully(id int64, exitPrice float64, exitOrderID string, exitTimeMs int64, totalRealizedPnL float64, totalFee float64, closeReason string) error {
var pos TraderPosition
if err := s.db.First(&pos, id).Error; err != nil {
return fmt.Errorf("failed to get position: %w", err)
@@ -205,11 +220,12 @@ func (s *PositionStore) ClosePositionFully(id int64, exitPrice float64, exitOrde
"quantity": quantity,
"exit_price": exitPrice,
"exit_order_id": exitOrderID,
"exit_time": exitTime,
"exit_time": exitTimeMs,
"realized_pnl": totalRealizedPnL,
"fee": totalFee,
"status": "CLOSED",
"close_reason": closeReason,
"updated_at": time.Now().UTC().UnixMilli(),
}).Error
}
@@ -432,13 +448,13 @@ func (s *PositionStore) GetRecentTrades(traderID string, limit int) ([]RecentTra
EntryPrice: pos.EntryPrice,
ExitPrice: pos.ExitPrice,
RealizedPnL: pos.RealizedPnL,
EntryTime: pos.EntryTime.Unix(),
EntryTime: pos.EntryTime / 1000, // Convert ms to seconds for API compatibility
}
if pos.ExitTime != nil {
t.ExitTime = pos.ExitTime.Unix()
duration := pos.ExitTime.Sub(pos.EntryTime)
t.HoldDuration = formatDuration(duration)
if pos.ExitTime > 0 {
t.ExitTime = pos.ExitTime / 1000 // Convert ms to seconds
durationMs := pos.ExitTime - pos.EntryTime
t.HoldDuration = formatDurationMs(durationMs)
}
if pos.EntryPrice > 0 {
@@ -457,26 +473,34 @@ func (s *PositionStore) GetRecentTrades(traderID string, limit int) ([]RecentTra
// formatDuration formats a duration
func formatDuration(d time.Duration) string {
if d < time.Minute {
return fmt.Sprintf("%ds", int(d.Seconds()))
return formatDurationMs(d.Milliseconds())
}
// formatDurationMs formats a duration in milliseconds
func formatDurationMs(ms int64) string {
seconds := ms / 1000
minutes := seconds / 60
hours := minutes / 60
days := hours / 24
if seconds < 60 {
return fmt.Sprintf("%ds", seconds)
}
if d < time.Hour {
return fmt.Sprintf("%dm", int(d.Minutes()))
if minutes < 60 {
return fmt.Sprintf("%dm", minutes)
}
if d < 24*time.Hour {
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
if minutes == 0 {
if hours < 24 {
remainingMins := minutes % 60
if remainingMins == 0 {
return fmt.Sprintf("%dh", hours)
}
return fmt.Sprintf("%dh%dm", hours, minutes)
return fmt.Sprintf("%dh%dm", hours, remainingMins)
}
days := int(d.Hours()) / 24
hours := int(d.Hours()) % 24
if hours == 0 {
remainingHours := hours % 24
if remainingHours == 0 {
return fmt.Sprintf("%dd", days)
}
return fmt.Sprintf("%dd%dh", days, hours)
return fmt.Sprintf("%dd%dh", days, remainingHours)
}
// calculateSharpeRatioFromPnls calculates Sharpe ratio
@@ -566,8 +590,8 @@ func (s *PositionStore) GetSymbolStats(traderID string, limit int) ([]SymbolStat
s.WinTrades++
}
if pos.ExitTime != nil {
holdMins := pos.ExitTime.Sub(pos.EntryTime).Minutes()
if pos.ExitTime > 0 {
holdMins := float64(pos.ExitTime-pos.EntryTime) / 60000.0 // ms to minutes
symbolHoldMins[pos.Symbol] = append(symbolHoldMins[pos.Symbol], holdMins)
}
}
@@ -615,7 +639,7 @@ type HoldingTimeStats struct {
// GetHoldingTimeStats analyzes performance by holding duration
func (s *PositionStore) GetHoldingTimeStats(traderID string) ([]HoldingTimeStats, error) {
var positions []TraderPosition
err := s.db.Where("trader_id = ? AND status = ? AND exit_time IS NOT NULL", traderID, "CLOSED").Find(&positions).Error
err := s.db.Where("trader_id = ? AND status = ? AND exit_time > 0", traderID, "CLOSED").Find(&positions).Error
if err != nil {
return nil, fmt.Errorf("failed to query holding time stats: %w", err)
}
@@ -632,10 +656,10 @@ func (s *PositionStore) GetHoldingTimeStats(traderID string) ([]HoldingTimeStats
}
for _, pos := range positions {
if pos.ExitTime == nil {
if pos.ExitTime == 0 {
continue
}
holdHours := pos.ExitTime.Sub(pos.EntryTime).Hours()
holdHours := float64(pos.ExitTime-pos.EntryTime) / 3600000.0 // ms to hours
var rangeKey string
switch {
@@ -792,12 +816,12 @@ func (s *PositionStore) GetHistorySummary(traderID string) (*HistorySummary, err
// Calculate average holding time
var positions []TraderPosition
s.db.Where("trader_id = ? AND status = ? AND exit_time IS NOT NULL", traderID, "CLOSED").Find(&positions)
s.db.Where("trader_id = ? AND status = ? AND exit_time > 0", traderID, "CLOSED").Find(&positions)
if len(positions) > 0 {
var totalMins float64
for _, pos := range positions {
if pos.ExitTime != nil {
totalMins += pos.ExitTime.Sub(pos.EntryTime).Minutes()
if pos.ExitTime > 0 {
totalMins += float64(pos.ExitTime-pos.EntryTime) / 60000.0 // ms to minutes
}
}
summary.AvgHoldingMins = totalMins / float64(len(positions))
@@ -917,6 +941,7 @@ func (s *PositionStore) GetOpenPositionByExchangePositionID(exchangeID, exchange
}
// ClosedPnLRecord represents a closed position record from exchange
// All time fields use int64 millisecond timestamps (UTC)
type ClosedPnLRecord struct {
Symbol string
Side string
@@ -926,8 +951,8 @@ type ClosedPnLRecord struct {
RealizedPnL float64
Fee float64
Leverage int
EntryTime time.Time
ExitTime time.Time
EntryTime int64 // Unix milliseconds UTC
ExitTime int64 // Unix milliseconds UTC
OrderID string
CloseType string
ExchangeID string
@@ -954,7 +979,7 @@ func (s *PositionStore) CreateFromClosedPnL(traderID, exchangeID, exchangeType s
exchangePositionID := record.ExchangeID
if exchangePositionID == "" {
exchangePositionID = fmt.Sprintf("%s_%s_%d_%.8f", record.Symbol, side, record.ExitTime.UnixMilli(), record.RealizedPnL)
exchangePositionID = fmt.Sprintf("%s_%s_%d_%.8f", record.Symbol, side, record.ExitTime, record.RealizedPnL)
}
exists, err := s.ExistsWithExchangePositionID(exchangeID, exchangePositionID)
@@ -965,19 +990,22 @@ func (s *PositionStore) CreateFromClosedPnL(traderID, exchangeID, exchangeType s
return false, nil
}
exitTime := record.ExitTime
entryTime := record.EntryTime
exitTimeMs := record.ExitTime
entryTimeMs := record.EntryTime
if exitTime.IsZero() || exitTime.Year() < 2000 {
// Validate timestamps (must be after year 2000 = ~946684800000 ms)
minValidTime := int64(946684800000) // 2000-01-01 UTC in milliseconds
if exitTimeMs < minValidTime {
return false, nil
}
if entryTime.IsZero() || entryTime.Year() < 2000 {
entryTime = exitTime
if entryTimeMs < minValidTime {
entryTimeMs = exitTimeMs
}
if entryTime.After(exitTime) {
entryTime = exitTime
if entryTimeMs > exitTimeMs {
entryTimeMs = exitTimeMs
}
nowMs := time.Now().UTC().UnixMilli()
pos := &TraderPosition{
TraderID: traderID,
ExchangeID: exchangeID,
@@ -988,16 +1016,18 @@ func (s *PositionStore) CreateFromClosedPnL(traderID, exchangeID, exchangeType s
Quantity: record.Quantity,
EntryQuantity: record.Quantity,
EntryPrice: record.EntryPrice,
EntryTime: entryTime,
EntryTime: entryTimeMs,
ExitPrice: record.ExitPrice,
ExitOrderID: record.OrderID,
ExitTime: &exitTime,
ExitTime: exitTimeMs,
RealizedPnL: record.RealizedPnL,
Fee: record.Fee,
Leverage: record.Leverage,
Status: "CLOSED",
CloseReason: record.CloseType,
Source: "sync",
CreatedAt: nowMs,
UpdatedAt: nowMs,
}
err = s.db.Create(pos).Error
@@ -1011,21 +1041,21 @@ func (s *PositionStore) CreateFromClosedPnL(traderID, exchangeID, exchangeType s
return true, nil
}
// GetLastClosedPositionTime gets the most recent exit time
func (s *PositionStore) GetLastClosedPositionTime(traderID string) (time.Time, error) {
// GetLastClosedPositionTime gets the most recent exit time (Unix ms)
func (s *PositionStore) GetLastClosedPositionTime(traderID string) (int64, error) {
var pos TraderPosition
err := s.db.Where("trader_id = ? AND status = ? AND exit_time IS NOT NULL", traderID, "CLOSED").
err := s.db.Where("trader_id = ? AND status = ? AND exit_time > 0", traderID, "CLOSED").
Order("exit_time DESC").
First(&pos).Error
if err == gorm.ErrRecordNotFound || pos.ExitTime == nil {
return time.Now().Add(-30 * 24 * time.Hour), nil
if err == gorm.ErrRecordNotFound || pos.ExitTime == 0 {
return time.Now().UTC().Add(-30 * 24 * time.Hour).UnixMilli(), nil
}
if err != nil {
return time.Time{}, fmt.Errorf("failed to get last closed position time: %w", err)
return 0, fmt.Errorf("failed to get last closed position time: %w", err)
}
return *pos.ExitTime, nil
return pos.ExitTime, nil
}
// CreateOpenPosition creates an open position
@@ -1076,15 +1106,17 @@ func (s *PositionStore) CreateOpenPosition(pos *TraderPosition) error {
}
// ClosePositionWithAccurateData closes a position with accurate data from exchange
func (s *PositionStore) ClosePositionWithAccurateData(id int64, exitPrice float64, exitOrderID string, exitTime time.Time, realizedPnL float64, fee float64, closeReason string) error {
// exitTimeMs is Unix milliseconds UTC
func (s *PositionStore) ClosePositionWithAccurateData(id int64, exitPrice float64, exitOrderID string, exitTimeMs int64, realizedPnL float64, fee float64, closeReason string) error {
return s.db.Model(&TraderPosition{}).Where("id = ?", id).Updates(map[string]interface{}{
"exit_price": exitPrice,
"exit_order_id": exitOrderID,
"exit_time": exitTime,
"exit_time": exitTimeMs,
"realized_pnl": realizedPnL,
"fee": fee,
"status": "CLOSED",
"close_reason": closeReason,
"updated_at": time.Now().UTC().UnixMilli(),
}).Error
}

View File

@@ -25,25 +25,27 @@ func NewPositionBuilder(positionStore *PositionStore) *PositionBuilder {
}
// ProcessTrade processes a single trade and updates position accordingly
// tradeTimeMs is Unix milliseconds UTC
func (pb *PositionBuilder) ProcessTrade(
traderID, exchangeID, exchangeType, symbol, side, action string,
quantity, price, fee, realizedPnL float64,
tradeTime time.Time,
tradeTimeMs int64,
orderID string,
) error {
if strings.HasPrefix(action, "open_") {
return pb.handleOpen(traderID, exchangeID, exchangeType, symbol, side, quantity, price, fee, tradeTime, orderID)
return pb.handleOpen(traderID, exchangeID, exchangeType, symbol, side, quantity, price, fee, tradeTimeMs, orderID)
} else if strings.HasPrefix(action, "close_") {
return pb.handleClose(traderID, exchangeID, exchangeType, symbol, side, quantity, price, fee, realizedPnL, tradeTime, orderID)
return pb.handleClose(traderID, exchangeID, exchangeType, symbol, side, quantity, price, fee, realizedPnL, tradeTimeMs, orderID)
}
return nil
}
// handleOpen handles opening positions (create new or average into existing)
// tradeTimeMs is Unix milliseconds UTC
func (pb *PositionBuilder) handleOpen(
traderID, exchangeID, exchangeType, symbol, side string,
quantity, price, fee float64,
tradeTime time.Time,
tradeTimeMs int64,
orderID string,
) error {
// Get existing OPEN position for (symbol, side)
@@ -52,25 +54,26 @@ func (pb *PositionBuilder) handleOpen(
return fmt.Errorf("failed to get open position: %w", err)
}
nowMs := time.Now().UTC().UnixMilli()
if existing == nil {
// Create new position
position := &TraderPosition{
TraderID: traderID,
ExchangeID: exchangeID,
ExchangeType: exchangeType,
ExchangePositionID: fmt.Sprintf("sync_%s_%s_%d", symbol, side, tradeTime.UnixMilli()),
ExchangePositionID: fmt.Sprintf("sync_%s_%s_%d", symbol, side, tradeTimeMs),
Symbol: symbol,
Side: side,
Quantity: quantity,
EntryPrice: price,
EntryOrderID: orderID,
EntryTime: tradeTime,
EntryTime: tradeTimeMs,
Leverage: 1,
Status: "OPEN",
Source: "sync",
Fee: fee,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedAt: nowMs,
UpdatedAt: nowMs,
}
return pb.positionStore.CreateOpenPosition(position)
}
@@ -90,10 +93,11 @@ func (pb *PositionBuilder) handleOpen(
}
// handleClose handles closing positions (partial or full)
// tradeTimeMs is Unix milliseconds UTC
func (pb *PositionBuilder) handleClose(
traderID, exchangeID, exchangeType, symbol, side string,
quantity, price, fee, realizedPnL float64,
tradeTime time.Time,
tradeTimeMs int64,
orderID string,
) error {
// Get OPEN position
@@ -161,7 +165,7 @@ func (pb *PositionBuilder) handleClose(
position.ID,
finalExitPrice,
orderID,
tradeTime,
tradeTimeMs,
totalPnL,
totalFee,
"sync",

View File

@@ -32,6 +32,9 @@ func (Strategy) TableName() string { return "strategies" }
// StrategyConfig strategy configuration details (JSON structure)
type StrategyConfig struct {
// language setting: "zh" for Chinese, "en" for English
// This determines the language used for data formatting and prompt generation
Language string `json:"language,omitempty"`
// coin source configuration
CoinSource CoinSourceConfig `json:"coin_source"`
// quantitative data configuration
@@ -58,22 +61,21 @@ type PromptSectionsConfig struct {
// CoinSourceConfig coin source configuration
type CoinSourceConfig struct {
// source type: "static" | "coinpool" | "oi_top" | "mixed"
// source type: "static" | "ai500" | "oi_top" | "mixed"
SourceType string `json:"source_type"`
// static coin list (used when source_type = "static")
StaticCoins []string `json:"static_coins,omitempty"`
// excluded coins list (filtered out from all sources)
ExcludedCoins []string `json:"excluded_coins,omitempty"`
// whether to use AI500 coin pool
UseCoinPool bool `json:"use_coin_pool"`
UseAI500 bool `json:"use_ai500"`
// AI500 coin pool maximum count
CoinPoolLimit int `json:"coin_pool_limit,omitempty"`
// AI500 coin pool API URL (strategy-level configuration)
CoinPoolAPIURL string `json:"coin_pool_api_url,omitempty"`
AI500Limit int `json:"ai500_limit,omitempty"`
// whether to use OI Top
UseOITop bool `json:"use_oi_top"`
// OI Top maximum count
OITopLimit int `json:"oi_top_limit,omitempty"`
// OI Top API URL (strategy-level configuration)
OITopAPIURL string `json:"oi_top_api_url,omitempty"`
// Note: API URLs are now built automatically using NofxOSAPIKey from IndicatorConfig
}
// IndicatorConfig indicator configuration
@@ -101,16 +103,30 @@ type IndicatorConfig struct {
BOLLPeriods []int `json:"boll_periods,omitempty"` // default [20] - can select multiple timeframes
// external data sources
ExternalDataSources []ExternalDataSource `json:"external_data_sources,omitempty"`
// ========== NofxOS Unified API Configuration ==========
// Unified API Key for all NofxOS data sources
NofxOSAPIKey string `json:"nofxos_api_key,omitempty"`
// quantitative data sources (capital flow, position changes, price changes)
EnableQuantData bool `json:"enable_quant_data"` // whether to enable quantitative data
QuantDataAPIURL string `json:"quant_data_api_url,omitempty"` // quantitative data API address
EnableQuantOI bool `json:"enable_quant_oi"` // whether to show OI data
EnableQuantNetflow bool `json:"enable_quant_netflow"` // whether to show Netflow data
EnableQuantData bool `json:"enable_quant_data"` // whether to enable quantitative data
EnableQuantOI bool `json:"enable_quant_oi"` // whether to show OI data
EnableQuantNetflow bool `json:"enable_quant_netflow"` // whether to show Netflow data
// OI ranking data (market-wide open interest increase/decrease rankings)
EnableOIRanking bool `json:"enable_oi_ranking"` // whether to enable OI ranking data
OIRankingAPIURL string `json:"oi_ranking_api_url,omitempty"` // OI ranking API base URL
OIRankingDuration string `json:"oi_ranking_duration,omitempty"` // duration: 1h, 4h, 24h
OIRankingLimit int `json:"oi_ranking_limit,omitempty"` // number of entries (default 10)
// NetFlow ranking data (market-wide fund flow rankings - institution/personal)
EnableNetFlowRanking bool `json:"enable_netflow_ranking"` // whether to enable NetFlow ranking data
NetFlowRankingDuration string `json:"netflow_ranking_duration,omitempty"` // duration: 1h, 4h, 24h
NetFlowRankingLimit int `json:"netflow_ranking_limit,omitempty"` // number of entries (default 10)
// Price ranking data (market-wide gainers/losers)
EnablePriceRanking bool `json:"enable_price_ranking"` // whether to enable price ranking data
PriceRankingDuration string `json:"price_ranking_duration,omitempty"` // durations: "1h" or "1h,4h,24h"
PriceRankingLimit int `json:"price_ranking_limit,omitempty"` // number of entries per ranking (default 10)
}
// KlineConfig K-line configuration
@@ -172,14 +188,7 @@ func NewStrategyStore(db *gorm.DB) *StrategyStore {
}
func (s *StrategyStore) initTables() error {
// For PostgreSQL with existing table, skip AutoMigrate
if s.db.Dialector.Name() == "postgres" {
var tableExists int64
s.db.Raw(`SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'strategies'`).Scan(&tableExists)
if tableExists > 0 {
return nil
}
}
// AutoMigrate will add missing columns without dropping existing data
return s.db.AutoMigrate(&Strategy{})
}
@@ -190,15 +199,20 @@ func (s *StrategyStore) initDefaultData() error {
// GetDefaultStrategyConfig returns the default strategy configuration for the given language
func GetDefaultStrategyConfig(lang string) StrategyConfig {
// Normalize language to "zh" or "en"
normalizedLang := "en"
if lang == "zh" {
normalizedLang = "zh"
}
config := StrategyConfig{
Language: normalizedLang,
CoinSource: CoinSourceConfig{
SourceType: "coinpool",
UseCoinPool: true,
CoinPoolLimit: 10,
CoinPoolAPIURL: "http://nofxaios.com:30006/api/ai500/list?auth=cm_568c67eae410d912c54c",
UseOITop: false,
OITopLimit: 20,
OITopAPIURL: "http://nofxaios.com:30006/api/oi/top-ranking?limit=20&duration=1h&auth=cm_568c67eae410d912c54c",
SourceType: "ai500",
UseAI500: true,
AI500Limit: 10,
UseOITop: false,
OITopLimit: 20,
},
Indicators: IndicatorConfig{
Klines: KlineConfig{
@@ -222,15 +236,24 @@ func GetDefaultStrategyConfig(lang string) StrategyConfig {
RSIPeriods: []int{7, 14},
ATRPeriods: []int{14},
BOLLPeriods: []int{20},
// NofxOS unified API key
NofxOSAPIKey: "cm_568c67eae410d912c54c",
// Quant data
EnableQuantData: true,
QuantDataAPIURL: "http://nofxaios.com:30006/api/coin/{symbol}?include=netflow,oi,price&auth=cm_568c67eae410d912c54c",
EnableQuantOI: true,
EnableQuantNetflow: true,
// OI ranking data - market-wide OI increase/decrease rankings
// OI ranking data
EnableOIRanking: true,
OIRankingAPIURL: "http://nofxaios.com:30006",
OIRankingDuration: "1h",
OIRankingLimit: 10,
// NetFlow ranking data
EnableNetFlowRanking: true,
NetFlowRankingDuration: "1h",
NetFlowRankingLimit: 10,
// Price ranking data
EnablePriceRanking: true,
PriceRankingDuration: "1h,4h,24h",
PriceRankingLimit: 10,
},
RiskControl: RiskControlConfig{
MaxPositions: 3, // Max 3 coins simultaneously (CODE ENFORCED)
@@ -305,7 +328,7 @@ func (s *StrategyStore) Update(strategy *Strategy) error {
"config": strategy.Config,
"is_public": strategy.IsPublic,
"config_visible": strategy.ConfigVisible,
"updated_at": time.Now(),
"updated_at": time.Now().UTC(),
}).Error
}

View File

@@ -37,7 +37,7 @@ type Trader struct {
BTCETHLeverage int `gorm:"column:btc_eth_leverage;default:5" json:"btc_eth_leverage,omitempty"`
AltcoinLeverage int `gorm:"column:altcoin_leverage;default:5" json:"altcoin_leverage,omitempty"`
TradingSymbols string `gorm:"column:trading_symbols;default:''" json:"trading_symbols,omitempty"`
UseCoinPool bool `gorm:"column:use_coin_pool;default:false" json:"use_coin_pool,omitempty"`
UseAI500 bool `gorm:"column:use_coin_pool;default:false" json:"use_ai500,omitempty"`
UseOITop bool `gorm:"column:use_oi_top;default:false" json:"use_oi_top,omitempty"`
CustomPrompt string `gorm:"column:custom_prompt;default:''" json:"custom_prompt,omitempty"`
OverrideBasePrompt bool `gorm:"column:override_base_prompt;default:false" json:"override_base_prompt,omitempty"`
@@ -124,6 +124,9 @@ func (s *TraderStore) Update(trader *Trader) error {
}
if trader.ScanIntervalMinutes > 0 {
updates["scan_interval_minutes"] = trader.ScanIntervalMinutes
fmt.Printf("📊 TraderStore.Update: scan_interval_minutes=%d will be saved\n", trader.ScanIntervalMinutes)
} else {
fmt.Printf("⚠️ TraderStore.Update: scan_interval_minutes=%d (<=0, NOT updating)\n", trader.ScanIntervalMinutes)
}
return s.db.Model(&Trader{}).

View File

@@ -123,7 +123,7 @@ func (s *UserStore) UpdateOTPVerified(userID string, verified bool) error {
func (s *UserStore) UpdatePassword(userID, passwordHash string) error {
return s.db.Model(&User{}).Where("id = ?", userID).Updates(map[string]interface{}{
"password_hash": passwordHash,
"updated_at": time.Now(),
"updated_at": time.Now().UTC(),
}).Error
}

View File

@@ -34,7 +34,7 @@ func (t *AsterTrader) SyncOrdersFromAster(traderID string, exchangeID string, ex
// Sort trades by time ASC (oldest first) for proper position building
sort.Slice(trades, func(i, j int) bool {
return trades[i].Time.Before(trades[j].Time)
return trades[i].Time.UnixMilli() < trades[j].Time.UnixMilli()
})
// Process trades one by one (no transaction to avoid deadlock)
@@ -68,7 +68,8 @@ func (t *AsterTrader) SyncOrdersFromAster(traderID string, exchangeID string, ex
// Normalize side for storage
side := strings.ToUpper(trade.Side)
// Create order record
// Create order record - use Unix milliseconds UTC
tradeTimeMs := trade.Time.UTC().UnixMilli()
orderRecord := &store.TraderOrder{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -85,9 +86,9 @@ func (t *AsterTrader) SyncOrdersFromAster(traderID string, exchangeID string, ex
FilledQuantity: trade.Quantity,
AvgFillPrice: trade.Price,
Commission: trade.Fee,
FilledAt: trade.Time,
CreatedAt: trade.Time,
UpdatedAt: trade.Time,
FilledAt: tradeTimeMs,
CreatedAt: tradeTimeMs,
UpdatedAt: tradeTimeMs,
}
// Insert order record
@@ -96,7 +97,7 @@ func (t *AsterTrader) SyncOrdersFromAster(traderID string, exchangeID string, ex
continue
}
// Create fill record
// Create fill record - use Unix milliseconds UTC
fillRecord := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -113,7 +114,7 @@ func (t *AsterTrader) SyncOrdersFromAster(traderID string, exchangeID string, ex
CommissionAsset: "USDT",
RealizedPnL: trade.RealizedPnL,
IsMaker: false,
CreatedAt: trade.Time,
CreatedAt: tradeTimeMs,
}
if err := orderStore.CreateFill(fillRecord); err != nil {
@@ -125,7 +126,7 @@ func (t *AsterTrader) SyncOrdersFromAster(traderID string, exchangeID string, ex
traderID, exchangeID, exchangeType,
symbol, positionSide, orderAction,
trade.Quantity, trade.Price, trade.Fee, trade.RealizedPnL,
trade.Time, trade.TradeID,
tradeTimeMs, trade.TradeID,
); err != nil {
logger.Infof(" ⚠️ Failed to sync position for trade %s: %v", trade.TradeID, err)
} else {

View File

@@ -1407,10 +1407,16 @@ func (t *AsterTrader) GetTrades(startTime time.Time, limit int) ([]TradeRecord,
Quantity: qty,
RealizedPnL: pnl,
Fee: fee,
Time: time.UnixMilli(at.Time),
Time: time.UnixMilli(at.Time).UTC(),
}
result = append(result, trade)
}
return result, nil
}
// GetOpenOrders gets all open/pending orders for a symbol
func (t *AsterTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) {
// TODO: Implement Aster open orders
return []OpenOrder{}, nil
}

View File

@@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"math"
"nofx/decision"
"nofx/kernel"
"nofx/experience"
"nofx/logger"
"nofx/market"
@@ -104,7 +104,7 @@ type AutoTrader struct {
trader Trader // Use Trader interface (supports multiple platforms)
mcpClient mcp.AIClient
store *store.Store // Data storage (decision records, etc.)
strategyEngine *decision.StrategyEngine // Strategy engine (uses strategy configuration)
strategyEngine *kernel.StrategyEngine // Strategy engine (uses strategy configuration)
cycleNumber int // Current cycle number
initialBalance float64
dailyPnL float64
@@ -310,7 +310,7 @@ func NewAutoTrader(config AutoTraderConfig, st *store.Store, userID string) (*Au
if config.StrategyConfig == nil {
return nil, fmt.Errorf("[%s] strategy not configured", config.Name)
}
strategyEngine := decision.NewStrategyEngine(config.StrategyConfig)
strategyEngine := kernel.NewStrategyEngine(config.StrategyConfig)
logger.Infof("✓ [%s] Using strategy engine (strategy configuration loaded)", config.Name)
return &AutoTrader{
@@ -524,7 +524,7 @@ func (at *AutoTrader) runCycle() error {
// 5. Use strategy engine to call AI for decision
logger.Infof("🤖 Requesting AI analysis and decision... [Strategy Engine]")
aiDecision, err := decision.GetFullDecisionWithStrategy(ctx, at.mcpClient, at.strategyEngine, "balanced")
aiDecision, err := kernel.GetFullDecisionWithStrategy(ctx, at.mcpClient, at.strategyEngine, "balanced")
if aiDecision != nil && aiDecision.AIRequestDurationMs > 0 {
record.AIRequestDurationMs = aiDecision.AIRequestDurationMs
@@ -585,8 +585,8 @@ func (at *AutoTrader) runCycle() error {
// logger.Infof(strings.Repeat("-", 70) + "\n")
// 7. Print AI decisions
// logger.Infof("📋 AI decision list (%d items):\n", len(decision.Decisions))
// for i, d := range decision.Decisions {
// logger.Infof("📋 AI decision list (%d items):\n", len(kernel.Decisions))
// for i, d := range kernel.Decisions {
// logger.Infof(" [%d] %s: %s - %s", i+1, d.Symbol, d.Action, d.Reasoning)
// if d.Action == "open_long" || d.Action == "open_short" {
// logger.Infof(" Leverage: %dx | Position: %.2f USDT | Stop loss: %.4f | Take profit: %.4f",
@@ -637,7 +637,7 @@ func (at *AutoTrader) runCycle() error {
TakeProfit: d.TakeProfit,
Confidence: d.Confidence,
Reasoning: d.Reasoning,
Timestamp: time.Now(),
Timestamp: time.Now().UTC(),
Success: false,
}
@@ -664,7 +664,7 @@ func (at *AutoTrader) runCycle() error {
}
// buildTradingContext builds trading context
func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
func (at *AutoTrader) buildTradingContext() (*kernel.Context, error) {
// 1. Get account information
balance, err := at.trader.GetBalance()
if err != nil {
@@ -701,7 +701,7 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
return nil, fmt.Errorf("failed to get positions: %w", err)
}
var positionInfos []decision.PositionInfo
var positionInfos []kernel.PositionInfo
totalMarginUsed := 0.0
// Current position key set (for cleaning up closed position records)
@@ -744,8 +744,8 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
// Priority 1: Get from database (trader_positions table) - most accurate
if at.store != nil {
if dbPos, err := at.store.Position().GetOpenPositionBySymbol(at.id, symbol, side); err == nil && dbPos != nil {
if !dbPos.EntryTime.IsZero() {
updateTime = dbPos.EntryTime.UnixMilli()
if dbPos.EntryTime > 0 {
updateTime = dbPos.EntryTime
}
}
}
@@ -768,7 +768,7 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
peakPnlPct := at.peakPnLCache[posKey]
at.peakPnLCacheMutex.RUnlock()
positionInfos = append(positionInfos, decision.PositionInfo{
positionInfos = append(positionInfos, kernel.PositionInfo{
Symbol: symbol,
Side: side,
EntryPrice: entryPrice,
@@ -820,13 +820,13 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
logger.Infof("📋 [%s] Strategy leverage config: BTC/ETH=%dx, Altcoin=%dx", at.name, btcEthLeverage, altcoinLeverage)
// 6. Build context
ctx := &decision.Context{
ctx := &kernel.Context{
CurrentTime: time.Now().UTC().Format("2006-01-02 15:04:05 UTC"),
RuntimeMinutes: int(time.Since(at.startTime).Minutes()),
CallCount: at.callCount,
BTCETHLeverage: btcEthLeverage,
AltcoinLeverage: altcoinLeverage,
Account: decision.AccountInfo{
Account: kernel.AccountInfo{
TotalEquity: totalEquity,
AvailableBalance: availableBalance,
UnrealizedPnL: totalUnrealizedProfit,
@@ -859,7 +859,7 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
exitTimeStr = time.Unix(trade.ExitTime, 0).UTC().Format("01-02 15:04 UTC")
}
ctx.RecentOrders = append(ctx.RecentOrders, decision.RecentOrder{
ctx.RecentOrders = append(ctx.RecentOrders, kernel.RecentOrder{
Symbol: trade.Symbol,
Side: trade.Side,
EntryPrice: trade.EntryPrice,
@@ -881,7 +881,7 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
} else if stats.TotalTrades == 0 {
logger.Infof("⚠️ [%s] GetFullStats returned 0 trades (traderID=%s)", at.name, at.id)
} else {
ctx.TradingStats = &decision.TradingStats{
ctx.TradingStats = &kernel.TradingStats{
TotalTrades: stats.TotalTrades,
WinRate: stats.WinRate,
ProfitFactor: stats.ProfitFactor,
@@ -899,7 +899,7 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
}
// 8. Get quantitative data (if enabled in strategy config)
if strategyConfig.Indicators.EnableQuantData && strategyConfig.Indicators.QuantDataAPIURL != "" {
if strategyConfig.Indicators.EnableQuantData {
// Collect symbols to query (candidate coins + position coins)
symbolsToQuery := make(map[string]bool)
for _, coin := range candidateCoins {
@@ -929,11 +929,31 @@ func (at *AutoTrader) buildTradingContext() (*decision.Context, error) {
}
}
// 10. Get NetFlow ranking data (market-wide fund flow)
if strategyConfig.Indicators.EnableNetFlowRanking {
logger.Infof("💰 [%s] Fetching NetFlow ranking data...", at.name)
ctx.NetFlowRankingData = at.strategyEngine.FetchNetFlowRankingData()
if ctx.NetFlowRankingData != nil {
logger.Infof("💰 [%s] NetFlow ranking data ready: inst_in=%d, inst_out=%d",
at.name, len(ctx.NetFlowRankingData.InstitutionFutureTop), len(ctx.NetFlowRankingData.InstitutionFutureLow))
}
}
// 11. Get Price ranking data (market-wide gainers/losers)
if strategyConfig.Indicators.EnablePriceRanking {
logger.Infof("📈 [%s] Fetching Price ranking data...", at.name)
ctx.PriceRankingData = at.strategyEngine.FetchPriceRankingData()
if ctx.PriceRankingData != nil {
logger.Infof("📈 [%s] Price ranking data ready for %d durations",
at.name, len(ctx.PriceRankingData.Durations))
}
}
return ctx, nil
}
// executeDecisionWithRecord executes AI decision and records detailed information
func (at *AutoTrader) executeDecisionWithRecord(decision *decision.Decision, actionRecord *store.DecisionAction) error {
func (at *AutoTrader) executeDecisionWithRecord(decision *kernel.Decision, actionRecord *store.DecisionAction) error {
switch decision.Action {
case "open_long":
return at.executeOpenLongWithRecord(decision, actionRecord)
@@ -953,7 +973,7 @@ func (at *AutoTrader) executeDecisionWithRecord(decision *decision.Decision, act
// ExecuteDecision executes a trading decision from external sources (e.g., debate consensus)
// This is a public method that can be called by other modules
func (at *AutoTrader) ExecuteDecision(d *decision.Decision) error {
func (at *AutoTrader) ExecuteDecision(d *kernel.Decision) error {
logger.Infof("[%s] Executing external decision: %s %s", at.name, d.Action, d.Symbol)
// Create a minimal action record for tracking
@@ -979,7 +999,7 @@ func (at *AutoTrader) ExecuteDecision(d *decision.Decision) error {
}
// executeOpenLongWithRecord executes open long position and records detailed information
func (at *AutoTrader) executeOpenLongWithRecord(decision *decision.Decision, actionRecord *store.DecisionAction) error {
func (at *AutoTrader) executeOpenLongWithRecord(decision *kernel.Decision, actionRecord *store.DecisionAction) error {
logger.Infof(" 📈 Open long: %s", decision.Symbol)
// ⚠️ Get current positions for multiple checks
@@ -1096,7 +1116,7 @@ func (at *AutoTrader) executeOpenLongWithRecord(decision *decision.Decision, act
}
// executeOpenShortWithRecord executes open short position and records detailed information
func (at *AutoTrader) executeOpenShortWithRecord(decision *decision.Decision, actionRecord *store.DecisionAction) error {
func (at *AutoTrader) executeOpenShortWithRecord(decision *kernel.Decision, actionRecord *store.DecisionAction) error {
logger.Infof(" 📉 Open short: %s", decision.Symbol)
// ⚠️ Get current positions for multiple checks
@@ -1213,7 +1233,7 @@ func (at *AutoTrader) executeOpenShortWithRecord(decision *decision.Decision, ac
}
// executeCloseLongWithRecord executes close long position and records detailed information
func (at *AutoTrader) executeCloseLongWithRecord(decision *decision.Decision, actionRecord *store.DecisionAction) error {
func (at *AutoTrader) executeCloseLongWithRecord(decision *kernel.Decision, actionRecord *store.DecisionAction) error {
logger.Infof(" 🔄 Close long: %s", decision.Symbol)
// Get current price
@@ -1277,7 +1297,7 @@ func (at *AutoTrader) executeCloseLongWithRecord(decision *decision.Decision, ac
}
// executeCloseShortWithRecord executes close short position and records detailed information
func (at *AutoTrader) executeCloseShortWithRecord(decision *decision.Decision, actionRecord *store.DecisionAction) error {
func (at *AutoTrader) executeCloseShortWithRecord(decision *kernel.Decision, actionRecord *store.DecisionAction) error {
logger.Infof(" 🔄 Close short: %s", decision.Symbol)
// Get current price
@@ -1392,7 +1412,7 @@ func (at *AutoTrader) GetSystemPromptTemplate() string {
}
// saveEquitySnapshot saves equity snapshot independently (for drawing profit curve, decoupled from AI decision)
func (at *AutoTrader) saveEquitySnapshot(ctx *decision.Context) {
func (at *AutoTrader) saveEquitySnapshot(ctx *kernel.Context) {
if at.store == nil || ctx == nil {
return
}
@@ -1624,7 +1644,7 @@ func calculatePnLPercentage(unrealizedPnl, marginUsed float64) float64 {
// sortDecisionsByPriority sorts decisions: close positions first, then open positions, finally hold/wait
// This avoids position stacking overflow when changing positions
func sortDecisionsByPriority(decisions []decision.Decision) []decision.Decision {
func sortDecisionsByPriority(decisions []kernel.Decision) []kernel.Decision {
if len(decisions) <= 1 {
return decisions
}
@@ -1644,7 +1664,7 @@ func sortDecisionsByPriority(decisions []decision.Decision) []decision.Decision
}
// Copy decision list
sorted := make([]decision.Decision, len(decisions))
sorted := make([]kernel.Decision, len(decisions))
copy(sorted, decisions)
// Sort by priority
@@ -1947,6 +1967,7 @@ func (at *AutoTrader) recordPositionChange(orderID, symbol, side, action string,
switch action {
case "open_long", "open_short":
// Open position: create new position record
nowMs := time.Now().UTC().UnixMilli()
pos := &store.TraderPosition{
TraderID: at.id,
ExchangeID: at.exchangeID, // Exchange account UUID
@@ -1956,9 +1977,11 @@ func (at *AutoTrader) recordPositionChange(orderID, symbol, side, action string,
Quantity: quantity,
EntryPrice: price,
EntryOrderID: orderID,
EntryTime: time.Now(),
EntryTime: nowMs,
Leverage: leverage,
Status: "OPEN",
CreatedAt: nowMs,
UpdatedAt: nowMs,
}
if err := at.store.Position().Create(pos); err != nil {
logger.Infof(" ⚠️ Failed to record position: %v", err)
@@ -1976,7 +1999,7 @@ func (at *AutoTrader) recordPositionChange(orderID, symbol, side, action string,
at.id, at.exchangeID, at.exchange,
symbol, side, action,
quantity, price, fee, 0, // realizedPnL will be calculated
time.Now(), orderID,
time.Now().UTC().UnixMilli(), orderID,
); err != nil {
logger.Infof(" ⚠️ Failed to process close position: %v", err)
} else {
@@ -2029,8 +2052,8 @@ func (at *AutoTrader) createOrderRecord(orderID, symbol, action, positionSide st
ReduceOnly: reduceOnly,
ClosePosition: reduceOnly,
OrderAction: orderAction,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedAt: time.Now().UTC().UnixMilli(),
UpdatedAt: time.Now().UTC().UnixMilli(),
}
}
@@ -2071,7 +2094,7 @@ func (at *AutoTrader) recordOrderFill(orderRecordID int64, exchangeOrderID, symb
CommissionAsset: "USDT",
RealizedPnL: 0, // Will be calculated for close orders
IsMaker: false, // Market orders are usually taker
CreatedAt: time.Now(),
CreatedAt: time.Now().UTC().UnixMilli(),
}
// Calculate realized PnL for close orders
@@ -2195,3 +2218,8 @@ func getSideFromAction(action string) string {
}
}
// GetOpenOrders returns open orders (pending SL/TP) from exchange
func (at *AutoTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) {
return at.trader.GetOpenOrders(symbol)
}

View File

@@ -776,6 +776,64 @@ func (t *FuturesTrader) CancelStopOrders(symbol string) error {
return nil
}
// GetOpenOrders gets all open/pending orders for a symbol
func (t *FuturesTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) {
var result []OpenOrder
// 1. Get legacy open orders
orders, err := t.client.NewListOpenOrdersService().
Symbol(symbol).
Do(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get open orders: %w", err)
}
for _, order := range orders {
price, _ := strconv.ParseFloat(order.Price, 64)
stopPrice, _ := strconv.ParseFloat(order.StopPrice, 64)
quantity, _ := strconv.ParseFloat(order.OrigQuantity, 64)
result = append(result, OpenOrder{
OrderID: fmt.Sprintf("%d", order.OrderID),
Symbol: order.Symbol,
Side: string(order.Side),
PositionSide: string(order.PositionSide),
Type: string(order.Type),
Price: price,
StopPrice: stopPrice,
Quantity: quantity,
Status: string(order.Status),
})
}
// 2. Get Algo orders (new API for stop-loss/take-profit)
algoOrders, err := t.client.NewListOpenAlgoOrdersService().
Symbol(symbol).
Do(context.Background())
if err == nil {
for _, algoOrder := range algoOrders {
triggerPrice, _ := strconv.ParseFloat(algoOrder.TriggerPrice, 64)
quantity, _ := strconv.ParseFloat(algoOrder.Quantity, 64)
result = append(result, OpenOrder{
OrderID: fmt.Sprintf("%d", algoOrder.AlgoId),
Symbol: algoOrder.Symbol,
Side: string(algoOrder.Side),
PositionSide: string(algoOrder.PositionSide),
Type: string(algoOrder.OrderType),
Price: 0, // Algo orders use stop price
StopPrice: triggerPrice,
Quantity: quantity,
Status: "NEW",
})
}
}
return result, nil
}
// GetMarketPrice gets market price
func (t *FuturesTrader) GetMarketPrice(symbol string) (float64, error) {
prices, err := t.client.NewListPricesService().Symbol(symbol).Do(context.Background())
@@ -1122,7 +1180,7 @@ func (t *FuturesTrader) GetTrades(startTime time.Time, limit int) ([]TradeRecord
TradeID: strconv.FormatInt(income.TranID, 10),
Symbol: income.Symbol,
RealizedPnL: pnl,
Time: time.UnixMilli(income.Time),
Time: time.UnixMilli(income.Time).UTC(),
// Note: Income API doesn't provide price, quantity, side, fee
// For accurate data, use GetTradesForSymbol with specific symbol
}
@@ -1167,7 +1225,7 @@ func (t *FuturesTrader) GetTradesForSymbol(symbol string, startTime time.Time, l
Quantity: qty,
RealizedPnL: pnl,
Fee: fee,
Time: time.UnixMilli(at.Time),
Time: time.UnixMilli(at.Time).UTC(),
}
trades = append(trades, trade)
}
@@ -1210,7 +1268,7 @@ func (t *FuturesTrader) GetTradesForSymbolFromID(symbol string, fromID int64, li
Quantity: qty,
RealizedPnL: pnl,
Fee: fee,
Time: time.UnixMilli(at.Time),
Time: time.UnixMilli(at.Time).UTC(),
}
trades = append(trades, trade)
}
@@ -1244,3 +1302,30 @@ func (t *FuturesTrader) GetCommissionSymbols(lastSyncTime time.Time) ([]string,
return symbols, nil
}
// GetPnLSymbols returns symbols that have REALIZED_PNL records since lastSyncTime
// This is a fallback when COMMISSION detection fails (VIP users, BNB fee discount)
func (t *FuturesTrader) GetPnLSymbols(lastSyncTime time.Time) ([]string, error) {
incomes, err := t.client.NewGetIncomeHistoryService().
IncomeType("REALIZED_PNL").
StartTime(lastSyncTime.UnixMilli()).
Limit(1000).
Do(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get PnL history: %w", err)
}
symbolMap := make(map[string]bool)
for _, income := range incomes {
if income.Symbol != "" {
symbolMap[income.Symbol] = true
}
}
var symbols []string
for symbol := range symbolMap {
symbols = append(symbols, symbol)
}
return symbols, nil
}

View File

@@ -11,9 +11,9 @@ import (
"time"
)
// syncState stores the last sync time for incremental sync
// syncState stores the last sync time (Unix ms) for incremental sync
var (
binanceSyncState = make(map[string]time.Time) // exchangeID -> lastSyncTime
binanceSyncState = make(map[string]int64) // exchangeID -> lastSyncTimeMs (Unix ms)
binanceSyncStateMutex sync.RWMutex
)
@@ -25,42 +25,106 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
return fmt.Errorf("store is nil")
}
// Get last sync time (default to 24 hours ago for first sync)
orderStore := st.Order()
// Get last sync time (Unix ms) - first try memory, then database, then default
binanceSyncStateMutex.RLock()
lastSyncTime, exists := binanceSyncState[exchangeID]
lastSyncTimeMs, exists := binanceSyncState[exchangeID]
binanceSyncStateMutex.RUnlock()
nowMs := time.Now().UTC().UnixMilli()
if !exists {
lastSyncTime = time.Now().Add(-24 * time.Hour)
// Try to get last fill time from database (persist across restarts)
lastFillTimeMs, err := orderStore.GetLastFillTimeByExchange(exchangeID)
if err == nil && lastFillTimeMs > 0 {
// If recovered time is in the future, it's clearly wrong - use default
if lastFillTimeMs > nowMs {
logger.Infof("⚠️ DB sync time %d is in the future (now: %d), using default",
lastFillTimeMs, nowMs)
lastSyncTimeMs = nowMs - 24*60*60*1000 // 24 hours ago
} else {
// Add 1 second buffer to avoid re-fetching the same fill
lastSyncTimeMs = lastFillTimeMs + 1000
logger.Infof("📅 Recovered last sync time from DB: %s (UTC)",
time.UnixMilli(lastSyncTimeMs).UTC().Format("2006-01-02 15:04:05"))
}
} else {
// First sync: go back 24 hours
lastSyncTimeMs = nowMs - 24*60*60*1000
logger.Infof("📅 First sync, starting from 24 hours ago: %s (UTC)",
time.UnixMilli(lastSyncTimeMs).UTC().Format("2006-01-02 15:04:05"))
}
}
// Record current time BEFORE querying, to avoid missing trades during sync
// This prevents race condition where trades happen between query and lastSyncTime update
syncStartTime := time.Now()
syncStartTimeMs := nowMs
logger.Infof("🔄 Syncing Binance trades from: %s", lastSyncTime.Format(time.RFC3339))
logger.Infof("🔄 Syncing Binance trades from: %s (UTC)",
time.UnixMilli(lastSyncTimeMs).UTC().Format("2006-01-02 15:04:05"))
// Step 1: Get max trade IDs from local DB for incremental sync
orderStore := st.Order()
maxTradeIDs, err := orderStore.GetMaxTradeIDsByExchange(exchangeID)
if err != nil {
logger.Infof(" ⚠️ Failed to get max trade IDs: %v, will use time-based query", err)
maxTradeIDs = make(map[string]int64)
}
// Step 2: Use COMMISSION to detect which symbols have new trades (1 API call)
changedSymbols, err := t.GetCommissionSymbols(lastSyncTime)
// Step 2: Detect symbols to sync using multiple methods
// COMMISSION detection may miss trades (VIP users, BNB discount, 0-fee trades)
symbolMap := make(map[string]bool)
lastSyncTime := time.UnixMilli(lastSyncTimeMs) // Convert to time.Time for API calls
// Method 1: COMMISSION income detection
commissionSymbols, err := t.GetCommissionSymbols(lastSyncTime)
if err != nil {
logger.Infof(" ⚠️ Failed to get commission symbols: %v, falling back to positions", err)
// Fallback: only sync symbols with active positions
changedSymbols = t.getPositionSymbols()
logger.Infof(" ⚠️ Failed to get commission symbols: %v", err)
} else {
logger.Infof(" 📋 COMMISSION symbols found: %d - %v", len(commissionSymbols), commissionSymbols)
for _, s := range commissionSymbols {
symbolMap[s] = true
}
}
// Method 2: Always include active positions (catches trades that COMMISSION missed)
positionSymbols := t.getPositionSymbols()
logger.Infof(" 📋 Position symbols found: %d - %v", len(positionSymbols), positionSymbols)
for _, s := range positionSymbols {
symbolMap[s] = true
}
// Method 3: Include symbols from recent fills in DB (in case some were partially synced)
recentSymbols, _ := orderStore.GetRecentFillSymbolsByExchange(exchangeID, lastSyncTimeMs)
logger.Infof(" 📋 Recent fill symbols found: %d - %v", len(recentSymbols), recentSymbols)
for _, s := range recentSymbols {
symbolMap[s] = true
}
// Method 4: FALLBACK - Query REALIZED_PNL income to find symbols with closed trades
// This catches trades that COMMISSION missed (VIP users, BNB fee discount)
if len(symbolMap) == 0 {
logger.Infof(" 🔍 No symbols found, trying REALIZED_PNL fallback...")
pnlSymbols, err := t.GetPnLSymbols(lastSyncTime)
if err != nil {
logger.Infof(" ⚠️ Failed to get PnL symbols: %v", err)
} else {
logger.Infof(" 📋 REALIZED_PNL symbols found: %d - %v", len(pnlSymbols), pnlSymbols)
for _, s := range pnlSymbols {
symbolMap[s] = true
}
}
}
var changedSymbols []string
for s := range symbolMap {
changedSymbols = append(changedSymbols, s)
}
if len(changedSymbols) == 0 {
logger.Infof("📭 No symbols with new trades to sync")
// Update last sync time even if no changes
binanceSyncStateMutex.Lock()
binanceSyncState[exchangeID] = syncStartTime
binanceSyncState[exchangeID] = syncStartTimeMs
binanceSyncStateMutex.Unlock()
return nil
}
@@ -98,7 +162,7 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
// This prevents data loss when some symbols fail due to rate limit or network issues
if len(failedSymbols) == 0 {
binanceSyncStateMutex.Lock()
binanceSyncState[exchangeID] = syncStartTime
binanceSyncState[exchangeID] = syncStartTimeMs
binanceSyncStateMutex.Unlock()
} else {
logger.Infof(" ⚠️ %d symbols failed, not updating lastSyncTime to retry next time: %v", len(failedSymbols), failedSymbols)
@@ -110,7 +174,7 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
// Sort trades by time ASC (oldest first) for proper position building
sort.Slice(allTrades, func(i, j int) bool {
return allTrades[i].Time.Before(allTrades[j].Time)
return allTrades[i].Time.UnixMilli() < allTrades[j].Time.UnixMilli()
})
// Process trades one by one
@@ -145,7 +209,8 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
// Normalize side
side := strings.ToUpper(trade.Side)
// Create order record
// Create order record - use Unix milliseconds UTC
tradeTimeMs := trade.Time.UTC().UnixMilli()
orderRecord := &store.TraderOrder{
TraderID: traderID,
ExchangeID: exchangeID,
@@ -162,9 +227,9 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
FilledQuantity: trade.Quantity,
AvgFillPrice: trade.Price,
Commission: trade.Fee,
FilledAt: trade.Time,
CreatedAt: trade.Time,
UpdatedAt: trade.Time,
FilledAt: tradeTimeMs,
CreatedAt: tradeTimeMs,
UpdatedAt: tradeTimeMs,
}
// Insert order record
@@ -173,7 +238,7 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
continue
}
// Create fill record
// Create fill record - use Unix milliseconds UTC
fillRecord := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID,
@@ -190,7 +255,7 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
CommissionAsset: "USDT",
RealizedPnL: trade.RealizedPnL,
IsMaker: false,
CreatedAt: trade.Time,
CreatedAt: tradeTimeMs,
}
if err := orderStore.CreateFill(fillRecord); err != nil {
@@ -202,7 +267,7 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
traderID, exchangeID, exchangeType,
symbol, positionSide, orderAction,
trade.Quantity, trade.Price, trade.Fee, trade.RealizedPnL,
trade.Time, trade.TradeID,
tradeTimeMs, trade.TradeID,
); err != nil {
logger.Infof(" ⚠️ Failed to sync position for trade %s: %v", trade.TradeID, err)
} else {
@@ -210,8 +275,9 @@ func (t *FuturesTrader) SyncOrdersFromBinance(traderID string, exchangeID string
}
syncedCount++
logger.Infof(" ✅ Synced trade: %s %s %s qty=%.6f price=%.6f pnl=%.2f fee=%.6f action=%s",
trade.TradeID, symbol, side, trade.Quantity, trade.Price, trade.RealizedPnL, trade.Fee, orderAction)
logger.Infof(" ✅ Synced trade: %s %s %s qty=%.6f price=%.6f pnl=%.2f fee=%.6f action=%s time=%s(UTC)",
trade.TradeID, symbol, side, trade.Quantity, trade.Price, trade.RealizedPnL, trade.Fee, orderAction,
trade.Time.UTC().Format("01-02 15:04:05"))
}
logger.Infof("✅ Binance order sync completed: %d new trades synced", syncedCount)
@@ -278,6 +344,15 @@ func (t *FuturesTrader) determineOrderAction(side, positionSide string, realized
// StartOrderSync starts background order sync task for Binance
func (t *FuturesTrader) StartOrderSync(traderID string, exchangeID string, exchangeType string, st *store.Store, interval time.Duration) {
// Run first sync immediately
go func() {
logger.Infof("🔄 Running initial Binance order sync...")
if err := t.SyncOrdersFromBinance(traderID, exchangeID, exchangeType, st); err != nil {
logger.Infof("⚠️ Initial Binance order sync failed: %v", err)
}
}()
// Then run periodically
ticker := time.NewTicker(interval)
go func() {
for range ticker.C {

View File

@@ -0,0 +1,461 @@
package trader
import (
"context"
"fmt"
"os"
"testing"
"time"
)
func skipIfNoLiveTest(t *testing.T) {
if os.Getenv("BINANCE_LIVE_TEST") != "1" {
t.Skip("Skipping live test. Set BINANCE_LIVE_TEST=1 to run")
}
}
func getBinanceTestCredentials(t *testing.T) (string, string) {
apiKey := os.Getenv("BINANCE_TEST_API_KEY")
secretKey := os.Getenv("BINANCE_TEST_SECRET_KEY")
if apiKey == "" || secretKey == "" {
t.Skip("Skipping test. Set BINANCE_TEST_API_KEY and BINANCE_TEST_SECRET_KEY env vars")
}
return apiKey, secretKey
}
func createBinanceTestTrader(t *testing.T) *FuturesTrader {
apiKey, secretKey := getBinanceTestCredentials(t)
trader := NewFuturesTrader(apiKey, secretKey, "test-user")
return trader
}
// TestBinanceConnection tests basic API connectivity
func TestBinanceConnection(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
balance, err := trader.GetBalance()
if err != nil {
t.Fatalf("Failed to get balance: %v", err)
}
t.Logf("✅ Connection OK - Balance: %v", balance)
}
// TestBinanceGetPositions tests position retrieval
func TestBinanceGetPositions(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
positions, err := trader.GetPositions()
if err != nil {
t.Fatalf("Failed to get positions: %v", err)
}
t.Logf("📊 Found %d positions with non-zero amount:", len(positions))
for i, pos := range positions {
symbol := pos["symbol"].(string)
side := pos["side"].(string)
posAmt := pos["positionAmt"].(float64)
entryPrice := pos["entryPrice"].(float64)
unrealizedPnl := pos["unRealizedProfit"].(float64)
t.Logf(" [%d] %s %s: qty=%.6f entry=%.4f pnl=%.4f",
i+1, symbol, side, posAmt, entryPrice, unrealizedPnl)
}
}
// TestBinanceGetCommissionSymbols tests COMMISSION income detection
func TestBinanceGetCommissionSymbols(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
// Test different time ranges
timeRanges := []struct {
name string
duration time.Duration
}{
{"1 hour", 1 * time.Hour},
{"24 hours", 24 * time.Hour},
{"7 days", 7 * 24 * time.Hour},
{"30 days", 30 * 24 * time.Hour},
}
for _, tr := range timeRanges {
startTime := time.Now().Add(-tr.duration)
symbols, err := trader.GetCommissionSymbols(startTime)
if err != nil {
t.Logf("❌ %s: Failed to get commission symbols: %v", tr.name, err)
continue
}
t.Logf("📋 %s: COMMISSION symbols = %d - %v", tr.name, len(symbols), symbols)
}
}
// TestBinanceGetPnLSymbols tests REALIZED_PNL income detection
func TestBinanceGetPnLSymbols(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
timeRanges := []struct {
name string
duration time.Duration
}{
{"1 hour", 1 * time.Hour},
{"24 hours", 24 * time.Hour},
{"7 days", 7 * 24 * time.Hour},
{"30 days", 30 * 24 * time.Hour},
}
for _, tr := range timeRanges {
startTime := time.Now().Add(-tr.duration)
symbols, err := trader.GetPnLSymbols(startTime)
if err != nil {
t.Logf("❌ %s: Failed to get PnL symbols: %v", tr.name, err)
continue
}
t.Logf("📋 %s: REALIZED_PNL symbols = %d - %v", tr.name, len(symbols), symbols)
}
}
// TestBinanceGetAllIncomeTypes tests all income types to understand data availability
func TestBinanceGetAllIncomeTypes(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
// All possible income types from Binance API
incomeTypes := []string{
"TRANSFER",
"WELCOME_BONUS",
"REALIZED_PNL",
"FUNDING_FEE",
"COMMISSION",
"INSURANCE_CLEAR",
"REFERRAL_KICKBACK",
"COMMISSION_REBATE",
"API_REBATE",
"CONTEST_REWARD",
"CROSS_COLLATERAL_TRANSFER",
"OPTIONS_PREMIUM_FEE",
"OPTIONS_SETTLE_PROFIT",
"INTERNAL_TRANSFER",
"AUTO_EXCHANGE",
"DELIVERED_SETTELMENT",
"COIN_SWAP_DEPOSIT",
"COIN_SWAP_WITHDRAW",
"POSITION_LIMIT_INCREASE_FEE",
}
startTime := time.Now().Add(-7 * 24 * time.Hour)
t.Logf("🔍 Checking all income types from %s:", startTime.Format(time.RFC3339))
for _, incomeType := range incomeTypes {
incomes, err := trader.client.NewGetIncomeHistoryService().
IncomeType(incomeType).
StartTime(startTime.UnixMilli()).
Limit(100).
Do(context.Background())
if err != nil {
t.Logf(" ❌ %s: error - %v", incomeType, err)
continue
}
if len(incomes) > 0 {
symbolMap := make(map[string]int)
for _, inc := range incomes {
if inc.Symbol != "" {
symbolMap[inc.Symbol]++
}
}
t.Logf(" ✅ %s: %d records, symbols: %v", incomeType, len(incomes), symbolMap)
} else {
t.Logf(" ⚪ %s: 0 records", incomeType)
}
}
}
// TestBinanceGetTradesForSymbol tests trade retrieval for specific symbols
func TestBinanceGetTradesForSymbol(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
// Common trading pairs
symbols := []string{"BTCUSDT", "ETHUSDT", "SOLUSDT", "BNBUSDT", "XRPUSDT"}
startTime := time.Now().Add(-7 * 24 * time.Hour)
t.Logf("🔍 Checking trades for common symbols from %s:", startTime.Format(time.RFC3339))
for _, symbol := range symbols {
trades, err := trader.GetTradesForSymbol(symbol, startTime, 100)
if err != nil {
t.Logf(" ❌ %s: error - %v", symbol, err)
continue
}
if len(trades) > 0 {
t.Logf(" ✅ %s: %d trades", symbol, len(trades))
// Print first and last trade
first := trades[0]
last := trades[len(trades)-1]
t.Logf(" First: %s %s %s qty=%.6f price=%.4f pnl=%.4f time=%s",
first.TradeID, first.Symbol, first.Side,
first.Quantity, first.Price, first.RealizedPnL,
first.Time.Format(time.RFC3339))
if len(trades) > 1 {
t.Logf(" Last: %s %s %s qty=%.6f price=%.4f pnl=%.4f time=%s",
last.TradeID, last.Symbol, last.Side,
last.Quantity, last.Price, last.RealizedPnL,
last.Time.Format(time.RFC3339))
}
} else {
t.Logf(" ⚪ %s: 0 trades", symbol)
}
}
}
// TestBinanceTimestampFormats tests different timestamp formats
func TestBinanceTimestampFormats(t *testing.T) {
skipIfNoLiveTest(t)
now := time.Now()
nowUTC := time.Now().UTC()
t.Logf("🕐 Time comparison:")
t.Logf(" time.Now(): %s (UnixMilli: %d)", now.Format(time.RFC3339), now.UnixMilli())
t.Logf(" time.Now().UTC(): %s (UnixMilli: %d)", nowUTC.Format(time.RFC3339), nowUTC.UnixMilli())
t.Logf(" Difference: %v", now.Sub(nowUTC))
// The key insight: UnixMilli() should be the SAME regardless of timezone
if now.UnixMilli() != nowUTC.UnixMilli() {
t.Errorf("❌ UnixMilli() differs between local and UTC! This should never happen.")
} else {
t.Logf(" ✅ UnixMilli() is the same (correct behavior)")
}
// Test what happens when we parse a time stored in DB
// Simulate old DB value stored in local time
oldLocalTime := time.Date(2026, 1, 6, 18, 0, 0, 0, time.Local) // 18:00 local
oldLocalTimeAsUTC := time.Date(2026, 1, 6, 18, 0, 0, 0, time.UTC) // Same numbers but UTC
t.Logf("\n🔍 Timezone mismatch scenario:")
t.Logf(" Old DB time (local): %s (UnixMilli: %d)", oldLocalTime.Format(time.RFC3339), oldLocalTime.UnixMilli())
t.Logf(" Same time parsed as UTC: %s (UnixMilli: %d)", oldLocalTimeAsUTC.Format(time.RFC3339), oldLocalTimeAsUTC.UnixMilli())
t.Logf(" Difference: %v", time.Duration(oldLocalTimeAsUTC.UnixMilli()-oldLocalTime.UnixMilli())*time.Millisecond)
// If server is in +8 timezone, the difference should be 8 hours
_, offset := now.Zone()
t.Logf(" Local timezone offset: %d seconds (%d hours)", offset, offset/3600)
}
// TestBinanceFullSyncSimulation simulates the full sync process
func TestBinanceFullSyncSimulation(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
t.Logf("🔄 Simulating full sync process...")
// Step 1: Determine lastSyncTime (simulating first run)
lastSyncTime := time.Now().UTC().Add(-7 * 24 * time.Hour)
t.Logf("\n📅 Step 1: lastSyncTime = %s", lastSyncTime.Format(time.RFC3339))
// Step 2: Detect symbols using all methods
symbolMap := make(map[string]bool)
// Method 1: COMMISSION
commissionSymbols, err := trader.GetCommissionSymbols(lastSyncTime)
if err != nil {
t.Logf(" ⚠️ COMMISSION failed: %v", err)
} else {
t.Logf(" 📋 COMMISSION symbols: %d - %v", len(commissionSymbols), commissionSymbols)
for _, s := range commissionSymbols {
symbolMap[s] = true
}
}
// Method 2: Positions
positions, err := trader.GetPositions()
if err != nil {
t.Logf(" ⚠️ GetPositions failed: %v", err)
} else {
var posSymbols []string
for _, pos := range positions {
if symbol, ok := pos["symbol"].(string); ok && symbol != "" {
posSymbols = append(posSymbols, symbol)
symbolMap[symbol] = true
}
}
t.Logf(" 📋 Position symbols: %d - %v", len(posSymbols), posSymbols)
}
// Method 3: REALIZED_PNL (fallback)
pnlSymbols, err := trader.GetPnLSymbols(lastSyncTime)
if err != nil {
t.Logf(" ⚠️ REALIZED_PNL failed: %v", err)
} else {
t.Logf(" 📋 REALIZED_PNL symbols: %d - %v", len(pnlSymbols), pnlSymbols)
for _, s := range pnlSymbols {
symbolMap[s] = true
}
}
// Collect all symbols
var allSymbols []string
for s := range symbolMap {
allSymbols = append(allSymbols, s)
}
t.Logf("\n📊 Step 2: Total unique symbols to sync: %d - %v", len(allSymbols), allSymbols)
if len(allSymbols) == 0 {
t.Logf("❌ No symbols found! This is the bug - nothing to sync")
t.Logf("\n🔍 Investigating why no symbols found...")
// Try to query all income (without type filter) to see if there's ANY activity
incomes, err := trader.client.NewGetIncomeHistoryService().
StartTime(lastSyncTime.UnixMilli()).
Limit(100).
Do(context.Background())
if err != nil {
t.Logf(" Failed to get all income: %v", err)
} else {
t.Logf(" All income records (no type filter): %d", len(incomes))
typeCount := make(map[string]int)
for _, inc := range incomes {
typeCount[inc.IncomeType]++
}
t.Logf(" Income types breakdown: %v", typeCount)
}
return
}
// Step 3: Query trades for each symbol
t.Logf("\n📥 Step 3: Querying trades for each symbol...")
totalTrades := 0
for _, symbol := range allSymbols {
trades, err := trader.GetTradesForSymbol(symbol, lastSyncTime, 500)
if err != nil {
t.Logf(" ❌ %s: error - %v", symbol, err)
continue
}
totalTrades += len(trades)
t.Logf(" ✅ %s: %d trades", symbol, len(trades))
// Print sample trades
for i, trade := range trades {
if i >= 3 {
t.Logf(" ... and %d more trades", len(trades)-3)
break
}
t.Logf(" [%d] %s %s %s qty=%.6f price=%.4f pnl=%.4f fee=%.6f time=%s",
i+1, trade.TradeID, trade.Symbol, trade.Side,
trade.Quantity, trade.Price, trade.RealizedPnL, trade.Fee,
trade.Time.Format(time.RFC3339))
}
}
t.Logf("\n✅ Sync simulation complete: %d total trades found across %d symbols",
totalTrades, len(allSymbols))
}
// TestBinanceTradeIDRange tests trade ID ranges to understand the data
func TestBinanceTradeIDRange(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
// First find symbols with trades
startTime := time.Now().Add(-30 * 24 * time.Hour)
commissionSymbols, _ := trader.GetCommissionSymbols(startTime)
pnlSymbols, _ := trader.GetPnLSymbols(startTime)
symbolMap := make(map[string]bool)
for _, s := range commissionSymbols {
symbolMap[s] = true
}
for _, s := range pnlSymbols {
symbolMap[s] = true
}
if len(symbolMap) == 0 {
t.Log("No symbols with activity found")
return
}
t.Logf("🔍 Checking trade ID ranges for symbols with activity:")
for symbol := range symbolMap {
trades, err := trader.GetTradesForSymbol(symbol, startTime, 100)
if err != nil || len(trades) == 0 {
continue
}
var minID, maxID int64 = 1<<62, 0
for _, trade := range trades {
var id int64
fmt.Sscanf(trade.TradeID, "%d", &id)
if id < minID {
minID = id
}
if id > maxID {
maxID = id
}
}
t.Logf(" %s: %d trades, ID range [%d - %d]", symbol, len(trades), minID, maxID)
// Check if any ID exceeds PostgreSQL INTEGER max
if maxID > 2147483647 {
t.Logf(" ⚠️ Max trade ID %d exceeds PostgreSQL INTEGER max (2147483647)", maxID)
}
}
}
// TestBinanceIncomeAPIDirectCall makes direct API call to understand response
func TestBinanceIncomeAPIDirectCall(t *testing.T) {
skipIfNoLiveTest(t)
trader := createBinanceTestTrader(t)
startTime := time.Now().Add(-24 * time.Hour)
t.Logf("🔍 Direct income API call from %s:", startTime.Format(time.RFC3339))
t.Logf(" StartTime UnixMilli: %d", startTime.UnixMilli())
// Call without income type filter to get ALL income
incomes, err := trader.client.NewGetIncomeHistoryService().
StartTime(startTime.UnixMilli()).
Limit(1000).
Do(context.Background())
if err != nil {
t.Fatalf("Failed to get income: %v", err)
}
t.Logf("📋 Total income records: %d", len(incomes))
// Group by type and symbol
typeSymbolCount := make(map[string]map[string]int)
for _, inc := range incomes {
if typeSymbolCount[inc.IncomeType] == nil {
typeSymbolCount[inc.IncomeType] = make(map[string]int)
}
typeSymbolCount[inc.IncomeType][inc.Symbol]++
}
for incType, symbols := range typeSymbolCount {
t.Logf(" %s:", incType)
for symbol, count := range symbols {
if symbol == "" {
symbol = "(no symbol)"
}
t.Logf(" %s: %d records", symbol, count)
}
}
// Print sample records
if len(incomes) > 0 {
t.Logf("\n📝 Sample income records (first 5):")
for i, inc := range incomes {
if i >= 5 {
break
}
t.Logf(" [%d] Type=%s Symbol=%s Amount=%s Time=%s",
i+1, inc.IncomeType, inc.Symbol, inc.Income,
time.UnixMilli(inc.Time).Format(time.RFC3339))
}
}
}

View File

@@ -0,0 +1,216 @@
package trader
import (
"nofx/store"
"os"
"testing"
"time"
)
// TestBinanceSyncE2E tests the complete sync flow end-to-end
func TestBinanceSyncE2E(t *testing.T) {
skipIfNoLiveTest(t)
// Get credentials from environment
apiKey, secretKey := getBinanceTestCredentials(t)
// Create test database using full store initialization (includes table creation)
testDBPath := "/tmp/test_binance_sync.db"
os.Remove(testDBPath) // Clean up previous test
st, err := store.New(testDBPath)
if err != nil {
t.Fatalf("Failed to init test store: %v", err)
}
db := st.GormDB()
// Create trader
trader := NewFuturesTrader(apiKey, secretKey, "test-user")
// Test parameters
traderID := "test-trader-id"
exchangeID := "test-exchange-id"
exchangeType := "binance"
t.Logf("🧪 Running end-to-end sync test...")
t.Logf(" DB Path: %s", testDBPath)
// Run sync
t.Logf("\n📥 Running SyncOrdersFromBinance...")
startTime := time.Now()
err = trader.SyncOrdersFromBinance(traderID, exchangeID, exchangeType, st)
elapsed := time.Since(startTime)
if err != nil {
t.Fatalf("❌ Sync failed: %v", err)
}
t.Logf("✅ Sync completed in %v", elapsed)
// Check results in database
orderStore := st.Order()
// Count orders
var orderCount int64
db.Model(&store.TraderOrder{}).Where("exchange_id = ?", exchangeID).Count(&orderCount)
t.Logf("\n📊 Results:")
t.Logf(" Orders in DB: %d", orderCount)
// Count fills
var fillCount int64
db.Model(&store.TraderFill{}).Where("exchange_id = ?", exchangeID).Count(&fillCount)
t.Logf(" Fills in DB: %d", fillCount)
// Get symbols
var symbols []string
db.Model(&store.TraderFill{}).
Select("DISTINCT symbol").
Where("exchange_id = ?", exchangeID).
Pluck("symbol", &symbols)
t.Logf(" Unique symbols: %d - %v", len(symbols), symbols)
// Check max trade IDs (test the fix)
maxTradeIDs, err := orderStore.GetMaxTradeIDsByExchange(exchangeID)
if err != nil {
t.Logf(" ⚠️ GetMaxTradeIDsByExchange error: %v", err)
} else {
t.Logf(" Max trade IDs per symbol:")
for symbol, maxID := range maxTradeIDs {
if maxID > 2147483647 {
t.Logf(" %s: %d (⚠️ exceeds PostgreSQL INTEGER max)", symbol, maxID)
} else {
t.Logf(" %s: %d", symbol, maxID)
}
}
}
// Sample some orders
var sampleOrders []store.TraderOrder
db.Where("exchange_id = ?", exchangeID).Limit(5).Find(&sampleOrders)
if len(sampleOrders) > 0 {
t.Logf("\n📝 Sample orders:")
for i, order := range sampleOrders {
t.Logf(" [%d] %s %s %s qty=%.6f price=%.4f action=%s time=%s",
i+1, order.ExchangeOrderID, order.Symbol, order.Side,
order.Quantity, order.Price, order.OrderAction,
order.FilledAt.Format(time.RFC3339))
}
}
// Test incremental sync - run again, should find no new trades
t.Logf("\n🔄 Running incremental sync (should skip existing trades)...")
startTime = time.Now()
err = trader.SyncOrdersFromBinance(traderID, exchangeID, exchangeType, st)
elapsed = time.Since(startTime)
if err != nil {
t.Fatalf("❌ Incremental sync failed: %v", err)
}
t.Logf("✅ Incremental sync completed in %v", elapsed)
// Check counts again - should be the same
var newOrderCount int64
db.Model(&store.TraderOrder{}).Where("exchange_id = ?", exchangeID).Count(&newOrderCount)
t.Logf(" Orders after incremental sync: %d (was %d)", newOrderCount, orderCount)
if newOrderCount != orderCount {
t.Logf(" ⚠️ Order count changed - possible duplicate detection issue")
} else {
t.Logf(" ✅ No duplicates - incremental sync working correctly")
}
// Test GetLastFillTimeByExchange
lastFillTime, err := orderStore.GetLastFillTimeByExchange(exchangeID)
if err != nil {
t.Logf(" ⚠️ GetLastFillTimeByExchange error: %v", err)
} else {
t.Logf("\n📅 Last fill time from DB: %s", lastFillTime.Format(time.RFC3339))
// Check if it would be in the future (the bug we fixed)
now := time.Now().UTC()
if lastFillTime.After(now) {
t.Logf(" ❌ BUG: Last fill time is in the future! (now: %s)", now.Format(time.RFC3339))
} else {
t.Logf(" ✅ Last fill time is in the past (correct)")
}
}
// Cleanup
os.Remove(testDBPath)
t.Logf("\n✅ E2E test completed successfully!")
}
// TestBinanceSyncWithExistingData tests sync behavior with pre-existing data
func TestBinanceSyncWithExistingData(t *testing.T) {
skipIfNoLiveTest(t)
// Get credentials from environment
apiKey, secretKey := getBinanceTestCredentials(t)
testDBPath := "/tmp/test_binance_sync_existing.db"
os.Remove(testDBPath)
st, err := store.New(testDBPath)
if err != nil {
t.Fatalf("Failed to init test store: %v", err)
}
db := st.GormDB()
orderStore := st.Order()
trader := NewFuturesTrader(apiKey, secretKey, "test-user")
traderID := "test-trader-id"
exchangeID := "test-exchange-id"
exchangeType := "binance"
// Insert a fake "old" fill with LOCAL time (simulating the bug scenario)
// This tests that our timezone fix works
localTime := time.Now().Add(8 * time.Hour) // Simulate +8 timezone stored as if it were UTC
fakeFill := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID,
ExchangeType: exchangeType,
ExchangeOrderID: "fake-old-order",
ExchangeTradeID: "fake-old-trade",
Symbol: "BTCUSDT",
Side: "BUY",
Price: 50000,
Quantity: 0.001,
QuoteQuantity: 50,
CreatedAt: localTime, // This time is "in the future" if interpreted as UTC
}
if err := orderStore.CreateFill(fakeFill); err != nil {
t.Fatalf("Failed to create fake fill: %v", err)
}
t.Logf("🧪 Testing sync with existing 'future' data...")
t.Logf(" Fake fill time: %s", localTime.Format(time.RFC3339))
t.Logf(" Current UTC time: %s", time.Now().UTC().Format(time.RFC3339))
// Check GetLastFillTimeByExchange
lastFillTime, _ := orderStore.GetLastFillTimeByExchange(exchangeID)
t.Logf(" GetLastFillTimeByExchange returned: %s", lastFillTime.Format(time.RFC3339))
if lastFillTime.After(time.Now().UTC()) {
t.Logf(" ⚠️ Last fill time is in the future - this is the bug scenario!")
}
// Run sync - it should detect the future time and fall back
t.Logf("\n📥 Running sync (should detect future time and fall back)...")
err = trader.SyncOrdersFromBinance(traderID, exchangeID, exchangeType, st)
if err != nil {
t.Fatalf("❌ Sync failed: %v", err)
}
t.Logf("✅ Sync completed")
// Check that trades were actually synced despite the bad data
var fillCount int64
db.Model(&store.TraderFill{}).Where("exchange_id = ?", exchangeID).Count(&fillCount)
t.Logf(" Total fills in DB: %d (includes 1 fake)", fillCount)
if fillCount > 1 {
t.Logf(" ✅ Real trades were synced despite 'future' data!")
} else {
t.Logf(" ❌ No real trades synced - the bug might still exist")
}
os.Remove(testDBPath)
}

View File

@@ -0,0 +1,511 @@
package trader
import (
"context"
"math"
"nofx/store"
"os"
"sort"
"strings"
"testing"
"time"
)
func repeatStr(s string, n int) string {
return strings.Repeat(s, n)
}
// TestBinanceSyncVerification verifies synced data matches exchange data exactly
func TestBinanceSyncVerification(t *testing.T) {
skipIfNoLiveTest(t)
// Get credentials from environment
apiKey, secretKey := getBinanceTestCredentials(t)
// Create test database
testDBPath := "/tmp/test_binance_verify.db"
os.Remove(testDBPath)
st, err := store.New(testDBPath)
if err != nil {
t.Fatalf("Failed to init test store: %v", err)
}
db := st.GormDB()
trader := NewFuturesTrader(apiKey, secretKey, "test-user")
traderID := "test-trader-id"
exchangeID := "test-exchange-id"
exchangeType := "binance"
// Step 1: Run sync
t.Logf("%s", repeatStr("=", 60))
t.Logf("STEP 1: Running order sync...")
t.Logf("%s", repeatStr("=", 60))
err = trader.SyncOrdersFromBinance(traderID, exchangeID, exchangeType, st)
if err != nil {
t.Fatalf("Sync failed: %v", err)
}
// Step 2: Get all trades from exchange for verification
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("STEP 2: Fetching trades from exchange for verification...")
t.Logf("%s", repeatStr("=", 60))
startTime := time.Now().UTC().Add(-7 * 24 * time.Hour)
// Get symbols from DB
var symbols []string
db.Model(&store.TraderFill{}).
Select("DISTINCT symbol").
Where("exchange_id = ?", exchangeID).
Pluck("symbol", &symbols)
t.Logf("Symbols to verify: %v", symbols)
// Fetch all trades from exchange
type ExchangeTrade struct {
TradeID string
Symbol string
Side string
Price float64
Quantity float64
Fee float64
RealizedPnL float64
Time time.Time
}
var exchangeTrades []ExchangeTrade
for _, symbol := range symbols {
trades, err := trader.GetTradesForSymbol(symbol, startTime, 1000)
if err != nil {
t.Logf("⚠️ Failed to get trades for %s: %v", symbol, err)
continue
}
for _, trade := range trades {
exchangeTrades = append(exchangeTrades, ExchangeTrade{
TradeID: trade.TradeID,
Symbol: trade.Symbol,
Side: trade.Side,
Price: trade.Price,
Quantity: trade.Quantity,
Fee: trade.Fee,
RealizedPnL: trade.RealizedPnL,
Time: trade.Time,
})
}
}
t.Logf("Total trades from exchange: %d", len(exchangeTrades))
// Step 3: Get all fills from DB
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("STEP 3: Comparing with local database...")
t.Logf("%s", repeatStr("=", 60))
var dbFills []store.TraderFill
db.Where("exchange_id = ?", exchangeID).Find(&dbFills)
t.Logf("Total fills in DB: %d", len(dbFills))
// Create maps for comparison
exchangeTradeMap := make(map[string]ExchangeTrade)
for _, t := range exchangeTrades {
exchangeTradeMap[t.TradeID] = t
}
dbFillMap := make(map[string]store.TraderFill)
for _, f := range dbFills {
dbFillMap[f.ExchangeTradeID] = f
}
// Step 4: Check for missing trades
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("STEP 4: Checking for MISSING trades (in exchange but not in DB)...")
t.Logf("%s", repeatStr("=", 60))
var missingTrades []ExchangeTrade
for tradeID, trade := range exchangeTradeMap {
if _, exists := dbFillMap[tradeID]; !exists {
missingTrades = append(missingTrades, trade)
}
}
if len(missingTrades) > 0 {
t.Logf("❌ MISSING %d trades:", len(missingTrades))
for i, trade := range missingTrades {
if i >= 10 {
t.Logf(" ... and %d more", len(missingTrades)-10)
break
}
t.Logf(" - %s %s %s qty=%.6f price=%.4f time=%s",
trade.TradeID, trade.Symbol, trade.Side,
trade.Quantity, trade.Price, trade.Time.Format(time.RFC3339))
}
} else {
t.Logf("✅ No missing trades")
}
// Step 5: Check for extra/duplicate trades
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("STEP 5: Checking for EXTRA trades (in DB but not in exchange)...")
t.Logf("%s", repeatStr("=", 60))
var extraTrades []store.TraderFill
for tradeID, fill := range dbFillMap {
if _, exists := exchangeTradeMap[tradeID]; !exists {
extraTrades = append(extraTrades, fill)
}
}
if len(extraTrades) > 0 {
t.Logf("❌ EXTRA %d trades in DB:", len(extraTrades))
for i, fill := range extraTrades {
if i >= 10 {
t.Logf(" ... and %d more", len(extraTrades)-10)
break
}
t.Logf(" - %s %s %s qty=%.6f price=%.4f",
fill.ExchangeTradeID, fill.Symbol, fill.Side,
fill.Quantity, fill.Price)
}
} else {
t.Logf("✅ No extra/duplicate trades")
}
// Step 6: Check for data accuracy
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("STEP 6: Verifying data accuracy (price, qty, fee, pnl)...")
t.Logf("%s", repeatStr("=", 60))
type DataMismatch struct {
TradeID string
Field string
DB float64
Exchange float64
}
var mismatches []DataMismatch
for tradeID, exchangeTrade := range exchangeTradeMap {
dbFill, exists := dbFillMap[tradeID]
if !exists {
continue
}
// Compare price
if !floatEqual(dbFill.Price, exchangeTrade.Price, 0.0001) {
mismatches = append(mismatches, DataMismatch{
TradeID: tradeID, Field: "Price",
DB: dbFill.Price, Exchange: exchangeTrade.Price,
})
}
// Compare quantity
if !floatEqual(dbFill.Quantity, exchangeTrade.Quantity, 0.000001) {
mismatches = append(mismatches, DataMismatch{
TradeID: tradeID, Field: "Quantity",
DB: dbFill.Quantity, Exchange: exchangeTrade.Quantity,
})
}
// Compare fee
if !floatEqual(dbFill.Commission, exchangeTrade.Fee, 0.000001) {
mismatches = append(mismatches, DataMismatch{
TradeID: tradeID, Field: "Fee",
DB: dbFill.Commission, Exchange: exchangeTrade.Fee,
})
}
// Compare realized PnL
if !floatEqual(dbFill.RealizedPnL, exchangeTrade.RealizedPnL, 0.01) {
mismatches = append(mismatches, DataMismatch{
TradeID: tradeID, Field: "RealizedPnL",
DB: dbFill.RealizedPnL, Exchange: exchangeTrade.RealizedPnL,
})
}
}
if len(mismatches) > 0 {
t.Logf("❌ DATA MISMATCHES: %d", len(mismatches))
for i, m := range mismatches {
if i >= 20 {
t.Logf(" ... and %d more", len(mismatches)-20)
break
}
t.Logf(" - %s %s: DB=%.6f, Exchange=%.6f",
m.TradeID, m.Field, m.DB, m.Exchange)
}
} else {
t.Logf("✅ All data matches exactly")
}
// Step 7: Summary by symbol
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("STEP 7: Summary by symbol...")
t.Logf("%s", repeatStr("=", 60))
type SymbolSummary struct {
Symbol string
ExchangeCount int
DBCount int
TotalQty float64
TotalFee float64
TotalPnL float64
ExchangeTotalQty float64
ExchangeTotalFee float64
ExchangeTotalPnL float64
}
summaryMap := make(map[string]*SymbolSummary)
for _, trade := range exchangeTrades {
if summaryMap[trade.Symbol] == nil {
summaryMap[trade.Symbol] = &SymbolSummary{Symbol: trade.Symbol}
}
s := summaryMap[trade.Symbol]
s.ExchangeCount++
s.ExchangeTotalQty += trade.Quantity
s.ExchangeTotalFee += trade.Fee
s.ExchangeTotalPnL += trade.RealizedPnL
}
for _, fill := range dbFills {
if summaryMap[fill.Symbol] == nil {
summaryMap[fill.Symbol] = &SymbolSummary{Symbol: fill.Symbol}
}
s := summaryMap[fill.Symbol]
s.DBCount++
s.TotalQty += fill.Quantity
s.TotalFee += fill.Commission
s.TotalPnL += fill.RealizedPnL
}
t.Logf("\n%-15s %10s %10s %15s %15s %15s", "Symbol", "Exchange", "DB", "Fee(Exc/DB)", "PnL(Exc/DB)", "Match")
t.Logf("%s", repeatStr("-", 80))
for _, s := range summaryMap {
countMatch := s.ExchangeCount == s.DBCount
feeMatch := floatEqual(s.ExchangeTotalFee, s.TotalFee, 0.01)
pnlMatch := floatEqual(s.ExchangeTotalPnL, s.TotalPnL, 0.01)
matchStr := "✅"
if !countMatch || !feeMatch || !pnlMatch {
matchStr = "❌"
}
t.Logf("%-15s %10d %10d %7.2f/%-7.2f %7.2f/%-7.2f %s",
s.Symbol, s.ExchangeCount, s.DBCount,
s.ExchangeTotalFee, s.TotalFee,
s.ExchangeTotalPnL, s.TotalPnL,
matchStr)
}
// Step 8: Position verification
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("STEP 8: Verifying position calculations...")
t.Logf("%s", repeatStr("=", 60))
// Get positions from DB
var dbPositions []store.TraderPosition
db.Where("exchange_id = ? AND status = ?", exchangeID, "closed").Find(&dbPositions)
t.Logf("Closed positions in DB: %d", len(dbPositions))
// Get current positions from exchange
exchangePositions, err := trader.GetPositions()
if err != nil {
t.Logf("⚠️ Failed to get exchange positions: %v", err)
} else {
t.Logf("Active positions on exchange: %d", len(exchangePositions))
for _, pos := range exchangePositions {
t.Logf(" - %s %s qty=%.6f entry=%.4f pnl=%.4f",
pos["symbol"], pos["side"],
pos["positionAmt"], pos["entryPrice"], pos["unRealizedProfit"])
}
}
// Calculate total PnL from trades
var totalRealizedPnL float64
var totalFees float64
for _, fill := range dbFills {
totalRealizedPnL += fill.RealizedPnL
totalFees += fill.Commission
}
t.Logf("\n📊 PnL Summary from DB:")
t.Logf(" Total Realized PnL: %.4f USDT", totalRealizedPnL)
t.Logf(" Total Fees: %.4f USDT", totalFees)
t.Logf(" Net PnL: %.4f USDT", totalRealizedPnL-totalFees)
// Calculate from exchange
var exchangeTotalPnL float64
var exchangeTotalFees float64
for _, trade := range exchangeTrades {
exchangeTotalPnL += trade.RealizedPnL
exchangeTotalFees += trade.Fee
}
t.Logf("\n📊 PnL Summary from Exchange:")
t.Logf(" Total Realized PnL: %.4f USDT", exchangeTotalPnL)
t.Logf(" Total Fees: %.4f USDT", exchangeTotalFees)
t.Logf(" Net PnL: %.4f USDT", exchangeTotalPnL-exchangeTotalFees)
// Compare
pnlMatch := floatEqual(totalRealizedPnL, exchangeTotalPnL, 0.01)
feeMatch := floatEqual(totalFees, exchangeTotalFees, 0.01)
t.Logf("\n%s", repeatStr("=", 60))
t.Logf("FINAL VERIFICATION RESULT")
t.Logf("%s", repeatStr("=", 60))
allPassed := true
if len(missingTrades) > 0 {
t.Logf("❌ Missing trades: %d", len(missingTrades))
allPassed = false
} else {
t.Logf("✅ No missing trades")
}
if len(extraTrades) > 0 {
t.Logf("❌ Extra/duplicate trades: %d", len(extraTrades))
allPassed = false
} else {
t.Logf("✅ No extra/duplicate trades")
}
if len(mismatches) > 0 {
t.Logf("❌ Data mismatches: %d", len(mismatches))
allPassed = false
} else {
t.Logf("✅ All data accurate")
}
if !pnlMatch {
t.Logf("❌ PnL mismatch: DB=%.4f, Exchange=%.4f", totalRealizedPnL, exchangeTotalPnL)
allPassed = false
} else {
t.Logf("✅ PnL matches")
}
if !feeMatch {
t.Logf("❌ Fee mismatch: DB=%.4f, Exchange=%.4f", totalFees, exchangeTotalFees)
allPassed = false
} else {
t.Logf("✅ Fees match")
}
if allPassed {
t.Logf("\n🎉 ALL VERIFICATIONS PASSED!")
} else {
t.Logf("\n⚠ SOME VERIFICATIONS FAILED - CHECK ABOVE FOR DETAILS")
}
// Cleanup
os.Remove(testDBPath)
}
// floatEqual compares two floats with tolerance
func floatEqual(a, b, tolerance float64) bool {
return math.Abs(a-b) <= tolerance
}
// TestBinanceDetailedTradeComparison shows detailed trade-by-trade comparison
func TestBinanceDetailedTradeComparison(t *testing.T) {
skipIfNoLiveTest(t)
// Get credentials from environment
apiKey, secretKey := getBinanceTestCredentials(t)
trader := NewFuturesTrader(apiKey, secretKey, "test-user")
startTime := time.Now().UTC().Add(-24 * time.Hour)
// Get all income (to find symbols with activity)
incomes, err := trader.client.NewGetIncomeHistoryService().
StartTime(startTime.UnixMilli()).
Limit(100).
Do(context.Background())
if err != nil {
t.Fatalf("Failed to get income: %v", err)
}
// Find unique symbols
symbolMap := make(map[string]bool)
for _, inc := range incomes {
if inc.Symbol != "" {
symbolMap[inc.Symbol] = true
}
}
if len(symbolMap) == 0 {
t.Log("No trading activity in the last 24 hours")
return
}
t.Logf("=%s", repeatStr("=", 100))
t.Logf("DETAILED TRADE REPORT (Last 24 hours)")
t.Logf("=%s", repeatStr("=", 100))
var grandTotalQty float64
var grandTotalFee float64
var grandTotalPnL float64
for symbol := range symbolMap {
trades, err := trader.GetTradesForSymbol(symbol, startTime, 500)
if err != nil {
t.Logf("⚠️ Failed to get trades for %s: %v", symbol, err)
continue
}
if len(trades) == 0 {
continue
}
// Sort by time
sort.Slice(trades, func(i, j int) bool {
return trades[i].Time.Before(trades[j].Time)
})
t.Logf("\n%s", repeatStr("-", 100))
t.Logf("📊 %s - %d trades", symbol, len(trades))
t.Logf("%s", repeatStr("-", 100))
t.Logf("%-15s %-6s %12s %12s %12s %12s %20s",
"TradeID", "Side", "Quantity", "Price", "Fee", "PnL", "Time")
var totalQty, totalFee, totalPnL float64
var buyQty, sellQty float64
for _, trade := range trades {
t.Logf("%-15s %-6s %12.6f %12.4f %12.6f %12.4f %20s",
trade.TradeID, trade.Side,
trade.Quantity, trade.Price, trade.Fee, trade.RealizedPnL,
trade.Time.Format("2006-01-02 15:04:05"))
totalQty += trade.Quantity
totalFee += trade.Fee
totalPnL += trade.RealizedPnL
if trade.Side == "BUY" {
buyQty += trade.Quantity
} else {
sellQty += trade.Quantity
}
}
t.Logf("%s", repeatStr("-", 100))
t.Logf("SUBTOTAL: %d trades, Buy=%.6f, Sell=%.6f, Fee=%.6f, PnL=%.4f",
len(trades), buyQty, sellQty, totalFee, totalPnL)
grandTotalQty += totalQty
grandTotalFee += totalFee
grandTotalPnL += totalPnL
}
t.Logf("\n%s", repeatStr("=", 100))
t.Logf("GRAND TOTAL")
t.Logf("=%s", repeatStr("=", 100))
t.Logf("Total Fee: %.6f USDT", grandTotalFee)
t.Logf("Total PnL: %.4f USDT", grandTotalPnL)
t.Logf("Net PnL: %.4f USDT", grandTotalPnL-grandTotalFee)
}

View File

@@ -110,7 +110,7 @@ func (t *BitgetTrader) GetTrades(startTime time.Time, limit int) ([]BitgetTrade,
FillQty: fillQty,
Fee: -fee, // Bitget returns negative fee
FeeAsset: fill.FeeCcy,
ExecTime: time.UnixMilli(cTime),
ExecTime: time.UnixMilli(cTime).UTC(),
ProfitLoss: profit,
OrderType: "MARKET",
OrderAction: orderAction,
@@ -146,7 +146,7 @@ func (t *BitgetTrader) SyncOrdersFromBitget(traderID string, exchangeID string,
// Sort trades by time ASC (oldest first) for proper position building
sort.Slice(trades, func(i, j int) bool {
return trades[i].ExecTime.Before(trades[j].ExecTime)
return trades[i].ExecTime.UnixMilli() < trades[j].ExecTime.UnixMilli()
})
// Process trades one by one (no transaction to avoid deadlock)
@@ -174,7 +174,8 @@ func (t *BitgetTrader) SyncOrdersFromBitget(traderID string, exchangeID string,
// Normalize side for storage
side := strings.ToUpper(trade.Side)
// Create order record
// Create order record - use UTC time in milliseconds to avoid timezone issues
execTimeMs := trade.ExecTime.UTC().UnixMilli()
orderRecord := &store.TraderOrder{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -191,9 +192,9 @@ func (t *BitgetTrader) SyncOrdersFromBitget(traderID string, exchangeID string,
FilledQuantity: trade.FillQty,
AvgFillPrice: trade.FillPrice,
Commission: trade.Fee,
FilledAt: trade.ExecTime,
CreatedAt: trade.ExecTime,
UpdatedAt: trade.ExecTime,
FilledAt: execTimeMs,
CreatedAt: execTimeMs,
UpdatedAt: execTimeMs,
}
// Insert order record
@@ -202,7 +203,7 @@ func (t *BitgetTrader) SyncOrdersFromBitget(traderID string, exchangeID string,
continue
}
// Create fill record
// Create fill record - use UTC time in milliseconds
fillRecord := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -219,7 +220,7 @@ func (t *BitgetTrader) SyncOrdersFromBitget(traderID string, exchangeID string,
CommissionAsset: trade.FeeAsset,
RealizedPnL: trade.ProfitLoss,
IsMaker: false,
CreatedAt: trade.ExecTime,
CreatedAt: execTimeMs,
}
if err := orderStore.CreateFill(fillRecord); err != nil {
@@ -231,7 +232,7 @@ func (t *BitgetTrader) SyncOrdersFromBitget(traderID string, exchangeID string,
traderID, exchangeID, exchangeType,
symbol, positionSide, trade.OrderAction,
trade.FillQty, trade.FillPrice, trade.Fee, trade.ProfitLoss,
trade.ExecTime, trade.TradeID,
execTimeMs, trade.TradeID,
); err != nil {
logger.Infof(" ⚠️ Failed to sync position for trade %s: %v", trade.TradeID, err)
} else {

View File

@@ -1069,8 +1069,8 @@ func (t *BitgetTrader) GetClosedPnL(startTime time.Time, limit int) ([]ClosedPnL
cTime, _ := strconv.ParseInt(pos.CTime, 10, 64)
uTime, _ := strconv.ParseInt(pos.UTime, 10, 64)
record.EntryTime = time.UnixMilli(cTime)
record.ExitTime = time.UnixMilli(uTime)
record.EntryTime = time.UnixMilli(cTime).UTC()
record.ExitTime = time.UnixMilli(uTime).UTC()
record.CloseType = "unknown"
records = append(records, record)
@@ -1096,3 +1096,9 @@ func genBitgetClientOid() string {
rand := time.Now().Nanosecond() % 100000
return fmt.Sprintf("nofx%d%05d", timestamp, rand)
}
// GetOpenOrders gets all open/pending orders for a symbol
func (t *BitgetTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) {
// TODO: Implement Bitget open orders
return []OpenOrder{}, nil
}

View File

@@ -127,7 +127,7 @@ func (t *BybitTrader) parseTradesResult(list []map[string]interface{}) ([]BybitT
closedSize, _ := strconv.ParseFloat(closedSizeStr, 64)
closedPnl, _ := strconv.ParseFloat(closedPnlStr, 64)
execTimeMs, _ := strconv.ParseInt(execTimeStr, 10, 64)
execTime := time.UnixMilli(execTimeMs)
execTime := time.UnixMilli(execTimeMs).UTC()
// Determine order action based on side and closedSize
// If closedSize > 0, it's a close trade
@@ -195,7 +195,7 @@ func (t *BybitTrader) SyncOrdersFromBybit(traderID string, exchangeID string, ex
// Sort trades by time ASC (oldest first) for proper position building
sort.Slice(trades, func(i, j int) bool {
return trades[i].ExecTime.Before(trades[j].ExecTime)
return trades[i].ExecTime.UnixMilli() < trades[j].ExecTime.UnixMilli()
})
// Process trades one by one (no transaction to avoid deadlock)
@@ -223,7 +223,8 @@ func (t *BybitTrader) SyncOrdersFromBybit(traderID string, exchangeID string, ex
// Normalize side for storage
side := strings.ToUpper(trade.Side)
// Create order record
// Create order record - use UTC time in milliseconds to avoid timezone issues
execTimeMs := trade.ExecTime.UTC().UnixMilli()
orderRecord := &store.TraderOrder{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -240,9 +241,9 @@ func (t *BybitTrader) SyncOrdersFromBybit(traderID string, exchangeID string, ex
FilledQuantity: trade.ExecQty,
AvgFillPrice: trade.ExecPrice,
Commission: trade.ExecFee,
FilledAt: trade.ExecTime,
CreatedAt: trade.ExecTime,
UpdatedAt: trade.ExecTime,
FilledAt: execTimeMs,
CreatedAt: execTimeMs,
UpdatedAt: execTimeMs,
}
// Insert order record
@@ -251,7 +252,7 @@ func (t *BybitTrader) SyncOrdersFromBybit(traderID string, exchangeID string, ex
continue
}
// Create fill record
// Create fill record - use UTC time
fillRecord := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -268,7 +269,7 @@ func (t *BybitTrader) SyncOrdersFromBybit(traderID string, exchangeID string, ex
CommissionAsset: "USDT",
RealizedPnL: trade.ClosedPnL,
IsMaker: trade.IsMaker,
CreatedAt: trade.ExecTime,
CreatedAt: execTimeMs,
}
if err := orderStore.CreateFill(fillRecord); err != nil {
@@ -280,7 +281,7 @@ func (t *BybitTrader) SyncOrdersFromBybit(traderID string, exchangeID string, ex
traderID, exchangeID, exchangeType,
symbol, positionSide, trade.OrderAction,
trade.ExecQty, trade.ExecPrice, trade.ExecFee, trade.ClosedPnL,
trade.ExecTime, trade.ExecID,
execTimeMs, trade.ExecID,
); err != nil {
logger.Infof(" ⚠️ Failed to sync position for trade %s: %v", trade.ExecID, err)
} else {

View File

@@ -1032,8 +1032,8 @@ func (t *BybitTrader) parseClosedPnLResult(resultData interface{}) ([]ClosedPnLR
RealizedPnL: closedPnL,
Fee: fee,
Leverage: int(leverage),
EntryTime: time.UnixMilli(createdTime),
ExitTime: time.UnixMilli(updatedTime),
EntryTime: time.UnixMilli(createdTime).UTC(),
ExitTime: time.UnixMilli(updatedTime).UTC(),
OrderID: orderId,
CloseType: "unknown", // Bybit doesn't provide close type directly
ExchangeID: orderId, // Use orderId as exchange ID
@@ -1044,3 +1044,64 @@ func (t *BybitTrader) parseClosedPnLResult(resultData interface{}) ([]ClosedPnLR
return records, nil
}
// GetOpenOrders gets all open/pending orders for a symbol
func (t *BybitTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) {
var result []OpenOrder
// Get conditional orders (stop-loss, take-profit)
params := map[string]interface{}{
"category": "linear",
"symbol": symbol,
"orderFilter": "StopOrder",
}
resp, err := t.client.NewUtaBybitServiceWithParams(params).GetOpenOrders(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get open orders: %w", err)
}
if resp.RetCode == 0 {
resultData, ok := resp.Result.(map[string]interface{})
if ok {
list, _ := resultData["list"].([]interface{})
for _, item := range list {
order, ok := item.(map[string]interface{})
if !ok {
continue
}
orderId, _ := order["orderId"].(string)
sym, _ := order["symbol"].(string)
side, _ := order["side"].(string)
orderType, _ := order["orderType"].(string)
stopOrderType, _ := order["stopOrderType"].(string)
triggerPrice, _ := order["triggerPrice"].(string)
qty, _ := order["qty"].(string)
price, _ := strconv.ParseFloat(triggerPrice, 64)
quantity, _ := strconv.ParseFloat(qty, 64)
// Determine type based on stopOrderType
displayType := orderType
if stopOrderType != "" {
displayType = stopOrderType
}
result = append(result, OpenOrder{
OrderID: orderId,
Symbol: sym,
Side: side,
PositionSide: "", // Bybit doesn't use positionSide for UTA
Type: displayType,
Price: 0,
StopPrice: price,
Quantity: quantity,
Status: "NEW",
})
}
}
}
return result, nil
}

View File

@@ -34,7 +34,7 @@ func (t *HyperliquidTrader) SyncOrdersFromHyperliquid(traderID string, exchangeI
// Sort trades by time ASC (oldest first) for proper position building
sort.Slice(trades, func(i, j int) bool {
return trades[i].Time.Before(trades[j].Time)
return trades[i].Time.UnixMilli() < trades[j].Time.UnixMilli()
})
// Process trades one by one (no transaction to avoid deadlock)
@@ -61,7 +61,8 @@ func (t *HyperliquidTrader) SyncOrdersFromHyperliquid(traderID string, exchangeI
positionSide = "SHORT"
}
// Create order record
// Create order record - use Unix milliseconds UTC
tradeTimeMs := trade.Time.UTC().UnixMilli()
orderRecord := &store.TraderOrder{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -78,9 +79,9 @@ func (t *HyperliquidTrader) SyncOrdersFromHyperliquid(traderID string, exchangeI
FilledQuantity: trade.Quantity,
AvgFillPrice: trade.Price,
Commission: trade.Fee,
FilledAt: trade.Time,
CreatedAt: trade.Time,
UpdatedAt: trade.Time,
FilledAt: tradeTimeMs,
CreatedAt: tradeTimeMs,
UpdatedAt: tradeTimeMs,
}
// Insert order record
@@ -89,7 +90,7 @@ func (t *HyperliquidTrader) SyncOrdersFromHyperliquid(traderID string, exchangeI
continue
}
// Create fill record
// Create fill record - use Unix milliseconds UTC
fillRecord := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -106,7 +107,7 @@ func (t *HyperliquidTrader) SyncOrdersFromHyperliquid(traderID string, exchangeI
CommissionAsset: "USDT",
RealizedPnL: trade.RealizedPnL,
IsMaker: false, // Hyperliquid GetTrades doesn't provide maker/taker info
CreatedAt: trade.Time,
CreatedAt: tradeTimeMs,
}
if err := orderStore.CreateFill(fillRecord); err != nil {
@@ -118,7 +119,7 @@ func (t *HyperliquidTrader) SyncOrdersFromHyperliquid(traderID string, exchangeI
traderID, exchangeID, exchangeType,
symbol, positionSide, orderAction,
trade.Quantity, trade.Price, trade.Fee, trade.RealizedPnL,
trade.Time, trade.TradeID,
tradeTimeMs, trade.TradeID,
); err != nil {
logger.Infof(" ⚠️ Failed to sync position for trade %s: %v", trade.TradeID, err)
} else {

View File

@@ -1402,15 +1402,12 @@ func (t *HyperliquidTrader) placeXyzOrder(coin string, isBuy bool, size float64,
},
}
// Create OrderAction with builder (xyz dex requires builder info for order routing)
// Create OrderAction (no builder to avoid requiring builder fee approval)
action := hyperliquid.OrderAction{
Type: "order",
Orders: []hyperliquid.OrderWire{orderWire},
Grouping: "na",
Builder: &hyperliquid.BuilderInfo{
Builder: "0x891dc6f05ad47a3c1a05da55e7a7517971faaf0d",
Fee: 10,
},
Builder: nil,
}
// Sign the action
@@ -1592,15 +1589,12 @@ func (t *HyperliquidTrader) placeXyzTriggerOrder(coin string, isBuy bool, size f
},
}
// Create OrderAction with builder
// Create OrderAction (no builder to avoid requiring builder fee approval)
action := hyperliquid.OrderAction{
Type: "order",
Orders: []hyperliquid.OrderWire{orderWire},
Grouping: "na",
Builder: &hyperliquid.BuilderInfo{
Builder: "0x891dc6f05ad47a3c1a05da55e7a7517971faaf0d",
Fee: 10,
},
Builder: nil,
}
// Sign the action
@@ -2070,7 +2064,7 @@ func (t *HyperliquidTrader) GetTrades(startTime time.Time, limit int) ([]TradeRe
Quantity: qty,
RealizedPnL: pnl,
Fee: fee,
Time: time.UnixMilli(fill.Time),
Time: time.UnixMilli(fill.Time).UTC(),
}
trades = append(trades, trade)
}
@@ -2079,7 +2073,44 @@ func (t *HyperliquidTrader) GetTrades(startTime time.Time, limit int) ([]TradeRe
}
// defaultBuilder is the builder info for order routing
var defaultBuilder = &hyperliquid.BuilderInfo{
Builder: "0x891dc6f05ad47a3c1a05da55e7a7517971faaf0d",
Fee: 10,
// Set to nil to avoid requiring builder fee approval
//
// var defaultBuilder = &hyperliquid.BuilderInfo{
// Builder: "0x891dc6f05ad47a3c1a05da55e7a7517971faaf0d",
// Fee: 10,
// }
var defaultBuilder *hyperliquid.BuilderInfo = nil
// GetOpenOrders gets all open/pending orders for a symbol
func (t *HyperliquidTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) {
openOrders, err := t.exchange.Info().OpenOrders(t.ctx, t.walletAddr)
if err != nil {
return nil, fmt.Errorf("failed to get open orders: %w", err)
}
var result []OpenOrder
for _, order := range openOrders {
if order.Coin != symbol {
continue
}
side := "BUY"
if order.Side == "A" {
side = "SELL"
}
result = append(result, OpenOrder{
OrderID: fmt.Sprintf("%d", order.Oid),
Symbol: order.Coin,
Side: side,
PositionSide: "",
Type: "LIMIT",
Price: order.LimitPx,
StopPrice: 0,
Quantity: order.Size,
Status: "NEW",
})
}
return result, nil
}

View File

@@ -94,4 +94,21 @@ type Trader interface {
// limit: max number of records to return
// Returns accurate exit price, fees, and close reason for positions closed externally
GetClosedPnL(startTime time.Time, limit int) ([]ClosedPnLRecord, error)
// GetOpenOrders Get open/pending orders from exchange
// Returns stop-loss, take-profit, and limit orders that haven't been filled
GetOpenOrders(symbol string) ([]OpenOrder, error)
}
// OpenOrder represents a pending order on the exchange
type OpenOrder struct {
OrderID string `json:"order_id"`
Symbol string `json:"symbol"`
Side string `json:"side"` // BUY/SELL
PositionSide string `json:"position_side"` // LONG/SHORT
Type string `json:"type"` // LIMIT/STOP_MARKET/TAKE_PROFIT_MARKET
Price float64 `json:"price"` // Order price (for limit orders)
StopPrice float64 `json:"stop_price"` // Trigger price (for stop orders)
Quantity float64 `json:"quantity"`
Status string `json:"status"` // NEW
}

View File

@@ -34,7 +34,7 @@ func (t *LighterTraderV2) SyncOrdersFromLighter(traderID string, exchangeID stri
// Sort trades by time ASC (oldest first) for proper position building
sort.Slice(trades, func(i, j int) bool {
return trades[i].Time.Before(trades[j].Time)
return trades[i].Time.UnixMilli() < trades[j].Time.UnixMilli()
})
// Process trades one by one (no transaction to avoid deadlock)
@@ -70,7 +70,8 @@ func (t *LighterTraderV2) SyncOrdersFromLighter(traderID string, exchangeID stri
}
}
// Create order record
// Create order record - use Unix milliseconds UTC
tradeTimeMs := trade.Time.UTC().UnixMilli()
orderRecord := &store.TraderOrder{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -87,9 +88,9 @@ func (t *LighterTraderV2) SyncOrdersFromLighter(traderID string, exchangeID stri
FilledQuantity: trade.Quantity,
AvgFillPrice: trade.Price,
Commission: trade.Fee,
FilledAt: trade.Time,
CreatedAt: trade.Time,
UpdatedAt: trade.Time,
FilledAt: tradeTimeMs,
CreatedAt: tradeTimeMs,
UpdatedAt: tradeTimeMs,
}
// Insert order record
@@ -98,7 +99,7 @@ func (t *LighterTraderV2) SyncOrdersFromLighter(traderID string, exchangeID stri
continue
}
// Create fill record
// Create fill record - use Unix milliseconds UTC
fillRecord := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -115,7 +116,7 @@ func (t *LighterTraderV2) SyncOrdersFromLighter(traderID string, exchangeID stri
CommissionAsset: "USDT",
RealizedPnL: trade.RealizedPnL,
IsMaker: false,
CreatedAt: trade.Time,
CreatedAt: tradeTimeMs,
}
if err := orderStore.CreateFill(fillRecord); err != nil {
@@ -127,7 +128,7 @@ func (t *LighterTraderV2) SyncOrdersFromLighter(traderID string, exchangeID stri
traderID, exchangeID, exchangeType,
symbol, positionSide, orderAction,
trade.Quantity, trade.Price, trade.Fee, trade.RealizedPnL,
trade.Time, trade.TradeID,
tradeTimeMs, trade.TradeID,
); err != nil {
logger.Infof(" ⚠️ Failed to sync position for trade %s: %v", trade.TradeID, err)
} else {

View File

@@ -537,7 +537,7 @@ func (t *LighterTraderV2) GetTrades(startTime time.Time, limit int) ([]TradeReco
// - signChanged with position flip: split into close + open
const EPSILON = 0.0001
tradeTime := time.UnixMilli(lt.Timestamp)
tradeTime := time.UnixMilli(lt.Timestamp).UTC()
// Calculate position after trade
var posAfter float64
@@ -628,7 +628,7 @@ func (t *LighterTraderV2) GetTrades(startTime time.Time, limit int) ([]TradeReco
Quantity: qty,
RealizedPnL: 0, // Not available in API
Fee: fee,
Time: time.UnixMilli(lt.Timestamp),
Time: time.UnixMilli(lt.Timestamp).UTC(),
}
result = append(result, trade)
}

View File

@@ -686,3 +686,9 @@ func pow10(n int) int64 {
}
return result
}
// GetOpenOrders gets all open/pending orders for a symbol
func (t *LighterTraderV2) GetOpenOrders(symbol string) ([]OpenOrder, error) {
// TODO: Implement Lighter open orders
return []OpenOrder{}, nil
}

View File

@@ -133,7 +133,7 @@ func (t *OKXTrader) GetTrades(startTime time.Time, limit int) ([]OKXTrade, error
FillQtyBase: fillQtyBase,
Fee: -fee, // OKX returns negative fee
FeeAsset: fill.FeeCcy,
ExecTime: time.UnixMilli(ts),
ExecTime: time.UnixMilli(ts).UTC(),
IsMaker: fill.ExecType == "M",
OrderType: "MARKET",
OrderAction: orderAction,
@@ -169,7 +169,7 @@ func (t *OKXTrader) SyncOrdersFromOKX(traderID string, exchangeID string, exchan
// Sort trades by time ASC (oldest first) for proper position building
sort.Slice(trades, func(i, j int) bool {
return trades[i].ExecTime.Before(trades[j].ExecTime)
return trades[i].ExecTime.UnixMilli() < trades[j].ExecTime.UnixMilli()
})
// Process trades one by one (no transaction to avoid deadlock)
@@ -197,7 +197,8 @@ func (t *OKXTrader) SyncOrdersFromOKX(traderID string, exchangeID string, exchan
// Normalize side for storage
side := strings.ToUpper(trade.Side)
// Create order record
// Create order record - use UTC time in milliseconds to avoid timezone issues
execTimeMs := trade.ExecTime.UTC().UnixMilli()
orderRecord := &store.TraderOrder{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -214,9 +215,9 @@ func (t *OKXTrader) SyncOrdersFromOKX(traderID string, exchangeID string, exchan
FilledQuantity: trade.FillQtyBase,
AvgFillPrice: trade.FillPrice,
Commission: trade.Fee,
FilledAt: trade.ExecTime,
CreatedAt: trade.ExecTime,
UpdatedAt: trade.ExecTime,
FilledAt: execTimeMs,
CreatedAt: execTimeMs,
UpdatedAt: execTimeMs,
}
// Insert order record
@@ -225,7 +226,7 @@ func (t *OKXTrader) SyncOrdersFromOKX(traderID string, exchangeID string, exchan
continue
}
// Create fill record
// Create fill record - use UTC time in milliseconds
fillRecord := &store.TraderFill{
TraderID: traderID,
ExchangeID: exchangeID, // UUID
@@ -242,7 +243,7 @@ func (t *OKXTrader) SyncOrdersFromOKX(traderID string, exchangeID string, exchan
CommissionAsset: trade.FeeAsset,
RealizedPnL: 0, // OKX fills don't include PnL per trade
IsMaker: trade.IsMaker,
CreatedAt: trade.ExecTime,
CreatedAt: execTimeMs,
}
if err := orderStore.CreateFill(fillRecord); err != nil {
@@ -254,7 +255,7 @@ func (t *OKXTrader) SyncOrdersFromOKX(traderID string, exchangeID string, exchan
traderID, exchangeID, exchangeType,
symbol, positionSide, trade.OrderAction,
trade.FillQtyBase, trade.FillPrice, trade.Fee, 0, // No per-trade PnL from OKX
trade.ExecTime, trade.TradeID,
execTimeMs, trade.TradeID,
); err != nil {
logger.Infof(" ⚠️ Failed to sync position for trade %s: %v", trade.TradeID, err)
} else {

View File

@@ -1366,8 +1366,8 @@ func (t *OKXTrader) GetClosedPnL(startTime time.Time, limit int) ([]ClosedPnLRec
// Times
cTime, _ := strconv.ParseInt(pos.CTime, 10, 64)
uTime, _ := strconv.ParseInt(pos.UTime, 10, 64)
record.EntryTime = time.UnixMilli(cTime)
record.ExitTime = time.UnixMilli(uTime)
record.EntryTime = time.UnixMilli(cTime).UTC()
record.ExitTime = time.UnixMilli(uTime).UTC()
// Close type
switch pos.Type {
@@ -1387,3 +1387,9 @@ func (t *OKXTrader) GetClosedPnL(startTime time.Time, limit int) ([]ClosedPnLRec
return records, nil
}
// GetOpenOrders gets all open/pending orders for a symbol
func (t *OKXTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) {
// TODO: Implement OKX open orders
return []OpenOrder{}, nil
}

View File

@@ -40,7 +40,7 @@ func CreatePositionSnapshot(traderID, exchangeID, exchangeType string, trader Tr
logger.Infof("📥 Found %d positions on exchange", len(positions))
// Step 3: Create snapshot record for each position
now := time.Now()
nowMs := time.Now().UnixMilli()
createdCount := 0
for _, posMap := range positions {
@@ -74,18 +74,18 @@ func CreatePositionSnapshot(traderID, exchangeID, exchangeType string, trader Tr
TraderID: traderID,
ExchangeID: exchangeID,
ExchangeType: exchangeType,
ExchangePositionID: fmt.Sprintf("snapshot_%s_%s_%d", symbol, side, now.UnixMilli()),
ExchangePositionID: fmt.Sprintf("snapshot_%s_%s_%d", symbol, side, nowMs),
Symbol: symbol,
Side: side,
Quantity: positionAmt,
EntryPrice: entryPrice,
EntryOrderID: "snapshot", // Mark as snapshot
EntryTime: now,
EntryTime: nowMs,
Leverage: int(leverage),
Status: "OPEN",
Source: "snapshot", // Mark source as snapshot
CreatedAt: now,
UpdatedAt: now,
CreatedAt: nowMs,
UpdatedAt: nowMs,
}
if err := positionStore.CreateOpenPosition(snapshotPosition); err != nil {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -31,6 +31,19 @@ interface OrderMarker {
symbol: string
}
// 挂单接口定义 (交易所的止盈止损订单)
interface OpenOrder {
order_id: string
symbol: string
side: string // BUY/SELL
position_side: string // LONG/SHORT
type: string // LIMIT/STOP_MARKET/TAKE_PROFIT_MARKET
price: number // 限价单价格
stop_price: number // 触发价格 (止损/止盈)
quantity: number
status: string
}
interface AdvancedChartProps {
symbol: string
interval?: string
@@ -101,6 +114,7 @@ export function AdvancedChart({
const seriesMarkersRef = useRef<any>(null) // Markers primitive for v5
const currentMarkersDataRef = useRef<any[]>([]) // 存储当前的标记数据
const klineDataRef = useRef<Map<number, { volume: number; quoteVolume: number }>>(new Map()) // 存储 kline 额外数据
const priceLinesRef = useRef<any[]>([]) // 存储挂单价格线
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
@@ -179,9 +193,15 @@ export function AdvancedChart({
return 0
}
// 如果已经是数字Unix 时间戳),直接返回
// 如果已经是数字Unix 时间戳)
if (typeof time === 'number') {
console.log('[AdvancedChart] ✅ Unix timestamp:', time, '(', new Date(time * 1000).toISOString(), ')')
// 判断是毫秒还是秒:如果大于 10^12 则认为是毫秒2001年之后的毫秒时间戳
if (time > 1000000000000) {
const seconds = Math.floor(time / 1000)
console.log('[AdvancedChart] ✅ Unix timestamp (ms→s):', time, '→', seconds, '(', new Date(time).toISOString(), ')')
return seconds
}
console.log('[AdvancedChart] ✅ Unix timestamp (s):', time, '(', new Date(time * 1000).toISOString(), ')')
return time
}
@@ -221,8 +241,8 @@ export function AdvancedChart({
const fetchOrders = async (traderID: string, symbol: string): Promise<OrderMarker[]> => {
try {
console.log('[AdvancedChart] Fetching orders for trader:', traderID, 'symbol:', symbol)
// 获取已成交的订单,限制50条避免标记太多重叠
const result = await httpClient.get(`/api/orders?trader_id=${traderID}&symbol=${symbol}&status=FILLED&limit=50`)
// 获取已成交的订单,增加到200条以显示更多历史订单
const result = await httpClient.get(`/api/orders?trader_id=${traderID}&symbol=${symbol}&status=FILLED&limit=200`)
console.log('[AdvancedChart] Orders API response:', result)
@@ -301,13 +321,33 @@ export function AdvancedChart({
}
}
// 获取交易所挂单 (止盈止损订单)
const fetchOpenOrders = async (traderID: string, symbol: string): Promise<OpenOrder[]> => {
try {
console.log('[AdvancedChart] Fetching open orders for trader:', traderID, 'symbol:', symbol)
const result = await httpClient.get(`/api/open-orders?trader_id=${traderID}&symbol=${symbol}`)
console.log('[AdvancedChart] Open orders API response:', result)
if (!result.success || !result.data) {
console.warn('[AdvancedChart] No open orders found')
return []
}
return result.data as OpenOrder[]
} catch (err) {
console.error('[AdvancedChart] Error fetching open orders:', err)
return []
}
}
// 初始化图表
useEffect(() => {
if (!chartContainerRef.current) return
const chart = createChart(chartContainerRef.current, {
width: chartContainerRef.current.clientWidth,
height: height,
width: chartContainerRef.current.clientWidth || 800,
height: chartContainerRef.current.clientHeight || height,
layout: {
background: { color: '#0B0E11' },
textColor: '#B7BDC6',
@@ -407,16 +447,16 @@ export function AdvancedChart({
})
volumeSeriesRef.current = volumeSeries as any
// 响应式调整
const handleResize = () => {
if (chartContainerRef.current && chartRef.current) {
chartRef.current.applyOptions({
width: chartContainerRef.current.clientWidth,
})
}
}
// 响应式调整 (ResizeObserver)
const resizeObserver = new ResizeObserver((entries) => {
if (entries.length === 0 || !entries[0].contentRect) return
const { width, height } = entries[0].contentRect
chart.applyOptions({ width, height })
})
window.addEventListener('resize', handleResize)
if (chartContainerRef.current) {
resizeObserver.observe(chartContainerRef.current)
}
// 监听鼠标移动,显示 OHLC 信息
chart.subscribeCrosshairMove((param) => {
@@ -450,10 +490,11 @@ export function AdvancedChart({
})
return () => {
window.removeEventListener('resize', handleResize)
resizeObserver.disconnect()
chart.remove()
}
}, [height])
}, []) // Chart is created once, ResizeObserver handles dimension changes
// 加载数据和指标
useEffect(() => {
@@ -580,15 +621,8 @@ export function AdvancedChart({
return klineTimes[left]
}
// 过滤并对齐订单到 K 线时间
const markers: Array<{
time: Time
position: 'belowBar'
color: string
shape: 'circle'
text: string
size: number
}> = []
// 按 K 线时间分组统计订单
const ordersByCandle = new Map<number, { buys: number; sells: number }>()
orders.forEach(order => {
// 使用二分查找找到对应的 K 线蜡烛时间
@@ -600,15 +634,48 @@ export function AdvancedChart({
return
}
const isBuy = order.rawSide === 'buy'
markers.push({
time: candleTime as Time,
position: 'belowBar' as const,
color: isBuy ? '#0ECB81' : '#F6465D',
shape: 'circle' as const,
text: isBuy ? 'B' : 'S',
size: 1,
})
const existing = ordersByCandle.get(candleTime) || { buys: 0, sells: 0 }
if (order.rawSide === 'buy') {
existing.buys++
} else {
existing.sells++
}
ordersByCandle.set(candleTime, existing)
})
// 为每个有订单的 K 线创建标记
const markers: Array<{
time: Time
position: 'belowBar' | 'aboveBar'
color: string
shape: 'circle'
text: string
size: number
}> = []
ordersByCandle.forEach((counts, candleTime) => {
// 显示买入标记绿色在K线下方
if (counts.buys > 0) {
markers.push({
time: candleTime as Time,
position: 'belowBar' as const,
color: '#0ECB81',
shape: 'circle' as const,
text: counts.buys > 1 ? `B${counts.buys}` : 'B',
size: 1,
})
}
// 显示卖出标记红色在K线上方
if (counts.sells > 0) {
markers.push({
time: candleTime as Time,
position: 'aboveBar' as const,
color: '#F6465D',
shape: 'circle' as const,
text: counts.sells > 1 ? `S${counts.sells}` : 'S',
size: 1,
})
}
})
// 按时间排序lightweight-charts 要求标记按时间顺序)
@@ -674,6 +741,87 @@ export function AdvancedChart({
return () => clearInterval(refreshInterval)
}, [symbol, interval, traderID, exchange])
// 单独刷新挂单价格线 (60秒刷新一次避免频繁调用交易所API)
useEffect(() => {
if (!traderID || !candlestickSeriesRef.current) return
// 加载挂单并显示价格线
const loadOpenOrders = async () => {
try {
// 先清除旧的价格线
priceLinesRef.current.forEach(line => {
try {
candlestickSeriesRef.current?.removePriceLine(line)
} catch (e) {
// 忽略清除错误
}
})
priceLinesRef.current = []
const openOrders = await fetchOpenOrders(traderID, symbol)
console.log('[AdvancedChart] Open orders for price lines:', openOrders)
if (openOrders.length > 0 && candlestickSeriesRef.current) {
openOrders.forEach(order => {
// 获取触发价格 (止损/止盈用 stop_price限价单用 price)
const linePrice = order.stop_price > 0 ? order.stop_price : order.price
if (linePrice <= 0) return
// 判断订单类型
const isStopLoss = order.type.includes('STOP') || order.type.includes('SL')
const isTakeProfit = order.type.includes('TAKE_PROFIT') || order.type.includes('TP')
const isLimit = order.type === 'LIMIT'
// 设置价格线样式
let lineColor = '#F0B90B' // 默认黄色
const lineStyle = 2 // 虚线
let title = ''
if (isStopLoss) {
lineColor = '#F6465D' // 红色 - 止损
title = `SL ${order.quantity}`
} else if (isTakeProfit) {
lineColor = '#0ECB81' // 绿色 - 止盈
title = `TP ${order.quantity}`
} else if (isLimit) {
lineColor = '#F0B90B' // 黄色 - 限价单
title = `Limit ${order.side} ${order.quantity}`
} else {
title = `${order.type} ${order.quantity}`
}
const priceLine = candlestickSeriesRef.current?.createPriceLine({
price: linePrice,
color: lineColor,
lineWidth: 1,
lineStyle: lineStyle,
axisLabelVisible: true,
title: title,
})
if (priceLine) {
priceLinesRef.current.push(priceLine)
}
})
console.log('[AdvancedChart] ✅ Created', priceLinesRef.current.length, 'price lines for pending orders')
}
} catch (err) {
console.error('[AdvancedChart] Error loading open orders:', err)
}
}
// 初始加载 (延迟1秒等待图表初始化完成)
const initialTimeout = setTimeout(loadOpenOrders, 1000)
// 60秒刷新一次挂单
const openOrdersInterval = setInterval(loadOpenOrders, 60000)
return () => {
clearTimeout(initialTimeout)
clearInterval(openOrdersInterval)
}
}, [symbol, traderID])
// 单独处理订单标记的显示/隐藏,避免重新加载数据
useEffect(() => {
if (!seriesMarkersRef.current) return
@@ -767,12 +915,15 @@ export function AdvancedChart({
borderRadius: '12px',
overflow: 'hidden',
border: '1px solid rgba(43, 49, 57, 0.5)',
height: '100%',
display: 'flex',
flexDirection: 'column',
}}
>
{/* Compact Professional Header */}
<div
className="flex items-center justify-between px-4 py-2"
style={{ borderBottom: '1px solid rgba(43, 49, 57, 0.6)', background: '#0D1117' }}
style={{ borderBottom: '1px solid rgba(43, 49, 57, 0.6)', background: '#0D1117', flexShrink: 0 }}
>
{/* Left: Symbol Info + Price */}
<div className="flex items-center gap-4">
@@ -929,8 +1080,8 @@ export function AdvancedChart({
)}
{/* 图表容器 */}
<div style={{ position: 'relative' }}>
<div ref={chartContainerRef} />
<div style={{ position: 'relative', flex: 1, minHeight: 0 }}>
<div ref={chartContainerRef} style={{ height: '100%', width: '100%' }} />
{/* OHLC Tooltip */}
{tooltipData && (

File diff suppressed because it is too large Load Diff

View File

@@ -145,148 +145,152 @@ export function ChartTabs({ traderId, selectedSymbol, updateKey, exchangeId }: C
console.log('[ChartTabs] rendering, activeTab:', activeTab)
return (
<div className="binance-card" style={{ background: '#0D1117', borderRadius: '8px', overflow: 'hidden' }}>
{/* Clean Professional Toolbar */}
<div className={`nofx-glass rounded-lg border border-white/5 relative z-10 w-full flex flex-col transition-all duration-300 ${typeof window !== 'undefined' && window.innerWidth < 768 ? 'h-[500px]' : 'h-[600px]'
}`}>
{/*
Premium Professional Toolbar
Mobile: Single row, horizontal scroll with gradient mask
Desktop: Standard flex-wrap/nowrap
*/}
<div
className="flex items-center justify-between px-3 py-1.5"
style={{ borderBottom: '1px solid rgba(43, 49, 57, 0.6)', background: '#161B22' }}
className="relative z-20 flex flex-wrap md:flex-nowrap items-center justify-between gap-y-2 px-3 py-2 shrink-0 backdrop-blur-md bg-[#0B0E11]/80 rounded-t-lg"
style={{ borderBottom: '1px solid rgba(255, 255, 255, 0.05)' }}
>
{/* Left: Tab Switcher */}
<div className="flex items-center gap-1">
<div className="flex flex-wrap items-center gap-1">
<button
onClick={() => setActiveTab('equity')}
className={`flex items-center gap-1.5 px-2.5 py-1 rounded text-[11px] font-medium transition-all ${
activeTab === 'equity'
? 'bg-blue-500/15 text-blue-400'
: 'text-gray-500 hover:text-gray-300'
}`}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded-md text-[11px] font-medium transition-all ${activeTab === 'equity'
? 'bg-nofx-gold/10 text-nofx-gold border border-nofx-gold/20 shadow-[0_0_10px_rgba(240,185,11,0.1)]'
: 'text-nofx-text-muted hover:text-nofx-text-main hover:bg-white/5'
}`}
>
<BarChart3 className="w-3 h-3" />
<span>{t('accountEquityCurve', language)}</span>
<BarChart3 className="w-3.5 h-3.5" />
<span className="hidden md:inline">{t('accountEquityCurve', language)}</span>
<span className="md:hidden">Eq</span>
</button>
<button
onClick={() => setActiveTab('kline')}
className={`flex items-center gap-1.5 px-2.5 py-1 rounded text-[11px] font-medium transition-all ${
activeTab === 'kline'
? 'bg-blue-500/15 text-blue-400'
: 'text-gray-500 hover:text-gray-300'
}`}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded-md text-[11px] font-medium transition-all ${activeTab === 'kline'
? 'bg-nofx-gold/10 text-nofx-gold border border-nofx-gold/20 shadow-[0_0_10px_rgba(240,185,11,0.1)]'
: 'text-nofx-text-muted hover:text-nofx-text-main hover:bg-white/5'
}`}
>
<CandlestickChart className="w-3 h-3" />
<span>{t('marketChart', language)}</span>
<CandlestickChart className="w-3.5 h-3.5" />
<span className="hidden md:inline">{t('marketChart', language)}</span>
<span className="md:hidden">Kline</span>
</button>
{/* Market Type Pills - Only when kline active */}
{/* Market Type Pills - Only when kline active, HIDDEN on mobile to save space */}
{activeTab === 'kline' && (
<>
<div className="w-px h-3 bg-[#30363D] mx-1" />
<div className="flex items-center gap-0.5">
{(Object.keys(MARKET_CONFIG) as MarketType[]).map((type) => {
const config = MARKET_CONFIG[type]
const isActive = marketType === type
return (
<button
key={type}
onClick={() => handleMarketTypeChange(type)}
className={`px-2 py-0.5 text-[10px] font-medium rounded transition-all ${
isActive
? 'bg-[#21262D] text-white'
: 'text-gray-500 hover:text-gray-400'
<div className="hidden md:flex items-center gap-1 ml-2 border-l border-white/10 pl-2">
{(Object.keys(MARKET_CONFIG) as MarketType[]).map((type) => {
const config = MARKET_CONFIG[type]
const isActive = marketType === type
return (
<button
key={type}
onClick={() => handleMarketTypeChange(type)}
className={`px-2.5 py-1 text-[10px] font-medium rounded transition-all border ${isActive
? 'bg-white/10 text-white border-white/20'
: 'text-nofx-text-muted border-transparent hover:text-nofx-text-main hover:bg-white/5'
}`}
>
{config.icon} {language === 'zh' ? config.label.zh : config.label.en}
</button>
)
})}
</div>
</>
>
<span className="mr-1 opacity-70">{config.icon}</span>
{language === 'zh' ? config.label.zh : config.label.en}
</button>
)
})}
</div>
)}
</div>
{/* Right: Symbol + Interval */}
{activeTab === 'kline' && (
<div className="flex items-center gap-2">
<div className="flex items-center gap-2 md:gap-3 w-full md:w-auto min-w-0">
{/* Symbol Dropdown */}
{marketConfig.hasDropdown ? (
<div className="relative" ref={dropdownRef}>
<button
onClick={() => setShowDropdown(!showDropdown)}
className="flex items-center gap-1 px-2 py-1 bg-[#21262D] rounded text-[11px] font-bold text-white hover:bg-[#30363D] transition-all"
>
<span>{chartSymbol}</span>
<ChevronDown className={`w-3 h-3 text-gray-400 transition-transform ${showDropdown ? 'rotate-180' : ''}`} />
</button>
{showDropdown && (
<div className="absolute top-full right-0 mt-1 w-56 bg-[#161B22] border border-[#30363D] rounded-lg shadow-2xl z-50 max-h-72 overflow-hidden">
<div className="p-2 border-b border-[#30363D]">
<div className="flex items-center gap-2 px-2 py-1 bg-[#0D1117] rounded border border-[#30363D]">
<Search className="w-3 h-3 text-gray-500" />
<input
type="text"
value={searchFilter}
onChange={(e) => setSearchFilter(e.target.value)}
placeholder="Search..."
className="flex-1 bg-transparent text-[11px] text-white placeholder-gray-600 focus:outline-none"
autoFocus
/>
<div className="shrink-0 relative" ref={dropdownRef}>
{marketConfig.hasDropdown ? (
<>
<button
onClick={() => setShowDropdown(!showDropdown)}
className="flex items-center gap-1.5 px-2.5 py-1 bg-black/40 border border-white/10 rounded text-[11px] font-bold text-nofx-text-main hover:border-nofx-gold/30 hover:text-nofx-gold transition-all"
>
<span>{chartSymbol}</span>
<ChevronDown className={`w-3 h-3 text-nofx-text-muted transition-transform ${showDropdown ? 'rotate-180' : ''}`} />
</button>
{showDropdown && (
<div className="absolute top-full right-0 mt-2 w-64 bg-[#0B0E11] border border-white/10 rounded-lg shadow-[0_10px_40px_-10px_rgba(0,0,0,0.5)] z-50 overflow-hidden nofx-glass ring-1 ring-white/5">
<div className="p-2 border-b border-white/5">
<div className="flex items-center gap-2 px-2 py-1.5 bg-black/40 rounded border border-white/10 focus-within:border-nofx-gold/50 transition-colors">
<Search className="w-3.5 h-3.5 text-nofx-text-muted" />
<input
type="text"
value={searchFilter}
onChange={(e) => setSearchFilter(e.target.value)}
placeholder="Search symbol..."
className="flex-1 bg-transparent text-[11px] text-white placeholder-gray-600 focus:outline-none font-mono"
autoFocus
/>
</div>
</div>
<div className="overflow-y-auto max-h-60 custom-scrollbar">
{['crypto', 'stock', 'forex', 'commodity', 'index'].map(category => {
const categorySymbols = filteredSymbols.filter(s => s.category === category)
if (categorySymbols.length === 0) return null
const labels: Record<string, string> = { crypto: 'Crypto', stock: 'Stocks', forex: 'Forex', commodity: 'Commodities', index: 'Index' }
return (
<div key={category}>
<div className="px-3 py-1.5 text-[9px] font-bold text-nofx-text-muted/60 bg-white/5 uppercase tracking-wider">{labels[category]}</div>
{categorySymbols.map(s => (
<button
key={s.symbol}
onClick={() => { setChartSymbol(s.symbol); setShowDropdown(false); setSearchFilter('') }}
className={`w-full px-3 py-2 text-left text-[11px] font-mono hover:bg-white/5 transition-all flex items-center justify-between ${chartSymbol === s.symbol ? 'bg-nofx-gold/10 text-nofx-gold' : 'text-nofx-text-muted'}`}
>
<span>{s.symbol}</span>
<span className="text-[9px] opacity-40">{s.name}</span>
</button>
))}
</div>
)
})}
</div>
</div>
<div className="overflow-y-auto max-h-52">
{['crypto', 'stock', 'forex', 'commodity', 'index'].map(category => {
const categorySymbols = filteredSymbols.filter(s => s.category === category)
if (categorySymbols.length === 0) return null
const labels: Record<string, string> = { crypto: 'Crypto', stock: 'Stocks', forex: 'Forex', commodity: 'Commodities', index: 'Index' }
return (
<div key={category}>
<div className="px-3 py-1 text-[9px] font-medium text-gray-500 bg-[#0D1117] uppercase tracking-wider">{labels[category]}</div>
{categorySymbols.map(s => (
<button
key={s.symbol}
onClick={() => { setChartSymbol(s.symbol); setShowDropdown(false); setSearchFilter('') }}
className={`w-full px-3 py-1.5 text-left text-[11px] hover:bg-[#21262D] transition-all ${chartSymbol === s.symbol ? 'bg-blue-500/20 text-blue-400' : 'text-gray-300'}`}
>
{s.symbol}
</button>
))}
</div>
)
})}
</div>
</div>
)}
</div>
) : (
<span className="px-2 py-1 bg-[#21262D] rounded text-[11px] font-bold text-white">{chartSymbol}</span>
)}
)}
</>
) : (
<span className="px-2.5 py-1 bg-black/40 border border-white/10 rounded text-[11px] font-bold text-nofx-text-main font-mono">{chartSymbol}</span>
)}
</div>
{/* Interval Selector */}
<div className="flex items-center bg-[#21262D] rounded overflow-hidden">
{/* Interval Selector - Allow scrolling if needed */}
<div className="flex items-center bg-black/40 rounded border border-white/10 overflow-x-auto no-scrollbar max-w-[200px] md:max-w-none">
{INTERVALS.map((int) => (
<button
key={int.value}
onClick={() => setInterval(int.value)}
className={`px-2 py-1 text-[10px] font-medium transition-all ${
interval === int.value
? 'bg-blue-500/30 text-blue-400'
: 'text-gray-500 hover:text-gray-300 hover:bg-[#30363D]'
}`}
className={`px-2 py-1 text-[10px] font-medium transition-all ${interval === int.value
? 'bg-nofx-gold/20 text-nofx-gold'
: 'text-nofx-text-muted hover:text-white hover:bg-white/5'
}`}
>
{int.label}
</button>
))}
</div>
{/* Quick Input */}
<form onSubmit={handleSymbolSubmit} className="flex items-center">
{/* Quick Input - Hidden on mobile, dropdown search is enough */}
<form onSubmit={handleSymbolSubmit} className="hidden md:flex items-center shrink-0">
<input
type="text"
value={symbolInput}
onChange={(e) => setSymbolInput(e.target.value)}
placeholder="Symbol..."
className="w-20 px-2 py-1 bg-[#0D1117] border border-[#30363D] rounded-l text-[10px] text-white placeholder-gray-600 focus:outline-none focus:border-blue-500/50"
placeholder="Sym"
className="w-16 px-2 py-1 bg-black/40 border border-white/10 rounded-l text-[10px] text-white placeholder-gray-600 focus:outline-none focus:border-nofx-gold/50 font-mono transition-colors"
/>
<button type="submit" className="px-2 py-1 bg-[#21262D] border border-[#30363D] border-l-0 rounded-r text-[10px] text-gray-400 hover:text-white hover:bg-[#30363D] transition-all">
<button type="submit" className="px-2 py-1 bg-white/5 border border-white/10 border-l-0 rounded-r text-[10px] text-nofx-text-muted hover:text-white hover:bg-white/10 transition-all">
Go
</button>
</form>
@@ -294,34 +298,34 @@ export function ChartTabs({ traderId, selectedSymbol, updateKey, exchangeId }: C
)}
</div>
{/* Tab Content */}
<div className="relative overflow-hidden min-h-[400px]">
{/* Tab Content - Chart autosizes to this container */}
<div className="relative flex-1 bg-[#0B0E11]/50 rounded-b-lg overflow-hidden h-full min-h-0">
<AnimatePresence mode="wait">
{activeTab === 'equity' ? (
<motion.div
key="equity"
initial={{ opacity: 0, x: -20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 20 }}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="h-full"
className="h-full w-full absolute inset-0"
>
<EquityChart traderId={traderId} embedded />
</motion.div>
) : (
<motion.div
key={`kline-${chartSymbol}-${interval}-${currentExchange}`}
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: -20 }}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="h-full"
className="h-full w-full absolute inset-0"
>
<AdvancedChart
symbol={chartSymbol}
interval={interval}
traderID={traderId}
height={550}
// Dynamic auto-sizing via ResizeObserver
exchange={currentExchange}
onSymbolChange={setChartSymbol}
/>

View File

@@ -63,9 +63,15 @@ export function ChartWithOrders({
return 0
}
// 如果已经是数字Unix 时间戳),直接返回
// 如果已经是数字Unix 时间戳)
if (typeof time === 'number') {
console.log('[ChartWithOrders] ✅ Unix timestamp:', time, '(', new Date(time * 1000).toISOString(), ')')
// 判断是毫秒还是秒:如果大于 10^12 则认为是毫秒2001年之后的毫秒时间戳
if (time > 1000000000000) {
const seconds = Math.floor(time / 1000)
console.log('[ChartWithOrders] ✅ Unix timestamp (ms→s):', time, '→', seconds, '(', new Date(time).toISOString(), ')')
return seconds
}
console.log('[ChartWithOrders] ✅ Unix timestamp (s):', time, '(', new Date(time * 1000).toISOString(), ')')
return time
}

View File

@@ -9,6 +9,7 @@ import { getTraderColor } from '../utils/traderColors'
import { useLanguage } from '../contexts/LanguageContext'
import { t } from '../i18n/translations'
import { PunkAvatar, getTraderAvatar } from './PunkAvatar'
import { DeepVoidBackground } from './DeepVoidBackground'
export function CompetitionPage() {
const { language } = useLanguage()
@@ -44,83 +45,78 @@ export function CompetitionPage() {
if (!competition) {
return (
<div className="space-y-6">
<div className="binance-card p-8 animate-pulse">
<div className="flex items-center justify-between mb-6">
<div className="space-y-3 flex-1">
<div className="skeleton h-8 w-64"></div>
<div className="skeleton h-4 w-48"></div>
<DeepVoidBackground className="py-8" disableAnimation>
<div className="container mx-auto max-w-7xl px-4 md:px-8">
<div className="space-y-6">
<div className="animate-pulse bg-black/40 border border-white/10 rounded-xl p-8 backdrop-blur-md">
<div className="flex items-center justify-between mb-6">
<div className="space-y-3 flex-1">
<div className="h-8 w-64 bg-white/5 rounded"></div>
<div className="h-4 w-48 bg-white/5 rounded"></div>
</div>
<div className="h-12 w-32 bg-white/5 rounded"></div>
</div>
</div>
<div className="bg-black/40 border border-white/10 rounded-xl p-6 backdrop-blur-md">
<div className="h-6 w-40 mb-4 bg-white/5 rounded"></div>
<div className="space-y-3">
<div className="h-20 w-full bg-white/5 rounded"></div>
<div className="h-20 w-full bg-white/5 rounded"></div>
</div>
</div>
<div className="skeleton h-12 w-32"></div>
</div>
</div>
<div className="binance-card p-6">
<div className="skeleton h-6 w-40 mb-4"></div>
<div className="space-y-3">
<div className="skeleton h-20 w-full rounded"></div>
<div className="skeleton h-20 w-full rounded"></div>
</div>
</div>
</div>
</DeepVoidBackground>
)
}
// 如果有数据返回但没有交易员,显示空状态
if (!competition.traders || competition.traders.length === 0) {
return (
<div className="space-y-5 animate-fade-in">
{/* Competition Header - 精简版 */}
<div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-3 md:gap-0">
<div className="flex items-center gap-3 md:gap-4">
<div
className="w-10 h-10 md:w-12 md:h-12 rounded-xl flex items-center justify-center"
style={{
background: 'linear-gradient(135deg, #F0B90B 0%, #FCD535 100%)',
boxShadow: '0 4px 14px rgba(240, 185, 11, 0.4)',
}}
>
<Trophy
className="w-6 h-6 md:w-7 md:h-7"
style={{ color: '#000' }}
/>
</div>
<div>
<h1
className="text-xl md:text-2xl font-bold flex items-center gap-2"
style={{ color: '#EAECEF' }}
<DeepVoidBackground className="py-8" disableAnimation>
<div className="container mx-auto max-w-7xl px-4 md:px-8 space-y-8 animate-fade-in">
{/* Competition Header - 精简版 */}
<div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-3 md:gap-0">
<div className="flex items-center gap-3 md:gap-4">
<div
className="w-10 h-10 md:w-12 md:h-12 rounded-xl flex items-center justify-center bg-black/60 border border-nofx-gold/30 shadow-[0_0_15px_rgba(240,185,11,0.2)]"
>
{t('aiCompetition', language)}
<span
className="text-xs font-normal px-2 py-1 rounded"
style={{
background: 'rgba(240, 185, 11, 0.15)',
color: '#F0B90B',
}}
<Trophy
className="w-6 h-6 md:w-7 md:h-7 text-nofx-gold"
/>
</div>
<div>
<h1
className="text-xl md:text-2xl font-bold flex items-center gap-2 text-white"
>
0 {t('traders', language)}
</span>
</h1>
<p className="text-xs" style={{ color: '#848E9C' }}>
{t('liveBattle', language)}
</p>
{t('aiCompetition', language)}
<span
className="text-xs font-normal px-2 py-1 rounded bg-nofx-gold/10 text-nofx-gold border border-nofx-gold/20"
>
0 {t('traders', language)}
</span>
</h1>
<p className="text-xs text-zinc-400">
{t('liveBattle', language)}
</p>
</div>
</div>
</div>
</div>
{/* Empty State */}
<div className="binance-card p-8 text-center">
<Trophy
className="w-16 h-16 mx-auto mb-4 opacity-40"
style={{ color: '#848E9C' }}
/>
<h3 className="text-lg font-bold mb-2" style={{ color: '#EAECEF' }}>
{t('noTraders', language)}
</h3>
<p className="text-sm" style={{ color: '#848E9C' }}>
{t('createFirstTrader', language)}
</p>
{/* Empty State */}
<div className="bg-black/40 border border-white/10 rounded-xl p-16 text-center backdrop-blur-md">
<Trophy
className="w-16 h-16 mx-auto mb-4 text-zinc-700"
/>
<h3 className="text-lg font-bold mb-2 text-white">
{t('noTraders', language)}
</h3>
<p className="text-sm text-zinc-400">
{t('createFirstTrader', language)}
</p>
</div>
</div>
</div>
</DeepVoidBackground>
)
}
@@ -133,375 +129,358 @@ export function CompetitionPage() {
const leader = sortedTraders[0]
return (
<div className="space-y-5 animate-fade-in">
{/* Competition Header - 精简版 */}
<div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-3 md:gap-0">
<div className="flex items-center gap-3 md:gap-4">
<div
className="w-10 h-10 md:w-12 md:h-12 rounded-xl flex items-center justify-center"
style={{
background: 'linear-gradient(135deg, #F0B90B 0%, #FCD535 100%)',
boxShadow: '0 4px 14px rgba(240, 185, 11, 0.4)',
}}
>
<Trophy
className="w-6 h-6 md:w-7 md:h-7"
style={{ color: '#000' }}
/>
</div>
<div>
<h1
className="text-xl md:text-2xl font-bold flex items-center gap-2"
style={{ color: '#EAECEF' }}
<DeepVoidBackground className="py-8" disableAnimation>
<div className="w-full px-4 md:px-8 space-y-8 animate-fade-in">
{/* Competition Header - 精简版 */}
<div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-3 md:gap-0">
<div className="flex items-center gap-3 md:gap-4">
<div
className="w-10 h-10 md:w-12 md:h-12 rounded-xl flex items-center justify-center bg-black/60 border border-nofx-gold/30 shadow-[0_0_15px_rgba(240,185,11,0.2)]"
>
{t('aiCompetition', language)}
<span
className="text-xs font-normal px-2 py-1 rounded"
style={{
background: 'rgba(240, 185, 11, 0.15)',
color: '#F0B90B',
}}
<Trophy
className="w-6 h-6 md:w-7 md:h-7 text-nofx-gold"
/>
</div>
<div>
<h1
className="text-xl md:text-2xl font-bold flex items-center gap-2 text-white"
>
{competition.count} {t('traders', language)}
</span>
</h1>
<p className="text-xs" style={{ color: '#848E9C' }}>
{t('liveBattle', language)}
</p>
</div>
</div>
<div className="text-left md:text-right w-full md:w-auto">
<div className="text-xs mb-1" style={{ color: '#848E9C' }}>
{t('leader', language)}
</div>
<div
className="text-base md:text-lg font-bold"
style={{ color: '#F0B90B' }}
>
{leader?.trader_name}
</div>
<div
className="text-sm font-semibold"
style={{
color: (leader?.total_pnl ?? 0) >= 0 ? '#0ECB81' : '#F6465D',
}}
>
{(leader?.total_pnl ?? 0) >= 0 ? '+' : ''}
{leader?.total_pnl_pct?.toFixed(2) || '0.00'}%
</div>
</div>
</div>
{/* Left/Right Split: Performance Chart + Leaderboard */}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-5">
{/* Left: Performance Comparison Chart */}
<div
className="binance-card p-5 animate-slide-in"
style={{ animationDelay: '0.1s' }}
>
<div className="flex items-center justify-between mb-4">
<h2
className="text-lg font-bold flex items-center gap-2"
style={{ color: '#EAECEF' }}
>
{t('performanceComparison', language)}
</h2>
<div className="text-xs" style={{ color: '#848E9C' }}>
{t('realTimePnL', language)}
{t('aiCompetition', language)}
<span
className="text-xs font-normal px-2 py-1 rounded bg-nofx-gold/10 text-nofx-gold border border-nofx-gold/20"
>
{competition.count} {t('traders', language)}
</span>
</h1>
<p className="text-xs text-zinc-400">
{t('liveBattle', language)}
</p>
</div>
</div>
<ComparisonChart traders={sortedTraders.slice(0, 10)} />
</div>
{/* Right: Leaderboard */}
<div
className="binance-card p-5 animate-slide-in"
style={{ animationDelay: '0.1s' }}
>
<div className="flex items-center justify-between mb-4">
<h2
className="text-lg font-bold flex items-center gap-2"
style={{ color: '#EAECEF' }}
>
{t('leaderboard', language)}
</h2>
<div className="text-left md:text-right w-full md:w-auto">
<div className="text-xs mb-1 text-zinc-400">
{t('leader', language)}
</div>
<div
className="text-xs px-2 py-1 rounded"
className="text-base md:text-lg font-bold text-nofx-gold"
>
{leader?.trader_name}
</div>
<div
className="text-sm font-semibold"
style={{
background: 'rgba(240, 185, 11, 0.1)',
color: '#F0B90B',
border: '1px solid rgba(240, 185, 11, 0.2)',
color: (leader?.total_pnl ?? 0) >= 0 ? '#0ECB81' : '#F6465D',
}}
>
{t('live', language)}
{(leader?.total_pnl ?? 0) >= 0 ? '+' : ''}
{leader?.total_pnl_pct?.toFixed(2) || '0.00'}%
</div>
</div>
<div className="space-y-2">
{sortedTraders.map((trader, index) => {
const isLeader = index === 0
const traderColor = getTraderColor(
sortedTraders,
trader.trader_id
)
</div>
return (
<div
key={trader.trader_id}
onClick={() => handleTraderClick(trader.trader_id)}
className="rounded p-3 transition-all duration-300 hover:translate-y-[-1px] cursor-pointer hover:shadow-lg"
style={{
background: isLeader
? 'linear-gradient(135deg, rgba(240, 185, 11, 0.08) 0%, #0B0E11 100%)'
: '#0B0E11',
border: `1px solid ${isLeader ? 'rgba(240, 185, 11, 0.4)' : '#2B3139'}`,
boxShadow: isLeader
? '0 3px 15px rgba(240, 185, 11, 0.12), 0 0 0 1px rgba(240, 185, 11, 0.15)'
: '0 1px 4px rgba(0, 0, 0, 0.3)',
}}
>
<div className="flex items-center justify-between">
{/* Rank & Avatar & Name */}
<div className="flex items-center gap-3">
{/* Rank Badge */}
<div
className="w-6 h-6 rounded-full flex items-center justify-center text-xs font-bold"
style={{
background: index === 0
? 'linear-gradient(135deg, #F0B90B 0%, #FCD535 100%)'
: index === 1
? 'linear-gradient(135deg, #C0C0C0 0%, #E8E8E8 100%)'
: index === 2
? 'linear-gradient(135deg, #CD7F32 0%, #E8A64C 100%)'
: '#2B3139',
color: index < 3 ? '#000' : '#848E9C',
}}
>
{index + 1}
</div>
{/* Punk Avatar */}
<PunkAvatar
seed={getTraderAvatar(trader.trader_id, trader.trader_name)}
size={36}
className="rounded-lg"
/>
<div>
<div
className="font-bold text-sm"
style={{ color: '#EAECEF' }}
>
{trader.trader_name}
</div>
<div
className="text-xs mono font-semibold"
style={{ color: traderColor }}
>
{trader.ai_model.toUpperCase()} +{' '}
{trader.exchange.toUpperCase()}
</div>
</div>
</div>
{/* Left/Right Split: Performance Chart + Leaderboard */}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
{/* Left: Performance Comparison Chart */}
<div
className="bg-black/40 border border-white/10 rounded-xl p-6 backdrop-blur-md animate-slide-in hover:border-white/20 transition-colors"
style={{ animationDelay: '0.1s' }}
>
<div className="flex items-center justify-between mb-6">
<h2
className="text-lg font-bold flex items-center gap-2 text-white"
>
{t('performanceComparison', language)}
</h2>
<div className="text-xs text-zinc-400">
{t('realTimePnL', language)}
</div>
</div>
<ComparisonChart traders={sortedTraders.slice(0, 10)} />
</div>
{/* Stats */}
<div className="flex items-center gap-2 md:gap-3 flex-wrap md:flex-nowrap">
{/* Total Equity */}
<div className="text-right">
<div className="text-xs" style={{ color: '#848E9C' }}>
{t('equity', language)}
</div>
<div
className="text-xs md:text-sm font-bold mono"
style={{ color: '#EAECEF' }}
>
{trader.total_equity?.toFixed(2) || '0.00'}
</div>
</div>
{/* Right: Leaderboard */}
<div
className="bg-black/40 border border-white/10 rounded-xl p-6 backdrop-blur-md animate-slide-in hover:border-white/20 transition-colors"
style={{ animationDelay: '0.1s' }}
>
<div className="flex items-center justify-between mb-6">
<h2
className="text-lg font-bold flex items-center gap-2 text-white"
>
{t('leaderboard', language)}
</h2>
<div
className="text-xs px-2 py-1 rounded bg-nofx-gold/10 text-nofx-gold border border-nofx-gold/20 shadow-[0_0_8px_rgba(240,185,11,0.1)]"
>
{t('live', language)}
</div>
</div>
<div className="space-y-2">
{sortedTraders.map((trader, index) => {
const isLeader = index === 0
const traderColor = getTraderColor(
sortedTraders,
trader.trader_id
)
{/* P&L */}
<div className="text-right min-w-[70px] md:min-w-[90px]">
<div className="text-xs" style={{ color: '#848E9C' }}>
{t('pnl', language)}
</div>
return (
<div
key={trader.trader_id}
onClick={() => handleTraderClick(trader.trader_id)}
className="rounded p-3 transition-all duration-300 hover:translate-y-[-1px] cursor-pointer hover:shadow-lg"
style={{
background: isLeader
? 'linear-gradient(135deg, rgba(240, 185, 11, 0.08) 0%, #0B0E11 100%)'
: '#0B0E11',
border: `1px solid ${isLeader ? 'rgba(240, 185, 11, 0.4)' : '#2B3139'}`,
boxShadow: isLeader
? '0 3px 15px rgba(240, 185, 11, 0.12), 0 0 0 1px rgba(240, 185, 11, 0.15)'
: '0 1px 4px rgba(0, 0, 0, 0.3)',
}}
>
<div className="flex items-center justify-between">
{/* Rank & Avatar & Name */}
<div className="flex items-center gap-3">
{/* Rank Badge */}
<div
className="text-base md:text-lg font-bold mono"
className="w-6 h-6 rounded-full flex items-center justify-center text-xs font-bold"
style={{
color:
(trader.total_pnl ?? 0) >= 0
? '#0ECB81'
: '#F6465D',
background: index === 0
? 'linear-gradient(135deg, #F0B90B 0%, #FCD535 100%)'
: index === 1
? 'linear-gradient(135deg, #C0C0C0 0%, #E8E8E8 100%)'
: index === 2
? 'linear-gradient(135deg, #CD7F32 0%, #E8A64C 100%)'
: '#2B3139',
color: index < 3 ? '#000' : '#848E9C',
}}
>
{(trader.total_pnl ?? 0) >= 0 ? '+' : ''}
{trader.total_pnl_pct?.toFixed(2) || '0.00'}%
{index + 1}
</div>
<div
className="text-xs mono"
style={{ color: '#848E9C' }}
>
{(trader.total_pnl ?? 0) >= 0 ? '+' : ''}
{trader.total_pnl?.toFixed(2) || '0.00'}
{/* Punk Avatar */}
<PunkAvatar
seed={getTraderAvatar(trader.trader_id, trader.trader_name)}
size={36}
className="rounded-lg"
/>
<div>
<div
className="font-bold text-sm"
style={{ color: '#EAECEF' }}
>
{trader.trader_name}
</div>
<div
className="text-xs mono font-semibold"
style={{ color: traderColor }}
>
{trader.ai_model.toUpperCase()} +{' '}
{trader.exchange.toUpperCase()}
</div>
</div>
</div>
{/* Positions */}
<div className="text-right">
<div className="text-xs" style={{ color: '#848E9C' }}>
{t('pos', language)}
{/* Stats */}
<div className="flex items-center gap-2 md:gap-3 flex-wrap md:flex-nowrap">
{/* Total Equity */}
<div className="text-right">
<div className="text-xs" style={{ color: '#848E9C' }}>
{t('equity', language)}
</div>
<div
className="text-xs md:text-sm font-bold mono"
style={{ color: '#EAECEF' }}
>
{trader.total_equity?.toFixed(2) || '0.00'}
</div>
</div>
<div
className="text-xs md:text-sm font-bold mono"
style={{ color: '#EAECEF' }}
>
{trader.position_count}
</div>
<div className="text-xs" style={{ color: '#848E9C' }}>
{trader.margin_used_pct.toFixed(1)}%
</div>
</div>
{/* Status */}
<div>
<div
className="px-2 py-1 rounded text-xs font-bold"
style={
trader.is_running
? {
{/* P&L */}
<div className="text-right min-w-[70px] md:min-w-[90px]">
<div className="text-xs" style={{ color: '#848E9C' }}>
{t('pnl', language)}
</div>
<div
className="text-base md:text-lg font-bold mono"
style={{
color:
(trader.total_pnl ?? 0) >= 0
? '#0ECB81'
: '#F6465D',
}}
>
{(trader.total_pnl ?? 0) >= 0 ? '+' : ''}
{trader.total_pnl_pct?.toFixed(2) || '0.00'}%
</div>
<div
className="text-xs mono"
style={{ color: '#848E9C' }}
>
{(trader.total_pnl ?? 0) >= 0 ? '+' : ''}
{trader.total_pnl?.toFixed(2) || '0.00'}
</div>
</div>
{/* Positions */}
<div className="text-right">
<div className="text-xs" style={{ color: '#848E9C' }}>
{t('pos', language)}
</div>
<div
className="text-xs md:text-sm font-bold mono"
style={{ color: '#EAECEF' }}
>
{trader.position_count}
</div>
<div className="text-xs" style={{ color: '#848E9C' }}>
{trader.margin_used_pct.toFixed(1)}%
</div>
</div>
{/* Status */}
<div>
<div
className="px-2 py-1 rounded text-xs font-bold"
style={
trader.is_running
? {
background: 'rgba(14, 203, 129, 0.1)',
color: '#0ECB81',
}
: {
: {
background: 'rgba(246, 70, 93, 0.1)',
color: '#F6465D',
}
}
>
{trader.is_running ? '●' : '○'}
}
>
{trader.is_running ? '●' : '○'}
</div>
</div>
</div>
</div>
</div>
</div>
)
})}
)
})}
</div>
</div>
</div>
</div>
{/* Head-to-Head Stats */}
{competition.traders.length === 2 && (
<div
className="binance-card p-5 animate-slide-in"
style={{ animationDelay: '0.3s' }}
>
<h2
className="text-lg font-bold mb-4 flex items-center gap-2"
style={{ color: '#EAECEF' }}
{/* Head-to-Head Stats */}
{competition.traders.length === 2 && (
<div
className="bg-black/40 border border-white/10 rounded-xl p-6 backdrop-blur-md animate-slide-in"
style={{ animationDelay: '0.3s' }}
>
{t('headToHead', language)}
</h2>
<div className="grid grid-cols-2 gap-4">
{sortedTraders.map((trader, index) => {
const isWinning = index === 0
const opponent = sortedTraders[1 - index]
<h2
className="text-lg font-bold mb-6 flex items-center gap-2 text-white"
>
{t('headToHead', language)}
</h2>
<div className="grid grid-cols-2 gap-4">
{sortedTraders.map((trader, index) => {
const isWinning = index === 0
const opponent = sortedTraders[1 - index]
// Check if both values are valid numbers
const hasValidData =
trader.total_pnl_pct != null &&
opponent.total_pnl_pct != null &&
!isNaN(trader.total_pnl_pct) &&
!isNaN(opponent.total_pnl_pct)
// Check if both values are valid numbers
const hasValidData =
trader.total_pnl_pct != null &&
opponent.total_pnl_pct != null &&
!isNaN(trader.total_pnl_pct) &&
!isNaN(opponent.total_pnl_pct)
const gap = hasValidData
? trader.total_pnl_pct - opponent.total_pnl_pct
: NaN
const gap = hasValidData
? trader.total_pnl_pct - opponent.total_pnl_pct
: NaN
return (
<div
key={trader.trader_id}
className="p-4 rounded transition-all duration-300 hover:scale-[1.02]"
style={
isWinning
? {
return (
<div
key={trader.trader_id}
className="p-4 rounded transition-all duration-300 hover:scale-[1.02]"
style={
isWinning
? {
background:
'linear-gradient(135deg, rgba(14, 203, 129, 0.08) 0%, rgba(14, 203, 129, 0.02) 100%)',
border: '2px solid rgba(14, 203, 129, 0.3)',
boxShadow: '0 3px 15px rgba(14, 203, 129, 0.12)',
}
: {
: {
background: '#0B0E11',
border: '1px solid #2B3139',
boxShadow: '0 1px 4px rgba(0, 0, 0, 0.3)',
}
}
>
<div className="text-center">
{/* Avatar */}
<div className="flex justify-center mb-3">
<PunkAvatar
seed={getTraderAvatar(trader.trader_id, trader.trader_name)}
size={56}
className="rounded-xl"
/>
</div>
<div
className="text-sm md:text-base font-bold mb-2"
style={{
color: getTraderColor(sortedTraders, trader.trader_id),
}}
>
{trader.trader_name}
</div>
<div
className="text-lg md:text-2xl font-bold mono mb-1"
style={{
color:
(trader.total_pnl ?? 0) >= 0 ? '#0ECB81' : '#F6465D',
}}
>
{trader.total_pnl_pct != null &&
!isNaN(trader.total_pnl_pct)
? `${trader.total_pnl_pct >= 0 ? '+' : ''}${trader.total_pnl_pct.toFixed(2)}%`
: '—'}
</div>
{hasValidData && isWinning && gap > 0 && (
<div
className="text-xs font-semibold"
style={{ color: '#0ECB81' }}
>
{t('leadingBy', language, { gap: gap.toFixed(2) })}
}
>
<div className="text-center">
{/* Avatar */}
<div className="flex justify-center mb-3">
<PunkAvatar
seed={getTraderAvatar(trader.trader_id, trader.trader_name)}
size={56}
className="rounded-xl"
/>
</div>
)}
{hasValidData && !isWinning && gap < 0 && (
<div
className="text-xs font-semibold"
style={{ color: '#F6465D' }}
className="text-sm md:text-base font-bold mb-2"
style={{
color: getTraderColor(sortedTraders, trader.trader_id),
}}
>
{t('behindBy', language, {
gap: Math.abs(gap).toFixed(2),
})}
{trader.trader_name}
</div>
)}
{!hasValidData && (
<div
className="text-xs font-semibold"
style={{ color: '#848E9C' }}
className="text-lg md:text-2xl font-bold mono mb-1"
style={{
color:
(trader.total_pnl ?? 0) >= 0 ? '#0ECB81' : '#F6465D',
}}
>
{trader.total_pnl_pct != null &&
!isNaN(trader.total_pnl_pct)
? `${trader.total_pnl_pct >= 0 ? '+' : ''}${trader.total_pnl_pct.toFixed(2)}%`
: '—'}
</div>
)}
{hasValidData && isWinning && gap > 0 && (
<div
className="text-xs font-semibold"
style={{ color: '#0ECB81' }}
>
{t('leadingBy', language, { gap: gap.toFixed(2) })}
</div>
)}
{hasValidData && !isWinning && gap < 0 && (
<div
className="text-xs font-semibold"
style={{ color: '#F6465D' }}
>
{t('behindBy', language, {
gap: Math.abs(gap).toFixed(2),
})}
</div>
)}
{!hasValidData && (
<div
className="text-xs font-semibold"
style={{ color: '#848E9C' }}
>
</div>
)}
</div>
</div>
</div>
)
})}
)
})}
</div>
</div>
</div>
)}
)}
{/* Trader Config View Modal */}
<TraderConfigViewModal
isOpen={isModalOpen}
onClose={closeModal}
traderData={selectedTrader}
/>
</div>
{/* Trader Config View Modal */}
<TraderConfigViewModal
isOpen={isModalOpen}
onClose={closeModal}
traderData={selectedTrader}
/>
</div>
</DeepVoidBackground>
)
}

View File

@@ -0,0 +1,40 @@
import React from 'react'
interface DeepVoidBackgroundProps extends React.HTMLAttributes<HTMLDivElement> {
children?: React.ReactNode
className?: string
disableAnimation?: boolean
}
export function DeepVoidBackground({ children, className = '', disableAnimation = false, ...props }: DeepVoidBackgroundProps) {
return (
<div className={`relative w-full min-h-screen bg-nofx-bg text-nofx-text overflow-hidden flex flex-col ${className}`} {...props}>
{/* BACKGROUND LAYERS */}
{/* 1. Grain/Noise Texture */}
<div className="absolute inset-0 bg-[url('https://grainy-gradients.vercel.app/noise.svg')] opacity-20 mix-blend-soft-light pointer-events-none fixed z-0"></div>
{/* 2. Grid System */}
<div className="absolute inset-0 pointer-events-none fixed z-0">
<div className="absolute inset-x-0 bottom-0 h-[50vh] bg-[linear-gradient(to_right,#80808012_1px,transparent_1px),linear-gradient(to_bottom,#80808012_1px,transparent_1px)] bg-[size:40px_40px] [mask-image:radial-gradient(ellipse_60%_50%_at_50%_0%,#000_70%,transparent_100%)] opacity-50" style={{ transform: 'perspective(500px) rotateX(60deg) translateY(100px) scale(2)' }}></div>
<div className="absolute inset-0 bg-grid-pattern opacity-[0.03]"></div>
</div>
{/* 3. Ambient Glow Spots */}
<div className="absolute inset-0 overflow-hidden pointer-events-none fixed z-0">
<div className="absolute top-[-10%] left-[-10%] w-[40vw] h-[40vw] bg-nofx-gold/10 rounded-full blur-[120px] mix-blend-screen animate-pulse-slow"></div>
<div className="absolute bottom-[-10%] right-[-10%] w-[40vw] h-[40vw] bg-nofx-accent/5 rounded-full blur-[120px] mix-blend-screen animate-pulse-slow" style={{ animationDelay: '2s' }}></div>
</div>
{/* 4. CRT/Scanline Overlay */}
<div className="absolute inset-0 pointer-events-none fixed z-[9999] opacity-40">
<div className="absolute inset-0 bg-[linear-gradient(rgba(18,16,16,0)_50%,rgba(0,0,0,0.25)_50%),linear-gradient(90deg,rgba(255,0,0,0.06),rgba(0,255,0,0.02),rgba(0,0,255,0.06))] bg-[length:100%_4px,3px_100%] pointer-events-none"></div>
</div>
{/* Content Layer */}
<div className="relative z-10 flex-1 flex flex-col h-full w-full">
{children}
</div>
</div>
)
}

View File

@@ -1,6 +1,6 @@
import { useState, useEffect, useRef } from 'react'
import { useNavigate } from 'react-router-dom'
import { motion } from 'framer-motion'
import { motion, AnimatePresence } from 'framer-motion'
import { Menu, X, ChevronDown } from 'lucide-react'
import { t, type Language } from '../i18n/translations'
import { useSystemConfig } from '../hooks/useSystemConfig'
@@ -85,10 +85,7 @@ export default function HeaderBar({
className="flex items-center gap-2 hover:opacity-80 transition-opacity cursor-pointer"
>
<img src="/icons/nofx.svg" alt="NOFX Logo" className="w-7 h-7" />
<span
className="text-lg font-bold"
style={{ color: 'var(--brand-yellow)' }}
>
<span className="text-lg font-bold text-nofx-gold">
NOFX
</span>
</div>
@@ -101,11 +98,11 @@ export default function HeaderBar({
{(() => {
// Define all navigation tabs
const navTabs: { page: Page; path: string; label: string; requiresAuth: boolean }[] = [
{ page: 'competition', path: '/competition', label: t('realtimeNav', language), requiresAuth: true },
{ page: 'strategy-market', path: '/strategy-market', label: language === 'zh' ? '策略市场' : 'Market', requiresAuth: true },
{ page: 'traders', path: '/traders', label: t('configNav', language), requiresAuth: true },
{ page: 'trader', path: '/dashboard', label: t('dashboardNav', language), requiresAuth: true },
{ page: 'strategy', path: '/strategy', label: t('strategyNav', language), requiresAuth: true },
{ page: 'competition', path: '/competition', label: t('realtimeNav', language), requiresAuth: true },
{ page: 'debate', path: '/debate', label: t('debateNav', language), requiresAuth: true },
{ page: 'backtest', path: '/backtest', label: 'Backtest', requiresAuth: true },
{ page: 'faq', path: '/faq', label: t('faqNav', language), requiresAuth: false },
@@ -128,28 +125,12 @@ export default function HeaderBar({
<button
key={tab.page}
onClick={() => handleNavClick(tab)}
className="text-sm font-bold transition-all duration-300 relative focus:outline-2 focus:outline-yellow-500"
style={{
color: currentPage === tab.page ? 'var(--brand-yellow)' : 'var(--brand-light-gray)',
padding: '8px 12px',
borderRadius: '8px',
position: 'relative',
}}
onMouseEnter={(e) => {
if (currentPage !== tab.page) {
e.currentTarget.style.color = 'var(--brand-yellow)'
}
}}
onMouseLeave={(e) => {
if (currentPage !== tab.page) {
e.currentTarget.style.color = 'var(--brand-light-gray)'
}
}}
className={`text-sm font-bold transition-all duration-300 relative focus:outline-2 focus:outline-yellow-500 px-3 py-2 rounded-lg
${currentPage === tab.page ? 'text-nofx-gold' : 'text-nofx-text-muted hover:text-nofx-gold'}`}
>
{currentPage === tab.page && (
<span
className="absolute inset-0 rounded-lg"
style={{ background: 'rgba(240, 185, 11, 0.15)', zIndex: -1 }}
className="absolute inset-0 rounded-lg bg-nofx-gold/15 -z-10"
/>
)}
{tab.label}
@@ -167,16 +148,7 @@ export default function HeaderBar({
href={OFFICIAL_LINKS.github}
target="_blank"
rel="noopener noreferrer"
className="p-2 rounded-lg transition-all hover:scale-110"
style={{ color: '#848E9C' }}
onMouseEnter={(e) => {
e.currentTarget.style.color = '#EAECEF'
e.currentTarget.style.background = 'rgba(255, 255, 255, 0.05)'
}}
onMouseLeave={(e) => {
e.currentTarget.style.color = '#848E9C'
e.currentTarget.style.background = 'transparent'
}}
className="p-2 rounded-lg transition-all hover:scale-110 text-nofx-text-muted hover:text-white hover:bg-white/5"
title="GitHub"
>
<svg width="18" height="18" viewBox="0 0 16 16" fill="currentColor">
@@ -188,16 +160,7 @@ export default function HeaderBar({
href={OFFICIAL_LINKS.twitter}
target="_blank"
rel="noopener noreferrer"
className="p-2 rounded-lg transition-all hover:scale-110"
style={{ color: '#848E9C' }}
onMouseEnter={(e) => {
e.currentTarget.style.color = '#1DA1F2'
e.currentTarget.style.background = 'rgba(29, 161, 242, 0.1)'
}}
onMouseLeave={(e) => {
e.currentTarget.style.color = '#848E9C'
e.currentTarget.style.background = 'transparent'
}}
className="p-2 rounded-lg transition-all hover:scale-110 text-nofx-text-muted hover:text-[#1DA1F2] hover:bg-[#1DA1F2]/10"
title="Twitter"
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
@@ -209,16 +172,7 @@ export default function HeaderBar({
href={OFFICIAL_LINKS.telegram}
target="_blank"
rel="noopener noreferrer"
className="p-2 rounded-lg transition-all hover:scale-110"
style={{ color: '#848E9C' }}
onMouseEnter={(e) => {
e.currentTarget.style.color = '#0088cc'
e.currentTarget.style.background = 'rgba(0, 136, 204, 0.1)'
}}
onMouseLeave={(e) => {
e.currentTarget.style.color = '#848E9C'
e.currentTarget.style.background = 'transparent'
}}
className="p-2 rounded-lg transition-all hover:scale-110 text-nofx-text-muted hover:text-[#0088cc] hover:bg-[#0088cc]/10"
title="Telegram"
>
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
@@ -237,62 +191,24 @@ export default function HeaderBar({
<div className="relative" ref={userDropdownRef}>
<button
onClick={() => setUserDropdownOpen(!userDropdownOpen)}
className="flex items-center gap-2 px-3 py-2 rounded transition-colors"
style={{
background: 'var(--panel-bg)',
border: '1px solid var(--panel-border)',
}}
onMouseEnter={(e) =>
(e.currentTarget.style.background =
'rgba(255, 255, 255, 0.05)')
}
onMouseLeave={(e) =>
(e.currentTarget.style.background = 'var(--panel-bg)')
}
className="flex items-center gap-2 px-3 py-2 rounded transition-colors bg-nofx-bg-lighter border border-nofx-gold/20 hover:bg-white/5"
>
<div
className="w-6 h-6 rounded-full flex items-center justify-center text-xs font-bold"
style={{
background: 'var(--brand-yellow)',
color: 'var(--brand-black)',
}}
>
<div className="w-6 h-6 rounded-full flex items-center justify-center text-xs font-bold bg-nofx-gold text-black">
{user.email[0].toUpperCase()}
</div>
<span
className="text-sm"
style={{ color: 'var(--brand-light-gray)' }}
>
<span className="text-sm text-nofx-text-muted">
{user.email}
</span>
<ChevronDown
className="w-4 h-4"
style={{ color: 'var(--brand-light-gray)' }}
/>
<ChevronDown className="w-4 h-4 text-nofx-text-muted" />
</button>
{userDropdownOpen && (
<div
className="absolute right-0 top-full mt-2 w-48 rounded-lg shadow-lg overflow-hidden z-50"
style={{
background: 'var(--brand-dark-gray)',
border: '1px solid var(--panel-border)',
}}
>
<div
className="px-3 py-2 border-b"
style={{ borderColor: 'var(--panel-border)' }}
>
<div
className="text-xs"
style={{ color: 'var(--text-secondary)' }}
>
<div className="absolute right-0 top-full mt-2 w-48 rounded-lg shadow-lg overflow-hidden z-50 bg-nofx-bg-lighter border border-nofx-gold/20">
<div className="px-3 py-2 border-b border-nofx-gold/20">
<div className="text-xs text-nofx-text-muted">
{t('loggedInAs', language)}
</div>
<div
className="text-sm font-medium"
style={{ color: 'var(--brand-light-gray)' }}
>
<div className="text-sm font-medium text-nofx-text-muted">
{user.email}
</div>
</div>
@@ -302,11 +218,7 @@ export default function HeaderBar({
onLogout()
setUserDropdownOpen(false)
}}
className="w-full px-3 py-2 text-sm font-semibold transition-colors hover:opacity-80 text-center"
style={{
background: 'var(--binance-red-bg)',
color: 'var(--binance-red)',
}}
className="w-full px-3 py-2 text-sm font-semibold transition-colors hover:opacity-80 text-center bg-nofx-danger/20 text-nofx-danger"
>
{t('exitLogin', language)}
</button>
@@ -322,19 +234,14 @@ export default function HeaderBar({
<div className="flex items-center gap-3">
<a
href="/login"
className="px-3 py-2 text-sm font-medium transition-colors rounded"
style={{ color: 'var(--brand-light-gray)' }}
className="px-3 py-2 text-sm font-medium transition-colors rounded text-nofx-text-muted hover:text-white"
>
{t('signIn', language)}
</a>
{registrationEnabled && (
<a
href="/register"
className="px-4 py-2 rounded font-semibold text-sm transition-colors hover:opacity-90"
style={{
background: 'var(--brand-yellow)',
color: 'var(--brand-black)',
}}
className="px-4 py-2 rounded font-semibold text-sm transition-colors hover:opacity-90 bg-nofx-gold text-black"
>
{t('signUp', language)}
</a>
@@ -347,15 +254,7 @@ export default function HeaderBar({
<div className="relative" ref={dropdownRef}>
<button
onClick={() => setLanguageDropdownOpen(!languageDropdownOpen)}
className="flex items-center gap-2 px-3 py-2 rounded transition-colors"
style={{ color: 'var(--brand-light-gray)' }}
onMouseEnter={(e) =>
(e.currentTarget.style.background =
'rgba(255, 255, 255, 0.05)')
}
onMouseLeave={(e) =>
(e.currentTarget.style.background = 'transparent')
}
className="flex items-center gap-2 px-3 py-2 rounded transition-colors text-nofx-text-muted hover:bg-white/5"
>
<span className="text-lg">
{language === 'zh' ? '🇨🇳' : '🇺🇸'}
@@ -364,28 +263,14 @@ export default function HeaderBar({
</button>
{languageDropdownOpen && (
<div
className="absolute right-0 top-full mt-2 w-32 rounded-lg shadow-lg overflow-hidden z-50"
style={{
background: 'var(--brand-dark-gray)',
border: '1px solid var(--panel-border)',
}}
>
<div className="absolute right-0 top-full mt-2 w-32 rounded-lg shadow-lg overflow-hidden z-50 bg-nofx-bg-lighter border border-nofx-gold/20">
<button
onClick={() => {
onLanguageChange?.('zh')
setLanguageDropdownOpen(false)
}}
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors ${
language === 'zh' ? '' : 'hover:opacity-80'
}`}
style={{
color: 'var(--brand-light-gray)',
background:
language === 'zh'
? 'rgba(240, 185, 11, 0.1)'
: 'transparent',
}}
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-nofx-text-muted hover:text-white
${language === 'zh' ? 'bg-nofx-gold/10' : 'hover:bg-white/5'}`}
>
<span className="text-base">🇨🇳</span>
<span className="text-sm"></span>
@@ -395,16 +280,8 @@ export default function HeaderBar({
onLanguageChange?.('en')
setLanguageDropdownOpen(false)
}}
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors ${
language === 'en' ? '' : 'hover:opacity-80'
}`}
style={{
color: 'var(--brand-light-gray)',
background:
language === 'en'
? 'rgba(240, 185, 11, 0.1)'
: 'transparent',
}}
className={`w-full flex items-center gap-2 px-3 py-2 transition-colors text-nofx-text-muted hover:text-white
${language === 'en' ? 'bg-nofx-gold/10' : 'hover:bg-white/5'}`}
>
<span className="text-base">🇺🇸</span>
<span className="text-sm">English</span>
@@ -418,8 +295,7 @@ export default function HeaderBar({
{/* Mobile Menu Button */}
<motion.button
onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
className="md:hidden"
style={{ color: 'var(--brand-light-gray)' }}
className="md:hidden text-nofx-text-muted hover:text-white"
whileTap={{ scale: 0.9 }}
>
{mobileMenuOpen ? (
@@ -430,258 +306,170 @@ export default function HeaderBar({
</motion.button>
</div>
{/* Mobile Menu */}
<motion.div
initial={false}
animate={
mobileMenuOpen
? { height: 'auto', opacity: 1 }
: { height: 0, opacity: 0 }
}
transition={{ duration: 0.3 }}
className="md:hidden overflow-hidden"
style={{
background: 'var(--brand-dark-gray)',
borderTop: '1px solid rgba(240, 185, 11, 0.1)',
}}
>
<div className="px-4 py-4 space-y-2">
{/* Mobile Navigation Tabs - Show all tabs */}
{(() => {
const navTabs: { page: Page; path: string; label: string; requiresAuth: boolean }[] = [
{ page: 'competition', path: '/competition', label: t('realtimeNav', language), requiresAuth: true },
{ page: 'strategy-market', path: '/strategy-market', label: language === 'zh' ? '策略市场' : 'Market', requiresAuth: true },
{ page: 'traders', path: '/traders', label: t('configNav', language), requiresAuth: true },
{ page: 'trader', path: '/dashboard', label: t('dashboardNav', language), requiresAuth: true },
{ page: 'strategy', path: '/strategy', label: t('strategyNav', language), requiresAuth: true },
{ page: 'debate', path: '/debate', label: t('debateNav', language), requiresAuth: true },
{ page: 'backtest', path: '/backtest', label: 'Backtest', requiresAuth: true },
{ page: 'faq', path: '/faq', label: t('faqNav', language), requiresAuth: false },
]
const handleMobileNavClick = (tab: typeof navTabs[0]) => {
if (tab.requiresAuth && !isLoggedIn) {
onLoginRequired?.(tab.label)
setMobileMenuOpen(false)
return
}
if (onPageChange) {
onPageChange(tab.page)
}
navigate(tab.path)
setMobileMenuOpen(false)
}
return navTabs.map((tab) => (
<button
key={tab.page}
onClick={() => handleMobileNavClick(tab)}
className="block text-sm font-bold transition-all duration-300 relative focus:outline-2 focus:outline-yellow-500"
style={{
color: currentPage === tab.page ? 'var(--brand-yellow)' : 'var(--brand-light-gray)',
padding: '12px 16px',
borderRadius: '8px',
position: 'relative',
width: '100%',
textAlign: 'left',
}}
>
{currentPage === tab.page && (
<span
className="absolute inset-0 rounded-lg"
style={{ background: 'rgba(240, 185, 11, 0.15)', zIndex: -1 }}
/>
)}
{tab.label}
{tab.requiresAuth && !isLoggedIn && (
<span className="ml-2 text-[10px] px-1.5 py-0.5 rounded" style={{ background: 'rgba(240, 185, 11, 0.2)', color: '#F0B90B' }}>
{language === 'zh' ? '需登录' : 'Login'}
</span>
)}
</button>
))
})()}
{/* Original Navigation Items - Only on home page */}
{isHomePage &&
[
{ key: 'features', label: t('features', language) },
{ key: 'howItWorks', label: t('howItWorks', language) },
].map((item) => (
<a
key={item.key}
href={`#${item.key === 'features' ? 'features' : 'how-it-works'}`}
className="block text-sm py-2"
style={{ color: 'var(--brand-light-gray)' }}
>
{item.label}
</a>
))}
{/* Social Links - Mobile */}
<div className="py-3 flex items-center gap-3" style={{ borderTop: '1px solid #2B3139' }}>
<a
href={OFFICIAL_LINKS.github}
target="_blank"
rel="noopener noreferrer"
className="p-2 rounded-lg"
style={{ color: '#848E9C', background: 'rgba(255, 255, 255, 0.05)' }}
{/* Mobile Menu Overlay */}
<AnimatePresence>
{mobileMenuOpen && (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
className="fixed inset-0 z-40 md:hidden bg-black/90 backdrop-blur-xl"
style={{ top: '64px' }} // Below header
>
<motion.div
initial={{ y: -20, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{ delay: 0.1, duration: 0.3 }}
className="flex flex-col h-[calc(100vh-64px)] overflow-y-auto px-6 py-8"
>
<svg width="20" height="20" viewBox="0 0 16 16" fill="currentColor">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z" />
</svg>
</a>
<a
href={OFFICIAL_LINKS.twitter}
target="_blank"
rel="noopener noreferrer"
className="p-2 rounded-lg"
style={{ color: '#848E9C', background: 'rgba(255, 255, 255, 0.05)' }}
>
<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
<path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z" />
</svg>
</a>
<a
href={OFFICIAL_LINKS.telegram}
target="_blank"
rel="noopener noreferrer"
className="p-2 rounded-lg"
style={{ color: '#848E9C', background: 'rgba(255, 255, 255, 0.05)' }}
>
<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
<path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z" />
</svg>
</a>
</div>
{/* Navigation Links */}
<div className="flex flex-col gap-6 mb-12">
{(() => {
const navTabs: { page: Page; path: string; label: string; requiresAuth: boolean }[] = [
{ page: 'strategy-market', path: '/strategy-market', label: language === 'zh' ? '策略市场' : 'Market', requiresAuth: true },
{ page: 'traders', path: '/traders', label: t('configNav', language), requiresAuth: true },
{ page: 'trader', path: '/dashboard', label: t('dashboardNav', language), requiresAuth: true },
{ page: 'strategy', path: '/strategy', label: t('strategyNav', language), requiresAuth: true },
{ page: 'competition', path: '/competition', label: t('realtimeNav', language), requiresAuth: true },
{ page: 'debate', path: '/debate', label: t('debateNav', language), requiresAuth: true },
{ page: 'backtest', path: '/backtest', label: 'Backtest', requiresAuth: true },
{ page: 'faq', path: '/faq', label: t('faqNav', language), requiresAuth: false },
]
{/* Language Toggle */}
<div className="py-2">
<div className="flex items-center gap-2 mb-2">
<span
className="text-xs"
style={{ color: 'var(--brand-light-gray)' }}
>
{t('language', language)}:
</span>
</div>
<div className="space-y-1">
<button
onClick={() => {
onLanguageChange?.('zh')
setMobileMenuOpen(false)
}}
className={`w-full flex items-center gap-3 px-3 py-2 rounded transition-colors ${
language === 'zh'
? 'bg-yellow-500 text-black'
: 'text-gray-400 hover:text-white'
}`}
>
<span className="text-lg">🇨🇳</span>
<span className="text-sm"></span>
</button>
<button
onClick={() => {
onLanguageChange?.('en')
setMobileMenuOpen(false)
}}
className={`w-full flex items-center gap-3 px-3 py-2 rounded transition-colors ${
language === 'en'
? 'bg-yellow-500 text-black'
: 'text-gray-400 hover:text-white'
}`}
>
<span className="text-lg">🇺🇸</span>
<span className="text-sm">English</span>
</button>
</div>
</div>
{/* User info and logout for mobile when logged in */}
{isLoggedIn && user && (
<div
className="mt-4 pt-4"
style={{ borderTop: '1px solid var(--panel-border)' }}
>
<div
className="flex items-center gap-2 px-3 py-2 mb-2 rounded"
style={{ background: 'var(--panel-bg)' }}
>
<div
className="w-6 h-6 rounded-full flex items-center justify-center text-xs font-bold"
style={{
background: 'var(--brand-yellow)',
color: 'var(--brand-black)',
}}
>
{user.email[0].toUpperCase()}
</div>
<div>
<div
className="text-xs"
style={{ color: 'var(--text-secondary)' }}
>
{t('loggedInAs', language)}
</div>
<div
className="text-sm"
style={{ color: 'var(--brand-light-gray)' }}
>
{user.email}
</div>
</div>
</div>
{onLogout && (
<button
onClick={() => {
onLogout()
const handleMobileNavClick = (tab: typeof navTabs[0]) => {
if (tab.requiresAuth && !isLoggedIn) {
onLoginRequired?.(tab.label)
setMobileMenuOpen(false)
return
}
if (onPageChange) {
onPageChange(tab.page)
}
navigate(tab.path)
setMobileMenuOpen(false)
}}
className="w-full px-4 py-2 rounded text-sm font-semibold transition-colors text-center"
style={{
background: 'var(--binance-red-bg)',
color: 'var(--binance-red)',
}}
>
{t('exitLogin', language)}
</button>
)}
</div>
)}
}
{/* Show login/register buttons when not logged in and not on login/register pages */}
{!isLoggedIn &&
currentPage !== 'login' &&
currentPage !== 'register' && (
<div className="space-y-2 mt-2">
<a
href="/login"
className="block w-full px-4 py-2 rounded text-sm font-medium text-center transition-colors"
style={{
color: 'var(--brand-light-gray)',
border: '1px solid var(--brand-light-gray)',
}}
onClick={() => setMobileMenuOpen(false)}
>
{t('signIn', language)}
</a>
{registrationEnabled && (
<a
href="/register"
className="block w-full px-4 py-2 rounded font-semibold text-sm text-center transition-colors"
style={{
background: 'var(--brand-yellow)',
color: 'var(--brand-black)',
}}
onClick={() => setMobileMenuOpen(false)}
>
{t('signUp', language)}
</a>
return navTabs.map((tab, i) => (
<motion.button
key={tab.page}
initial={{ x: -20, opacity: 0 }}
animate={{ x: 0, opacity: 1 }}
transition={{ delay: 0.1 + i * 0.05 }}
onClick={() => handleMobileNavClick(tab)}
className={`text-2xl font-black tracking-tight text-left flex items-center gap-3
${currentPage === tab.page ? 'text-nofx-gold' : 'text-zinc-500'}`}
>
{currentPage === tab.page && (
<motion.div
layoutId="active-indicator"
className="w-1.5 h-1.5 rounded-full bg-nofx-gold"
/>
)}
{tab.label}
{tab.requiresAuth && !isLoggedIn && (
<span className="text-[10px] px-1.5 py-0.5 rounded border border-zinc-800 text-zinc-500 font-normal tracking-wide uppercase align-middle relative -top-1">
LOGIN_REQ
</span>
)}
</motion.button>
))
})()}
{/* Original Page Links */}
{isHomePage && (
<div className="pt-6 border-t border-white/5 space-y-4">
{[
{ key: 'features', label: t('features', language) },
{ key: 'howItWorks', label: t('howItWorks', language) },
].map((item, i) => (
<motion.a
key={item.key}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.5 + i * 0.1 }}
href={`#${item.key === 'features' ? 'features' : 'how-it-works'}`}
className="block text-lg font-mono text-zinc-600 hover:text-white"
onClick={() => setMobileMenuOpen(false)}
>
{'>'} {item.label}
</motion.a>
))}
</div>
)}
</div>
)}
</div>
</motion.div>
{/* Bottom Actions */}
<div className="mt-auto space-y-8">
{/* Social Links */}
<div className="flex items-center gap-4">
{[
{ href: OFFICIAL_LINKS.github, icon: <path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z" /> },
{ href: OFFICIAL_LINKS.twitter, icon: <path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z" /> },
{ href: OFFICIAL_LINKS.telegram, icon: <path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z" /> }
].map((link, i) => (
<a
key={i}
href={link.href}
target="_blank"
rel="noopener noreferrer"
className="w-12 h-12 rounded-full bg-zinc-900 border border-zinc-800 flex items-center justify-center text-zinc-500 hover:text-nofx-gold hover:border-nofx-gold transition-colors"
>
<svg width="20" height="20" viewBox="0 0 16 16" fill="currentColor">
{link.icon}
</svg>
</a>
))}
</div>
{/* Account / Lang */}
<div className="grid grid-cols-2 gap-4">
{/* Lang Switcher */}
<div className="flex bg-zinc-900 rounded-lg p-1 border border-zinc-800">
{['zh', 'en'].map((lang) => (
<button
key={lang}
onClick={() => {
onLanguageChange?.(lang as Language)
setMobileMenuOpen(false)
}}
className={`flex-1 py-3 text-sm font-bold rounded-md transition-colors ${language === lang
? 'bg-zinc-800 text-white shadow-sm'
: 'text-zinc-500'
}`}
>
{lang === 'zh' ? 'CN' : 'EN'}
</button>
))}
</div>
{/* Auth Actions */}
{isLoggedIn && user ? (
<button
onClick={() => {
onLogout?.()
setMobileMenuOpen(false)
}}
className="bg-red-500/10 border border-red-500/20 text-red-500 rounded-lg font-bold text-sm hover:bg-red-500/20 transition-colors"
>
{t('exitLogin', language)}
</button>
) : (
currentPage !== 'login' && currentPage !== 'register' && (
<a
href="/login"
className="flex items-center justify-center bg-nofx-gold text-black rounded-lg font-bold text-sm hover:bg-yellow-400 transition-colors"
>
{t('signIn', language)}
</a>
)
)}
</div>
</div>
</motion.div>
</motion.div>
)}
</AnimatePresence>
</nav>
)
}

View File

@@ -3,19 +3,22 @@ import { useAuth } from '../contexts/AuthContext'
import { useLanguage } from '../contexts/LanguageContext'
import { t } from '../i18n/translations'
import { Eye, EyeOff } from 'lucide-react'
import { DeepVoidBackground } from './DeepVoidBackground'
// import { Input } from './ui/input' // Removed unused import
import { toast } from 'sonner'
import { useSystemConfig } from '../hooks/useSystemConfig'
export function LoginPage() {
const { language } = useLanguage()
const { login, loginAdmin, verifyOTP } = useAuth()
const [step, setStep] = useState<'login' | 'otp'>('login')
const { login, loginAdmin, verifyOTP, completeRegistration } = useAuth()
const [step, setStep] = useState<'login' | 'otp' | 'setup-otp'>('login')
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [showPassword, setShowPassword] = useState(false)
const [otpCode, setOtpCode] = useState('')
const [userID, setUserID] = useState('')
const [qrCodeURL, setQrCodeURL] = useState('') // New state for recovery
const [otpSecret, setOtpSecret] = useState('') // New state for recovery
const [error, setError] = useState('')
const [loading, setLoading] = useState(false)
const [adminPassword, setAdminPassword] = useState('')
@@ -61,9 +64,25 @@ export function LoginPage() {
const result = await login(email, password)
if (result.success) {
if (result.requiresOTP && result.userID) {
// Check for incomplete OTP setup (user registered but didn't complete 2FA)
if (result.requiresOTPSetup && result.userID) {
setUserID(result.userID)
setStep('otp')
setQrCodeURL(result.qrCodeURL || '')
setOtpSecret(result.otpSecret || '')
setStep('setup-otp')
toast.info("Pending 2FA setup detected. Please complete configuration.")
} else if (result.requiresOTP && result.userID) {
setUserID(result.userID)
// Check if backend provided recovery data (meaning 2FA is pending setup)
if (result.qrCodeURL) {
setQrCodeURL(result.qrCodeURL)
setOtpSecret(result.otpSecret || '')
setStep('setup-otp')
toast.info("Pending 2FA setup detected. Please complete configuration.")
} else {
setStep('otp')
}
} else {
// Dismiss the "login expired" toast on successful login (no OTP required)
if (expiredToastId) {
@@ -71,9 +90,18 @@ export function LoginPage() {
}
}
} else {
const msg = result.message || t('loginFailed', language)
setError(msg)
toast.error(msg)
// Check if we have recovery data despite the error (e.g. "Account has not completed OTP setup")
if (result.qrCodeURL) {
setUserID(result.userID || '') // We might need to ensure userID is returned in error case too, or derived
setQrCodeURL(result.qrCodeURL)
setOtpSecret(result.otpSecret || '')
setStep('setup-otp')
toast.warning(t('completeGapSetup', language) || "Incomplete setup detected. Please configure 2FA.")
} else {
const msg = result.message || t('loginFailed', language)
setError(msg)
toast.error(msg)
}
}
setLoading(false)
@@ -84,7 +112,11 @@ export function LoginPage() {
setError('')
setLoading(true)
const result = await verifyOTP(userID, otpCode)
// If we have qrCodeURL, it means user needs to complete registration (first time OTP setup)
// Otherwise, it's a normal login OTP verification
const result = qrCodeURL
? await completeRegistration(userID, otpCode)
: await verifyOTP(userID, otpCode)
if (!result.success) {
const msg = result.message || t('verificationFailed', language)
@@ -95,20 +127,22 @@ export function LoginPage() {
if (expiredToastId) {
toast.dismiss(expiredToastId)
}
// Clear qrCodeURL after successful completion
setQrCodeURL('')
setOtpSecret('')
}
// 成功的话AuthContext会自动处理登录状态
setLoading(false)
}
return (
<div className="min-h-screen bg-black text-zinc-300 font-mono relative overflow-hidden flex items-center justify-center py-12">
{/* Background Effects */}
<div className="absolute inset-0 bg-[linear-gradient(to_right,#80808012_1px,transparent_1px),linear-gradient(to_bottom,#80808012_1px,transparent_1px)] bg-[size:24px_24px] pointer-events-none"></div>
<div className="absolute inset-0 bg-gradient-to-t from-black via-transparent to-transparent pointer-events-none"></div>
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text)
toast.success('Copied to clipboard')
}
{/* Scanline Effect */}
<div className="absolute inset-0 pointer-events-none opacity-[0.03] bg-[linear-gradient(transparent_50%,rgba(0,0,0,0.5)_50%)] bg-[length:100%_4px]"></div>
return (
<DeepVoidBackground className="min-h-screen flex items-center justify-center py-12 font-mono" disableAnimation>
<div className="w-full max-w-md relative z-10 px-6">
{/* Navigation - Top Bar (Mobile/Desktop Friendly) */}
@@ -207,6 +241,66 @@ export function LoginPage() {
{loading ? '> VERIFYING...' : '> EXECUTE_LOGIN'}
</button>
</form>
) : step === 'setup-otp' ? (
<div className="space-y-6">
<div className="text-center bg-zinc-900/50 p-4 rounded border border-zinc-800">
<div className="text-xs font-mono text-zinc-400 mb-2">COMPLETE 2FA CONFIGURATION</div>
{qrCodeURL ? (
<div className="bg-white p-2 rounded inline-block shadow-[0_0_30px_rgba(255,255,255,0.1)]">
<img
src={`https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${encodeURIComponent(`otpauth://totp/NoFX:${email}?secret=${otpSecret}&issuer=NoFX`)}`}
alt="QR Code"
className="w-32 h-32"
/>
</div>
) : (
<div className="w-32 h-32 bg-zinc-800 animate-pulse rounded inline-block"></div>
)}
<div className="mt-4">
<p className="text-[10px] text-zinc-500 uppercase tracking-widest mb-1">Backup Secret Key</p>
<div className="flex items-center gap-2 justify-center bg-black/50 p-2 rounded border border-zinc-700/50 max-w-[200px] mx-auto">
<code className="text-xs font-mono text-nofx-gold">{otpSecret}</code>
<button
onClick={() => copyToClipboard(otpSecret)}
className="text-zinc-500 hover:text-white transition-colors"
>
<span className="text-[10px] uppercase border border-zinc-700 px-1 rounded">Copy</span>
</button>
</div>
</div>
</div>
<div className="space-y-4 font-mono text-xs text-zinc-400 bg-black/20 p-4 rounded border border-zinc-800/50">
<div className="flex gap-3 items-start">
<span className="text-nofx-gold font-bold mt-0.5">01</span>
<div>
<p className="font-bold text-white mb-1">Install Authenticator App</p>
<p className="mb-2">Recommended: <span className="text-nofx-gold">Google Authenticator</span>.</p>
<div className="flex gap-2">
<span className="px-1.5 py-0.5 bg-zinc-800 rounded text-[10px] text-zinc-300 border border-zinc-700">iOS</span>
<span className="px-1.5 py-0.5 bg-zinc-800 rounded text-[10px] text-zinc-300 border border-zinc-700">Android</span>
</div>
</div>
</div>
<div className="w-full h-px bg-zinc-800/50"></div>
<div className="flex gap-3 items-start">
<span className="text-nofx-gold font-bold mt-0.5">02</span>
<div>
<p className="font-bold text-white mb-1">Scan & Verify</p>
<p>Scan code above, then enter the 6-digit token below to activate your account.</p>
</div>
</div>
</div>
<button
onClick={() => setStep('otp')}
className="w-full bg-nofx-gold text-black font-bold py-3 px-4 rounded text-sm tracking-wide uppercase hover:bg-yellow-400 transition-colors font-mono shadow-lg"
>
I HAVE SCANNED THE CODE
</button>
</div>
) : step === 'login' ? (
<form onSubmit={handleLogin} className="space-y-5">
<div className="space-y-4">
@@ -361,6 +455,6 @@ export function LoginPage() {
</div>
)}
</div>
</div>
</DeepVoidBackground>
)
}

View File

@@ -1,5 +1,6 @@
import { motion, AnimatePresence } from 'framer-motion'
import { LogIn, UserPlus, X, AlertTriangle, Terminal } from 'lucide-react'
import { DeepVoidBackground } from './DeepVoidBackground'
import { useLanguage } from '../contexts/LanguageContext'
interface LoginRequiredOverlayProps {
@@ -51,111 +52,114 @@ export function LoginRequiredOverlay({ isOpen, onClose, featureName }: LoginRequ
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/90 backdrop-blur-sm"
onClick={onClose}
className="fixed inset-0 z-50"
>
{/* Scanline Effect */}
<div className="absolute inset-0 pointer-events-none opacity-[0.03] bg-[linear-gradient(transparent_50%,rgba(0,0,0,0.5)_50%)] bg-[length:100%_4px]"></div>
<motion.div
initial={{ opacity: 0, scale: 0.95, y: 10 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: 10 }}
transition={{ type: 'spring', damping: 20, stiffness: 300 }}
className="relative max-w-md w-full overflow-hidden bg-black border border-nofx-gold/30 shadow-[0_0_50px_rgba(240,185,11,0.1)] rounded-sm group font-mono"
onClick={(e) => e.stopPropagation()}
<DeepVoidBackground
className="w-full h-full bg-nofx-bg/95 backdrop-blur-md flex items-center justify-center p-4 text-nofx-text"
disableAnimation
onClick={onClose}
>
{/* Terminal Window Header */}
<div className="flex items-center justify-between px-3 py-2 bg-zinc-900 border-b border-zinc-800">
<div className="flex items-center gap-2">
<Terminal size={12} className="text-nofx-gold" />
<span className="text-[10px] text-zinc-500 uppercase tracking-wider">auth_protocol.exe</span>
<motion.div
initial={{ opacity: 0, scale: 0.95, y: 10 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: 10 }}
transition={{ type: 'spring', damping: 20, stiffness: 300 }}
className="relative max-w-md w-full overflow-hidden bg-nofx-bg border border-nofx-gold/30 shadow-neon rounded-sm group font-mono"
onClick={(e) => e.stopPropagation()}
>
{/* Terminal Window Header */}
<div className="flex items-center justify-between px-3 py-2 bg-nofx-bg-lighter border-b border-nofx-gold/20">
<div className="flex items-center gap-2">
<Terminal size={12} className="text-nofx-gold" />
<span className="text-[10px] text-nofx-text-muted uppercase tracking-wider">auth_protocol.exe</span>
</div>
<button
onClick={onClose}
className="text-nofx-text-muted hover:text-nofx-danger transition-colors"
>
<X size={14} />
</button>
</div>
<button
onClick={onClose}
className="text-zinc-600 hover:text-red-500 transition-colors"
>
<X size={14} />
</button>
</div>
{/* Main Content */}
<div className="p-8 relative">
{/* Background Grid */}
<div className="absolute inset-0 bg-[linear-gradient(to_right,#80808008_1px,transparent_1px),linear-gradient(to_bottom,#80808008_1px,transparent_1px)] bg-[size:14px_14px] pointer-events-none"></div>
{/* Main Content */}
<div className="p-8 relative">
{/* Background Grid */}
<div className="absolute inset-0 bg-[linear-gradient(to_right,#80808008_1px,transparent_1px),linear-gradient(to_bottom,#80808008_1px,transparent_1px)] bg-[size:14px_14px] pointer-events-none"></div>
<div className="relative z-10">
{/* Flashing Access Denied */}
<div className="flex justify-center mb-6">
<div className="relative">
<div className="absolute inset-0 bg-red-500/20 blur-xl animate-pulse"></div>
<div className="bg-black border border-red-500/50 text-red-500 px-4 py-2 flex items-center gap-3 shadow-[0_0_15px_rgba(239,68,68,0.2)]">
<AlertTriangle size={18} className="animate-pulse" />
<span className="font-bold tracking-widest text-sm uppercase">{language === 'zh' ? '访问被拒绝' : 'ACCESS DENIED'}</span>
<div className="relative z-10">
{/* Flashing Access Denied */}
<div className="flex justify-center mb-6">
<div className="relative">
<div className="absolute inset-0 bg-red-500/20 blur-xl animate-pulse"></div>
<div className="bg-nofx-bg border border-red-500/50 text-red-500 px-4 py-2 flex items-center gap-3 shadow-[0_0_15px_rgba(239,68,68,0.2)]">
<AlertTriangle size={18} className="animate-pulse" />
<span className="font-bold tracking-widest text-sm uppercase">{language === 'zh' ? '访问被拒绝' : 'ACCESS DENIED'}</span>
</div>
</div>
</div>
</div>
{/* Terminal Text */}
<div className="space-y-4 mb-8">
<div className="text-center">
<h2 className="text-xl font-bold text-white uppercase tracking-wider mb-2">{t.title}</h2>
<p className="text-nofx-gold text-xs uppercase tracking-widest border-b border-nofx-gold/20 pb-4 inline-block">{t.subtitle}</p>
{/* Terminal Text */}
<div className="space-y-4 mb-8">
<div className="text-center">
<h2 className="text-xl font-bold text-white uppercase tracking-wider mb-2">{t.title}</h2>
<p className="text-nofx-gold text-xs uppercase tracking-widest border-b border-nofx-gold/20 pb-4 inline-block">{t.subtitle}</p>
</div>
<div className="bg-nofx-bg-lighter border-l-2 border-nofx-gold/20 p-3 my-4">
<p className="text-xs text-nofx-text-muted leading-relaxed font-mono">
<span className="text-green-500 mr-2">$</span>
{t.description}
</p>
</div>
<div className="grid grid-cols-2 gap-2">
{t.benefits.map((benefit, i) => (
<div key={i} className="flex items-center gap-2 text-[10px] text-nofx-text-muted uppercase tracking-wide">
<span className="text-nofx-gold"></span> {benefit}
</div>
))}
</div>
</div>
<div className="bg-zinc-900/50 border-l-2 border-zinc-700 p-3 my-4">
<p className="text-xs text-zinc-400 leading-relaxed font-mono">
<span className="text-green-500 mr-2">$</span>
{t.description}
</p>
{/* Action Buttons */}
<div className="space-y-3">
<a
href="/login"
className="flex items-center justify-center gap-2 w-full py-3 bg-nofx-gold text-black font-bold text-xs uppercase tracking-widest hover:bg-yellow-400 transition-all shadow-neon hover:shadow-[0_0_25px_rgba(240,185,11,0.4)] group"
>
<LogIn size={14} />
<span>{t.login}</span>
<span className="opacity-0 group-hover:opacity-100 transition-opacity -ml-2 group-hover:ml-0">-&gt;</span>
</a>
<a
href="/register"
className="flex items-center justify-center gap-2 w-full py-3 bg-transparent border border-nofx-gold/20 text-nofx-text-muted hover:text-white hover:border-nofx-gold font-bold text-xs uppercase tracking-widest transition-all hover:bg-nofx-gold/10"
>
<UserPlus size={14} />
<span>{t.register}</span>
</a>
</div>
<div className="grid grid-cols-2 gap-2">
{t.benefits.map((benefit, i) => (
<div key={i} className="flex items-center gap-2 text-[10px] text-zinc-500 uppercase tracking-wide">
<span className="text-nofx-gold"></span> {benefit}
</div>
))}
<div className="mt-4 text-center">
<button
onClick={onClose}
className="text-[10px] text-nofx-text-muted hover:text-nofx-danger uppercase tracking-widest hover:underline decoration-red-500/30"
>
[ {t.later} ]
</button>
</div>
</div>
{/* Action Buttons */}
<div className="space-y-3">
<a
href="/login"
className="flex items-center justify-center gap-2 w-full py-3 bg-nofx-gold text-black font-bold text-xs uppercase tracking-widest hover:bg-yellow-400 transition-all shadow-[0_0_15px_rgba(240,185,11,0.2)] hover:shadow-[0_0_25px_rgba(240,185,11,0.4)] group"
>
<LogIn size={14} />
<span>{t.login}</span>
<span className="opacity-0 group-hover:opacity-100 transition-opacity -ml-2 group-hover:ml-0">-&gt;</span>
</a>
<a
href="/register"
className="flex items-center justify-center gap-2 w-full py-3 bg-transparent border border-zinc-700 text-zinc-400 hover:text-white hover:border-zinc-500 font-bold text-xs uppercase tracking-widest transition-all hover:bg-zinc-900"
>
<UserPlus size={14} />
<span>{t.register}</span>
</a>
</div>
<div className="mt-4 text-center">
<button
onClick={onClose}
className="text-[10px] text-zinc-600 hover:text-red-500 uppercase tracking-widest hover:underline decoration-red-500/30"
>
[ {t.later} ]
</button>
</div>
</div>
</div>
{/* Corner Accents */}
<div className="absolute top-0 right-0 w-2 h-2 border-t border-r border-nofx-gold"></div>
<div className="absolute bottom-0 left-0 w-2 h-2 border-b border-l border-nofx-gold"></div>
{/* Corner Accents */}
<div className="absolute top-0 right-0 w-2 h-2 border-t border-r border-nofx-gold"></div>
<div className="absolute bottom-0 left-0 w-2 h-2 border-b border-l border-nofx-gold"></div>
</motion.div>
</motion.div>
</DeepVoidBackground>
</motion.div>
)}
</AnimatePresence>

View File

@@ -303,6 +303,11 @@ function PositionRow({ position }: { position: HistoricalPosition }) {
{displayQty.toFixed(4)}
</td>
{/* Position Value (Entry Price * Quantity) */}
<td className="py-3 px-4 text-right font-mono" style={{ color: '#EAECEF' }}>
{formatNumber(entryPrice * displayQty)}
</td>
{/* P&L */}
<td className="py-3 px-4 text-right">
<div className="font-mono font-semibold" style={{ color: pnlColor }}>
@@ -764,6 +769,12 @@ export function PositionHistory({ traderId }: PositionHistoryProps) {
>
{t('positionHistory.qty', language)}
</th>
<th
className="py-3 px-4 text-right text-xs font-semibold uppercase tracking-wider"
style={{ color: '#848E9C' }}
>
{t('positionHistory.value', language)}
</th>
<th
className="py-3 px-4 text-right text-xs font-semibold uppercase tracking-wider"
style={{ color: '#848E9C' }}

Some files were not shown because too many files have changed in this diff Show More