Fix #219: Use real exchange lot_size (stepSize) and MIN_NOTIONAL for crypto perps - #222
Open
mwaleedta wants to merge 1 commit into
Open
Fix #219: Use real exchange lot_size (stepSize) and MIN_NOTIONAL for crypto perps#222mwaleedta wants to merge 1 commit into
mwaleedta wants to merge 1 commit into
Conversation
…TIONAL for crypto perps - Add _get_lot_size_and_min_notional() to CryptoDataSource to fetch exchange precision/limits (stepSize and minNotional) from CCXT market data - Pass lot_size and min_notional through format_kline() into bar data - Update _lot_size() in runtime to use bar.lot_size from exchange data - Add _min_notional() helper and validate MIN_NOTIONAL during order execution - Force sub-lot position residuals to zero to prevent dust blocking re-entry - Fix precision in _round_to_lot (1e-12 instead of 1e-8) - Add tests for integer lot sizes, MIN_NOTIONAL rejection, dust elimination, and capital-independent backtest results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes issue #219 where the Strategy V2 engine hardcoded crypto lot size to 1e-8 for all crypto symbols, while real Binance perpetuals use integer coin lots (stepSize = '1' for many perps). This caused sub-lot 'dust' to remain after partial fills due to the per-bar liquidity cap, which then blocked symbol re-entry because
position.amount > 0.Changes
1. CryptoDataSource (
backend_api_python/app/data_sources/crypto.py)_get_lot_size_and_min_notional()method that fetchesstepSize(fromprecision.amountorlimits.amount.min) andMIN_NOTIONAL(fromlimits.cost.min) from CCXT market dataget_kline()to includelot_sizeandmin_notionalin each bar viaformat_kline()2. Base Data Source (
backend_api_python/app/data_sources/base.py)format_kline()to accept optionallot_sizeandmin_notionalparameters3. Strategy V2 Data Portal (
backend_api_python/app/services/strategy_v2/data.py)min_notionalto the bar cache extraction so it's available during execution4. Strategy V2 Runtime (
backend_api_python/app/services/strategy_v2/runtime.py)_lot_size()to usebar.lot_sizefrom exchange data (falls back to 1e-8 for backward compatibility)_min_notional()helper to readbar.min_notionalabs(current.amount) <= lot_size) to prevent dust_round_to_lot()precision from 1e-8 to 1e-125. Tests (
backend_api_python/tests/test_strategy_v2_runtime.py)test_crypto_integer_lot_size_no_dust_on_close: Verifies real lot_size prevents dust on closetest_crypto_min_notional_rejection: Verifies MIN_NOTIONAL validation workstest_crypto_position_dust_forced_to_zero: Verifies sub-lot residuals are zeroedtest_backtest_results_independent_of_initial_capital: Verifies the core fix - backtest results no longer depend on initial capitalTesting
All 1419 existing tests pass + 4 new tests for this fix.