import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly
import random
import plotly.graph_objs as go
from collections import deque
import time
import tradehull
import datetime
TH = tradehull.Tradehull("6hhe14i4siw3j2p2",
"9vltgi7zkzbeztcvkd5aar871k4qbggy", "yes")
kite = TH.kite
counter=0
nifty_strike=0
# Initialize X and Y
X = deque(maxlen=20)
X.append(1)
Y = deque(maxlen=20)
Y.append(1)
app = dash.Dash(__name__)
app.layout = html.Div(
[
dcc.Graph(id='live-graph', animate=True),
dcc.Interval(
id='graph-update',
interval=1000,
n_intervals=0
),
]
)
# ... (imports and initializations)
def update_y_values():
try:
ltp1=19000
time.sleep(2)
ltp = TH.get_data_for_single_script("MCX", "CRUDEOIL23AUGFUT", "ltp")
print("ltp= ", ltp)
if ltp is None:
ltp=ltp1
else:
ltp1=ltp
except Exception as e:
print("Exception:", e)
time.sleep(2)
ltp = ltp1 # Provide a default value or handle the exception accordingly
print("ltp1= ", ltp1)
return ltp
@app.callback(
Output('live-graph', 'figure'),
[Input('graph-update', 'n_intervals')]
)
def update_graph_scatter(n):
X.append(X[-1] + 1)
updated_y = update_y_values()
Y.append(updated_y)
print("updated_y= ", Y)
print("updated_x= ", X)
# Ensure that the updated_y value is within a visible range on the chart
if updated_y < min(Y):
updated_y = min(Y)
elif updated_y > max(Y):
updated_y = max(Y)
data = plotly.graph_objs.Scatter(
x=list(X),
y=list(Y),
name='Scatter',
mode='lines+markers'
)
layout = go.Layout(
xaxis=dict(range=[min(X), max(X)]),
yaxis=dict(range=[min(Y), max(Y)])
)
return {'data': [data], 'layout': layout}
if __name__ == '__main__':
app.run_server()
127.0.0.1 - - [09/Aug/2023 17:02:32] "GET /_dash-layout HTTP/1.1" 200 -
127.0.0.1 - - [09/Aug/2023 17:02:32] "GET /_dash-dependencies HTTP/1.1" 200 -
127.0.0.1 - - [09/Aug/2023 17:02:32] "GET /_dash-component-suites/dash/dcc/async-graph.js HTTP/1.1" 304 -
127.0.0.1 - - [09/Aug/2023 17:02:32] "GET /_dash-component-suites/dash/dcc/async-plotlyjs.js HTTP/1.1" 304 -
ltp= 6951
updated_y= deque([1, 6951], maxlen=20)
updated_x= deque([1, 2, 3, 4], maxlen=20)
127.0.0.1 - - [09/Aug/2023 17:02:34] "POST /_dash-update-component HTTP/1.1" 200 -
ltp= 6951
updated_y= deque([1, 6951, 6951], maxlen=20)
updated_x= deque([1, 2, 3, 4, 5], maxlen=20)
127.0.0.1 - - [09/Aug/2023 17:02:35] "POST /_dash-update-component HTTP/1.1" 200 -
ltp= 6950
updated_y= deque([1, 6951, 6951, 6950], maxlen=20)
updated_x= deque([1, 2, 3, 4, 5, 6], maxlen=20)
127.0.0.1 - - [09/Aug/2023 17:02:36] "POST /_dash-update-component HTTP/1.1" 200 -
ltp= 6950
updated_y= deque([1, 6951, 6951, 6950, 6950], maxlen=20)
updated_x= deque([1, 2, 3, 4, 5, 6, 7], maxlen=20)
127.0.0.1 - - [09/Aug/2023 17:02:37] "POST /_dash-update-component HTTP/1.1" 200 -
ltp= 6950
updated_y= deque([1, 6951, 6951, 6950, 6950, 6950], maxlen=20)
updated_x= deque([1, 2, 3, 4, 5, 6, 7, 8], maxlen=20)
127.0.0.1 - - [09/Aug/2023 17:02:38] "POST /_dash-update-component HTTP/1.1" 200 -
ltp= 6950
updated_y= deque([1, 6951, 6951, 6950, 6950, 6950, 6950], maxlen=20)
updated_x= deque([1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=20)
127.0.0.1 - - [09/Aug/2023 17:02:39] "POST /_dash-update-component HTTP/1.1" 200 -
ltp= 6950
updated_y= deque([1, 6951, 6951, 6950, 6950, 6950, 6950, 6950], maxlen=20)
updated_x= deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=20)
127.0.0.1 - - [09/Aug/2023 17:02:40] "POST /_dash-update-component HTTP/1.1" 200 -