我正在尝试使此代码能够在彭博终端中获取我可以访问的任何帮助的大多数零日期权交易

问题描述 投票:0回答:1

这是代码:

import pandas as pd
import matplotlib.pyplot as plt

from xbbg import blp

def inspect_options_data(specific_day='2023-8-17'):
    try:
        # Fetch bulk options data for S&P 500 Index
        options_data = blp.bds(tickers='SPX Index', flds=['OPT_EXIRE_DT'== specific_day, 'OPT_CHAIN'])
        
        # Filter for call options with the specified expiration date
        call_options = options_data[options_data['security_description'].str.contains(f'{specific_day} C')].copy()
        
        # Filter for put options with the specified expiration date
        put_options = options_data[options_data['security_description'].str.contains(f'{specific_day} P')].copy()
        
        # Extract the strike price from the 'security_description' column
        call_options['strike'] = call_options['security_description'].str.extract(f'C(\d+) ').astype(float)
        call_options['type'] = 'Call'
        
        # Extract the strike price from the 'security_description' column
        put_options['strike'] = put_options['security_description'].str.extract(f'P(\d+) ').astype(float)
        put_options['type'] = 'Put'
        
        # Fetch volume data for each option and ensure it's a scalar value
        put_options['px_volume'] = put_options['security_description'].apply(lambda x: blp.bdp(tickers=x, flds=['PX_VOLUME']).iloc[0, 0])
        call_options['px_volume'] = call_options['security_description'].apply(lambda x: blp.bdp(tickers=x, flds=['PX_VOLUME']).iloc[0, 0])
        
        # Combine puts and calls data
        combined_data = pd.concat([put_options, call_options])
        
        # Sort by volume in descending order and select the top 10
        top_10_options = combined_data.sort_values(by='px_volume', ascending=False).head(10)
        
        # Visualization
        plt.figure(figsize=(12, 6))
        plt.bar(top_10_options['security_description'], top_10_options['px_volume'], color=['red' if t == 'Put' else 'green' for t in top_10_options['type']])
        plt.xticks(rotation=45, ha='right')
        plt.xlabel('Option Description')
        plt.ylabel('Volume')
        plt.title(f'Top 10 Options for {specific_day}')
        plt.tight_layout()
        plt.show()
        
    except Exception as e:
        print(f"Error: {e}")

# Inspect the options data
inspect_options_data()

但它只为我提供定期到期的结果。

python bloomberg
1个回答
0
投票

您需要为此使用 BQL。

import bql
import matplotlib.pyplot as plt
import seaborn as sb

bq = bql.Service()

expire_date = '2023-09-20'

query = f"""
get(SECURITY_DES, STRIKE_PX, OPEN_INT, PX_VOLUME, DELTA, EXPIRE_DT)
for(filter(options('SPY US EQUITY'), GROUPRANK(PX_VOLUME(D), ORDER=DESC) <= 11 and EXPIRE_DT() == {expire_date}))
with(MODE=CACHED)
"""

data = bql.combined_df(bq.execute(query)).groupby('ID').last().sort_values(by='PX_VOLUME',ascending=False)

f, ax = plt.subplots(figsize=(12, 9))
sb.barplot(data=data, x='SECURITY_DES',y='PX_VOLUME', hue="PUT_CALL", palette=['red','green'], dodge=False)
plt.title(f'Top 10 Options for {expire_date}')
plt.xticks(rotation=45)
plt.xlabel('Option Description')
plt.ylabel('Volume')
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.