How do I get the components of CHFFWD=?

Hi, I need to get all the components of CHFFWD= (including but not limited to CHFON, CHFTN, CHFSW, CHF1M...) and their BID and ASK values.

The following code

data, err = ek.get_data(instruments=["CHFFWD="], fields=['BID', 'ASK'])

Throws an error. What am I missing?

Thank you

Best Answer

  • chavalit-jintamalit
    Answer ✓

    Hi @mi.v.

    Please read the chain data structure section in this article.

    Here is the sample code:

    LONGNEXTLR = 'CHFFWD='
    fields = ['LONGLINK{}'.format(x) for x in range(1, 15)]
    fields.append('LONGNEXTLR')

    all_components = []

    while LONGNEXTLR!='':
        df,e = ek.get_data(LONGNEXTLR,fields)
        LONGNEXTLR = df.iloc[0]['LONGNEXTLR'] if pd.notnull(df.iloc[0]['LONGNEXTLR']) else ''
        for x in range(1, 15):
            currentField = 'LONGLINK{}'.format(x)
            all_components.append(df.iloc[0][currentField]) if pd.notnull(df.iloc[0][currentField]) else None
           
    df,e = ek.get_data(all_components,['BID','ASK'])
    df


    Output:

    image