Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(trading): Dhan paper orders reject fractional share quantities instead of truncating to zero

Lanre Shittuddc634df28bc
diff --git a/agent/src/trading/connectors/dhan/sdk.py b/agent/src/trading/connectors/dhan/sdk.py
index a2159b6b6..d6fd3f629 100644
--- a/agent/src/trading/connectors/dhan/sdk.py
+++ b/agent/src/trading/connectors/dhan/sdk.py
@@ -482,8 +482,13 @@ def place_order(
if type_token not in ("MARKET", "LIMIT"):
return {"status": "error", "error": "order_type must be 'market' or 'limit'"}
- if quantity is None or float(quantity) <= 0:
- return {"status": "error", "error": "quantity must be positive"}
+ if quantity is None or float(quantity) < 1:
+ return {
+ "status": "error",
+ "error": "quantity must be a positive whole number of shares",
+ }
+ if float(quantity) != int(float(quantity)):
+ return {"status": "error", "error": "quantity must be a whole number of shares"}
qty = int(float(quantity))
sec_id = str(security_id or symbol).strip()
diff --git a/agent/tests/test_sdk_connectors.py b/agent/tests/test_sdk_connectors.py
index 16cbbfbb2..f61e7aa69 100644
--- a/agent/tests/test_sdk_connectors.py
+++ b/agent/tests/test_sdk_connectors.py
@@ -875,6 +875,20 @@ def test_in_broker_paper_place_order_simulated_locally(mod, Config) -> None:
assert result["paper_guard"] == "simulated_locally"
+def test_dhan_place_order_rejects_fractional_quantity() -> None:
+ """A fractional quantity must not silently truncate to a zero-share fill.
+
+ Before the fix, ``int(0.5)`` truncated to 0 after the ``> 0`` check had
+ already passed, so a fractional order came back ``status: ok`` with
+ ``quantity: 0`` — a fabricated successful fill for zero shares.
+ """
+ result = dh.place_order(
+ dh.DhanConfig(profile="paper"), symbol="RELIANCE", side="buy", quantity=0.5
+ )
+ assert result["status"] == "error"
+ assert "whole number" in result["error"]
+
+
@pytest.mark.parametrize("mod, Config", [(dh, dh.DhanConfig), (sh, sh.ShoonyaConfig)])
def test_in_broker_paper_cancel_order_simulated(mod, Config) -> None:
placed = mod.place_order(Config(profile="paper"), symbol="RELIANCE", side="buy", quantity=10)