You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现交易行情数据快速读取fastRead方法编程求助

Java fastRead方法实现方案

实现思路

  • 利用约束给出的固定JSON结构特性,跳过不需要的timestamp、book字段整段内容,无需逐字符解析无关内容
  • 维护遍历指针和动作列表,按规则记录跳过(S)和读取(R)动作
  • 先遍历bids数组:每个报价固定跳过{"price":前缀,仅读取price数字段判断是否满足minSellPrice <= 价格的盈利条件,满足则直接返回动作序列
  • bids数组无符合条件的报价则继续处理asks数组,逻辑和bids一致,判断条件为maxBuyPrice >= 价格
  • 所有报价处理完无盈利机会则直接返回累计的动作序列

可运行Java代码

import java.util.ArrayList;
import java.util.List;

public class FastMarketReader {
    public static String fastRead(double maxBuyPrice, double minSellPrice, String marketState) {
        int pos = 0;
        List<String> actions = new ArrayList<>();

        // 定位到bids字段起始位置,跳过前面所有无关内容
        int bidsStart = marketState.indexOf("\"bids\":");
        if (bidsStart > pos) {
            actions.add("S" + (bidsStart - pos));
        }
        // 跳过"bids:"字段标识
        pos = bidsStart + 7;

        // 跳过bids数组的左括号[
        actions.add("S1");
        pos += 1;

        // 处理bids数组
        while (pos < marketState.length()) {
            char curr = marketState.charAt(pos);
            if (curr == ']') {
                // bids数组结束
                actions.add("S1");
                pos += 1;
                break;
            }
            if (curr == ',') {
                // 跳过报价之间的逗号
                actions.add("S1");
                pos += 1;
            }
            // 跳过每个报价的{"price":固定前缀,共9个字符
            actions.add("S9");
            pos += 9;

            // 读取price数字
            int priceStart = pos;
            while (pos < marketState.length() && Character.isDigit(marketState.charAt(pos))) {
                pos++;
            }
            int readLen = pos - priceStart;
            actions.add("R" + readLen);
            int bidPrice = Integer.parseInt(marketState.substring(priceStart, pos));

            // 检查bid盈利条件
            if (minSellPrice <= bidPrice) {
                return String.join(" ", actions);
            }

            // 跳过,"volume":固定前缀,共10个字符
            actions.add("S10");
            pos += 10;

            // 跳过volume数字
            while (pos < marketState.length() && Character.isDigit(marketState.charAt(pos))) {
                pos++;
            }

            // 跳过报价结尾的}
            actions.add("S1");
            pos += 1;
        }

        // 定位到asks字段起始位置
        int asksStart = marketState.indexOf("\"asks\":");
        if (asksStart > pos) {
            actions.add("S" + (asksStart - pos));
        }
        // 跳过"asks:"字段标识
        pos = asksStart + 7;

        // 跳过asks数组的左括号[
        actions.add("S1");
        pos += 1;

        // 处理asks数组
        while (pos < marketState.length()) {
            char curr = marketState.charAt(pos);
            if (curr == ']') {
                // asks数组结束
                actions.add("S1");
                pos += 1;
                break;
            }
            if (curr == ',') {
                // 跳过报价之间的逗号
                actions.add("S1");
                pos += 1;
            }
            // 跳过每个报价的{"price":固定前缀,共9个字符
            actions.add("S9");
            pos += 9;

            // 读取price数字
            int priceStart = pos;
            while (pos < marketState.length() && Character.isDigit(marketState.charAt(pos))) {
                pos++;
            }
            int readLen = pos - priceStart;
            actions.add("R" + readLen);
            int askPrice = Integer.parseInt(marketState.substring(priceStart, pos));

            // 检查ask盈利条件
            if (maxBuyPrice >= askPrice) {
                return String.join(" ", actions);
            }

            // 跳过,"volume":固定前缀,共10个字符
            actions.add("S10");
            pos += 10;

            // 跳过volume数字
            while (pos < marketState.length() && Character.isDigit(marketState.charAt(pos))) {
                pos++;
            }

            // 跳过报价结尾的}
            actions.add("S1");
            pos += 1;
        }

        return String.join(" ", actions);
    }
}

内容的提问来源于stack exchange,提问作者Samy Maud Debbou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 07:27:03