Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

AI for Investment

Save for later
  • 12 min read
  • 12 Apr 2024

article-image

Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights and books. Don't miss out – sign up today!

Introduction

One of the most important activities for an investor is to always keep up to date with the latest and relevant news. Usually, it’s done by reading at least a dozen news articles starting from macroeconomic issues, political issues, news related to the sector of the corresponding stock, analyst reports, and whatnot. This, of course, takes a lot of time and also sometimes can be overwhelming for new investors since the amount of information to be processed is too much.

Many ML developers have tried to solve this issue by building a traditional ML workflow usually called the sentiment analyzer. This system will take text from the news as the input and return the sentiment score as the output. This is no doubt helpful for the investor, but it doesn’t solve the bigger problem which is the need to curate relevant articles and also knowing what’s the impact of each news toward their investment decision. In other words, it’s lacking of broader insight.

 What if there’s an AI assistant that can act as our personal investment news analyst?

 What if there’s an AI assistant that is able to analyze dozens of news articles and generate the insights summary along with the investment recommendation?

 And, what if I told you that this AI assistant is personalized toward your risk appetite and investment portfolio allocation?

 ai-for-investment-img-0

In this article, I’ll guide you on how to build an AI assistant that can do all the above-mentioned things with only a few lines of code - thanks to GPT4! We’ll discuss several ways to get the news data in bulk and in real-time. We’ll discuss what are the important search keywords we need to use to get relevant news data. We’ll also discuss how to construct the prompt to fulfill all of the above-mentioned criteria while also getting a great generated output. Finally, we’ll see how to put all of this together to build our AI assistant!

Without wasting any more time, let’s take a deep breath, make yourselves comfortable, and be ready to learn how to build your personal AI investment news analyst!

News Data Sources

Getting as much news data as possible is important since we don’t want to miss any important information out there. Once we get all the information, we just need to filter them out with the help of our AI assistant.

SerpAPI is one of the best all-in-one scraping tools that we can utilize to get news data from Google, Yahoo, Bing, DuckDuckGo, and many other search engines. It also provides a free plan with a 100 searches/month limit. However, this limit is surely not enough for our use case. If you don’t mind spending some money and want to get multiple search results from different search engines, then this tool is suitable for you.

Another solution that is more budget-friendly is by utilizing DuckDuckGo search engine API directly. DuckDuckGo is a search engine that offers data privacy as their main unique selling point. No search history will be stored. Moreover, they also open their search engine API for free. We will use DuckDuckGo in this article and learn how to utilize it via Python!

The more effective way to widen our search results is actually not by using different search engines but by having a diverse yet mutually exclusive set of search keywords. The goal of our AI investment assistant is to summarize the important insights that are relevant to a particular stock that we’re interested in. Hence, we need to provide relevant news data to be able to achieve our goal.

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at ₹800/month. Cancel anytime

The following are some of the search keywords that we can use. Note that this list is not exhaustive, you can surely expand the search keywords based on your own needs. We’ll use AAPL as the ticker example. You can change it to any ticker you want.

  • $AAPL stock 
  • $AAPL industry and competitors 
  • $AAPL business model and strategy 
  • $AAPL management and leadership

Besides ticker-specific search keywords, we can also search for more general information that is not ticker-specific. The following is an example list of such keywords.

  • economic growth this year
  • monetary and fiscal policies today
  • politic today
  • economic today
  • inflation rate today
  • interest rate today
  • real estate today

DuckDuckGo API

Once we have the keywords list, we can easily get the news data using DuckDuckGo via Python. First, we need to install the duckduckgo package by running the following command. 

pip install duckduckgo-search

Once it is installed we can create the general Python function that can take the search keyword as the input and return the search results.

from duckduckgo_search import DDGS
import json
ddgs = DDGS()
 
def web_search(query: str, num_results: int = 4,debug=True) -> str:
    """Useful for general internet search queries."""
    if debug:
        print("Searching with query {0}...".format(query))
    search_results = []
    if not query:
        return json.dumps(search_results)
 
    results = ddgs.text(query)
    if not results:
        return json.dumps(search_results)
 
    total_added = 0
    for j in results:
        search_results.append(j.get('body',''))
        total_added += 1
        if total_added >= num_results:
            break
 
    return search_results

Using this function is very simple. We just need to pass the search keyword along with the number of search results to this function and get the list of search results.

apple_competitors_news = web_search(“$AAPL industry and competitors”, num_results = 10)

Prompt Engineering

