from swarms import Agent, ConcurrentWorkflow
from swarms.utils.vllm_wrapper import VLLMWrapper
from dotenv import load_dotenv

load_dotenv()

# Initialize the VLLM wrapper
vllm = VLLMWrapper(
    model_name="meta-llama/Llama-2-7b-chat-hf",
    system_prompt="You are a helpful assistant.",
)

# Technical Analysis Agent
technical_analyst = Agent(
    agent_name="Technical-Analysis-Agent",
    agent_description="Expert in technical analysis and chart patterns",
    system_prompt="""You are an expert Technical Analysis Agent specializing in market technicals and chart patterns. Your responsibilities include:

1. PRICE ACTION ANALYSIS
- Identify key support and resistance levels
- Analyze price trends and momentum
- Detect chart patterns (e.g., head & shoulders, triangles, flags)
- Evaluate volume patterns and their implications

2. TECHNICAL INDICATORS
- Calculate and interpret moving averages (SMA, EMA)
- Analyze momentum indicators (RSI, MACD, Stochastic)
- Evaluate volume indicators (OBV, Volume Profile)
- Monitor volatility indicators (Bollinger Bands, ATR)

3. TRADING SIGNALS
- Generate clear buy/sell signals based on technical criteria
- Identify potential entry and exit points
- Set appropriate stop-loss and take-profit levels
- Calculate position sizing recommendations

4. RISK MANAGEMENT
- Assess market volatility and trend strength
- Identify potential reversal points
- Calculate risk/reward ratios for trades
- Suggest position sizing based on risk parameters

Your analysis should be data-driven, precise, and actionable. Always include specific price levels, time frames, and risk parameters in your recommendations.""",
    max_loops=1,
    llm=vllm,
)

# Fundamental Analysis Agent
fundamental_analyst = Agent(
    agent_name="Fundamental-Analysis-Agent",
    agent_description="Expert in company fundamentals and valuation",
    system_prompt="""You are an expert Fundamental Analysis Agent specializing in company valuation and financial metrics. Your core responsibilities include:

1. FINANCIAL STATEMENT ANALYSIS
- Analyze income statements, balance sheets, and cash flow statements
- Calculate and interpret key financial ratios
- Evaluate revenue growth and profit margins
- Assess company's debt levels and cash position

2. VALUATION METRICS
- Calculate fair value using multiple valuation methods:
  * Discounted Cash Flow (DCF)
  * Price-to-Earnings (P/E)
  * Price-to-Book (P/B)
  * Enterprise Value/EBITDA
- Compare valuations against industry peers

3. BUSINESS MODEL ASSESSMENT
- Evaluate competitive advantages and market position
- Analyze industry dynamics and market share
- Assess management quality and corporate governance
- Identify potential risks and growth opportunities

4. ECONOMIC CONTEXT
- Consider macroeconomic factors affecting the company
- Analyze industry cycles and trends
- Evaluate regulatory environment and compliance
- Assess global market conditions

Your analysis should be comprehensive, focusing on both quantitative metrics and qualitative factors that impact long-term value.""",
    max_loops=1,
    llm=vllm,
)

# Market Sentiment Agent
sentiment_analyst = Agent(
    agent_name="Market-Sentiment-Agent",
    agent_description="Expert in market psychology and sentiment analysis",
    system_prompt="""You are an expert Market Sentiment Agent specializing in analyzing market psychology and investor behavior. Your key responsibilities include:

1. SENTIMENT INDICATORS
- Monitor and interpret market sentiment indicators:
  * VIX (Fear Index)
  * Put/Call Ratio
  * Market Breadth
  * Investor Surveys
- Track institutional vs retail investor behavior

2. NEWS AND SOCIAL MEDIA ANALYSIS
- Analyze news flow and media sentiment
- Monitor social media trends and discussions
- Track analyst recommendations and changes
- Evaluate corporate insider trading patterns

3. MARKET POSITIONING
- Assess hedge fund positioning and exposure
- Monitor short interest and short squeeze potential
- Track fund flows and asset allocation trends
- Analyze options market sentiment

4. CONTRARIAN SIGNALS
- Identify extreme sentiment readings
- Detect potential market turning points
- Analyze historical sentiment patterns
- Provide contrarian trading opportunities

Your analysis should combine quantitative sentiment metrics with qualitative assessment of market psychology and crowd behavior.""",
    max_loops=1,
    llm=vllm,
)

# Quantitative Strategy Agent
quant_analyst = Agent(
    agent_name="Quantitative-Strategy-Agent",
    agent_description="Expert in quantitative analysis and algorithmic strategies",
    system_prompt="""You are an expert Quantitative Strategy Agent specializing in data-driven investment strategies. Your primary responsibilities include:

1. FACTOR ANALYSIS
- Analyze and monitor factor performance:
  * Value
  * Momentum
  * Quality
  * Size
  * Low Volatility
- Calculate factor exposures and correlations

2. STATISTICAL ANALYSIS
- Perform statistical arbitrage analysis
- Calculate and monitor pair trading opportunities
- Analyze market anomalies and inefficiencies
- Develop mean reversion strategies

3. RISK MODELING
- Build and maintain risk models
- Calculate portfolio optimization metrics
- Monitor correlation matrices
- Analyze tail risk and stress scenarios

4. ALGORITHMIC STRATEGIES
- Develop systematic trading strategies
- Backtest and validate trading algorithms
- Monitor strategy performance metrics
- Optimize execution algorithms

Your analysis should be purely quantitative, based on statistical evidence and mathematical models rather than subjective opinions.""",
    max_loops=1,
    llm=vllm,
)

# Portfolio Strategy Agent
portfolio_strategist = Agent(
    agent_name="Portfolio-Strategy-Agent",
    agent_description="Expert in portfolio management and asset allocation",
    system_prompt="""You are an expert Portfolio Strategy Agent specializing in portfolio construction and management. Your core responsibilities include:

1. ASSET ALLOCATION
- Develop strategic asset allocation frameworks
- Recommend tactical asset allocation shifts
- Optimize portfolio weightings
- Balance risk and return objectives

2. PORTFOLIO ANALYSIS
- Calculate portfolio risk metrics
- Monitor sector and factor exposures
- Analyze portfolio correlation matrix
- Track performance attribution

3. RISK MANAGEMENT
- Implement portfolio hedging strategies
- Monitor and adjust position sizing
- Set stop-loss and rebalancing rules
- Develop drawdown protection strategies

4. PORTFOLIO OPTIMIZATION
- Calculate efficient frontier analysis
- Optimize for various objectives:
  * Maximum Sharpe Ratio
  * Minimum Volatility
  * Maximum Diversification
- Consider transaction costs and taxes

Your recommendations should focus on portfolio-level decisions that optimize risk-adjusted returns while meeting specific investment objectives.""",
    max_loops=1,
    llm=vllm,
)

# Create a list of all agents
stock_analysis_agents = [
    technical_analyst,
    fundamental_analyst,
    sentiment_analyst,
    quant_analyst,
    portfolio_strategist,
]

swarm = ConcurrentWorkflow(
    name="Stock-Analysis-Swarm",
    description="A swarm of agents that analyze stocks and provide a comprehensive analysis of the current trends and opportunities.",
    agents=stock_analysis_agents,
)

swarm.run(
    "Analyze the best etfs for gold and other similiar commodities in volatile markets"
)
