Add Plot
Here is the `plot_graph()` that is in char of the bars and scatter plot items. Also all the necessary code for the graph to be shown.
parent
f0fd447af7
commit
d2b6e9c00e
|
@ -10,6 +10,10 @@ from piker.brokers.deribit.api import (
|
||||||
get_client,
|
get_client,
|
||||||
maybe_open_oi_feed,
|
maybe_open_oi_feed,
|
||||||
)
|
)
|
||||||
|
import sys
|
||||||
|
import pyqtgraph as pg
|
||||||
|
from pyqtgraph import ScatterPlotItem
|
||||||
|
from PyQt6.QtWidgets import QApplication
|
||||||
|
|
||||||
def check_if_complete(
|
def check_if_complete(
|
||||||
oi: dict[str, dict[str, Decimal | None]]
|
oi: dict[str, dict[str, Decimal | None]]
|
||||||
|
@ -87,6 +91,50 @@ async def max_pain_daemon(
|
||||||
|
|
||||||
return total_intrinsic_value, max_pain
|
return total_intrinsic_value, max_pain
|
||||||
|
|
||||||
|
def plot_graph(
|
||||||
|
oi_by_strikes: dict[str, dict[str, Decimal]],
|
||||||
|
plot,
|
||||||
|
):
|
||||||
|
"""Update the bar graph with new open interest data."""
|
||||||
|
plot.clear()
|
||||||
|
|
||||||
|
for strike_str in sorted(oi_by_strikes, key=lambda x: int(x)):
|
||||||
|
strike = int(strike_str)
|
||||||
|
calls_val = float(oi_by_strikes[strike_str]['C'])
|
||||||
|
puts_val = float(oi_by_strikes[strike_str]['P'])
|
||||||
|
|
||||||
|
bar_c = pg.BarGraphItem(
|
||||||
|
x=[strike - 100],
|
||||||
|
height=[calls_val],
|
||||||
|
width=200,
|
||||||
|
pen='w',
|
||||||
|
brush=(0, 0, 255, 150)
|
||||||
|
)
|
||||||
|
plot.addItem(bar_c)
|
||||||
|
|
||||||
|
bar_p = pg.BarGraphItem(
|
||||||
|
x=[strike + 100],
|
||||||
|
height=[puts_val],
|
||||||
|
width=200,
|
||||||
|
pen='w',
|
||||||
|
brush=(255, 0, 0, 150)
|
||||||
|
)
|
||||||
|
plot.addItem(bar_p)
|
||||||
|
|
||||||
|
for strike_str in sorted(intrinsic_values, key=lambda x: int(x)):
|
||||||
|
strike = int(strike_str)
|
||||||
|
total_val = float(intrinsic_values[strike_str]['total']) / 100000
|
||||||
|
|
||||||
|
scatter_iv = ScatterPlotItem(
|
||||||
|
x=[strike - 100],
|
||||||
|
y=[total_val],
|
||||||
|
pen=pg.mkPen(color=(0, 255, 0), width=2),
|
||||||
|
brush=pg.mkBrush(0, 255, 0, 150),
|
||||||
|
size=3,
|
||||||
|
symbol='o'
|
||||||
|
)
|
||||||
|
plot.addItem(scatter_iv)
|
||||||
|
|
||||||
def update_oi_by_strikes(msg: tuple):
|
def update_oi_by_strikes(msg: tuple):
|
||||||
nonlocal oi_by_strikes
|
nonlocal oi_by_strikes
|
||||||
if 'oi' == msg[0]:
|
if 'oi' == msg[0]:
|
||||||
|
@ -127,6 +175,16 @@ async def max_pain_daemon(
|
||||||
async with maybe_open_oi_feed(
|
async with maybe_open_oi_feed(
|
||||||
instruments,
|
instruments,
|
||||||
) as oi_feed:
|
) as oi_feed:
|
||||||
|
# Initialize QApplication
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
|
||||||
|
win = pg.GraphicsLayoutWidget(show=True)
|
||||||
|
win.setWindowTitle('Calls (blue) vs Puts (red)')
|
||||||
|
|
||||||
|
plot = win.addPlot(title='OI by Strikes')
|
||||||
|
plot.showGrid(x=True, y=True)
|
||||||
|
print('Plot initialized...')
|
||||||
|
|
||||||
async for msg in oi_feed:
|
async for msg in oi_feed:
|
||||||
|
|
||||||
update_oi_by_strikes(msg)
|
update_oi_by_strikes(msg)
|
||||||
|
@ -134,6 +192,11 @@ async def max_pain_daemon(
|
||||||
if 'oi' == msg[0]:
|
if 'oi' == msg[0]:
|
||||||
timestamp = msg[1]['timestamp']
|
timestamp = msg[1]['timestamp']
|
||||||
max_pain = get_max_pain(oi_by_strikes)
|
max_pain = get_max_pain(oi_by_strikes)
|
||||||
|
intrinsic_values = get_total_intrinsic_values(oi_by_strikes)
|
||||||
|
|
||||||
|
# graph here
|
||||||
|
plot_graph(oi_by_strikes, plot)
|
||||||
|
|
||||||
print('-----------------------------------------------')
|
print('-----------------------------------------------')
|
||||||
print(f'timestamp: {datetime.fromtimestamp(max_pain['timestamp'])}')
|
print(f'timestamp: {datetime.fromtimestamp(max_pain['timestamp'])}')
|
||||||
print(f'expiry_date: {max_pain['expiry_date']}')
|
print(f'expiry_date: {max_pain['expiry_date']}')
|
||||||
|
@ -141,6 +204,9 @@ async def max_pain_daemon(
|
||||||
print(f'total intrinsic value: {max_pain['total_intrinsic_value']}')
|
print(f'total intrinsic value: {max_pain['total_intrinsic_value']}')
|
||||||
print('-----------------------------------------------')
|
print('-----------------------------------------------')
|
||||||
|
|
||||||
|
# Process GUI events to keep the window responsive
|
||||||
|
app.processEvents()
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue