openstockapi 0.1.0__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.
Files changed (41) hide show
  1. openstockapi-0.1.0/LICENSE +176 -0
  2. openstockapi-0.1.0/NOTICE +5 -0
  3. openstockapi-0.1.0/PKG-INFO +46 -0
  4. openstockapi-0.1.0/README.md +19 -0
  5. openstockapi-0.1.0/openstockapi/__init__.py +32 -0
  6. openstockapi-0.1.0/openstockapi/api/financial.py +48 -0
  7. openstockapi-0.1.0/openstockapi/api/fund.py +32 -0
  8. openstockapi-0.1.0/openstockapi/api/macro.py +35 -0
  9. openstockapi-0.1.0/openstockapi/api/news.py +60 -0
  10. openstockapi-0.1.0/openstockapi/api/orderbook.py +15 -0
  11. openstockapi-0.1.0/openstockapi/api/stock.py +116 -0
  12. openstockapi-0.1.0/openstockapi/api/trading.py +79 -0
  13. openstockapi-0.1.0/openstockapi/config/settings.py +13 -0
  14. openstockapi-0.1.0/openstockapi/core/base_provider.py +54 -0
  15. openstockapi-0.1.0/openstockapi/core/exceptions.py +29 -0
  16. openstockapi-0.1.0/openstockapi/core/http_client.py +62 -0
  17. openstockapi-0.1.0/openstockapi/core/models.py +67 -0
  18. openstockapi-0.1.0/openstockapi/core/models_fund.py +22 -0
  19. openstockapi-0.1.0/openstockapi/core/models_macro.py +12 -0
  20. openstockapi-0.1.0/openstockapi/core/models_news.py +20 -0
  21. openstockapi-0.1.0/openstockapi/core/models_trading.py +39 -0
  22. openstockapi-0.1.0/openstockapi/core/rate_limiter.py +36 -0
  23. openstockapi-0.1.0/openstockapi/core/security.py +19 -0
  24. openstockapi-0.1.0/openstockapi/core/types.py +6 -0
  25. openstockapi-0.1.0/openstockapi/core/utils.py +23 -0
  26. openstockapi-0.1.0/openstockapi/license/session.py +36 -0
  27. openstockapi-0.1.0/openstockapi/providers/__init__.py +21 -0
  28. openstockapi-0.1.0/openstockapi/providers/dnse.py +104 -0
  29. openstockapi-0.1.0/openstockapi/providers/fmarket.py +58 -0
  30. openstockapi-0.1.0/openstockapi/providers/kbs.py +69 -0
  31. openstockapi-0.1.0/openstockapi/providers/mas.py +92 -0
  32. openstockapi-0.1.0/openstockapi/providers/mbk.py +49 -0
  33. openstockapi-0.1.0/openstockapi/providers/vci.py +108 -0
  34. openstockapi-0.1.0/openstockapi/providers/vndirect.py +49 -0
  35. openstockapi-0.1.0/openstockapi.egg-info/PKG-INFO +46 -0
  36. openstockapi-0.1.0/openstockapi.egg-info/SOURCES.txt +39 -0
  37. openstockapi-0.1.0/openstockapi.egg-info/dependency_links.txt +1 -0
  38. openstockapi-0.1.0/openstockapi.egg-info/requires.txt +11 -0
  39. openstockapi-0.1.0/openstockapi.egg-info/top_level.txt +1 -0
  40. openstockapi-0.1.0/pyproject.toml +46 -0
  41. openstockapi-0.1.0/setup.cfg +4 -0
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if distributed along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,5 @@
1
+ OpenStockAPI
2
+ Copyright 2026 OpenStockAPI Contributors
3
+
4
+ This product includes software developed at
5
+ https://github.com/openstockapi/openstockapi (or your repository URL)
@@ -0,0 +1,46 @@
1
+ Metadata-Version: 2.4
2
+ Name: openstockapi
3
+ Version: 0.1.0
4
+ Summary: Vietnam stock market data library — open source data plane
5
+ Author-email: Your Name <your.email@example.com>
6
+ License: Apache-2.0
7
+ Classifier: License :: OSI Approved :: Apache Software License
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Operating System :: OS Independent
13
+ Requires-Python: >=3.10
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ License-File: NOTICE
17
+ Requires-Dist: httpx>=0.27
18
+ Requires-Dist: pydantic>=2.0
19
+ Provides-Extra: pandas
20
+ Requires-Dist: pandas>=2.0; extra == "pandas"
21
+ Provides-Extra: dev
22
+ Requires-Dist: pytest; extra == "dev"
23
+ Requires-Dist: pytest-asyncio; extra == "dev"
24
+ Requires-Dist: mypy; extra == "dev"
25
+ Requires-Dist: ruff; extra == "dev"
26
+ Dynamic: license-file
27
+
28
+ # OpenStockAPI
29
+
30
+ An open-source Python library serving as a modular **Data Plane** to collect and normalize Vietnam stock market data.
31
+
32
+ ## Features
33
+
34
+ - **Multi-source Providers**: Abstracted design supporting multiple sources (DNSE, MAS, VCI, VNDIRECT, etc.) with automatic fallback.
35
+ - **Freemium Tier Model**: Supports Free, Pro (200 req/min), and Premium (500 req/min) tiers with local rate limiting.
36
+ - **Control Plane Ready**: Built-in hooks to integrate with a license/heartbeat server.
37
+
38
+ ## Installation
39
+
40
+ ```bash
41
+ pip install -e .
42
+ ```
43
+
44
+ ## License
45
+
46
+ Apache License 2.0
@@ -0,0 +1,19 @@
1
+ # OpenStockAPI
2
+
3
+ An open-source Python library serving as a modular **Data Plane** to collect and normalize Vietnam stock market data.
4
+
5
+ ## Features
6
+
7
+ - **Multi-source Providers**: Abstracted design supporting multiple sources (DNSE, MAS, VCI, VNDIRECT, etc.) with automatic fallback.
8
+ - **Freemium Tier Model**: Supports Free, Pro (200 req/min), and Premium (500 req/min) tiers with local rate limiting.
9
+ - **Control Plane Ready**: Built-in hooks to integrate with a license/heartbeat server.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ pip install -e .
15
+ ```
16
+
17
+ ## License
18
+
19
+ Apache License 2.0
@@ -0,0 +1,32 @@
1
+ __version__ = "0.1.0"
2
+
3
+ from openstockapi.api.stock import ohlcv, profile, quote, async_ohlcv
4
+ from openstockapi.api.financial import income_statement, balance_sheet, cashflow, ratios
5
+ from openstockapi.api.trading import foreign, insider, prop_trade
6
+ from openstockapi.api.orderbook import bid_ask, depth
7
+ from openstockapi.api.macro import indicators
8
+ from openstockapi.api.fund import details as fund_details
9
+ from openstockapi.api.news import news as company_news, events as company_events
10
+
11
+ from openstockapi.core import exceptions
12
+ from openstockapi.license import session
13
+
14
+ __all__ = [
15
+ "ohlcv",
16
+ "async_ohlcv",
17
+ "profile",
18
+ "quote",
19
+ "income_statement",
20
+ "balance_sheet",
21
+ "cashflow",
22
+ "ratios",
23
+ "foreign",
24
+ "insider",
25
+ "prop_trade",
26
+ "bid_ask",
27
+ "depth",
28
+ "indicators",
29
+ "fund_details",
30
+ "company_news",
31
+ "company_events",
32
+ ]
@@ -0,0 +1,48 @@
1
+ from typing import List, Optional, Union, Any
2
+ from openstockapi.core.types import DataTier
3
+ from openstockapi.core.security import enforce_tier_and_rate_limit
4
+ from openstockapi.core.utils import clean_symbol
5
+ from openstockapi.config.settings import DEFAULT_PROVIDER_PRIORITY
6
+ from openstockapi.providers import get_provider
7
+ from openstockapi.core.exceptions import ProviderUnavailableError
8
+
9
+ try:
10
+ import pandas as pd
11
+ HAS_PANDAS = True
12
+ except ImportError:
13
+ HAS_PANDAS = False
14
+
15
+ def _fetch_financial(symbol: str, stmt_type: str, period: str, provider: Optional[str]) -> Union[List[dict], Any]:
16
+ enforce_tier_and_rate_limit(DataTier.FREE, f"financial.{stmt_type}")
17
+ symbol = clean_symbol(symbol)
18
+
19
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("financials", ["mas"])
20
+
21
+ last_err = None
22
+ for p_name in providers_to_try:
23
+ p_inst = get_provider(p_name)
24
+ if not p_inst:
25
+ continue
26
+ try:
27
+ reports = p_inst.get_financial_statements(symbol, stmt_type, period)
28
+ data_list = [rep.model_dump() for rep in reports]
29
+ if HAS_PANDAS:
30
+ return pd.DataFrame(data_list)
31
+ return data_list
32
+ except Exception as e:
33
+ last_err = e
34
+ continue
35
+
36
+ raise ProviderUnavailableError(f"No provider succeeded in fetching financial statement '{stmt_type}' for '{symbol}': {last_err}")
37
+
38
+ def income_statement(symbol: str, period: str = "Q", provider: Optional[str] = None) -> Union[List[dict], Any]:
39
+ return _fetch_financial(symbol, "income", period, provider)
40
+
41
+ def balance_sheet(symbol: str, period: str = "Q", provider: Optional[str] = None) -> Union[List[dict], Any]:
42
+ return _fetch_financial(symbol, "balance", period, provider)
43
+
44
+ def cashflow(symbol: str, period: str = "Q", provider: Optional[str] = None) -> Union[List[dict], Any]:
45
+ return _fetch_financial(symbol, "cashflow", period, provider)
46
+
47
+ def ratios(symbol: str, period: str = "Q", provider: Optional[str] = None) -> Union[List[dict], Any]:
48
+ return _fetch_financial(symbol, "ratios", period, provider)
@@ -0,0 +1,32 @@
1
+ from typing import Optional, Union, Any
2
+ from openstockapi.core.types import DataTier
3
+ from openstockapi.core.security import enforce_tier_and_rate_limit
4
+ from openstockapi.config.settings import DEFAULT_PROVIDER_PRIORITY
5
+ from openstockapi.providers import get_provider
6
+ from openstockapi.core.exceptions import ProviderUnavailableError
7
+
8
+ try:
9
+ import pandas as pd
10
+ HAS_PANDAS = True
11
+ except ImportError:
12
+ HAS_PANDAS = False
13
+
14
+ def details(fund_id: int, provider: Optional[str] = None) -> Union[dict, Any]:
15
+ # Mutual fund data is a FREE endpoint
16
+ enforce_tier_and_rate_limit(DataTier.FREE, "fund.details")
17
+
18
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("fund", ["fmarket"])
19
+
20
+ last_err = None
21
+ for p_name in providers_to_try:
22
+ p_inst = get_provider(p_name)
23
+ if not p_inst:
24
+ continue
25
+ try:
26
+ fund_data = p_inst.get_fund_details(fund_id)
27
+ return fund_data.model_dump()
28
+ except Exception as e:
29
+ last_err = e
30
+ continue
31
+
32
+ raise ProviderUnavailableError(f"No provider succeeded in fetching details for fund_id '{fund_id}': {last_err}")
@@ -0,0 +1,35 @@
1
+ from typing import List, Optional, Union, Any
2
+ from openstockapi.core.types import DataTier
3
+ from openstockapi.core.security import enforce_tier_and_rate_limit
4
+ from openstockapi.config.settings import DEFAULT_PROVIDER_PRIORITY
5
+ from openstockapi.providers import get_provider
6
+ from openstockapi.core.exceptions import ProviderUnavailableError
7
+
8
+ try:
9
+ import pandas as pd
10
+ HAS_PANDAS = True
11
+ except ImportError:
12
+ HAS_PANDAS = False
13
+
14
+ def indicators(provider: Optional[str] = None) -> Union[List[dict], Any]:
15
+ # Macro indicators is a FREE endpoint
16
+ enforce_tier_and_rate_limit(DataTier.FREE, "macro.indicators")
17
+
18
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("macro", ["mbk"])
19
+
20
+ last_err = None
21
+ for p_name in providers_to_try:
22
+ p_inst = get_provider(p_name)
23
+ if not p_inst:
24
+ continue
25
+ try:
26
+ records = p_inst.get_macro_indicators()
27
+ data_list = [rec.model_dump() for rec in records]
28
+ if HAS_PANDAS:
29
+ return pd.DataFrame(data_list)
30
+ return data_list
31
+ except Exception as e:
32
+ last_err = e
33
+ continue
34
+
35
+ raise ProviderUnavailableError(f"No provider succeeded in fetching macro indicators: {last_err}")
@@ -0,0 +1,60 @@
1
+ from typing import List, Optional, Union, Any
2
+ from openstockapi.core.types import DataTier
3
+ from openstockapi.core.security import enforce_tier_and_rate_limit
4
+ from openstockapi.core.utils import clean_symbol
5
+ from openstockapi.config.settings import DEFAULT_PROVIDER_PRIORITY
6
+ from openstockapi.providers import get_provider
7
+ from openstockapi.core.exceptions import ProviderUnavailableError
8
+
9
+ try:
10
+ import pandas as pd
11
+ HAS_PANDAS = True
12
+ except ImportError:
13
+ HAS_PANDAS = False
14
+
15
+ def news(symbol: str, limit: int = 10, provider: Optional[str] = None) -> Union[List[dict], Any]:
16
+ # News is a FREE endpoint
17
+ enforce_tier_and_rate_limit(DataTier.FREE, "news")
18
+ symbol = clean_symbol(symbol)
19
+
20
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("news", ["kbs"])
21
+
22
+ last_err = None
23
+ for p_name in providers_to_try:
24
+ p_inst = get_provider(p_name)
25
+ if not p_inst:
26
+ continue
27
+ try:
28
+ items = p_inst.get_company_news(symbol, limit)
29
+ data_list = [item.model_dump() for item in items]
30
+ if HAS_PANDAS:
31
+ return pd.DataFrame(data_list)
32
+ return data_list
33
+ except Exception as e:
34
+ last_err = e
35
+ continue
36
+
37
+ raise ProviderUnavailableError(f"No provider succeeded in fetching news for symbol '{symbol}': {last_err}")
38
+
39
+ def events(symbol: str, limit: int = 10, provider: Optional[str] = None) -> Union[List[dict], Any]:
40
+ enforce_tier_and_rate_limit(DataTier.FREE, "events")
41
+ symbol = clean_symbol(symbol)
42
+
43
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("news", ["kbs"])
44
+
45
+ last_err = None
46
+ for p_name in providers_to_try:
47
+ p_inst = get_provider(p_name)
48
+ if not p_inst:
49
+ continue
50
+ try:
51
+ items = p_inst.get_company_events(symbol, limit)
52
+ data_list = [item.model_dump() for item in items]
53
+ if HAS_PANDAS:
54
+ return pd.DataFrame(data_list)
55
+ return data_list
56
+ except Exception as e:
57
+ last_err = e
58
+ continue
59
+
60
+ raise ProviderUnavailableError(f"No provider succeeded in fetching events for symbol '{symbol}': {last_err}")
@@ -0,0 +1,15 @@
1
+ from typing import Optional, Union, Any
2
+ from openstockapi.core.types import DataTier
3
+ from openstockapi.core.security import enforce_tier_and_rate_limit
4
+ from openstockapi.core.utils import clean_symbol
5
+
6
+ def bid_ask(symbol: str) -> Union[dict, Any]:
7
+ # PRO or PREMIUM Tier required
8
+ enforce_tier_and_rate_limit(DataTier.PRO, "orderbook.bid_ask")
9
+ symbol = clean_symbol(symbol)
10
+ raise NotImplementedError("Real-time Order Book APIs are premium only.")
11
+
12
+ def depth(symbol: str) -> Union[dict, Any]:
13
+ enforce_tier_and_rate_limit(DataTier.PRO, "orderbook.depth")
14
+ symbol = clean_symbol(symbol)
15
+ raise NotImplementedError("Real-time Order Book Depth APIs are premium only.")
@@ -0,0 +1,116 @@
1
+ from typing import List, Union, Optional, Any
2
+ from openstockapi.core.types import DataTier
3
+ from openstockapi.core.security import enforce_tier_and_rate_limit
4
+ from openstockapi.core.utils import clean_symbol
5
+ from openstockapi.config.settings import DEFAULT_PROVIDER_PRIORITY
6
+ from openstockapi.providers import get_provider
7
+ from openstockapi.core.exceptions import ProviderUnavailableError
8
+ from openstockapi.core.models import OHLCVBar, CompanyProfile, RealtimeQuote
9
+
10
+ # Optional import for Pandas support
11
+ try:
12
+ import pandas as pd
13
+ HAS_PANDAS = True
14
+ except ImportError:
15
+ HAS_PANDAS = False
16
+
17
+ def ohlcv(symbol: str, resolution: str = "1D", start: Optional[str] = None, end: Optional[str] = None, provider: Optional[str] = None) -> Union[List[dict], Any]:
18
+ # 1. Enforce safety checks
19
+ enforce_tier_and_rate_limit(DataTier.FREE, "stock.ohlcv")
20
+
21
+ # 2. Standardize inputs
22
+ symbol = clean_symbol(symbol)
23
+ if start is None:
24
+ start = "2020-01-01"
25
+ if end is None:
26
+ import datetime
27
+ end = datetime.datetime.now().strftime("%Y-%m-%d")
28
+
29
+ # 3. Resolve provider priority list
30
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("ohlcv", ["dnse"])
31
+
32
+ last_err = None
33
+ for p_name in providers_to_try:
34
+ p_inst = get_provider(p_name)
35
+ if not p_inst:
36
+ continue
37
+ try:
38
+ bars = p_inst.get_ohlcv(symbol, resolution, start, end)
39
+ data_list = [bar.model_dump() for bar in bars]
40
+ if HAS_PANDAS:
41
+ return pd.DataFrame(data_list)
42
+ return data_list
43
+ except Exception as e:
44
+ last_err = e
45
+ continue
46
+
47
+ raise ProviderUnavailableError(f"No provider succeeded in fetching OHLCV for '{symbol}': {last_err}")
48
+
49
+ async def async_ohlcv(symbol: str, resolution: str = "1D", start: Optional[str] = None, end: Optional[str] = None, provider: Optional[str] = None) -> Union[List[dict], Any]:
50
+ enforce_tier_and_rate_limit(DataTier.FREE, "stock.ohlcv")
51
+ symbol = clean_symbol(symbol)
52
+ if start is None:
53
+ start = "2020-01-01"
54
+ if end is None:
55
+ import datetime
56
+ end = datetime.datetime.now().strftime("%Y-%m-%d")
57
+
58
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("ohlcv", ["dnse"])
59
+
60
+ last_err = None
61
+ for p_name in providers_to_try:
62
+ p_inst = get_provider(p_name)
63
+ if not p_inst:
64
+ continue
65
+ try:
66
+ bars = await p_inst.async_get_ohlcv(symbol, resolution, start, end)
67
+ data_list = [bar.model_dump() for bar in bars]
68
+ if HAS_PANDAS:
69
+ return pd.DataFrame(data_list)
70
+ return data_list
71
+ except Exception as e:
72
+ last_err = e
73
+ continue
74
+
75
+ raise ProviderUnavailableError(f"No provider succeeded in fetching async OHLCV for '{symbol}': {last_err}")
76
+
77
+ def profile(symbol: str, provider: Optional[str] = None) -> Union[dict, Any]:
78
+ enforce_tier_and_rate_limit(DataTier.FREE, "stock.profile")
79
+ symbol = clean_symbol(symbol)
80
+
81
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("profile", ["vndirect"])
82
+
83
+ last_err = None
84
+ for p_name in providers_to_try:
85
+ p_inst = get_provider(p_name)
86
+ if not p_inst:
87
+ continue
88
+ try:
89
+ prof = p_inst.get_company_profile(symbol)
90
+ return prof.model_dump()
91
+ except Exception as e:
92
+ last_err = e
93
+ continue
94
+
95
+ raise ProviderUnavailableError(f"No provider succeeded in fetching profile for '{symbol}': {last_err}")
96
+
97
+ def quote(symbol: str, provider: Optional[str] = None) -> Union[dict, Any]:
98
+ # PREMIUM / PRO Tier endpoint
99
+ enforce_tier_and_rate_limit(DataTier.PRO, "stock.quote")
100
+ symbol = clean_symbol(symbol)
101
+
102
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("quote", ["dnse"])
103
+
104
+ last_err = None
105
+ for p_name in providers_to_try:
106
+ p_inst = get_provider(p_name)
107
+ if not p_inst:
108
+ continue
109
+ try:
110
+ q = p_inst.get_realtime_quote(symbol)
111
+ return q.model_dump()
112
+ except Exception as e:
113
+ last_err = e
114
+ continue
115
+
116
+ raise ProviderUnavailableError(f"No provider succeeded in fetching quote for '{symbol}': {last_err}")
@@ -0,0 +1,79 @@
1
+ from typing import List, Optional, Union, Any
2
+ from openstockapi.core.types import DataTier
3
+ from openstockapi.core.security import enforce_tier_and_rate_limit
4
+ from openstockapi.core.utils import clean_symbol
5
+ from openstockapi.config.settings import DEFAULT_PROVIDER_PRIORITY
6
+ from openstockapi.providers import get_provider
7
+ from openstockapi.core.exceptions import ProviderUnavailableError
8
+
9
+ try:
10
+ import pandas as pd
11
+ HAS_PANDAS = True
12
+ except ImportError:
13
+ HAS_PANDAS = False
14
+
15
+ def foreign(symbol: str, limit: int = 10, provider: Optional[str] = None) -> Union[List[dict], Any]:
16
+ enforce_tier_and_rate_limit(DataTier.PRO, "trading.foreign")
17
+ symbol = clean_symbol(symbol)
18
+
19
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("trading", ["vci"])
20
+
21
+ last_err = None
22
+ for p_name in providers_to_try:
23
+ p_inst = get_provider(p_name)
24
+ if not p_inst:
25
+ continue
26
+ try:
27
+ records = p_inst.get_foreign_trading(symbol, limit)
28
+ data_list = [rec.model_dump() for rec in records]
29
+ if HAS_PANDAS:
30
+ return pd.DataFrame(data_list)
31
+ return data_list
32
+ except Exception as e:
33
+ last_err = e
34
+ continue
35
+ raise ProviderUnavailableError(f"No provider succeeded in fetching foreign trading for '{symbol}': {last_err}")
36
+
37
+ def insider(symbol: str, limit: int = 10, provider: Optional[str] = None) -> Union[List[dict], Any]:
38
+ enforce_tier_and_rate_limit(DataTier.PRO, "trading.insider")
39
+ symbol = clean_symbol(symbol)
40
+
41
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("trading", ["vci"])
42
+
43
+ last_err = None
44
+ for p_name in providers_to_try:
45
+ p_inst = get_provider(p_name)
46
+ if not p_inst:
47
+ continue
48
+ try:
49
+ records = p_inst.get_insider_trading(symbol, limit)
50
+ data_list = [rec.model_dump() for rec in records]
51
+ if HAS_PANDAS:
52
+ return pd.DataFrame(data_list)
53
+ return data_list
54
+ except Exception as e:
55
+ last_err = e
56
+ continue
57
+ raise ProviderUnavailableError(f"No provider succeeded in fetching insider transactions for '{symbol}': {last_err}")
58
+
59
+ def prop_trade(symbol: str, limit: int = 10, provider: Optional[str] = None) -> Union[List[dict], Any]:
60
+ enforce_tier_and_rate_limit(DataTier.PRO, "trading.prop_trade")
61
+ symbol = clean_symbol(symbol)
62
+
63
+ providers_to_try = [provider] if provider else DEFAULT_PROVIDER_PRIORITY.get("trading", ["vci"])
64
+
65
+ last_err = None
66
+ for p_name in providers_to_try:
67
+ p_inst = get_provider(p_name)
68
+ if not p_inst:
69
+ continue
70
+ try:
71
+ records = p_inst.get_prop_trading(symbol, limit)
72
+ data_list = [rec.model_dump() for rec in records]
73
+ if HAS_PANDAS:
74
+ return pd.DataFrame(data_list)
75
+ return data_list
76
+ except Exception as e:
77
+ last_err = e
78
+ continue
79
+ raise ProviderUnavailableError(f"No provider succeeded in fetching proprietary trading for '{symbol}': {last_err}")