| import pandas as pd |
| import ta |
| import json |
| import os |
|
|
| from data_fetcher import fetch_market_data |
| from technical_agent import analyze_technical |
| from fundamental_agent import analyze_fundamentals |
| from sentiment_agent_llm import analyze_sentiment |
| from macro_agent import analyze_market |
|
|
| from volume_profile_agent import volume_profile_signal |
| from regime_agent import detect_regime |
| from rl_agent import rl_signal |
|
|
| from backtest_engine import backtest_strategy |
| from ml_model import predict_probability |
| from monte_carlo import monte_carlo_probability |
| from market_overview import compute_sector_strength |
|
|
| WEIGHTS_FILE = "ticker_weights.json" |
|
|
| def load_optimized_weights(): |
| if os.path.exists(WEIGHTS_FILE): |
| with open(WEIGHTS_FILE, 'r') as f: |
| return json.load(f) |
| return {} |
|
|
| def run_pipeline(): |
| |
| data = fetch_market_data() |
| technical = analyze_technical(data) |
| tickers = list(data.keys()) |
| fundamentals = analyze_fundamentals(tickers) |
| macro = analyze_market() |
| sector_strength = compute_sector_strength(data) |
| |
| optimized_weights = load_optimized_weights() |
| results = [] |
|
|
| for ticker in tickers: |
| df = data[ticker] |
| if len(df) < 60: |
| continue |
|
|
| close = df["Close"].squeeze() |
| high = df["High"].squeeze() |
| low = df["Low"].squeeze() |
| price = float(close.iloc[-1]) |
|
|
| tech = technical.get(ticker, 0) |
| fund = fundamentals.get(ticker, 0) |
| sentiment = analyze_sentiment(ticker) |
| volume_signal = volume_profile_signal(df) |
| regime = detect_regime(df) |
|
|
| regime_score = {"bull": 1, "bear": -1, "volatile": 0, "sideways": 0}.get(regime, 0) |
| rl_score = rl_signal(df) |
|
|
| total_score = (tech + fund + sentiment + volume_signal + regime_score + rl_score + macro) |
| confidence = max(0, min(100, round((total_score / 15) * 100, 2))) |
|
|
| atr = ta.volatility.AverageTrueRange(high=high, low=low, close=close, window=14).average_true_range() |
| atr_value = float(atr.iloc[-1]) |
|
|
| stop = round(price - (1.5 * atr_value), 2) |
|
|
| if confidence > 70: |
| target = round(price + (3 * atr_value), 2) |
| elif confidence > 50: |
| target = round(price + (2.5 * atr_value), 2) |
| else: |
| target = round(price + (2 * atr_value), 2) |
|
|
| risk_reward = round((target - price) / (price - stop), 2) if (price - stop) != 0 else 0 |
| winrate = backtest_strategy(df) |
| ml_probability = predict_probability(df, ticker) |
| mc_probability = monte_carlo_probability(df, target, stop) |
|
|
| |
| t_key = ticker.replace(".NS", "") |
| if t_key in optimized_weights: |
| w = optimized_weights[t_key] |
| else: |
| |
| w = {"w1": 0.30, "w2": 0.20, "w3": 12, "w4": 0.20, "w5": 0.18} |
|
|
| ranking_score = ( |
| confidence * w["w1"] + |
| winrate * w["w2"] + |
| risk_reward * w["w3"] + |
| ml_probability * w["w4"] + |
| mc_probability * w["w5"] |
| ) |
|
|
| results.append({ |
| "Ticker": t_key, |
| "Close": round(price, 2), |
| "Target": target, |
| "Stop Loss": stop, |
| "Risk Reward": risk_reward, |
| "Confidence %": confidence, |
| "ML Probability %": ml_probability, |
| "Monte Carlo %": mc_probability, |
| "Backtest Win Rate": winrate, |
| "Market Regime": regime, |
| "Signal Strength": total_score, |
| "Ranking Score": ranking_score |
| }) |
|
|
| df_results = pd.DataFrame(results) |
|
|
| if len(df_results) == 0: |
| return df_results, sector_strength |
|
|
| df_results = df_results.sort_values(by="Ranking Score", ascending=False) |
| df_results = df_results.head(10) |
| df_results = df_results.drop(columns=["Ranking Score"]) |
|
|
| return df_results, sector_strength |