优化:限价单按规则只取135个档位

This commit is contained in:
ivan 2026-02-14 13:22:18 +08:00
parent f15e40d74c
commit 89ba32bdbc

View File

@ -182,6 +182,7 @@
hide-details
density="compact"
@update:model-value="onLimitPriceInput"
@blur="onLimitPriceBlur"
@keydown="onLimitPriceKeydown"
@paste="onLimitPricePaste"
></v-text-field>
@ -439,6 +440,7 @@
hide-details
density="compact"
@update:model-value="onLimitPriceInput"
@blur="onLimitPriceBlur"
@keydown="onLimitPriceKeydown"
@paste="onLimitPricePaste"
></v-text-field>
@ -936,6 +938,42 @@ import { OrderType, Side } from '../api/constants'
const { mobile } = useDisplay()
const userStore = useUserStore()
/** 限价单允许的 135 个价格档位01 区间规则19/1090/1009900/99109990/99919999 */
function buildAllowedLimitPrices(): number[] {
const list: number[] = []
for (let i = 1; i <= 9; i++) list.push(i / 10000)
for (let i = 10; i <= 90; i += 10) list.push(i / 10000)
for (let i = 100; i <= 9900; i += 100) list.push(i / 10000)
for (let i = 9910; i <= 9990; i += 10) list.push(i / 10000)
for (let i = 9991; i <= 9999; i++) list.push(i / 10000)
return list
}
const ALLOWED_LIMIT_PRICES = buildAllowedLimitPrices()
/** 将限价吸附到最近的允许档位 */
function snapToAllowedPrice(v: number): number {
const clamped = Math.min(1, Math.max(0, Number.isFinite(v) ? v : 0))
const list = ALLOWED_LIMIT_PRICES
if (list.length === 0) return clamped
let nearest = list[0] as number
let minDiff = Math.abs(clamped - nearest)
for (const p of list) {
const d = Math.abs(clamped - p)
if (d < minDiff) {
minDiff = d
nearest = p
}
}
return nearest
}
/** 获取当前价格在允许列表中的索引,-1 表示不在列表中 */
function indexOfAllowedPrice(v: number): number {
const snapped = snapToAllowedPrice(v)
const idx = ALLOWED_LIMIT_PRICES.findIndex((p) => Math.abs(p - snapped) < 1e-9)
return idx >= 0 ? idx : 0
}
export interface TradeMarketPayload {
marketId?: string
yesPrice: number
@ -1089,8 +1127,9 @@ function applyInitialOption(option: 'yes' | 'no') {
syncLimitPriceFromMarket()
}
/** 限价限制到 [0,1] 并吸附到 135 个允许档位之一 */
function clampLimitPrice(v: number): number {
return Math.min(1, Math.max(0, Number.isFinite(v) ? v : 0))
return snapToAllowedPrice(Math.min(1, Math.max(0, Number.isFinite(v) ? v : 0)))
}
/** 根据当前 props.market 与 selectedOption 同步 limitPrice组件显示或 market 更新时调用) */
@ -1133,11 +1172,16 @@ const handleOptionChange = (option: 'yes' | 'no') => {
emit('optionChange', option)
}
/** 仅在值在 [0,1] 且为有效数字时更新,否则保持原值不变 */
/** 仅接受 135 个允许档位:输入值吸附到最近档位,非法值忽略 */
function onLimitPriceInput(v: unknown) {
const num = v == null ? NaN : Number(v)
if (!Number.isFinite(num) || num < 0 || num > 1) return
limitPrice.value = num
limitPrice.value = snapToAllowedPrice(num)
}
/** 失焦时吸附到允许档位 */
function onLimitPriceBlur() {
limitPrice.value = snapToAllowedPrice(limitPrice.value)
}
/** 只允许数字和小数点输入 */
@ -1162,13 +1206,17 @@ function onLimitPricePaste(e: ClipboardEvent) {
}
}
// 01
// 135
const decreasePrice = () => {
limitPrice.value = clampLimitPrice(limitPrice.value - 0.01)
const idx = indexOfAllowedPrice(limitPrice.value)
const nextIdx = Math.max(0, idx - 1)
limitPrice.value = ALLOWED_LIMIT_PRICES[nextIdx] ?? limitPrice.value
}
const increasePrice = () => {
limitPrice.value = clampLimitPrice(limitPrice.value + 0.01)
const idx = indexOfAllowedPrice(limitPrice.value)
const nextIdx = Math.min(ALLOWED_LIMIT_PRICES.length - 1, idx + 1)
limitPrice.value = ALLOWED_LIMIT_PRICES[nextIdx] ?? limitPrice.value
}
/** 将 shares 限制为正整数(>= 1 */