pwb-toolbox 0.1.4__tar.gz → 0.1.5__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pwb-toolbox
3
- Version: 0.1.4
3
+ Version: 0.1.5
4
4
  Summary: A toolbox library for quant traders
5
5
  Home-page: https://github.com/paperswithbacktest/pwb-toolbox
6
6
  Author: Your Name
@@ -627,7 +627,9 @@ def load_dataset(
627
627
  return df
628
628
 
629
629
 
630
- def __convert_indices_to_usd(df_indices, df_forex):
630
+ def __convert_indices_to_usd(
631
+ df_indices: pd.DataFrame, df_forex: pd.DataFrame
632
+ ) -> pd.DataFrame:
631
633
  mapping = {
632
634
  "ADSMI": "AED", # United Arab Emirates
633
635
  "AEX": "EUR", # Netherlands
@@ -727,32 +729,40 @@ def __convert_indices_to_usd(df_indices, df_forex):
727
729
  "SX5E": "EUR", # Europe
728
730
  "TA125": "ILS", # Israel
729
731
  }
730
- symbols = df_indices.symbol.unique()
731
- mapping = {k: v for k, v in mapping.items() if k in symbols}
732
732
  frames = []
733
- for symbol, currency in mapping.items():
734
- df_index = df_indices[df_indices["symbol"] == symbol].copy()
735
- if currency == "USD":
736
- frames.append(df_index)
733
+
734
+ # iterate over the symbols that actually exist in df_indices
735
+ for symbol in df_indices["symbol"].unique():
736
+ df_idx = df_indices[df_indices["symbol"] == symbol].copy()
737
+
738
+ # 1) Figure out what currency the index is quoted in.
739
+ ccy = mapping.get(symbol) # None if not mapped
740
+ if ccy is None or ccy == "USD":
741
+ # Unknown or already USD – just keep the original rows
742
+ frames.append(df_idx)
737
743
  continue
738
- df_forex_currency = df_forex[df_forex["symbol"] == currency + "USD"].copy()
739
- if df_index.empty or df_forex_currency.empty:
744
+
745
+ # 2) Find the matching FX rate (home-ccy → USD)
746
+ pair = ccy + "USD"
747
+ df_fx = df_forex[df_forex["symbol"] == pair].copy()
748
+
749
+ if df_idx.empty or df_fx.empty:
750
+ # No FX data – keep raw index levels instead of dropping them
751
+ frames.append(df_idx)
740
752
  continue
741
- # Merge dataframes on the date column
742
- merged_df = pd.merge(
743
- df_index, df_forex_currency, on="date", suffixes=("", "_forex")
744
- )
745
753
 
746
- # Multiply the index prices by the corresponding forex rates
747
- merged_df["open"] = merged_df["open"] * merged_df["open_forex"]
748
- merged_df["high"] = merged_df["high"] * merged_df["high_forex"]
749
- merged_df["low"] = merged_df["low"] * merged_df["low_forex"]
750
- merged_df["close"] = merged_df["close"] * merged_df["close_forex"]
754
+ # 3) Merge on date and convert OHLC
755
+ merged = pd.merge(df_idx, df_fx, on="date", suffixes=("", "_fx"))
756
+ for col in ("open", "high", "low", "close"):
757
+ merged[col] = merged[col] * merged[f"{col}_fx"]
751
758
 
752
- frames.append(merged_df[["symbol", "date", "open", "high", "low", "close"]])
759
+ frames.append(merged[["symbol", "date", "open", "high", "low", "close"]])
753
760
 
754
- df = pd.concat(frames, ignore_index=True)
755
- return df
761
+ if not frames:
762
+ return pd.DataFrame(columns=df_indices.columns)
763
+
764
+ # Combine everything back into one DataFrame
765
+ return pd.concat(frames, ignore_index=True)
756
766
 
757
767
 
758
768
  def __extract_years_to_maturity(bond_symbol):
@@ -909,16 +919,24 @@ def get_pricing(
909
919
  raise ValueError(f"Invalid field(s): {bad}. Allowed: {sorted(ALLOWED_FIELDS)}")
910
920
 
911
921
  # --------------------------------------------------------------- download
912
- df = pd.concat(
913
- [
914
- load_dataset("Stocks-Daily-Price", symbol_list, extend=extend),
915
- load_dataset("ETFs-Daily-Price", symbol_list, extend=extend),
916
- load_dataset("Cryptocurrencies-Daily-Price", symbol_list, extend=extend),
917
- load_dataset("Bonds-Daily-Price", symbol_list, extend=extend),
918
- load_dataset("Commodities-Daily-Price", symbol_list, extend=extend),
919
- ],
920
- ignore_index=True,
921
- )
922
+ DATASETS = [
923
+ ("Stocks-Daily-Price", extend),
924
+ ("ETFs-Daily-Price", extend),
925
+ ("Cryptocurrencies-Daily-Price", extend),
926
+ ("Bonds-Daily-Price", extend),
927
+ ("Commodities-Daily-Price", extend),
928
+ ("Indices-Daily-Price", False), # indices generally have no proxy data
929
+ ]
930
+ remaining = set(symbol_list) # symbols still to fetch
931
+ frames = []
932
+ for dataset_name, ext_flag in DATASETS:
933
+ if not remaining: # all symbols resolved → stop early
934
+ break
935
+ df_part = load_dataset(dataset_name, list(remaining), extend=ext_flag)
936
+ if not df_part.empty:
937
+ frames.append(df_part)
938
+ remaining -= set(df_part["symbol"].unique())
939
+ df = pd.concat(frames, ignore_index=True)
922
940
 
923
941
  df["date"] = pd.to_datetime(df["date"])
924
942
  df.set_index("date", inplace=True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pwb-toolbox
3
- Version: 0.1.4
3
+ Version: 0.1.5
4
4
  Summary: A toolbox library for quant traders
5
5
  Home-page: https://github.com/paperswithbacktest/pwb-toolbox
6
6
  Author: Your Name
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = pwb-toolbox
3
- version = 0.1.4
3
+ version = 0.1.5
4
4
  author = Your Name
5
5
  author_email = hello@paperswithbacktest.com
6
6
  description = A toolbox library for quant traders
File without changes
File without changes
File without changes