Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded Sentiment Analysis with Real-time Market Insights #28

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

buabaj
Copy link

@buabaj buabaj commented Dec 27, 2024

I've updated the sentiment analysis system to provide more comprehensive market insights. The main improvements focus on news sentiment analysis, working alongside the existing insider trading signals.

Key changes:

  • Integrated NewsAPI to fetch real-time news articles about our target companies
  • Added company name resolution using yfinance for more accurate news searching
  • Expanded sentiment analysis to process all relevant articles from the date range passed in the run command (default of 3 months might need a higher tier news API key to fetch)
  • Using GPT-4 to analyze news sentiment with a focus on market impact
  • Combined news and insider trading signals (60/40 weight) for final sentiment as insider trading sentiment is more concrete

The agent now looks at both insider trading patterns and broad market sentiment from news, giving us a more complete picture. News analysis focuses on concrete factors like revenue impact, market competition, and product developments.

I've kept the original insider trading logic intact since it was working well, just added the news layer on top of it. Happy to adjust the weights or time window if we think it needs tuning.

sample sentiment analysis result:

==========   Sentiment Analysis Agent   ==========
{
  "signal": "bearish",
  "confidence": "60%",
  "reasoning": {
    "insider_sentiment": {
      "signal": "bearish",
      "confidence": "100%",
      "bullish_trades": 0,
      "bearish_trades": 4
    },
    "news_sentiment": {
      "signal": "bullish",
      "confidence": "68%",
      "articles_analyzed": 2,
      "key_points": [
        "Apple's AI rollout is expected to drive demand for the iPhone 16, boosting revenue.",
        "The article discusses AI chatbots selecting stocks, which doesn't directly impact company fundamentals.",
        "No specific company or innovation/product development details were provided that could influence stock value.",
        "The potential increase in market value to $4 trillion indicates strong investor confidence."
      ]
    }
  }
}
================================================

@umershahid84
Copy link

umershahid84 commented Dec 27, 2024 via email

@buabaj
Copy link
Author

buabaj commented Dec 27, 2024

hi @umershahid84 thanks for trying this out. would you be kind enough to share what error message or logs you get that shows this doesn't work, please? as the only extra dependency added to this feature is yfinance, and should work as expected if you have all existing dependencies installed via poetry install.

off the top of my head, an issue you might encounter is a rate limit from the newsAPI if you have a free tier and try to get responses from over a month as the free tier API does not allow that.

if this is the problem and you do have a free tier API from newsAPI, you can resolve this by running the tool with a specific date range command to ensure you are not hitting the rate limit. you can do this like:

poetry run python src/main.py --ticker AAPL --start-date 2024-12-01 --end-date 2024-12-26 --show-reasoning

this should fix the issue as this range is between the allowed range for the free tier. alternatively, you could let me know and I am more than happy to share an API key from my account as I have paid credits if you would like to test this for a much longer span, like the 3 month default if a date range is not specified.

While this should resolve the issue, I am also open to modifying the code to always only query for the last month's worth of news so it is unaffected by whatever date range is being used to run the project as long as an API key for the newsAPI is provided. thanks

@umershahid84
Copy link

umershahid84 commented Dec 27, 2024 via email

@buabaj
Copy link
Author

buabaj commented Dec 28, 2024

@umershahid84 the image you attached seems to be attached improperly.

@vinniejames
Copy link

This is a great addition, would be neat to additionally incorporate X data feeds for sentiment there as well as breaking news

@buabaj
Copy link
Author

buabaj commented Dec 31, 2024

@vinniejames that sounds amazing and would be a neat addition but the X API is so expensive at $100/mo for a basic plan with 10k pulls per month. if @virattt gives the go ahead, I would be more than happy to add that!

@marc-at-brightnight
Copy link

@vinniejames that sounds amazing and would be a neat addition but the X API is so expensive at $100/mo for a basic plan with 10k pulls per month. if @virattt gives the go ahead, I would be more than happy to add that!

I think even 10k pulls/month is not enough to get a good understanding of market sentiment