The next important thing to do is to build our AI assistant. Here, we’ll utilize GPT4 to build our assistant. Since it’s an LLM, we just need to provide the prompt without the need to train it from scratch. However, creating the prompt itself is indeed not an easy task. I have published another article regarding prompt engineering if you’re interested to learn more about it.

Remember that the goal of our assistant is to analyze the provided news data dump and return the summary insights along with the recommendation as the output. However, to be able to give a recommendation, our assistant needs to know our risk appetite along with our portfolio condition. The following is an example of the system prompt that we can give to GPT4.

system_prompt = “””You are an expert in giving recommendation to BUY / SELL / HOLD for {} ({}).
You can only return in JSON format with 5 fields:
"Investment Thesis" (dictionary of string. Consist of elaborated decision reasoning (in bullet points) based on the risk profile of the investor, unrealized profit, and all of the factors as the basis of your recommendation. Provide numbers to justify your assertions, a lot ideally. The deeper the analysis the better.),
"Investor Profiling" (dictionary of string. Connect the investment thesis with each of the investor profiles, including risk profile and unrealized profit.)
"Summary Thesis" (string. Summary of your all investment thesis as the basis of the given recommendation.  You have to take into account all factors in the investment thesis as well as the investor profiles.),
"recommendation" ("BUY"/"SELL"/"HOLD")
 In the investment thesis, please cover the following factors. If a particular factor needed to write the investment thesis does not exist,
don't try to make up the answer, just write "The information needed is unavailable".
(1) Industry and Competitive Analysis: Assess the company's position within its industry and analyze industry trends, competition, barriers to entry, and market dynamics.
(2) News and Events: Stay updated on relevant news, earnings announcements, product launches, regulatory changes, and other events that can impact the company or the overall market.
(3) Market and Economic Conditions: Assess broader macroeconomic factors from news, including economic growth, interest rates, inflation, monetary and fiscal policies, geopolitical events, gold price, bond price, index price, real estate.”””

And here’s an example of the user prompt that consists of all necessary data points. Risk profiles can be “Moderate”, “Aggresive”, or “Conservative”. 

user_prompt = “””<INVESTOR PROFILE>
Risk Profile: {}
Unrealized Profit: {}%
{}”””

Putting All Together

Now, we just need to create the main function that will act as our personal AI investment assistant. 

def personal_investment_assistant(company_name:str, ticker:str, risk_profile: str,  unrealized_profit_perc: float):
    news_data = []
    for search_keyword in search_kwrds_lst:
          news_data.extend(web_search(search_keyword))
    news_data = "\n".join(news_data)
         
    messages = [
                        {
                            "role": "system",
                            "content": system_prompt.format(company_name,ticker)
                        },
                        {
                            "role": "user",
                            "content": user_prompt.format(risk_profile,unrealized_profit_perc,news_data)
                }
            ]
 
    response = get_gpt_response("gpt-4",
                                temperature = 0.0,
                                messages = messages
                                                )
 
    return response["choices"][0]["message"]["content"].strip()
 
 
import requests
import json
import os
def get_gpt_response(model: str,temperature: float,messages: list):
headers = {
                        'content-type': "application/json",
                        'Authorization': "Bearer " + os.environ["OPENAI_API_KEY"]
                        }
endpoint = 'https://api.openai.com/v1/chat/completions'
 
             data = json.dumps({
                                    "model": model, "messages": messages,
                                    "temperature": temperature,
                                     })
   
            try:
data = requests.post(endpoint, data=data, headers=headers)
                        openai_response = json.loads(data.text)
                        return openai_response
            except Exception as e:
                        print(e)
                        return ""

Conclusion

Congratulations on keeping up to this point! Throughout this article, you have learned how to build your own personal AI investment analyst based on news data. You have learned how to get the news data, a list of useful search keywords, also the code implementation to build the AI assistant. Hope the best for your investment journey and see you in the next article!

Author Bio

Louis Owen is a data scientist/AI engineer from Indonesia who is always hungry for new knowledge. Throughout his career journey, he has worked in various fields of industry, including NGOs, e-commerce, conversational AI, OTA, Smart City, and FinTech. Outside of work, he loves to spend his time helping data science enthusiasts to become data scientists, either through his articles or through mentoring sessions. He also loves to spend his spare time doing his hobbies: watching movies and conducting side projects.

 Currently, Louis is an NLP Research Engineer at Yellow.ai, the world’s leading CX automation platform. Check out Louis’ website to learn more about him! Lastly, if you have any queries or any topics to be discussed, please reach out to Louis via LinkedIn.