Skip to content

Conversation

@asdv23
Copy link

@asdv23 asdv23 commented Oct 27, 2025

问题描述

Binance API 在发送订单时返回错误 -1111: Precision is over the maximum defined for this asset,导致交易失败。

根因:代码使用了硬编码的精度映射表,这些值可能过期或不准确。

解决方案

  • 从 Binance API 动态获取 stepSize(数量精度)和 tickSize(价格精度)
  • 自动计算小数位数,无需手动维护映射表
  • 在发送订单前预加载符号信息

主要改动

// 新增:从 API 获取精度
private async getPrecisions(symbol: string)

// 更新:使用缓存和动态精度
formatQuantity() -  LOT_SIZE 过滤器读取
formatPrice() -  PRICE_FILTER 读取 tickSize
placeOrder() - 预加载符号信息

优势

  • 自适应:自动适应任何交易对的精度要求
  • 维护性:无需手动更新硬编码值
  • 准确性:直接使用 Binance 官方规格
  • 健壮性:添加错误处理和回退机制

测试状态

  • ✅ 编译通过
  • ✅ 无 linter 错误
  • ✅ 移除了所有硬编码精度映射

…d values

Problem:
- Binance API was rejecting orders with error -1111: 'Precision is over the maximum defined for this asset'
- Hardcoded precision maps became outdated and didn't match actual API requirements

Changes:
- Add getPrecisions() method to fetch stepSize and tickSize from Binance API
- Update formatQuantity() to use cached symbol info with dynamic precision calculation
- Update formatPrice() to use PRICE_FILTER tickSize from API
- Pre-load symbol info in placeOrder() before formatting to ensure cache is populated
- Add debug logging for quantity formatting

Benefits:
- Eliminates precision errors by using actual API specifications
- Automatically adapts to any symbol without code changes
- More maintainable - no need to update hardcoded values
- Improved error handling with fallback defaults
@terryso
Copy link
Owner

terryso commented Oct 27, 2025

不错...我之前也试过有些情况出错, 不过为了快速验证功能, 就先硬编码了.
你加了这个功能, 有补充单元测试?

我现在jest那里配置覆盖率要达80

coverageThreshold: {
global: {
branches: 75,
functions: 80,
lines: 80,
statements: 80
}
},

你可以先跑一次npm run test:coverage 看看, 如果不达标, 最好补一下单元测试

@terryso
Copy link
Owner

terryso commented Oct 27, 2025

image 我拉了你的分支, 跑了单元测试, 但是目前还是有很多单元测试失败, 请先修复一下, 同时看看覆盖率是不是达标, 不达标的话还要补一下用例.

@terryso terryso requested a review from Copilot October 27, 2025 13:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses the Binance API error -1111: Precision is over the maximum defined for this asset by replacing hardcoded precision mappings with dynamic precision fetching from the Binance API. The solution retrieves stepSize and tickSize from exchange info filters and automatically calculates the required decimal places for quantities and prices.

Key Changes:

  • Added getPrecisions() method to fetch precision dynamically from LOT_SIZE and PRICE_FILTER
  • Introduced formatQuantityAsync() for async precision formatting
  • Updated formatQuantity() and formatPrice() to use cached symbol info
  • Modified placeOrder() to preload symbol information before order submission

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@asdv23
Copy link
Author

asdv23 commented Oct 28, 2025

-----------------------------|---------|----------|---------|---------|----------------------------------------------------------

File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All files 89.42 76.68 91.16 90.09
config 81.48 58.33 100 81.48
constants.ts 81.48 58.33 100 81.48 36-40
scripts 90.76 96 85.71 90.76
analyze-api.ts 90.76 96 85.71 90.76 106-113,224-231
services 89.09 76.29 91.07 89.85
api-client.ts 82.66 73.33 77.77 81.94 51-73
binance-service.ts 86.36 78.81 87.23 89.21 143,218,552,634,692,762-816
config-manager.ts 93.33 94.11 81.81 93.33 101-111
follow-service.ts 92.8 78.14 96.42 93.95 ...6,281-282,321,341-342,363,423-424,527,567,641-643,649
futures-capital-manager.ts 82.35 73.33 92.85 82.6 35-38,173-179
order-history-manager.ts 87.59 71.11 96.87 87.2 74-76,107,119,258-270,311,324,374
position-manager.ts 92.07 76.25 100 91.92 115-117,158-161,291,294,375-386
profit-calculator.ts 100 100 100 100
risk-manager.ts 89.65 83.33 87.5 89.65 49,114,128
trade-history-service.ts 95.87 76.36 92.3 95.87 88,133-135
trading-executor.ts 82.79 66.66 84.61 83.24 141-151,191-202,355-357,370-372,382-385,421-430
types 100 100 100 100
api.ts 100 100 100 100
utils 95.83 72 93.75 95.71
errors.ts 96.55 70.83 100 96.42 125,132
logger.ts 92.85 100 83.33 92.85 49
----------------------------- --------- ---------- --------- --------- ----------------------------------------------------------
Test Suites: 29 passed, 29 total
Tests: 634 passed, 634 total
Snapshots: 0 total
Time: 3.451 s
Ran all test suites.

@terryso 修复并增加了对应的单元测试,现在覆盖率已达标

@terryso
Copy link
Owner

terryso commented Oct 28, 2025

有一个issue说拉了你这个PR好像还是解决不了问题. 你可以看看 #6
现在还有另外一个PR #16 是解决同样问题的, 不知道你们哪个能更好的解决这个问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants