Eikon Codebook - Matplotlib Bar Chart Open Interest by Strike

Hi Everyone,

I am using python in codebook and trying to graph open interest by strike in a chart. I am trying to get it to illustrate the calls in one color and the puts in another color. I can get as far as the graph, however, I can not figure out how to break out the PUTCALLIND to illustrate put vs call with different colors. Any guidance would be greatly apprecaited. Thanks!


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

%matplotlib inline

import seaborn as sns

plt.style.use('seaborn-dark-palette')

import warnings

warnings.filterwarnings('ignore')


import eikon as tr

tr.set_app_key('DEFAULT_CODE_BOOK_APP_KEY')

df, e= tr.get_data('0#IBM*.U',fields = ['PUTCALLIND','STRIKE_PRC','OPINT_1'])

df

df1 = df.groupby(['STRIKE_PRC']).sum()

df1.plot(figsize=(12,5), title='Total Open Interest Per Strike Price', kind='bar');

image

image

Best Answer

  • Hello @brandonwarnecke,

    Try:

    import numpy as 
    import pandas as
    import matplotlib.pyplot as p
    %matplotlib inli
    import seaborn as sns

    plt.style.use('seaborn-dark-palette')

    import warnings

    warnings.filterwarnings('ignore')

    import eikon as tr

    tr.set_app_key('DEFAULT_CODE_BOOK_APP_KEY')

    df, e= tr.get_data('0#IBM*.U',fields = ['PUTCALLIND','STRIKE_PRC','OPINT_1'])

    df1 = df.groupby(['STRIKE_PRC','PUTCALLIND']).sum()
                       
    df1.unstack().plot(figsize=(12,5), title='Total Open Interest Per Strike Price', kind='bar', color =['black','red']);

    For me results in:

    image