Files
Polymarket/ng.py
2026-04-01 17:37:19 +00:00

117 lines
4.3 KiB
Python

import os
from nicegui import ui, app
from sqlalchemy import create_engine
# import requests
import json
# import time
# import re
import valkey
# import asyncio
# import datetime as dt
# from random import random
# from nicegui_modules import data
# from nicegui_modules import ui_components
# from glide import GlideClient, NodeAddress, GlideClientConfiguration
LISTENING_CLIENT = None
LH_PAIR = 'BTC'
RH_PAIR = 'USD'
DEFAULT_TO_DARKMODE: bool = True
ALLOW_BODY_SCROLL: bool = True
LOOKBACK: int = 60
LOOKBACK_RT_TV_MAX_POINTS: int = 300
REFRESH_INTERVAL_SEC: int = 10
REFRESH_INTERVAL_RT_SEC: int = 1/30
ENGINE = create_engine('mysql+pymysql://root:pwd@localhost/polymarket')
VALKEY_R = valkey.Valkey(host='localhost', port=6379, db=0, decode_responses=True)
# VALKEY_P = VALKEY_R.pubsub()
# VALKEY_P.subscribe('mexc_mkt_bookTicker')
def root():
app.add_static_files(max_cache_age=0, url_path='/static', local_directory=os.path.join(os.path.dirname(__file__), 'nicegui_modules/static'))
ui.add_head_html('''
<meta name="darkreader-lock">
<link rel="stylesheet" type="text/css" href="/static/styles.css">
<script type="text/javascript" src="https://unpkg.com/lightweight-charts/dist/lightweight-charts.standalone.production.js"></script>
<script src="/static/script.js"></script>
'''
)
# ui.add_head_html('<meta name="darkreader-lock">')
update_body_scroll(bool_override=ALLOW_BODY_SCROLL)
ui.sub_pages({
'/': rt_chart_page,
}).classes('w-full')
async def update_tv():
series_update = json.loads(VALKEY_R.get('poly_rtds_cl_btcusd'))
series_update_b = json.loads(VALKEY_R.get('poly_binance_btcusd'))
series_update_c = json.loads(VALKEY_R.get('poly_5min_btcusd'))
timestamp = round( ( series_update['timestamp_arrival'] / 1000 ) , 2)
timestamp_b = round( ( series_update_b['timestamp_arrival'] / 1000 ) , 2)
timestamp_c = round( ( series_update_c['timestamp_arrival'] / 1000 ) , 2)
value = float(series_update['value'])
value_b = float(series_update_b['value'])
value_c = float(series_update_c['price'])
data_dict = {
'timestamp': timestamp,
'timestamp_b': timestamp_b,
'timestamp_c': timestamp_c,
'value': value,
'value_b': value_b,
'value_c': value_c,
'target': series_update_c['target_price'],
'LOOKBACK_RT_TV_MAX_POINTS': LOOKBACK_RT_TV_MAX_POINTS,
}
ui.run_javascript(f'await update_tv(data_dict={data_dict});')
def update_body_scroll(e=None, bool_override=False):
if e is None:
if bool_override:
ui.query('body').style('height: 100%; overflow-y: auto;')
else:
ui.query('body').style('height: 100%; overflow-y: hidden;')
else:
if e.value:
ui.query('body').style('height: 100%; overflow-y: auto;')
else:
ui.query('body').style('height: 100%; overflow-y: hidden;')
# async def refresh_lookback_funcs(lookback: int = LOOKBACK):
# lookback = app.storage.user.get('lookback', lookback)
# await data.trades_pnl_graph.refresh(ENGINE=ENGINE, LH_PAIR=LH_PAIR, RH_PAIR=RH_PAIR, lookback=lookback)
# await ui_components.er_table.refresh(ENGINE=ENGINE, lookback=lookback)
# await ui_components.trades_table.refresh(ENGINE=ENGINE, lookback=lookback)
# await ui_components.er_stats.refresh(ENGINE=ENGINE, lookback=lookback)
async def rt_chart_page():
global LOOKBACK
LOOKBACK = app.storage.user.get('lookback', LOOKBACK)
timer = ui.timer(REFRESH_INTERVAL_RT_SEC, update_tv)
with ui.row():
with ui.column():
ui.switch('☸︎', value=ALLOW_BODY_SCROLL, on_change=lambda e: update_body_scroll(e))
with ui.column():
ui.switch('▶️', value=True).bind_value_to(timer, 'active')
with ui.column().style('position: absolute; right: 20px; font-family: monospace; align-self: center;'):
ui.label('Atwater Trading: Orderbook')
with ui.grid(columns=16).classes('w-full gap-0 auto-fit'):
with ui.card().tight().classes('w-full col-span-full no-shadow border border-black-200').style('overflow: auto;'):
ui.html('<div id="tv" style="width:100%; height:800px;"></div>', sanitize=False).classes('w-full')
ui.run_javascript('await create_tv();')
ui.run(root, storage_secret="123ABC", reload=True, dark=True, title='Atwater Trading')