Question about getting bid ask of Isincodes by Codebook V2

I use the code below to get bid ask by codebook. My code is working but cannnot get the data whose isincode starts from JP. Could you tell me the reason if possible.

import pandas as pd
import lseg.data as ld
from lseg.data.content import symbol_conversion

------------------------ 設定 ------------------------

EXCEL_IN = "2409_IsinCode.xlsx"
EXCEL_OUT = "BidAsk_20240930.xlsx"
TRADE_DT = "2024-09-30"

-----------------------------------------------------

ld.open_session() # 1 回で OK

① ISIN 一覧を読み込む

df_isin = (pd.read_excel(EXCEL_IN, dtype=str)
.rename(columns=str.upper)
.dropna(subset=["ISINCODE"])
.assign(ISINCODE=lambda x: x["ISINCODE"].str.strip()))

isin_list = df_isin["ISINCODE"].tolist()

② 利用可能な RIC タイプを動的に抽出

candidate_names = ["RIC_PRIMARY_EXCHANGE", "RIC_COMMON", "RIC_QUICK", "RIC"]
targets = [getattr(symbol_conversion.SymbolTypes, n)
for n in candidate_names if hasattr(symbol_conversion.SymbolTypes, n)]

if not targets:
raise RuntimeError("利用可能な RIC 系 SymbolTypes が見つかりません")

③ ISIN → RIC 変換

resp = symbol_conversion.Definition(
symbols = isin_list,
from_symbol_type = symbol_conversion.SymbolTypes.ISIN,
to_symbol_types = targets
).get_data()

------------------- 変換結果を整形 -------------------パターン A (ISINがindex) / B (FROM_SYMBOL列) の両対応

df_raw = resp.data.df.copy()

if "FROM_SYMBOL" not in df_raw.columns:
df_raw = df_raw.reset_index().rename(columns={"index": "FROM_SYMBOL"})

df_conv = (df_raw
.rename(columns={
"FROM_SYMBOL": "ISINCODE",
"TO_SYMBOL_VALUE":"RIC",
"TO_SYMBOL_TYPE": "RIC_TYPE",
"DocumentTitle": "DocumentTitle"
}))

if "RIC_TYPE" not in df_conv.columns:
df_conv["RIC_TYPE"] = "RIC"

同一 ISIN に複数 RIC があれば優先度順に 1 件だけ採用

df_conv = (df_conv
.sort_values(["ISINCODE", "RIC_TYPE"])
.groupby("ISINCODE").first()
.reset_index()[["ISINCODE", "RIC", "DocumentTitle"]])

④ 元 ISIN 一覧と結合(変換失敗は RIC=NaN)

df_all = df_isin.merge(df_conv, on="ISINCODE", how="left")

⑤ RIC が取れた銘柄だけ価格取得

ric_list = list(df_all["RIC"].dropna().unique())

if ric_list:
df_px = (ld.get_data(universe=ric_list,
fields=["BID", "ASK"])
.rename(columns={"Instrument": "RIC"}))
else:
df_px = pd.DataFrame(columns=["RIC", "BID", "ASK"])

⑥ 価格をマージ(価格取れない銘柄は BID/ASK=NaN)

df_final = df_all.merge(df_px, on="RIC", how="left")

⑦ Excel 出力

df_final.to_excel(EXCEL_OUT, index=False)
print(f"✔ 完了: {EXCEL_OUT} を保存しました({len(df_final)} 行)")

ld.close_session()

Answers

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    @Akira0426

    Thank you for reaching out to us.

    You need to enable debug log in the library by using the following code to verify what the problem is.

    import lseg.data as ld
    from lseg.data.content import symbol_conversion
    config = ld.get_config()
    config.set_param("logs.transports.file.enabled", True)
    config.set_param("logs.transports.file.name", "lseg-data-lib.log")
    config.set_param("logs.level", "debug")


    ld.open_session() …

    With this code before opening a session, the <date>-log-data-lib.log file will created in the current directory.

    Please share the log file when the problem occurred.

  • Akira0426
    Akira0426 Newcomer

    Thank you for reply!
    I will organize the issues and ask the questions again.