src/tools/api.py Outdated Show resolved Hide resolved
src/agents/sentiment.py Outdated Show resolved Hide resolved
src/agents/sentiment.py Show resolved Hide resolved
src/agents/sentiment.py Outdated Show resolved Hide resolved
src/agents/sentiment.py Outdated Show resolved Hide resolved
src/agents/sentiment.py Show resolved Hide resolved
src/agents/sentiment.py Show resolved Hide resolved
src/agents/sentiment.py Outdated Show resolved Hide resolved
@buabaj
Copy link
Author

buabaj commented Jan 18, 2025

@virattt any chance this will be looked at? if there are no plans on adding something like this to the project, please feel free to let me know :)

this is what an output currently looks like for running with reasoning:

poetry run python src/main.py --ticker MSTR --start-date 2024-06-01 --end-date 2025-01-18 --show-reasoning

? Select your AI analysts. done (4 selections)

Selected analysts: Technical Analyst, Fundamentals Analyst, Sentiment Analyst, Valuation Analyst


==========  Fundamental Analysis Agent  ==========
{
  "MSTR": {
    "signal": "neutral",
    "confidence": 25.0,
    "reasoning": {
      "profitability_signal": {
        "signal": "bearish",
        "details": "ROE: -14.60%, Net Margin: -87.00%, Op Margin: -194.94%"
      },
      "growth_signal": {
        "signal": "neutral",
        "details": "Revenue Growth: -2.79%, Earnings Growth: -93.69%"
      },
      "financial_health_signal": {
        "signal": "neutral",
        "details": "Current Ratio: 0.65, D/E: 1.21"
      },
      "price_ratios_signal": {
        "signal": "bullish",
        "details": "P/E: -80.55, P/B: 8.68, P/S: 70.12"
      }
    }
  }
}
================================================

==========      Technical Analyst       ==========
{
  "MSTR": {
    "signal": "bullish",
    "confidence": 55,
    "strategy_signals": {
      "trend_following": {
        "signal": "bullish",
        "confidence": 22,
        "metrics": {
          "adx": 21.51270550408803,
          "trend_strength": 0.21512705504088028
        }
      },
      "mean_reversion": {
        "signal": "neutral",
        "confidence": 50,
        "metrics": {
          "z_score": 0.8347289724883126,
          "price_vs_bb": 1.0339300537585485,
          "rsi_14": 60.63605324740093,
          "rsi_28": 50.87719298245614
        }
      },
      "momentum": {
        "signal": "bullish",
        "confidence": 100,
        "metrics": {
          "momentum_1m": 0.0244798701087523,
          "momentum_3m": 0.8946524250356122,
          "momentum_6m": 1.212044477017976,
          "volume_momentum": 1.147511293360851
        }
      },
      "volatility": {
        "signal": "neutral",
        "confidence": 50,
        "metrics": {
          "historical_volatility": 1.177242999205214,
          "volatility_regime": 0.9971884739588139,
          "volatility_z_score": -0.016163006457079663,
          "atr_ratio": 0.07520807061790667
        }
      },
      "statistical_arbitrage": {
        "signal": "neutral",
        "confidence": 50,
        "metrics": {
          "hurst_exponent": 4.4162737839765496e-15,
          "skewness": 0.3850078148661564,
          "kurtosis": 0.48159889181993865
        }
      }
    }
  }
}
================================================

==========   Valuation Analysis Agent   ==========
{
  "MSTR": {
    "signal": "bearish",
    "confidence": 100.0,
    "reasoning": {
      "dcf_analysis": {
        "signal": "bearish",
        "details": "Intrinsic Value: $-36,235,027.64, Market Cap: $32,762,470,020.00, Gap: -100.1%"
      },
      "owner_earnings_analysis": {
        "signal": "bearish",
        "details": "Owner Earnings Value: $0.00, Market Cap: $32,762,470,020.00, Gap: -100.0%"
      }
    }
  }
}
================================================

