RayoDeCodigos commited on
Commit
c0bc531
·
verified ·
1 Parent(s): 2f4b8e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -28
app.py CHANGED
@@ -1,62 +1,69 @@
1
  import gradio as gr
2
- from chronos import ChronosBoltPipeline
3
  import torch
4
  import numpy as np
5
 
6
- # Load the Brain
7
- pipeline = Chronos2Pipeline.from_pretrained("amazon/chronos-bolt-tiny")
8
 
9
  def analyze_trade(prices_string, rr_ratio):
10
  try:
11
- # 1. Process Input
12
  prices = [float(x.strip()) for x in prices_string.split(",")]
13
  current_price = prices[-1]
14
- context = torch.tensor(prices)
 
 
15
 
16
  # 2. Run AI Prediction
17
- # We fetch 0.1, 0.5 (median), and 0.9 quantiles
18
  forecast = pipeline.predict(context, prediction_length=20)
19
 
20
- p10 = forecast.quantile(0.1).item()
21
- p50 = forecast.quantile(0.5).item() # The "Most Likely" path
22
- p90 = forecast.quantile(0.9).item()
 
 
23
 
24
- # 3. CALCULATE CERTAINTY (Confidence)
25
- # If p90 and p10 are close together, the AI is very certain.
26
- # If they are far apart, the AI is confused (high volatility).
27
  volatility = (p90 - p10) / p50
28
  certainty = max(0, min(100, 100 * (1 - volatility)))
29
 
30
- # 4. RR STRATEGY LOGIC
31
- # Risk is the distance to the AI "Floor" (p10)
32
  risk_amount = current_price - p10
 
33
  if risk_amount <= 0:
34
- return "Inconclusive (Price below Floor)", 0, 0, 0, "0%"
35
 
36
- # Take Profit is Risk * RR Ratio
37
  target_tp = current_price + (risk_amount * rr_ratio)
38
 
39
- # 5. POTENTIAL OUTCOME
40
- potential_gain_pct = ((target_tp - current_price) / current_price) * 100
41
- potential_loss_pct = ((current_price - p10) / current_price) * 100
 
 
 
42
 
43
  return (
44
- f"${p10:.2f}", # Stop Loss
45
- f"${target_tp:.2f}", # Take Profit
46
- f"{potential_gain_pct:.2f}%", # Potential Win
47
- f"{potential_loss_pct:.2f}%", # Potential Loss
48
- f"{certainty:.1f}%" # AI CERTAINTY
49
  )
50
 
51
  except Exception as e:
52
  return f"Error: {str(e)}", "", "", "", ""
53
 
54
- # 6. GRADIO INTERFACE (The "Hidden" API + Simple Dashboard)
55
  demo = gr.Interface(
56
  fn=analyze_trade,
57
  inputs=[
58
- gr.Textbox(label="Stock Price History (Last 250 days)", placeholder="150.1, 150.5..."),
59
- gr.Slider(minimum=1.0, maximum=5.0, value=2.0, label="Risk/Reward Ratio (Desired)")
60
  ],
61
  outputs=[
62
  gr.Text(label="Suggested Stop Loss"),
@@ -65,7 +72,7 @@ demo = gr.Interface(
65
  gr.Text(label="Potential Loss if Stopped"),
66
  gr.Text(label="AI Confidence Level")
67
  ],
68
- title="Chronos Trading Intelligence"
69
  )
70
 
71
  demo.launch()
 
1
  import gradio as gr
2
+ from chronos import ChronosBoltPipeline # Corrected Import
3
  import torch
4
  import numpy as np
5
 
6
+ # 1. LOAD THE BRAIN (Matched to the correct model engine)
7
+ pipeline = ChronosBoltPipeline.from_pretrained("amazon/chronos-bolt-tiny")
8
 
9
  def analyze_trade(prices_string, rr_ratio):
10
  try:
11
+ # 1. Clean and Process Input
12
  prices = [float(x.strip()) for x in prices_string.split(",")]
13
  current_price = prices[-1]
14
+
15
+ # Chronos-Bolt expects [Batch, Sequence] shape
16
+ context = torch.tensor(prices).unsqueeze(0)
17
 
18
  # 2. Run AI Prediction
19
+ # length 20 = roughly 2 business weeks
20
  forecast = pipeline.predict(context, prediction_length=20)
21
 
22
+ # 3. Calculate Quantiles (Bolt-Tiny outputs raw samples)
23
+ # forecast[0] is the matrix of possible futures
24
+ p10 = np.quantile(forecast[0].numpy(), 0.1)
25
+ p50 = np.quantile(forecast[0].numpy(), 0.5)
26
+ p90 = np.quantile(forecast[0].numpy(), 0.9)
27
 
28
+ # 4. CERTAINTY (Confidence)
29
+ # Narrow gap = AI is certain. Wide gap = High Market Noise.
 
30
  volatility = (p90 - p10) / p50
31
  certainty = max(0, min(100, 100 * (1 - volatility)))
32
 
33
+ # 5. DYNAMIC TRADING LOGIC
34
+ # Floor (Stop Loss) is the P10
35
  risk_amount = current_price - p10
36
+
37
  if risk_amount <= 0:
38
+ return "Wait for Dip (Price < Floor)", "N/A", "0%", "0%", "Low Probability"
39
 
40
+ # TP = Current Price + (Risk * RR Ratio)
41
  target_tp = current_price + (risk_amount * rr_ratio)
42
 
43
+ # Potential Gain/Loss Percentages
44
+ gain_pct = ((target_tp - current_price) / current_price) * 100
45
+ loss_pct = ((current_price - p10) / current_price) * 100
46
+
47
+ # Confidence Label
48
+ status = " HIGH" if certainty > 75 else " MED" if certainty > 50 else " LOW"
49
 
50
  return (
51
+ f"${p10:.2f}",
52
+ f"${target_tp:.2f}",
53
+ f"+{gain_pct:.2f}%",
54
+ f"-{loss_pct:.2f}%",
55
+ f"{certainty:.1f}% ({status})"
56
  )
57
 
58
  except Exception as e:
59
  return f"Error: {str(e)}", "", "", "", ""
60
 
61
+ # 6. INTERFACE SETUP
62
  demo = gr.Interface(
63
  fn=analyze_trade,
64
  inputs=[
65
+ gr.Textbox(label="Stock History (Comma Separated)", placeholder="150, 152, 151, 155..."),
66
+ gr.Slider(minimum=1.0, maximum=5.0, value=2.0, label="Risk/Reward Ratio")
67
  ],
68
  outputs=[
69
  gr.Text(label="Suggested Stop Loss"),
 
72
  gr.Text(label="Potential Loss if Stopped"),
73
  gr.Text(label="AI Confidence Level")
74
  ],
75
+ title="Chronos-Bolt Live Analytics"
76
  )
77
 
78
  demo.launch()