GoshawkVortexAI commited on
Commit
d3da462
·
verified ·
1 Parent(s): 7c10ddd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -3
app.py CHANGED
@@ -65,8 +65,12 @@ ALIASES = {
65
  "timeframe": ["timeframe", "interval", "tf"],
66
  "exchange": ["exchange"],
67
  "limit": ["limit", "count", "n", "bars"],
 
68
  }
69
 
 
 
 
70
 
71
  # ----------------------------------------------------------------- MCP yardımcıları
72
  def build_args(tool, **wanted):
@@ -98,6 +102,46 @@ async def call_tool(session, tools, name, **wanted):
98
  return {"text": text}
99
 
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  def to_df(payload) -> pd.DataFrame:
102
  rows = payload
103
  if isinstance(payload, dict):
@@ -326,8 +370,7 @@ async def run_async(symbol, timeframe, exchange, horizon, limit):
326
  await session.initialize()
327
  tools = {t.name: t for t in (await session.list_tools()).tools}
328
 
329
- ohlcv = await call_tool(session, tools, "get_ohlcv", symbol=symbol,
330
- timeframe=timeframe, exchange=exchange, limit=int(limit))
331
  if ohlcv is None:
332
  raise RuntimeError(f"get_ohlcv bulunamadı. Mevcut tool'lar: {list(tools)}")
333
  if isinstance(ohlcv, dict) and "error" in ohlcv:
@@ -402,7 +445,7 @@ with gr.Blocks(title="Tickscope Tahmin") as demo:
402
  timeframe = gr.Dropdown(["5m", "15m", "1h", "4h", "1d"], value="1h", label="Timeframe")
403
  with gr.Row():
404
  horizon = gr.Slider(1, 24, value=4, step=1, label="Tahmin ufku (bar)")
405
- limit = gr.Slider(300, 1000, value=1000, step=100, label="Mum sayısı")
406
  btn = gr.Button("Tahmin et", variant="primary")
407
  out_md = gr.Markdown()
408
  out_plot = gr.Plot()
 
65
  "timeframe": ["timeframe", "interval", "tf"],
66
  "exchange": ["exchange"],
67
  "limit": ["limit", "count", "n", "bars"],
68
+ "until": ["until", "end", "end_time", "endTime", "to", "before"],
69
  }
70
 
71
+ TF_MS = {"1m": 60_000, "5m": 300_000, "15m": 900_000, "1h": 3_600_000,
72
+ "4h": 14_400_000, "1d": 86_400_000}
73
+
74
 
75
  # ----------------------------------------------------------------- MCP yardımcıları
76
  def build_args(tool, **wanted):
 
102
  return {"text": text}
103
 
104
 
105
+ async def fetch_ohlcv(session, tools, symbol, timeframe, exchange, total):
106
+ """Borsa sayfa limiti (~1000) yüzünden geriye doğru sayfalayarak `total` muma kadar çeker.
107
+ Tool bitiş-zamanı parametresi desteklemiyorsa ilk sayfayla yetinir."""
108
+ tool = tools.get("get_ohlcv")
109
+ if tool is None:
110
+ return None
111
+ page = min(1000, total)
112
+ first = await call_tool(session, tools, "get_ohlcv", symbol=symbol,
113
+ timeframe=timeframe, exchange=exchange, limit=page)
114
+ if first is None or (isinstance(first, dict) and "error" in first) or total <= page:
115
+ return first
116
+ props = (tool.inputSchema or {}).get("properties", {})
117
+ if not any(n in props for n in ALIASES["until"]):
118
+ return first # sayfalama desteklenmiyor
119
+ try:
120
+ frames = [to_df(first)]
121
+ except Exception:
122
+ return first
123
+ got = len(frames[0])
124
+ epoch = pd.Timestamp("1970-01-01")
125
+ while got < total:
126
+ oldest = frames[-1]["ts"].iloc[0]
127
+ end_ms = int((oldest - epoch) // pd.Timedelta("1ms")) - 1
128
+ res = await call_tool(session, tools, "get_ohlcv", symbol=symbol, timeframe=timeframe,
129
+ exchange=exchange, limit=page, until=end_ms)
130
+ if res is None or (isinstance(res, dict) and "error" in res):
131
+ break
132
+ try:
133
+ d = to_df(res)
134
+ except Exception:
135
+ break
136
+ d = d[d["ts"] < oldest]
137
+ if d.empty:
138
+ break
139
+ frames.append(d)
140
+ got += len(d)
141
+ out = pd.concat(frames[::-1]).drop_duplicates("ts").sort_values("ts").tail(total)
142
+ return out.to_dict("records")
143
+
144
+
145
  def to_df(payload) -> pd.DataFrame:
146
  rows = payload
147
  if isinstance(payload, dict):
 
370
  await session.initialize()
371
  tools = {t.name: t for t in (await session.list_tools()).tools}
372
 
373
+ ohlcv = await fetch_ohlcv(session, tools, symbol, timeframe, exchange, int(limit))
 
374
  if ohlcv is None:
375
  raise RuntimeError(f"get_ohlcv bulunamadı. Mevcut tool'lar: {list(tools)}")
376
  if isinstance(ohlcv, dict) and "error" in ohlcv:
 
445
  timeframe = gr.Dropdown(["5m", "15m", "1h", "4h", "1d"], value="1h", label="Timeframe")
446
  with gr.Row():
447
  horizon = gr.Slider(1, 24, value=4, step=1, label="Tahmin ufku (bar)")
448
+ limit = gr.Slider(300, 20000, value=5000, step=100, label="Mum sayısı (1000 üstü sayfalanır)")
449
  btn = gr.Button("Tahmin et", variant="primary")
450
  out_md = gr.Markdown()
451
  out_plot = gr.Plot()