==========   Sentiment Analysis Agent   ==========
{
  "MSTR": {
    "signal": "bearish",
    "confidence": 56.05,
    "reasoning": {
      "insider_sentiment": {
        "signal": "bearish",
        "confidence": 93.42,
        "bullish_trades": 64,
        "bearish_trades": 909
      },
      "news_sentiment": {
        "signal": "neutral",
        "confidence": 60.0,
        "articles_analyzed": 5,
        "key_points": [
          "No direct information on revenue, competition, regulation, or innovation, hence neutral on stock price impact.",
          "Increased stake by Salem Investment Counselors may indicate institutional confidence but does not directly impact competition or product 
development.",
          "Mentions of future rate cuts are noted but do not directly provide information on stock value impacts for MicroStrategy.",
          "Continued emphasis on Bitcoin over core software operations may undermine traditional revenue streams.",
          "No mention of revenue, profit, regulatory changes, or product innovations in the report affecting stock value.",
          "Brookstone Capital Management's increase in holdings could signal confidence in MicroStrategy's future performance, potentially influencing other
investors.",
          "Increase in institutional holdings by Wedmont Private Capital, indicating confidence but not directly linked to company's financial 
performance.",
          "MicroStrategy's financial focus on Bitcoin increases volatility risk due to cryptocurrency market fluctuations.",
          "The article discusses MicroStrategy's position among growth stocks, but lacks specific information on revenue, competition, regulations, or 
innovations."
        ]
      }
    }
  }
}
================================================

==========    Risk Management Agent     ==========
{
  "MSTR": {
    "remaining_position_limit": 20000.0,
    "current_price": 396.5,
    "reasoning": {
      "portfolio_value": 100000.0,
      "current_position": 0.0,
      "position_limit": 20000.0,
      "remaining_limit": 20000.0,
      "available_cash": 100000.0
    }
  }
}
================================================

==========  Portfolio Management Agent  ==========
{
  "MSTR": {
    "action": "hold",
    "quantity": 0,
    "confidence": 56.05,
    "reasoning": "Despite the technical analyst's bullish signal with moderate confidence, the valuation and sentiment agents both indicate bearish signals 
with high confidence. Given these mixed signals and the high bearish confidence from the valuation agent, I'll opt to hold this position rather than 
initiating a buy."
  }
}
================================================
 ✓ Fundamentals        [MSTR] Done                                                                    
 ✓ Sentiment           [MSTR] Done                                                                    
 ✓ Technical Analyst   [MSTR] Done                                                                    
 ✓ Valuation           [MSTR] Done                                                                    
 ✓ Risk Management     [MSTR] Done                                                                    
 ✓ Portfolio Management[MSTR] Done                                                                    

Analysis for MSTR
==================================================

ANALYST SIGNALS: [MSTR]
+-------------------+----------+--------------+
| Analyst           |  Signal  |   Confidence |
+===================+==========+==============+
| Fundamentals      | NEUTRAL  |        25.0% |
+-------------------+----------+--------------+
| Technical Analyst | BULLISH  |          55% |
+-------------------+----------+--------------+
| Valuation         | BEARISH  |       100.0% |
+-------------------+----------+--------------+
| Sentiment         | BEARISH  |       56.05% |
+-------------------+----------+--------------+
| Risk Management   |          |        None% |
+-------------------+----------+--------------+

TRADING DECISION: [MSTR]
+------------+-------+
| Action     |  HOLD |
+------------+-------+
| Quantity   |     0 |
+------------+-------+
| Confidence | 56.0% |
+------------+-------+

Reasoning: Despite the technical analyst's bullish signal with moderate confidence, the valuation and sentiment agents both indicate bearish signals with high confidence. Given these mixed signals and the high bearish confidence from the valuation agent, I'll opt to hold this position rather than initiating a buy.

PORTFOLIO SUMMARY:
+----------+----------+------------+--------------+
| Ticker   |  Action  |   Quantity |   Confidence |
+==========+==========+============+==============+
| MSTR     |   HOLD   |          0 |        56.0% |
+----------+----------+------------+--------------+

@virattt
Copy link
Owner

virattt commented Jan 19, 2025

@buabaj - I'm adding news to the sentiment agent tomorrow.

Haven't heard of NewsAPI, but at $449/month just for news data, it is really steep.

@virattt
Copy link
Owner

virattt commented Jan 19, 2025

Any idea who the creator is of NewsAPI?

@buabaj
Copy link
Author

buabaj commented Jan 19, 2025

Hi @virattt yes, i do agree that the pricing is quite steep and I do not the creator, it just came up as one of the good news consolidation sources more affordable than BBG. NewsData seems a lot more affordable at $150/mo (https://newsdata.io/pricing) if you want us to consider that instead. I've been testing NewsAPI with the free tier on this project with no issues though.

Is there something different you want done with the news sentiment though? I'm willing to contribute that if possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants