0

Background:

  • I have 2 csv files: c.csv, p.csv
  • Every 15 secs, a set of new records are appended to c.csv
  • Every 15 secs, a set of new records (but different from the c.csv set) are appended to p.csv
  • c.csv contains call options data for SPY (columns = contract_name, strike, volume, timestamp)
  • p.csv contains put options data for SPY (columns = contract_name, strike, volume, timestamp)

chart.py:

import pandas as pd
import numpy as np
import plotly.express as px
import dash

app = dash.Dash()

columns = ['contract', 'strike', 'vol', 'ts']
c = pd.read_csv('c.csv', header = None, names=columns)
p = pd.read_csv('p.csv', header = None, names=columns)

mode = 'lines+markers+text'

fig1 = px.scatter()
fig1.add_scatter(name='call volume', x=c['strike'], y=c['vol'], mode=mode)
fig1.add_scatter(name='put volume', x=p['strike'], y=p['vol'], mode=mode)

if __name__ == '__main__':
    app.run_server()

c.csv:

# 1st 15 sec extract...
SPY200615C00304000,304.0,29507.0,,2020-06-13 13:00:00.000000 
SPY200615C00303000,303.0,31638.0,2020-06-13 13:00:00.000000
SPY200615C00302000,302.0,22896.0,2020-06-13 13:00:00.000000
# 2nd 15 sec extract...
SPY200615C00304000,304.0,29777.0,,2020-06-13 13:00:15.000000
SPY200615C00303000,303.0,31987.0,2020-06-13 13:00:15.000000
SPY200615C00302000,302.0,24555.0,2020-06-13 13:00:15.000000
.
.
.

p.csv:

# 1st 15 sec extract...
SPY200615P00304000,304.0,37002.0,2020-06-13 13:00:00.000000
SPY200615P00303000,303.0,54339.0,2020-06-13 13:00:00.000000
SPY200615P00302000,302.0,43387.0,2020-06-13 13:00:00.000000
# 2nd 15 sec extract...
SPY200615P00304000,304.0,39032.0,2020-06-13 13:00:15.000000
SPY200615P00303000,303.0,67339.0,2020-06-13 13:00:15.000000
SPY200615P00302000,302.0,64342.0,2020-06-13 13:00:15.000000
.
.
.

Problem/what I am trying to achieve:

How can I get fig1 to update live as new records are being appended to both csv files? Basically I am looking to just replace the old fig1 with the latest records in c.csv and p.csv. i want the x-axis (strike) to be static and NOT dynamic because the strikes are always the same (304,303,302). Also would like to keep y axis (vol) static as well. Is there any way I can achieve this?

Jason Park
  • 77
  • 1
  • 1
  • 10

1 Answers1

1

I think what you would need is a callback running with an interval as input. See this page in the docs.

You'd have a component like this in the layout:

        dcc.Interval(
            id='interval-component',
            interval=15*1000, # in milliseconds
            n_intervals=0,
        )

And then a callback using that as the trigger to output to the component that holds your chart:

@app.callback(Output('live-update-figure', 'children'),
              [Input('interval-component', 'n_intervals')])
def update_figures(n):
    columns = ['contract', 'strike', 'vol', 'ts']
    c = pd.read_csv('c.csv', header = None, names=columns)
    p = pd.read_csv('p.csv', header = None, names=columns)

    mode = 'lines+markers+text'

    fig1 = px.scatter()
    fig1.add_scatter(name='call volume', x=c['strike'], y=c['vol'], mode=mode)
    fig1.add_scatter(name='put volume', x=p['strike'], y=p['vol'], mode=mode)

    return fig1

coralvanda
  • 6,431
  • 2
  • 15
  • 25