disLodge commited on
Commit
05a7335
·
1 Parent(s): ead841b

Making interactive

Browse files
Files changed (1) hide show
  1. app.py +60 -29
app.py CHANGED
@@ -1,31 +1,62 @@
1
- # app.py
2
  import asyncio
3
- import os
4
- os.environ["USE_MINIMAL_LIBS"] = "true"
5
 
6
- import gradio as gr
7
- import triangular_arbitrage.detector as detector
8
-
9
- EXCHANGE = "binanceus" # or "binanceus"
10
- REFRESH_INTERVAL = 30 # seconds
11
-
12
- def run_detection_sync():
13
- best_opps, best_profit = asyncio.run(detector.run_detection(EXCHANGE, max_cycle=3))
14
- if best_opps:
15
- profit_pct = round((best_profit - 1) * 100, 5)
16
- lines = [f"**{profit_pct}% opportunity found**\n"]
17
- for i, opp in enumerate(best_opps):
18
- side = 'buy' if opp.reversed else 'sell'
19
- conn = 'with' if side == 'buy' else 'for'
20
- lines.append(f"{i+1}. {side} {opp.symbol.base} {conn} {opp.symbol.quote} @ {opp.last_price:.5f}")
21
- return "\n".join(lines)
22
- return "No opportunity detected. Scanning..."
23
-
24
- with gr.Blocks() as demo:
25
- output = gr.Textbox()
26
- demo.load(run_detection_sync, outputs=output)
27
- # Auto-refresh every REFRESH_INTERVAL seconds
28
- timer = gr.Timer(REFRESH_INTERVAL)
29
- timer.tick(run_detection_sync, outputs=output)
30
-
31
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import asyncio
3
+ from triangular_arbitrage import detector
 
4
 
5
+ async def run_detection_ui(exchange_name):
6
+ try:
7
+ best_opps, best_profit = await detector.run_detection(
8
+ exchange_name=exchange_name,
9
+ max_cycle=3, # fixed triangular only
10
+ )
11
+
12
+ if not best_opps:
13
+ return "No arbitrage opportunity found."
14
+
15
+ result_lines = []
16
+ for ticker in best_opps:
17
+ result_lines.append(
18
+ f"{ticker.symbol} | price: {ticker.last_price:.8f}"
19
+ )
20
+
21
+ result_text = "\n".join(result_lines)
22
+ result_text += f"\n\n**Total cycle profit:** {best_profit:.6f}"
23
+
24
+ return result_text
25
+
26
+ except Exception as e:
27
+ return f"Error while scanning: {str(e)}"
28
+
29
+
30
+ with gr.Blocks(title="Triangular Arbitrage Scanner") as demo:
31
+ gr.Markdown("# Triangular Arbitrage Scanner")
32
+
33
+ exchange = gr.Dropdown(
34
+ choices=[
35
+ "binanceus", # safer default for Spaces
36
+ "kraken",
37
+ "bitget",
38
+ "bybit",
39
+ "okx",
40
+ "coinbase",
41
+ "kucoin",
42
+ "gateio",
43
+ "huobi",
44
+ ],
45
+ value="kraken",
46
+ label="Exchange",
47
+ )
48
+
49
+ scan_btn = gr.Button("Scan for opportunities", variant="primary")
50
+
51
+ output = gr.Markdown(
52
+ value="Select exchange and click **Scan**.",
53
+ label="Result"
54
+ )
55
+
56
+ scan_btn.click(
57
+ fn=run_detection_ui,
58
+ inputs=exchange,
59
+ outputs=output,
60
+ )
61
+
62
+ demo.launch()