LLMs in Financial Data
LLMs in Financial Data
Collecting openai
Downloading openai-1.6.0-py3-none-any.whl (225 kB)
���������������������������������������� 225.4/225.4
kB 3.1 MB/s eta 0:00:00
Requirement already satisfied: anyio<5,>=3.5.0 in
/usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)
Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-
packages (from openai) (1.7.0)
Collecting httpx<1,>=0.23.0 (from openai)
Downloading httpx-0.26.0-py3-none-any.whl (75 kB)
���������������������������������������� 75.9/75.9 kB
8.8 MB/s eta 0:00:00
Requirement already satisfied: pydantic<3,>=1.9.0 in
/usr/local/lib/python3.10/dist-packages (from openai) (1.10.13)
Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-
packages (from openai) (1.3.0)
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages
(from openai) (4.66.1)
Collecting typing-extensions<5,>=4.7 (from openai)
Downloading typing_extensions-4.9.0-py3-none-any.whl (32 kB)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-
packages (from anyio<5,>=3.5.0->openai) (3.6)
Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-
packages (from anyio<5,>=3.5.0->openai) (1.2.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-
packages (from httpx<1,>=0.23.0->openai) (2023.11.17)
Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai)
Downloading httpcore-1.0.2-py3-none-any.whl (76 kB)
���������������������������������������� 76.9/76.9 kB
8.3 MB/s eta 0:00:00
Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai)
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
���������������������������������������� 58.3/58.3 kB
6.2 MB/s eta 0:00:00
1
Installing collected packages: typing-extensions, h11, httpcore, httpx,
openai
Attempting uninstall: typing-extensions
Found existing installation: typing_extensions 4.5.0
Uninstalling typing_extensions-4.5.0:
Successfully uninstalled typing_extensions-4.5.0
ERROR: pip's dependency resolver does not currently take into account all
the packages that are installed. This behaviour is the source of the following
dependency conflicts.
llmx 0.0.15a0 requires cohere, which is not installed.
llmx 0.0.15a0 requires tiktoken, which is not installed.
tensorflow-probability 0.22.0 requires typing-extensions<4.6.0, but you have
typing-extensions 4.9.0 which is incompatible.
Successfully installed h11-0.14.0 httpcore-1.0.2 httpx-0.26.0 openai-1.6.0
typing-extensions-4.9.0
Collecting tiktoken
Downloading
tiktoken-0.5.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0
MB)
���������������������������������������� 2.0/2.0 MB
18.2 MB/s eta 0:00:00
Requirement already satisfied: regex>=2022.1.18 in
/usr/local/lib/python3.10/dist-packages (from tiktoken) (2023.6.3)
Requirement already satisfied: requests>=2.26.0 in
/usr/local/lib/python3.10/dist-packages (from tiktoken) (2.31.0)
Requirement already satisfied: charset-normalizer<4,>=2 in
/usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken)
(3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-
packages (from requests>=2.26.0->tiktoken) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in
/usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken)
(2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in
/usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken)
(2023.11.17)
Installing collected packages: tiktoken
ERROR: pip's dependency resolver does not currently take into account all
the packages that are installed. This behaviour is the source of the following
dependency conflicts.
llmx 0.0.15a0 requires cohere, which is not installed.
Successfully installed tiktoken-0.5.2
2
[6]: from google.colab import userdata
openai_api_key = userdata.get('OPENAI_API_KEY')
#Call gpt-3.5-turbo
def summarize_report(report_text):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": file_content}
]
)
return response.choices[0].message
# """
summary = summarize_report(file_content)
3
print("Summary:\n" + summary.content)
Summary:
Based on the discussion provided in the transcript, here is a summary of the
outlook for the macro economy in 2024:
2. Currencies:
- The US dollar is expected to continue strengthening in 2024 as the US
outperforms other regions in terms of growth and rate differentials.
- The dollar is seen as a defensive currency that offers liquidity and safe-
haven status, as well as high yields.
- However, the dollar's strength is expected to fade as fiscal support wanes, US
growth slows down, and the Federal Reserve cuts rates.
- Once the Fed starts cutting rates, it is expected to continue easing,
potentially pushing the dollar down.
3. Credit Markets:
- The credit markets are expected to perform well in 2024, supported by
favorable fundamentals, technicals, and average valuations.
- Higher-quality fixed income, especially investment-grade corporate credit, is
seen as attractive compared to other assets.
- The earnings yield on S&P 500 stocks relative to U.S. triple B-rated corporate
bonds has flipped, making bonds relatively more appealing.
- A potential undersupply of corporate bonds could increase demand, especially
if the Federal Reserve begins cutting rates.
- Credit curves, such as the 3 to 5-year part of the U.S. credit curve and the 5
to 10-year part of the investment-grade curve in Europe, are seen as offering
opportunities.
4. Housing Market:
- The US housing market held up remarkably well in 2023 despite higher mortgage
rates.
- Affordability is expected to improve, and for-sale inventory is projected to
4
increase, supporting housing activity.
- Home prices are forecasted to see modest declines as the growth in inventory
offsets increased demand.
- Homeowners are expected to retain strong hands in the housing cycle, reducing
the likelihood of significant price corrections.
- Issuance in the agency mortgage-backed securities (MBS) market is expected to
slightly increase in 2024.
- The Federal Reserve is anticipated to reduce its mortgage portfolio throughout
the year, which will need to be absorbed by the private market.
Please note that these insights are based on the discussion provided and may not
capture the complete outlook for the macro economy in 2024. It is always
advisable to refer to additional sources and market research for a comprehensive
understanding.
# reader = PdfReader(pdf) # In pypdf directly you can't use the pdf url␣
↪directly, you need to load it locally beore using it or Use LangChain pdf␣
↪loader
# pages = reader.pages
# file_content=""
# for page in pages:
# file_content += page.extract_text()
2 Sentiment Analysis
In the dynamic world of finance, staying ahead of market sentiment is crucial for making informed
investment decisions. Whether it’s monitoring news articles, tweets, or social media chatter, gauging
sentiment provides valuable insights.
Market sentiment, the overall attitude of investors and traders toward the market, can significantly
impact stock prices and market trends. Positive sentiment often leads to bullish markets, while
negative sentiment can trigger bearish movements. Keeping tabs on this sentiment can give you a
competitive edge in the financial landscape.
Main topics:
• Predictive Insights: Sentiment analysis can help predict market movements and identify po-
tential trends before they materialize.
• Risk Management: Understanding sentiment can aid in managing risks associated with in-
vestments and portfolios.
5
• Real-Time Monitoring: With ChatGPT, you can perform real-time sentiment analysis on
news articles, social media posts, and financial reports to make timely decisions.
2.1 Fetch data from the web using : yfinance, request and BeautifulSoup
You can use yahoo finance to fetch news and then pass them through an LLMs to analyse sentiment:
[19]: import yfinance as yf
# Create a Yahoo Finance ticker symbol for the stock or topic you're interested␣
↪in
# Get the latest news headlines related to the specified stock or topic
news_data = ticker.news
Headline 1: Dow Jones Futures Fall As Nike Dives; Apple Leads Six Stocks To
Watch With Inflation Data Due
URL: https://finance.yahoo.com/m/5c596659-27b6-309c-a7bf-88719cb53abf/dow-jones-
futures-fall-as.html
Headline 5: Magnificent Seven Stocks To Buy And Watch: Alphabet, Nvidia, Tesla
Stock Rally
URL:
https://finance.yahoo.com/m/4205eaa9-f620-3a0b-a81a-0e82c7c9fd0b/magnificent-
6
seven-stocks-to.html
I’ll take this news about Apple to ask for sentiment analysis classification from LLms:
[29]: title = news_data[5]['title']
link = news_data[5]['link']
print(f"Headline : {title}")
print(f"URL: {link}\n")
Headline : UPDATE 1-Apple cannot repair older out-of-warranty watches during ban
- Bloomberg News
URL: https://finance.yahoo.com/news/1-apple-cannot-repair-older-204128889.html
# link = 'https://finance.yahoo.com/news/apple-aapl-stock-moves-1-224518814.
↪html'
# link = 'https://finance.yahoo.com/news/1-apple-cannot-repair-older-204128889.
↪html'
response = requests.get(link)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
article_content = soup.find('div', {'class': 'caas-body'})
article_text = article_content.get_text()
print(article_text)
else:
print('Failed to fetch the article. Check the URL or your internet␣
↪connection.')
7
(Adds status of models Series 9 and Ultra 2 in paragraph 3)Dec 21 (Reuters) -
Apple customer service teams were informed in a company memo this week that it
will no longer replace out-of-warranty models going back to Apple Watch Series
6, Bloomberg News reported on Thursday.The iPhone maker had said on Monday it
would pause sales of its Series 9 and Ultra 2 smartwatches in the U.S. from this
week, in relation to the patent dispute over the technology that enables the
blood oxygen feature on the devices. These two models are currently unavailable
on Apple's U.S. website.If a customer has a broken screen, for instance, they
would not be able to get the issue fixed by Apple, the Bloomberg News report
said, adding the company will still offer help that can be done via software,
such as reinstalling the operating system.Company representatives were told to
inform affected customers they will be contacted when hardware replacements are
allowed again, according to the report.Apple did not immediately respond to a
Reuters request for comment. (Reporting by Arsheeya Bajwa in Bengaluru; Editing
by Krishna Chandra Eluri)
[31]: article_content
8
2.2 Use gpt-3.5-turbo to get sentiment analysis
[32]: def analyze_sentiment(text):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Analyze the sentiment of the following␣
↪text:\n" + text}
]
)
return response.choices[0].message
sentiment = analyze_sentiment(article_text)
print("Summary:\n" + sentiment.content)
Summary:
The sentiment of the text is predominantly negative. It highlights the
discontinuation of out-of-warranty replacements for Apple Watch models and the
pause in sales of Series 9 and Ultra 2 smartwatches in the U.S. due to a patent
dispute. The inability of customers to get their broken screens fixed by Apple
adds to the negative sentiment. Additionally, the fact that hardware
replacements are currently not allowed and that Apple did not respond to a
request for comment further contributes to the negative tone.
9
[40]: import base64
import requests
base64_image = encode_image(path_img)
10
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
}
],
max_tokens = 300
)
return response.choices[0].message
image_description = get_image_description(base64_image)
print("Image Description:\n" + image_description.content)
Image Description:
The image shows a financial chart from Yahoo Finance tracking the price of
Bitcoin in USD (BTC-USD). This is a type of line and bar chart that is commonly
used to represent the price movements of financial instruments. The blue line
represents the price of Bitcoin over time, while the red and green bars likely
represent the trading volume or another similar metric, with red typically
indicating a decrease in price (selling) and green indicating an increase
(buying) on a particular day.
The chart includes various features and tools for analyzing the financial data
such as indicators, comparison options, and the ability to select different date
ranges. The chart also displays the price of Bitcoin at two distinct points,
with one price tag reading "BTC-USD 27583.68" likely indicative of a past price
point, and the current price at the top left corner reading "42,330.02 +362.44
(+0.86%)" indicating the most recent price and the daily change in price and
percentage terms as of the time the image was captured.
11