tradingapi 0.5.3__tar.gz → 0.5.4__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.3 → tradingapi-0.5.4}/PKG-INFO +1 -1
- {tradingapi-0.5.3 → tradingapi-0.5.4}/pyproject.toml +1 -1
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_flattrade_symbology.py +25 -1
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/flattrade.py +54 -11
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi.egg-info/PKG-INFO +1 -1
- {tradingapi-0.5.3 → tradingapi-0.5.4}/README.md +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/setup.cfg +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_broker_side_terminal_order.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_calculate_delta_realtime_quotes.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_calculate_mtm.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_dhan_historical_diagnostics.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_find_option_with_delta.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_fivepaisa_historical_recovery.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_fivepaisa_stream_reconnect.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_market_hours_config.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_place_combo_order_price_broker.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_symbol_matches_order.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tests/test_unfilled_eod_pnl.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/__init__.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/allocation.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/attribution.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/broker_base.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/config/commissions_20241216.yaml +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/config/config_sample.yaml +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/config.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/dhan.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/error_handling.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/exceptions.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/fivepaisa.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/globals.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/ib.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/icicidirect.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/icicidirect_generate_session.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/market_data_exchanges.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/market_hours.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/proxy_utils.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/shoonya.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/span.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi/utils.py +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi.egg-info/SOURCES.txt +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi.egg-info/dependency_links.txt +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi.egg-info/entry_points.txt +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi.egg-info/requires.txt +0 -0
- {tradingapi-0.5.3 → tradingapi-0.5.4}/tradingapi.egg-info/top_level.txt +0 -0
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import datetime as dt
|
|
2
|
+
import json
|
|
2
3
|
import sys
|
|
3
4
|
import tempfile
|
|
4
5
|
import unittest
|
|
5
6
|
from pathlib import Path
|
|
6
|
-
from unittest.mock import patch
|
|
7
|
+
from unittest.mock import Mock, patch
|
|
7
8
|
|
|
8
9
|
sys.path.insert(0, "/home/psharma/onedrive/code/tradingapi2")
|
|
9
10
|
|
|
@@ -11,6 +12,29 @@ from tradingapi import flattrade
|
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class FlatTradeSymbologyTest(unittest.TestCase):
|
|
15
|
+
def test_time_price_series_retries_json_decode_errors(self):
|
|
16
|
+
broker = object.__new__(flattrade.FlatTrade)
|
|
17
|
+
broker.api = Mock()
|
|
18
|
+
broker.api.get_time_price_series.side_effect = [
|
|
19
|
+
json.JSONDecodeError("empty", "", 0),
|
|
20
|
+
json.JSONDecodeError("empty", "", 0),
|
|
21
|
+
[{"time": "21-10-2026 09:15:00"}],
|
|
22
|
+
]
|
|
23
|
+
broker._wait_for_historical_rate_limit = Mock()
|
|
24
|
+
|
|
25
|
+
with patch.object(flattrade.time, "sleep"):
|
|
26
|
+
result = broker._get_time_price_series_with_retry(
|
|
27
|
+
exchange="NSE",
|
|
28
|
+
token="26000",
|
|
29
|
+
starttime=1.0,
|
|
30
|
+
endtime=2.0,
|
|
31
|
+
interval=1,
|
|
32
|
+
symbol="NIFTY_IND___",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
self.assertEqual(result, [{"time": "21-10-2026 09:15:00"}])
|
|
36
|
+
self.assertEqual(broker.api.get_time_price_series.call_count, 3)
|
|
37
|
+
|
|
14
38
|
def test_update_uses_bod_filename(self):
|
|
15
39
|
with tempfile.TemporaryDirectory() as folder:
|
|
16
40
|
symbols_path = Path(folder) / "20260902_symbols.csv"
|
|
@@ -1860,6 +1860,44 @@ class FlatTrade(BrokerBase):
|
|
|
1860
1860
|
)
|
|
1861
1861
|
self._wait_for_market_data_rate_limit_local(self._historical_rate_limit_interval_secs)
|
|
1862
1862
|
|
|
1863
|
+
def _get_time_price_series_with_retry(
|
|
1864
|
+
self,
|
|
1865
|
+
*,
|
|
1866
|
+
exchange: str,
|
|
1867
|
+
token: str,
|
|
1868
|
+
starttime: float,
|
|
1869
|
+
endtime: float,
|
|
1870
|
+
interval: int,
|
|
1871
|
+
symbol: str,
|
|
1872
|
+
) -> Optional[List]:
|
|
1873
|
+
for attempt in range(1, 4):
|
|
1874
|
+
if self.api is None:
|
|
1875
|
+
raise BrokerConnectionError("API client not initialized")
|
|
1876
|
+
try:
|
|
1877
|
+
self._wait_for_historical_rate_limit()
|
|
1878
|
+
return self.api.get_time_price_series(
|
|
1879
|
+
exchange=exchange,
|
|
1880
|
+
token=token,
|
|
1881
|
+
starttime=starttime,
|
|
1882
|
+
endtime=endtime,
|
|
1883
|
+
interval=interval,
|
|
1884
|
+
)
|
|
1885
|
+
except json.JSONDecodeError as error:
|
|
1886
|
+
trading_logger.log_warning(
|
|
1887
|
+
"JSON decode error in get_time_price_series",
|
|
1888
|
+
{
|
|
1889
|
+
"long_symbol": symbol,
|
|
1890
|
+
"exchange": exchange,
|
|
1891
|
+
"attempt": attempt,
|
|
1892
|
+
"max_attempts": 3,
|
|
1893
|
+
"error": str(error),
|
|
1894
|
+
},
|
|
1895
|
+
)
|
|
1896
|
+
if attempt == 3:
|
|
1897
|
+
raise
|
|
1898
|
+
time.sleep(0.4 * attempt)
|
|
1899
|
+
return None
|
|
1900
|
+
|
|
1863
1901
|
def _wait_for_stream_request_rate_limit(self):
|
|
1864
1902
|
limiter_redis = self._get_rate_limit_redis()
|
|
1865
1903
|
if limiter_redis is not None:
|
|
@@ -1999,11 +2037,16 @@ class FlatTrade(BrokerBase):
|
|
|
1999
2037
|
# Determine the format of symbols and create a DataFrame
|
|
2000
2038
|
if isinstance(symbols, str):
|
|
2001
2039
|
exchange = self.map_exchange_for_api(symbols, exchange)
|
|
2040
|
+
lookup_symbol = (
|
|
2041
|
+
"NIFTY_IND___"
|
|
2042
|
+
if periodicity == "1d" and symbols == "NSENIFTY_IND___"
|
|
2043
|
+
else symbols
|
|
2044
|
+
)
|
|
2002
2045
|
# Use refresh_symbol_map if available, otherwise use default mapping
|
|
2003
2046
|
if refresh_symbol_map and exchange in refresh_symbol_map:
|
|
2004
|
-
scripCode = refresh_symbol_map[exchange].get(
|
|
2047
|
+
scripCode = refresh_symbol_map[exchange].get(lookup_symbol)
|
|
2005
2048
|
else:
|
|
2006
|
-
scripCode = self.exchange_mappings[exchange]["symbol_map"].get(
|
|
2049
|
+
scripCode = self.exchange_mappings[exchange]["symbol_map"].get(lookup_symbol)
|
|
2007
2050
|
if scripCode:
|
|
2008
2051
|
symbols_pd = pd.DataFrame([{"long_symbol": symbols, "Scripcode": scripCode}])
|
|
2009
2052
|
else:
|
|
@@ -2039,10 +2082,7 @@ class FlatTrade(BrokerBase):
|
|
|
2039
2082
|
resolved_market_open_time = market_open_time or get_market_open_time(
|
|
2040
2083
|
exchange=exchange, symbol=row_outer["long_symbol"], as_of=date_start
|
|
2041
2084
|
)
|
|
2042
|
-
|
|
2043
|
-
row_outer["long_symbol"] = "NSENIFTY" + s[s.find("_") :] if s.startswith("NIFTY_") else s
|
|
2044
|
-
# we do the above remapping for downloading permin data to database for legacy reasons.
|
|
2045
|
-
# once NSENIFTY is amended to NIFTY in databae, we can remove this line.
|
|
2085
|
+
row_outer["long_symbol"] = row_outer["long_symbol"].replace("/", "-")
|
|
2046
2086
|
|
|
2047
2087
|
# Parse date_start (accepts datetime, date, or string) -> datetime for API
|
|
2048
2088
|
date_start_parsed = None
|
|
@@ -2074,19 +2114,22 @@ class FlatTrade(BrokerBase):
|
|
|
2074
2114
|
raise BrokerConnectionError("API client not initialized")
|
|
2075
2115
|
data: Optional[List] = None
|
|
2076
2116
|
if periodicity.endswith("m"):
|
|
2077
|
-
self.
|
|
2078
|
-
data = self.api.get_time_price_series(
|
|
2117
|
+
data = self._get_time_price_series_with_retry(
|
|
2079
2118
|
exchange=exch,
|
|
2080
2119
|
token=str(row_outer["Scripcode"]),
|
|
2081
2120
|
starttime=date_start_dt.timestamp(),
|
|
2082
2121
|
endtime=date_end_dt.timestamp(),
|
|
2083
2122
|
interval=extract_number(periodicity),
|
|
2123
|
+
symbol=row_outer["long_symbol"],
|
|
2084
2124
|
)
|
|
2085
2125
|
elif periodicity == "1d":
|
|
2086
|
-
|
|
2087
|
-
|
|
2126
|
+
lookup_symbol = (
|
|
2127
|
+
"NIFTY_IND___"
|
|
2128
|
+
if row_outer["long_symbol"] == "NSENIFTY_IND___"
|
|
2129
|
+
else row_outer["long_symbol"]
|
|
2130
|
+
)
|
|
2088
2131
|
trading_symbol = self.exchange_mappings[exchange]["tradingsymbol_map"].get(
|
|
2089
|
-
|
|
2132
|
+
lookup_symbol
|
|
2090
2133
|
)
|
|
2091
2134
|
|
|
2092
2135
|
def _timeout_handler(signum, frame) -> None:
|
|
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
|
|
File without changes
|
|
File without changes
|