tradingapi 0.5.2__tar.gz → 0.5.3__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.2 → tradingapi-0.5.3}/PKG-INFO +1 -1
- {tradingapi-0.5.2 → tradingapi-0.5.3}/pyproject.toml +1 -1
- tradingapi-0.5.3/tests/test_calculate_mtm.py +26 -0
- tradingapi-0.5.3/tests/test_symbol_matches_order.py +21 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/utils.py +37 -26
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi.egg-info/PKG-INFO +1 -1
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi.egg-info/SOURCES.txt +2 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/README.md +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/setup.cfg +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_broker_side_terminal_order.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_calculate_delta_realtime_quotes.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_dhan_historical_diagnostics.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_find_option_with_delta.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_fivepaisa_historical_recovery.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_fivepaisa_stream_reconnect.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_flattrade_symbology.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_market_hours_config.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_place_combo_order_price_broker.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tests/test_unfilled_eod_pnl.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/__init__.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/allocation.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/attribution.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/broker_base.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/config/commissions_20241216.yaml +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/config/config_sample.yaml +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/config.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/dhan.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/error_handling.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/exceptions.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/fivepaisa.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/flattrade.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/globals.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/ib.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/icicidirect.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/icicidirect_generate_session.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/market_data_exchanges.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/market_hours.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/proxy_utils.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/shoonya.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi/span.py +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi.egg-info/dependency_links.txt +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi.egg-info/entry_points.txt +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi.egg-info/requires.txt +0 -0
- {tradingapi-0.5.2 → tradingapi-0.5.3}/tradingapi.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from tradingapi.broker_base import Price
|
|
5
|
+
from tradingapi import utils
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_calculate_mtm_uses_last_when_bid_and_ask_are_zero(monkeypatch):
|
|
9
|
+
monkeypatch.setattr(
|
|
10
|
+
utils,
|
|
11
|
+
"get_price",
|
|
12
|
+
lambda *args, **kwargs: Price(bid=0, ask=0, last=-1188, prior_close=-1692),
|
|
13
|
+
)
|
|
14
|
+
trades = pd.DataFrame([{
|
|
15
|
+
"symbol": "PUT?-20:CALL?-20",
|
|
16
|
+
"entry_quantity": 18,
|
|
17
|
+
"exit_quantity": 0,
|
|
18
|
+
"entry_price": -1957.83332,
|
|
19
|
+
"exit_price": 0,
|
|
20
|
+
"mtm": -1957.83332,
|
|
21
|
+
}])
|
|
22
|
+
|
|
23
|
+
marked = utils.calculate_mtm([], trades, mds="mds")
|
|
24
|
+
|
|
25
|
+
assert marked.loc[0, "mtm"] == -1188
|
|
26
|
+
assert marked.loc[0, "gross_pnl"] == pytest.approx(13856.99976)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from tradingapi.utils import symbol_matches_order
|
|
2
|
+
|
|
3
|
+
PUT = "SENSEX_OPT_20261015_PUT_71800"
|
|
4
|
+
CALL_741 = "SENSEX_OPT_20261015_CALL_74100"
|
|
5
|
+
CALL_743 = "SENSEX_OPT_20261015_CALL_74300"
|
|
6
|
+
COMBO_741 = f"{PUT}?-20:{CALL_741}?-20"
|
|
7
|
+
COMBO_743 = f"{PUT}?-20:{CALL_743}?-20"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_shared_put_does_not_match_other_strangle():
|
|
11
|
+
assert symbol_matches_order(COMBO_741, COMBO_743) is False
|
|
12
|
+
assert symbol_matches_order(COMBO_743, COMBO_741) is False
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_same_combo_matches():
|
|
16
|
+
assert symbol_matches_order(COMBO_741, COMBO_741) is True
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_single_leg_matches_parent_combo():
|
|
20
|
+
assert symbol_matches_order(PUT, COMBO_743) is True
|
|
21
|
+
assert symbol_matches_order(CALL_741, COMBO_743) is False
|
|
@@ -964,6 +964,38 @@ def contains_earlier_date(
|
|
|
964
964
|
return now > expiry_dt
|
|
965
965
|
|
|
966
966
|
|
|
967
|
+
def _combo_leg_names(symbol_value: str) -> set:
|
|
968
|
+
"""Leg names in a symbol. A combo uses every leg, not only the first."""
|
|
969
|
+
value = str(symbol_value or "").strip()
|
|
970
|
+
if not value:
|
|
971
|
+
return set()
|
|
972
|
+
if "?" not in value and ":" not in value:
|
|
973
|
+
return {value}
|
|
974
|
+
try:
|
|
975
|
+
return set(parse_combo_symbol(value).keys())
|
|
976
|
+
except Exception:
|
|
977
|
+
name = value.split(":", 1)[0].split("?", 1)[0].strip()
|
|
978
|
+
return {name} if name else set()
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
def symbol_matches_order(requested_symbol: str, stored_symbol: str) -> bool:
|
|
982
|
+
"""Match an exit request to a stored parent symbol.
|
|
983
|
+
|
|
984
|
+
A one-leg request may hit a combo that contains that leg.
|
|
985
|
+
A combo request matches only the same set of legs, so two strangles
|
|
986
|
+
that share one strike are not both closed.
|
|
987
|
+
"""
|
|
988
|
+
if requested_symbol == stored_symbol:
|
|
989
|
+
return True
|
|
990
|
+
requested_legs = _combo_leg_names(requested_symbol)
|
|
991
|
+
stored_legs = _combo_leg_names(stored_symbol)
|
|
992
|
+
if not requested_legs or not stored_legs:
|
|
993
|
+
return False
|
|
994
|
+
if len(requested_legs) == 1:
|
|
995
|
+
return requested_legs <= stored_legs
|
|
996
|
+
return requested_legs == stored_legs
|
|
997
|
+
|
|
998
|
+
|
|
967
999
|
@log_execution_time
|
|
968
1000
|
@validate_inputs(
|
|
969
1001
|
strategy=lambda x: isinstance(x, str) and len(x.strip()) > 0,
|
|
@@ -990,31 +1022,8 @@ def get_orders_by_symbol(broker, strategy: str, long_symbol: str, broker_entry_s
|
|
|
990
1022
|
try:
|
|
991
1023
|
int_order_ids = []
|
|
992
1024
|
|
|
993
|
-
def _normalize_leg_symbol(symbol_value: str) -> str:
|
|
994
|
-
value = str(symbol_value or "").strip()
|
|
995
|
-
if not value:
|
|
996
|
-
return ""
|
|
997
|
-
if ":" in value:
|
|
998
|
-
value = value.split(":", 1)[0]
|
|
999
|
-
if "?" in value:
|
|
1000
|
-
value = value.split("?", 1)[0]
|
|
1001
|
-
return value.strip()
|
|
1002
|
-
|
|
1003
1025
|
def _symbol_matches(requested_symbol: str, stored_symbol: str) -> bool:
|
|
1004
|
-
|
|
1005
|
-
if requested_symbol == stored_symbol:
|
|
1006
|
-
return True
|
|
1007
|
-
requested_leg = _normalize_leg_symbol(requested_symbol)
|
|
1008
|
-
stored_leg = _normalize_leg_symbol(stored_symbol)
|
|
1009
|
-
if requested_leg and requested_leg == stored_leg:
|
|
1010
|
-
return True
|
|
1011
|
-
if "?" in stored_symbol:
|
|
1012
|
-
try:
|
|
1013
|
-
combo_legs = parse_combo_symbol(stored_symbol)
|
|
1014
|
-
return requested_leg in combo_legs
|
|
1015
|
-
except Exception:
|
|
1016
|
-
return False
|
|
1017
|
-
return False
|
|
1026
|
+
return symbol_matches_order(requested_symbol, stored_symbol)
|
|
1018
1027
|
|
|
1019
1028
|
# Scan for internal order IDs
|
|
1020
1029
|
try:
|
|
@@ -1903,7 +1912,9 @@ def _process_broker_order_update(broker: BrokerBase, order: Order, long_symbol:
|
|
|
1903
1912
|
exit_keys.append(broker_key)
|
|
1904
1913
|
new_orders = " ".join(exit_keys)
|
|
1905
1914
|
cast(bool, broker.redis_o.hset(order.orderRef, "exit_keys", new_orders))
|
|
1906
|
-
cast(
|
|
1915
|
+
existing_symbol = cast(str, broker.redis_o.hget(order.internal_order_id, "long_symbol") or "")
|
|
1916
|
+
if not existing_symbol:
|
|
1917
|
+
cast(bool, broker.redis_o.hset(order.internal_order_id, "long_symbol", long_symbol))
|
|
1907
1918
|
|
|
1908
1919
|
broker.redis_o.hmset(str(order.broker_order_id), {key: str(val) for key, val in order.to_dict().items()})
|
|
1909
1920
|
update_order_status(broker, order.internal_order_id, str(order.broker_order_id), eod=False)
|
|
@@ -4166,7 +4177,7 @@ def calculate_mtm(brokers: list[BrokerBase], pnl: pd.DataFrame, mds: Optional[st
|
|
|
4166
4177
|
exchange=exchange,
|
|
4167
4178
|
mds=mds,
|
|
4168
4179
|
)
|
|
4169
|
-
if all(not math.isnan(attr) for attr in [quote.bid, quote.ask]):
|
|
4180
|
+
if all(not math.isnan(attr) and attr != 0 for attr in [quote.bid, quote.ask]):
|
|
4170
4181
|
mtm_price = (quote.bid + quote.ask) / 2
|
|
4171
4182
|
else:
|
|
4172
4183
|
mtm_price = quote.last if not math.isnan(quote.last) else quote.prior_close
|
|
@@ -2,6 +2,7 @@ README.md
|
|
|
2
2
|
pyproject.toml
|
|
3
3
|
tests/test_broker_side_terminal_order.py
|
|
4
4
|
tests/test_calculate_delta_realtime_quotes.py
|
|
5
|
+
tests/test_calculate_mtm.py
|
|
5
6
|
tests/test_dhan_historical_diagnostics.py
|
|
6
7
|
tests/test_find_option_with_delta.py
|
|
7
8
|
tests/test_fivepaisa_historical_recovery.py
|
|
@@ -9,6 +10,7 @@ tests/test_fivepaisa_stream_reconnect.py
|
|
|
9
10
|
tests/test_flattrade_symbology.py
|
|
10
11
|
tests/test_market_hours_config.py
|
|
11
12
|
tests/test_place_combo_order_price_broker.py
|
|
13
|
+
tests/test_symbol_matches_order.py
|
|
12
14
|
tests/test_unfilled_eod_pnl.py
|
|
13
15
|
tradingapi/__init__.py
|
|
14
16
|
tradingapi/allocation.py
|
|
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
|
|
File without changes
|
|
File without changes
|