tradingapi 0.5.1__tar.gz → 0.5.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tradingapi-0.5.1 → tradingapi-0.5.2}/PKG-INFO +1 -1
- {tradingapi-0.5.1 → tradingapi-0.5.2}/pyproject.toml +1 -1
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_dhan_historical_diagnostics.py +14 -1
- tradingapi-0.5.2/tests/test_place_combo_order_price_broker.py +109 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/dhan.py +18 -9
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/utils.py +7 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi.egg-info/PKG-INFO +1 -1
- tradingapi-0.5.1/tests/test_place_combo_order_price_broker.py +0 -55
- {tradingapi-0.5.1 → tradingapi-0.5.2}/README.md +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/setup.cfg +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_broker_side_terminal_order.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_calculate_delta_realtime_quotes.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_find_option_with_delta.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_fivepaisa_historical_recovery.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_fivepaisa_stream_reconnect.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_flattrade_symbology.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_market_hours_config.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tests/test_unfilled_eod_pnl.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/__init__.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/allocation.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/attribution.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/broker_base.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/config/commissions_20241216.yaml +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/config/config_sample.yaml +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/config.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/error_handling.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/exceptions.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/fivepaisa.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/flattrade.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/globals.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/ib.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/icicidirect.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/icicidirect_generate_session.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/market_data_exchanges.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/market_hours.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/proxy_utils.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/shoonya.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi/span.py +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi.egg-info/SOURCES.txt +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi.egg-info/dependency_links.txt +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi.egg-info/entry_points.txt +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi.egg-info/requires.txt +0 -0
- {tradingapi-0.5.1 → tradingapi-0.5.2}/tradingapi.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
from types import SimpleNamespace
|
|
2
2
|
|
|
3
|
-
from tradingapi.dhan import
|
|
3
|
+
from tradingapi.dhan import (
|
|
4
|
+
Dhan,
|
|
5
|
+
_extract_dhan_error_details,
|
|
6
|
+
_is_dhan_auth_error,
|
|
7
|
+
_is_dhan_no_data_error,
|
|
8
|
+
)
|
|
4
9
|
|
|
5
10
|
|
|
6
11
|
class _Response:
|
|
@@ -45,6 +50,14 @@ def test_historical_failure_includes_http_diagnostics():
|
|
|
45
50
|
assert details["response_body"] == "upstream unavailable"
|
|
46
51
|
|
|
47
52
|
|
|
53
|
+
def test_dhan_907_is_expected_no_data():
|
|
54
|
+
details = _extract_dhan_error_details(
|
|
55
|
+
{"status": "failure", "remarks": {"error_code": "DH-907"}}
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
assert _is_dhan_no_data_error(details)
|
|
59
|
+
|
|
60
|
+
|
|
48
61
|
def test_auth_error_refreshes_token_and_retries_order():
|
|
49
62
|
auth_failure = {
|
|
50
63
|
"status": "failure",
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from types import SimpleNamespace
|
|
2
|
+
from unittest.mock import Mock
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from tradingapi import utils
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _execution_broker():
|
|
10
|
+
broker = Mock()
|
|
11
|
+
broker.broker = SimpleNamespace(name="DHAN")
|
|
12
|
+
broker.map_exchange_for_api.return_value = "NSE"
|
|
13
|
+
broker.exchange_mappings = {
|
|
14
|
+
"NSE": {"exchangetype_map": {"TCS_STK___": "C"}},
|
|
15
|
+
}
|
|
16
|
+
broker.starting_order_ids_int = {}
|
|
17
|
+
return broker
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_place_combo_order_forwards_price_broker_for_entry(monkeypatch):
|
|
21
|
+
execution_broker = _execution_broker()
|
|
22
|
+
data_broker = Mock()
|
|
23
|
+
transmit = Mock(return_value="TEST_1")
|
|
24
|
+
monkeypatch.setattr(utils, "transmit_entry_order", transmit)
|
|
25
|
+
|
|
26
|
+
utils.place_combo_order(
|
|
27
|
+
execution_broker,
|
|
28
|
+
"TEST",
|
|
29
|
+
["TCS_STK___"],
|
|
30
|
+
[1],
|
|
31
|
+
entry=True,
|
|
32
|
+
exchanges=["NSE"],
|
|
33
|
+
price_broker=[data_broker],
|
|
34
|
+
price_types=["LMT"],
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
assert transmit.call_args.kwargs["price_broker"] == [data_broker]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@pytest.mark.parametrize("target,expected", [("TEST_2", ["TEST_2"]), ("", ["TEST_1"])])
|
|
41
|
+
def test_exit_fill_attribution_and_retry(monkeypatch, target, expected):
|
|
42
|
+
broker = _execution_broker()
|
|
43
|
+
broker.redis_o.hgetall.return_value = {}
|
|
44
|
+
symbol = "TCS_STK___"
|
|
45
|
+
positions = {"TEST_1": -520, "TEST_2": -130}
|
|
46
|
+
filled = []
|
|
47
|
+
monkeypatch.setattr(utils, "get_exit_candidates", lambda *args: ["TEST_1", "TEST_2"])
|
|
48
|
+
monkeypatch.setattr(utils, "get_open_position_by_order", lambda _, key: {
|
|
49
|
+
symbol: SimpleNamespace(size=positions[key]),
|
|
50
|
+
})
|
|
51
|
+
monkeypatch.setattr(utils, "get_limit_price", lambda *args, **kwargs: 114.3)
|
|
52
|
+
monkeypatch.setattr(utils, "_process_broker_order_update", lambda *args: None)
|
|
53
|
+
|
|
54
|
+
def fill(order):
|
|
55
|
+
positions[order.internal_order_id] += order.quantity
|
|
56
|
+
filled.append(order.internal_order_id)
|
|
57
|
+
return order
|
|
58
|
+
|
|
59
|
+
broker.place_order.side_effect = fill
|
|
60
|
+
utils.place_combo_order(broker, "TEST", [symbol], [130], entry=False,
|
|
61
|
+
price_types=["MKT"], exit_int_order_id=target)
|
|
62
|
+
assert filled == expected
|
|
63
|
+
if target:
|
|
64
|
+
assert positions == {"TEST_1": -520, "TEST_2": 0}
|
|
65
|
+
utils.place_combo_order(broker, "TEST", [symbol], [130], entry=False,
|
|
66
|
+
price_types=["MKT"], exit_int_order_id=target)
|
|
67
|
+
assert filled == ["TEST_2"]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_target_survives_freeze_slices(monkeypatch):
|
|
71
|
+
broker = _execution_broker()
|
|
72
|
+
transmit = Mock()
|
|
73
|
+
monkeypatch.setattr(utils, "transmit_exit_order", transmit)
|
|
74
|
+
monkeypatch.setattr(utils.time, "sleep", lambda _: None)
|
|
75
|
+
utils.place_combo_order(broker, "TEST", ["TCS_STK___"], [260], entry=False,
|
|
76
|
+
price_types=["MKT"], freeze_limit_lots=130,
|
|
77
|
+
exit_int_order_id="TEST_2")
|
|
78
|
+
assert transmit.call_count == 2
|
|
79
|
+
assert all(call.kwargs["int_order_id"] == "TEST_2" for call in transmit.call_args_list)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@pytest.mark.parametrize("entry,validate,target", [
|
|
83
|
+
(True, True, "TEST_2"), (False, False, "TEST_2"), (False, True, "OTHER_2"),
|
|
84
|
+
])
|
|
85
|
+
def test_invalid_exit_target_rejected(entry, validate, target):
|
|
86
|
+
with pytest.raises(ValueError, match="exit_int_order_id"):
|
|
87
|
+
utils.place_combo_order(_execution_broker(), "TEST", ["TCS_STK___"], [130],
|
|
88
|
+
entry=entry, validate_db_position=validate,
|
|
89
|
+
price_types=["MKT"], exit_int_order_id=target)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_place_combo_order_forwards_price_broker_for_exit(monkeypatch):
|
|
93
|
+
execution_broker = _execution_broker()
|
|
94
|
+
data_broker = Mock()
|
|
95
|
+
transmit = Mock()
|
|
96
|
+
monkeypatch.setattr(utils, "transmit_exit_order", transmit)
|
|
97
|
+
|
|
98
|
+
utils.place_combo_order(
|
|
99
|
+
execution_broker,
|
|
100
|
+
"TEST",
|
|
101
|
+
["TCS_STK___"],
|
|
102
|
+
[-1],
|
|
103
|
+
entry=False,
|
|
104
|
+
exchanges=["NSE"],
|
|
105
|
+
price_broker=[data_broker],
|
|
106
|
+
price_types=["LMT"],
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
assert transmit.call_args.kwargs["price_broker"] == [data_broker]
|
|
@@ -243,6 +243,10 @@ def _is_dhan_auth_error(details: Dict[str, Any]) -> bool:
|
|
|
243
243
|
return code == "DH-901" or error_type == "invalid_authentication"
|
|
244
244
|
|
|
245
245
|
|
|
246
|
+
def _is_dhan_no_data_error(details: Dict[str, Any]) -> bool:
|
|
247
|
+
return str(details.get("error_code") or "").upper() == "DH-907"
|
|
248
|
+
|
|
249
|
+
|
|
246
250
|
def _symbol_supports_oi(long_symbol: str) -> bool:
|
|
247
251
|
return "_FUT_" in long_symbol or "_OPT_" in long_symbol
|
|
248
252
|
|
|
@@ -1913,15 +1917,20 @@ class Dhan(BrokerBase):
|
|
|
1913
1917
|
historical_data_list.sort(key=lambda x: x.date)
|
|
1914
1918
|
else:
|
|
1915
1919
|
_hist_err = _extract_dhan_error_details(data)
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
"
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1920
|
+
_hist_context = {"symbol": long_symbol, **_hist_err}
|
|
1921
|
+
_is_no_data = _is_dhan_no_data_error(_hist_err)
|
|
1922
|
+
if _is_no_data:
|
|
1923
|
+
trading_logger.log_info(
|
|
1924
|
+
"No historical candles available from Dhan",
|
|
1925
|
+
_hist_context,
|
|
1926
|
+
)
|
|
1927
|
+
else:
|
|
1928
|
+
trading_logger.log_error(
|
|
1929
|
+
"No data from Dhan historical API",
|
|
1930
|
+
None,
|
|
1931
|
+
_hist_context,
|
|
1932
|
+
)
|
|
1933
|
+
self._audit_dhan_call("no_data" if _is_no_data else "error", {
|
|
1925
1934
|
"call_type": "historical",
|
|
1926
1935
|
"symbol": long_symbol,
|
|
1927
1936
|
"security_id": security_id,
|
|
@@ -3931,6 +3931,7 @@ def place_combo_order(
|
|
|
3931
3931
|
validate_db_position: bool = True,
|
|
3932
3932
|
paper: bool = True,
|
|
3933
3933
|
freeze_limit_lots: Optional[int] = None,
|
|
3934
|
+
exit_int_order_id: str = "",
|
|
3934
3935
|
) -> dict[str, Any]:
|
|
3935
3936
|
"""Place a combo order with broker with enhanced error handling.
|
|
3936
3937
|
|
|
@@ -3946,6 +3947,7 @@ def place_combo_order(
|
|
|
3946
3947
|
price_types: List of price types for each symbol
|
|
3947
3948
|
validate_db_position: Whether to validate database position
|
|
3948
3949
|
paper: Whether this is a paper trade
|
|
3950
|
+
exit_int_order_id: Restrict all exit legs/slices to this internal order; empty uses FIFO.
|
|
3949
3951
|
|
|
3950
3952
|
Returns:
|
|
3951
3953
|
dict: Dictionary with symbol to internal order ID mapping
|
|
@@ -3972,6 +3974,10 @@ def place_combo_order(
|
|
|
3972
3974
|
dict: for entry orders a dict containing symbol:int order id is returned. dict is empty for exit orders
|
|
3973
3975
|
"""
|
|
3974
3976
|
|
|
3977
|
+
if exit_int_order_id and (
|
|
3978
|
+
entry or not validate_db_position or not exit_int_order_id.startswith(f"{strategy}_")
|
|
3979
|
+
):
|
|
3980
|
+
raise ValueError("exit_int_order_id requires a validated exit for the same strategy")
|
|
3975
3981
|
if price_broker is None:
|
|
3976
3982
|
price_broker = [execution_broker]
|
|
3977
3983
|
if not isinstance(symbols, list):
|
|
@@ -4120,6 +4126,7 @@ def place_combo_order(
|
|
|
4120
4126
|
paper=paper,
|
|
4121
4127
|
price_broker=price_broker,
|
|
4122
4128
|
mds=mds,
|
|
4129
|
+
int_order_id=exit_int_order_id,
|
|
4123
4130
|
)
|
|
4124
4131
|
if idx < len(slices):
|
|
4125
4132
|
time.sleep(5)
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
from types import SimpleNamespace
|
|
2
|
-
from unittest.mock import Mock
|
|
3
|
-
|
|
4
|
-
from tradingapi import utils
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def _execution_broker():
|
|
8
|
-
broker = Mock()
|
|
9
|
-
broker.broker = SimpleNamespace(name="DHAN")
|
|
10
|
-
broker.map_exchange_for_api.return_value = "NSE"
|
|
11
|
-
broker.exchange_mappings = {
|
|
12
|
-
"NSE": {"exchangetype_map": {"TCS_STK___": "C"}},
|
|
13
|
-
}
|
|
14
|
-
broker.starting_order_ids_int = {}
|
|
15
|
-
return broker
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def test_place_combo_order_forwards_price_broker_for_entry(monkeypatch):
|
|
19
|
-
execution_broker = _execution_broker()
|
|
20
|
-
data_broker = Mock()
|
|
21
|
-
transmit = Mock(return_value="TEST_1")
|
|
22
|
-
monkeypatch.setattr(utils, "transmit_entry_order", transmit)
|
|
23
|
-
|
|
24
|
-
utils.place_combo_order(
|
|
25
|
-
execution_broker,
|
|
26
|
-
"TEST",
|
|
27
|
-
["TCS_STK___"],
|
|
28
|
-
[1],
|
|
29
|
-
entry=True,
|
|
30
|
-
exchanges=["NSE"],
|
|
31
|
-
price_broker=[data_broker],
|
|
32
|
-
price_types=["LMT"],
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
assert transmit.call_args.kwargs["price_broker"] == [data_broker]
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def test_place_combo_order_forwards_price_broker_for_exit(monkeypatch):
|
|
39
|
-
execution_broker = _execution_broker()
|
|
40
|
-
data_broker = Mock()
|
|
41
|
-
transmit = Mock()
|
|
42
|
-
monkeypatch.setattr(utils, "transmit_exit_order", transmit)
|
|
43
|
-
|
|
44
|
-
utils.place_combo_order(
|
|
45
|
-
execution_broker,
|
|
46
|
-
"TEST",
|
|
47
|
-
["TCS_STK___"],
|
|
48
|
-
[-1],
|
|
49
|
-
entry=False,
|
|
50
|
-
exchanges=["NSE"],
|
|
51
|
-
price_broker=[data_broker],
|
|
52
|
-
price_types=["LMT"],
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
assert transmit.call_args.kwargs["price_broker"] == [data_broker]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|