pwb-toolbox 0.1.3__tar.gz → 0.1.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.
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/PKG-INFO +1 -1
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pwb_toolbox/datasets/__init__.py +58 -13
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pwb_toolbox.egg-info/PKG-INFO +1 -1
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/setup.cfg +1 -1
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/LICENSE.txt +0 -0
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/README.md +0 -0
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pwb_toolbox/__init__.py +0 -0
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pwb_toolbox.egg-info/SOURCES.txt +0 -0
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pwb_toolbox.egg-info/dependency_links.txt +0 -0
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pwb_toolbox.egg-info/requires.txt +0 -0
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pwb_toolbox.egg-info/top_level.txt +0 -0
- {pwb_toolbox-0.1.3 → pwb_toolbox-0.1.4}/pyproject.toml +0 -0
@@ -861,22 +861,54 @@ def __extend_etfs(df_etfs):
|
|
861
861
|
return df
|
862
862
|
|
863
863
|
|
864
|
+
ALLOWED_FIELDS = {"open", "high", "low", "close"}
|
865
|
+
|
866
|
+
|
864
867
|
def get_pricing(
|
865
868
|
symbol_list,
|
866
|
-
fields=
|
869
|
+
fields=None, # ← default set below
|
867
870
|
start_date="1980-01-01",
|
868
871
|
end_date=date.today().isoformat(),
|
869
872
|
extend=False,
|
873
|
+
keep_single_level=True, # backward-compat flag
|
870
874
|
):
|
871
875
|
"""
|
872
|
-
|
876
|
+
Fetch OHLC pricing for the requested symbols.
|
877
|
+
|
878
|
+
Parameters
|
879
|
+
----------
|
880
|
+
symbol_list : str | list[str]
|
881
|
+
One ticker or a list of tickers.
|
882
|
+
fields : list[str] | None
|
883
|
+
Any subset of ["open", "high", "low", "close"].
|
884
|
+
Defaults to ["close"] for backward compatibility.
|
885
|
+
start_date, end_date : str (YYYY-MM-DD)
|
886
|
+
Slice the date index (inclusive).
|
887
|
+
extend : bool
|
888
|
+
Pass-through to `load_dataset(..., extend=extend)`.
|
889
|
+
keep_single_level : bool
|
890
|
+
If `True` and only one field is requested, flatten the columns so the
|
891
|
+
output matches the old behaviour (columns = symbols). If `False`
|
892
|
+
you always get a two-level MultiIndex `(symbol, field)`.
|
893
|
+
|
894
|
+
Returns
|
895
|
+
-------
|
896
|
+
pd.DataFrame
|
897
|
+
* MultiIndex columns (symbol, field) when `len(fields) > 1`
|
898
|
+
* Single-level columns (symbol) when one field & keep_single_level=True
|
873
899
|
"""
|
900
|
+
# ------------------------------------------------------------------ sanity
|
901
|
+
if fields is None:
|
902
|
+
fields = ["close"]
|
874
903
|
if isinstance(symbol_list, str):
|
875
904
|
symbol_list = [symbol_list]
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
905
|
+
|
906
|
+
fields = [f.lower() for f in fields]
|
907
|
+
bad = [f for f in fields if f not in ALLOWED_FIELDS]
|
908
|
+
if bad:
|
909
|
+
raise ValueError(f"Invalid field(s): {bad}. Allowed: {sorted(ALLOWED_FIELDS)}")
|
910
|
+
|
911
|
+
# --------------------------------------------------------------- download
|
880
912
|
df = pd.concat(
|
881
913
|
[
|
882
914
|
load_dataset("Stocks-Daily-Price", symbol_list, extend=extend),
|
@@ -884,14 +916,27 @@ def get_pricing(
|
|
884
916
|
load_dataset("Cryptocurrencies-Daily-Price", symbol_list, extend=extend),
|
885
917
|
load_dataset("Bonds-Daily-Price", symbol_list, extend=extend),
|
886
918
|
load_dataset("Commodities-Daily-Price", symbol_list, extend=extend),
|
887
|
-
]
|
919
|
+
],
|
920
|
+
ignore_index=True,
|
888
921
|
)
|
922
|
+
|
889
923
|
df["date"] = pd.to_datetime(df["date"])
|
890
924
|
df.set_index("date", inplace=True)
|
891
925
|
df.sort_index(inplace=True)
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
)
|
896
|
-
|
897
|
-
|
926
|
+
df = df.loc[start_date:end_date]
|
927
|
+
|
928
|
+
# ------------------------------------------------------------- reshape
|
929
|
+
# Pivot can accept a list of values: returns columns = (field, symbol)
|
930
|
+
prices = df.pivot_table(values=fields, index=df.index, columns="symbol")
|
931
|
+
|
932
|
+
# Make outer level = symbol, inner = field → pivot_df[sym] gives OHLC block
|
933
|
+
prices = prices.swaplevel(axis=1).sort_index(axis=1)
|
934
|
+
|
935
|
+
# Optional: flatten back to the legacy layout if only one field requested
|
936
|
+
if keep_single_level and len(fields) == 1:
|
937
|
+
field = fields[0]
|
938
|
+
prices.columns = prices.columns.droplevel(1) # keep only the symbol names
|
939
|
+
# optional: rename index level for clarity
|
940
|
+
prices.name = field
|
941
|
+
|
942
|
+
return prices
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|