-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption_chain_volume_plotly.py
More file actions
48 lines (41 loc) · 1.59 KB
/
Copy pathoption_chain_volume_plotly.py
File metadata and controls
48 lines (41 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import yfinance as yf
import time
import pandas as pd
import streamlit as st
import plotly.graph_objs as go
# 基本数据
ticker = 'spy'
date = '2023-09-08'
stock = yf.Ticker(ticker)
option_chain = stock.option_chain(date)
price = 450
screenshoot_time = time.ctime()
# 获取期权数据
calls = option_chain.calls
puts = option_chain.puts
# oi跟volume的和
calls_oi_sum = sum(calls['openInterest'])
puts_oi_sum = sum(puts['openInterest'])
calls_vol_sum = sum(calls['volume'])
puts_vol_sum = sum(puts['volume'])
# 差别计算
if calls_oi_sum > puts_oi_sum:
difference = ((calls_oi_sum / puts_oi_sum) - 1) * 100
elif puts_oi_sum > calls_oi_sum:
difference = ((puts_oi_sum / calls_oi_sum) - 1) * 100
# 画图
calls_oi_bar = go.Bar(x=calls['strike'], y=calls['openInterest'], name='call openInterest')
puts_oi_bar = go.Bar(x=calls['strike'], y=puts['openInterest'], name='put openInterest')
calls_vol_bar = go.Bar(x=calls['strike'], y=calls['volume'], name='call volume')
puts_vol_bar = go.Bar(x=calls['strike'], y=puts['volume'], name='put volume')
fig = go.Figure([calls_oi_bar, puts_oi_bar, calls_vol_bar, puts_vol_bar])
# 创建图形
fig = go.Figure([calls_oi_bar, puts_oi_bar, calls_vol_bar, puts_vol_bar])
# 标题
fig.update_layout(title=f'Options for {ticker} on {date} at {screenshoot_time}\n calls oi sum: {calls_oi_sum} puts oi sum: {puts_oi_sum}\n ',
xaxis_title='strike price',
yaxis_title='openInterest')
# 范围
fig.update_xaxes(range=[price*0.97, price*1.03])
fig.show()
# {round(difference, 2)}% difference