Skip to content

fix:implemented centralized freeze quantity splitting (issue #1241).#1251

Open
iAbhi001 wants to merge 1 commit intomarketcalls:mainfrom
iAbhi001:fix/close-position-freeze-limit
Open

fix:implemented centralized freeze quantity splitting (issue #1241).#1251
iAbhi001 wants to merge 1 commit intomarketcalls:mainfrom
iAbhi001:fix/close-position-freeze-limit

Conversation

@iAbhi001
Copy link
Copy Markdown
Contributor

@iAbhi001 iAbhi001 commented Apr 13, 2026

This pull request addresses the issue identified under Reference Number 1241 where large trading positions were being rejected by brokers because they exceeded exchange freeze quantity limits. I have implemented a fix in the service layer that automatically detects when a position size is too large and splits it into smaller manageable orders before sending them to the broker.

By placing this logic in the central service layer we avoid the need to modify code for every individual broker module. The system now looks up the specific freeze limit for each symbol in our database and executes the square off in a sequential loop. I also added a small time delay between these split orders to ensure we do not hit any broker rate limits or trigger spam protections.

This update ensures that the close position feature works reliably for high volume traders across all supported brokers. I have carefully maintained all existing features including sandbox mode support and the event bus system for logging and alerts. These changes provide a stable and unified way to handle exchange restrictions without complicating the broker specific files.


Summary by cubic

Centralized freeze-quantity splitting for close-position to prevent broker rejections on large lots (issue #1241). Finalized with stricter parsing and clearer partial-error reporting.

  • Bug Fixes
    • Freeze-limit splitting per symbol: fetch via broker_module.get_positions; derive action from netqty; resolve symbol from symboltoken/token/instrument_token using database.token_db.get_symbol; read limit via database.qty_freeze_db.get_freeze_qty (fallback to total qty); place MARKET splits via broker_module.place_order_api with reverse_map_product_type; wait 0.2s between orders.
    • Error handling: count invalid quantity rows as failures; stop further splits on first failure per symbol; aggregate failures and return 207 partial_error (else 200 success); return success when no positions; publish PositionClosedEvent with symbol/exchange/product=ALL; improved 500/403/400 responses.

Written for commit 994e160. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="services/close_position_service.py">

<violation number="1" location="services/close_position_service.py:156">
P2: Centralized close-all logic can crash on broker schema/type variations, causing whole operation to fail with 500.</violation>

<violation number="2" location="services/close_position_service.py:208">
P2: Fallback `else` branch after `if status_code == 200` is unreachable because `status_code` is always set to 200 or function returns earlier on exception.</violation>

<violation number="3" location="services/close_position_service.py:208">
P1: Split-order placement failures are swallowed and overall close-position call still returns/publishes success.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread services/close_position_service.py Outdated
Comment thread services/close_position_service.py Outdated
Comment thread services/close_position_service.py Outdated
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="services/close_position_service.py">

<violation number="1" location="services/close_position_service.py:162">
P1: Invalid quantity rows are silently skipped and not counted as failures, allowing false-success close responses with positions left open.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread services/close_position_service.py
@iAbhi001 iAbhi001 force-pushed the fix/close-position-freeze-limit branch from d9a02d4 to 994e160 Compare April 14, 2026 18:15
@marketcalls
Copy link
Copy Markdown
Owner

Which brokers you tested in the live market. this feature affects 30+ live brokers.
Ensure atleast two brokers you are testing live and ensure the feature works

@iAbhi001
Copy link
Copy Markdown
Contributor Author

I can test it now with only 1 as I don't have access to 2.

@marketcalls
Copy link
Copy Markdown
Owner

avoid using trackback as we have a cetralized trackack at utils/logging.py
read the claude.md for better context

@marketcalls
Copy link
Copy Markdown
Owner

Also remember openalgo has crypto exchanges as well. so we need to write the logic seperately for indian exchanges and crypto exchanges as they dont have the concept of freeze limit.

And even for Indian Exchanges Freeze Quantity should be applied for Derviative exchanges only.

@iAbhi001
Copy link
Copy Markdown
Contributor Author

I would just request you to check how my code is working for different brokers, I will check the Crypto Exchanges.

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