Skip to content

Fixed-point/subpenny migration with backward compatibility#5

Closed
arshka wants to merge 5 commits intomainfrom
pr-4
Closed

Fixed-point/subpenny migration with backward compatibility#5
arshka wants to merge 5 commits intomainfrom
pr-4

Conversation

@arshka
Copy link
Owner

@arshka arshka commented Mar 10, 2026

Summary

  • Complete fixed-point/subpenny migration per Kalshi API changes — prices are now dollar strings (yes_price_dollars, yes_ask_dollars, etc.) and counts are fixed-point strings (count_fp, position_fp, etc.)
  • Add backward-compatibility layer (CompatModel) so old integer field names still work with a DeprecationWarning, easing migration for existing users
  • Fix spread_bps crash on one-sided orderbooks and normalize compat output format
  • Add tests for tick size and fractional trading validation

Model field name corrections (e5d1162)

Several models had incorrect field names after the initial migration which blindly renamed all fields. Fixed to match actual Kalshi API responses:

  • BalanceModel: reverted to balance/portfolio_value (int cents — API never migrated these)
  • FillModel: yes_price_dollars/no_price_dollarsyes_price_fixed/no_price_fixed (API uses _fixed suffix for fill prices)
  • PositionModel: total_traded_fptotal_traded_dollars (it's a dollar amount, not a count)
  • MarketModel: liquidity_fpliquidity_dollars
  • SettlementModel: reverted costs/revenue to int cents, kept fee_cost as str (FixedPointDollars)
  • QueuePositionModel: queue_position (int) → queue_position_fp (str)

Transition period support

Added model_validator(mode='before') to CompatModel that remaps old API field names to new ones using the existing _legacy_fields mapping. This handles the transition period (until March 12) where the API may return old field names.

Test plan

  • All 309 unit tests pass
  • Field names verified against official Kalshi API documentation
  • Integration tests against demo API

🤖 Generated with Claude Code

Agnimandur and others added 5 commits March 7, 2026 18:33
Migrate all integer cent fields to `_dollars` string fields and all integer
count fields to `_fp` string fields, matching Kalshi's fixed-point migration
guide. Add order validation for price_level_structure tick sizes and
fractional trading.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Existing code using old integer field names (yes_bid, count, balance, etc.)
now works via CompatModel with deprecation warnings pointing to new names.

GET (reading): Legacy integer accessors derive from new string fields via
__getattr__, e.g. market.yes_bid → int(Decimal("0.45") * 100) = 45.

POST (writing): Old integer params (count=5, yes_price=45) convert to new
string params (count_fp="5", yes_price_dollars="0.45") with warnings.

- New pykalshi/_compat.py: CompatModel base class, conversion functions,
  convert_legacy_kwargs helper, and standard legacy param mappings
- Updated 20 model classes in models.py with _legacy_fields mappings
- Updated 6 WebSocket message models in feed.py with _legacy_fields
- Added legacy params to all POST methods in portfolio.py (sync+async)
- Added legacy params to Order.amend/decrease in orders.py (sync+async)
- Added legacy params to create_rfq in communications.py (sync+async)
- 34 new tests in tests/test_compat.py (294 total tests passing)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…put format

spread_bps would crash with TypeError when spread/mid were None (empty or
one-sided orderbook). cents_to_dollars and int_to_fp now produce canonical
fixed-point strings (e.g. "0.50", "5.00") matching the Kalshi API format.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Cover _validate_tick_size for all three price_level_structures
(linear_cent, deci_cent, tapered_deci_cent) including boundary
conditions, and _validate_fractional for enabled/disabled cases.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Several models had incorrect field names after the fixed-point migration
(f73888e) which blindly renamed all fields. This fixes them to match the
actual API:

- BalanceModel: revert to balance/portfolio_value (int cents, never migrated)
- FillModel: yes/no_price_dollars → yes/no_price_fixed (API uses _fixed suffix)
- PositionModel: total_traded_fp → total_traded_dollars
- MarketModel: liquidity_fp → liquidity_dollars
- SettlementModel: revert costs/revenue to int cents, keep fee_cost as str
- QueuePositionModel: queue_position (int) → queue_position_fp (str)

Also adds model_validator to CompatModel for transition period input
aliasing — remaps old API field names to new ones using _legacy_fields.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@arshka arshka closed this Mar 10, 2026
@arshka arshka deleted the pr-4 branch March 10, 2026 00:33
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