Skip to main content

No project description provided

Project description

DuckDB 连接器文档

DataBase 类

类说明

用于连接和管理DuckDB数据库,处理币安交易数据的存储、因子计算和查询操作


初始化方法

def __init__(
    self,
    cache_path: str | None = None,
    db_path: str | None = None,
    read_only: bool = True
)

参数说明:

  • cache_path: 本地缓存路径(可选)
  • db_path: DuckDB数据库文件路径(可选,内存数据库如果未指定)
  • read_only: 是否以只读模式打开数据库(默认True)

主要公有方法

更新K线数据

def update_klines(self)

功能:

  • 遍历所有资产类别和时间频率,创建/更新K线数据表
  • 自动处理不同时间粒度的数据聚合
  • 进度条显示处理进度

更新因子数据

def update_factors(self)

功能:

  • 计算动量(momentum)、波动率(volatility)、贝塔(beta)等因子
  • 使用滑动窗口计算(窗口大小自动适配不同时间频率)
  • 进度条显示处理进度

获取因子数据

def df_factors(
    self,
    symbols: list[str] | None = None,
    freq: Literal["1m", "3m", "15m", "30m", "1h", "4h", "12h", "1d"] = "1m",
    asset_class: Literal["spot", "um"] = "um",
    start_date: str | None = None,
    end_date: str | None = None,
    order_by_timestamp: bool = False,
) -> pd.DataFrame

参数说明:

  • symbols: 交易对列表(可选,默认全部)
  • freq: 时间频率(默认1分钟)
  • asset_class: 资产类别(默认永续合约)
  • start_date/end_date: 时间范围(ISO格式字符串)
  • order_by_timestamp: 是否按时间排序

返回:

  • 包含以下字段的DataFrame: timestamp, symbol, close, return, momentum, volatility, beta

获取原始K线数据

def df_klines(
    self,
    symbols: list[str] | None = None,
    freq: Literal["1m", "3m", "15m", "30m", "1h", "4h", "12h", "1d"] = "1m",
    asset_class: Literal["spot", "um"] = "um",
    start_date: str | None = None,
    end_date: str | None = None,
    order_by_timestamp: bool = False,
) -> pd.DataFrame

参数同df_factors

返回:

  • 包含以下字段的DataFrame: symbol, timestamp, open, high, low, close, volume, quote_volume, taker_buy_volume, taker_buy_quote_volume

获取因子矩阵

def factors_matrix(
    self,
    symbols: list[str] | None = None,
    factor: Literal["return", "momentum", "volatility", "beta"] = "return",
    freq: Literal["1m", "3m", "15m", "30m", "1h", "4h", "12h", "1d"] = "1m",
    asset_class: Literal["spot", "um"] = "um",
    start_date: str | None = None,
    end_date: str | None = None,
) -> pd.DataFrame

特殊说明:

  • 返回以时间为索引、交易对为列名的二维矩阵
  • 使用PIVOT操作将纵向数据转换为横向矩阵
  • 适合量化分析中的因子研究

获取所有交易对

def list_all_symbols(self, asset_class: Literal["spot", "um"] = "um", data_type: Literal["klines", "metrics"] = "klines") -> list[str]:

参数说明:

  • asset_class: 资产类别(默认永续合约)
  • data_type: 数据类型(默认K线数据)

返回:

  • 包含所有交易对的列表

异常类型

  • DBReadOnlyError: 尝试在只读模式下执行写操作时抛出
  • DBError: 参数错误或无效操作时抛出

使用示例

# 初始化数据库连接
db = DataBase(db_path="binance.db", read_only=False)

# 更新数据
db.update_klines()
db.update_factors()

# 查询数据
df = db.df_factors(
    symbols=["BTCUSDT", "ETHUSDT"],
    freq="1h",
    start_date="2023-01-01",
    end_date="2023-01-31"
)

# 获取因子矩阵
matrix = db.factors_matrix(
    factor="momentum",
    freq="4h",
    asset_class="um"
)

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

duck_client-0.1.12.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

duck_client-0.1.12-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file duck_client-0.1.12.tar.gz.

File metadata

  • Download URL: duck_client-0.1.12.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-51-generic

File hashes

Hashes for duck_client-0.1.12.tar.gz
Algorithm Hash digest
SHA256 67d91823d1dd2f7770163ba855e28384df9ad366d770f5cb36c84bbb3d568466
MD5 b47c3f731d6295b5bd9cbddf55c2dd58
BLAKE2b-256 c8e1cc78ee16e80d7a8473683b1cf4b8b5e9793870dc032a795d863de7069112

See more details on using hashes here.

File details

Details for the file duck_client-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: duck_client-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-51-generic

File hashes

Hashes for duck_client-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 f3d65e4d2c8e541c0b039fb5d3c46c8ea56f4922c57e4d65f7df6adeb91cf6f7
MD5 55d6a2e08c1ab78a313ff47224b24e04
BLAKE2b-256 e01ff2838d8f5f3e97644abf44291268bb125c3cecff27befa562c333b1b877d

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