Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 7 |
-
pipeline =
|
| 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 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# 2. Run AI Prediction
|
| 17 |
-
#
|
| 18 |
forecast = pipeline.predict(context, prediction_length=20)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
#
|
| 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 |
-
#
|
| 31 |
-
#
|
| 32 |
risk_amount = current_price - p10
|
|
|
|
| 33 |
if risk_amount <= 0:
|
| 34 |
-
return "
|
| 35 |
|
| 36 |
-
#
|
| 37 |
target_tp = current_price + (risk_amount * rr_ratio)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
return (
|
| 44 |
-
f"${p10:.2f}",
|
| 45 |
-
f"${target_tp:.2f}",
|
| 46 |
-
f"{
|
| 47 |
-
f"{
|
| 48 |
-
f"{certainty:.1f}%
|
| 49 |
)
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error: {str(e)}", "", "", "", ""
|
| 53 |
|
| 54 |
-
# 6.
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=analyze_trade,
|
| 57 |
inputs=[
|
| 58 |
-
gr.Textbox(label="Stock
|
| 59 |
-
gr.Slider(minimum=1.0, maximum=5.0, value=2.0, label="Risk/Reward Ratio
|
| 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
|
| 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()
|