Skip to main content

Personal quantification tool

Project description

jettquant

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install jettquant

Introduction

示例1:订阅数据

from jettquant import RadarEngine

market_engine = RadarEngine(is_verbose=True)


def on_all_tick(data: dict):
    """每3秒推送一次截面数据"""
    print(data)


# 订阅全推数据
market_engine.subscribe_all(on_all_tick)


def on_tick(data: dict):
    """每3秒推送一次截面数据"""
    print(data)


# 订阅单只标的
market_engine.subscribe("300750.SZ", on_tick)

market_engine.start()
market_engine.run_forever()

示例2:数据回放

from pandas import DataFrame
import numpy as np

from jettquant import EventEngine, RadarEngine

event_engine = EventEngine(interval=0.03)  # 10倍回放数据
radar_engine = RadarEngine(event_engine=event_engine, is_verbose=True)


def calculate_auction_metrics_vectorized(df):
    """
    向量化计算集合竞价指标
    性能优化版本,适用于大批量数据
    """
    # 提取列表类型的数据
    ask_prices = df['askPrice'].values
    bid_prices = df['bidPrice'].values
    ask_vols = df['askVol'].values
    bid_vols = df['bidVol'].values
    last_closes = df['lastClose'].values

    # 初始化结果数组
    n = len(df)
    match_prices = np.zeros(n)
    auction_volumes = np.zeros(n, dtype=int)
    auction_amounts = np.zeros(n)
    price_change_pcts = np.zeros(n)
    unmatched_volumes = np.zeros(n, dtype=int)

    # 向量化提取第一档价格和量
    for i in range(n):
        # 匹配价格(买一或卖一)
        if isinstance(ask_prices[i], list) and len(ask_prices[i]) > 0 and ask_prices[i][0] > 0:
            match_prices[i] = ask_prices[i][0]
        elif isinstance(bid_prices[i], list) and len(bid_prices[i]) > 0 and bid_prices[i][0] > 0:
            match_prices[i] = bid_prices[i][0]

        # 第一档买卖量
        bid_vol_1 = bid_vols[i][0] if isinstance(bid_vols[i], list) and len(bid_vols[i]) > 0 else 0
        ask_vol_1 = ask_vols[i][0] if isinstance(ask_vols[i], list) and len(ask_vols[i]) > 0 else 0

        # 竞价量 = min(买一, 卖一)
        auction_volumes[i] = min(bid_vol_1, ask_vol_1)

        # 所有档位总量
        total_bid = sum([v for v in bid_vols[i] if isinstance(v, (int, float)) and v > 0]) if isinstance(bid_vols[i],
                                                                                                         list) else 0
        total_ask = sum([v for v in ask_vols[i] if isinstance(v, (int, float)) and v > 0]) if isinstance(ask_vols[i],
                                                                                                         list) else 0
        unmatched_volumes[i] = total_bid - total_ask

    # 向量化计算金额和涨幅
    auction_amounts = auction_volumes * match_prices * 100

    # 避免除零
    valid_close = last_closes > 0
    price_change_pcts[valid_close] = (match_prices[valid_close] - last_closes[valid_close]) / last_closes[
        valid_close] * 100

    # 构建结果DataFrame(只保留关键列)
    result = DataFrame({
        '证券代码': df['证券代码'].values,
        '时间': df['timetag'].values,
        '昨收': last_closes,
        '匹配价格': match_prices,
        '竞价量': auction_volumes,
        '竞价金额': auction_amounts,
        '竞价涨幅': price_change_pcts,
        '未匹配量': unmatched_volumes
    })

    return result


def on_all_tick(data):
    df = (
        DataFrame.from_dict(data)
        .T.reset_index()
        .rename(columns={'index': '证券代码'})
    )

    # 计算集合竞价指标
    df_auction = calculate_auction_metrics_vectorized(df)

    # 只输出有竞价的股票(竞价量>0)
    active_df = df_auction[df_auction['竞价量'] > 0].copy()

    if not active_df.empty:
        # 按竞价金额排序
        active_df = active_df.sort_values('竞价金额', ascending=False)

        # 格式化输出
        active_df['竞价金额'] = active_df['竞价金额'].apply(lambda x: f"{x:,.0f}")
        active_df['竞价涨幅'] = active_df['竞价涨幅'].apply(lambda x: f"{x:.2f}%")

        radar_engine.output(active_df)
    else:
        radar_engine.output("无活跃竞价数据")


radar_engine.subscribe_all(on_all_tick)

# 开始回放
radar_engine.start_replay("ticks-2025-11-14-auction.parquet")

radar_engine.start()
radar_engine.run_forever()

License

jettquant is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jettquant-1.2.1.tar.gz (46.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jettquant-1.2.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file jettquant-1.2.1.tar.gz.

File metadata

  • Download URL: jettquant-1.2.1.tar.gz
  • Upload date:
  • Size: 46.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.2

File hashes

Hashes for jettquant-1.2.1.tar.gz
Algorithm Hash digest
SHA256 7b77b54b88243d6a5e8ddc27d6f8aae610fb6242d5924ff2a3e40b6a4fa6360c
MD5 21ec5c07a207d5f7d5422f32d9f71eee
BLAKE2b-256 b7e22245d19875201b31d6704a36b2af7fcf149460ea8fb860c3f40fa3bddb2f

See more details on using hashes here.

File details

Details for the file jettquant-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: jettquant-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.2

File hashes

Hashes for jettquant-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc0023f453b31af908f8c1d6e74d557659238bad3e6a193436e8ecac7c26f5e2
MD5 24a225ee1f35fa93459836e8a1e655b6
BLAKE2b-256 254ac783139f4583915402237f2c1d72bdc9fabcbf2dabe919f544d88332eec5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page