Using Python, GPT, TiDB, and Aliyun to Analyze Stocks

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 用python gpt tidb 和aliyun 来分析股票

| username: tidb狂热爱好者

Get all stock IDs of the Nasdaq 100

import requests

from bs4 import BeautifulSoup

# Set request header information

headers = {

"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"

}

# Send GET request

res = requests.get("https://api.nasdaq.com/api/quote/list-type/nasdaq100", headers=headers)

# Parse JSON data

main_data = res.json()['data']['data']['rows']

# Print company names

for i in range(len(main_data)):

print(main_data[i]['symbol'])

import yfinance as yf

import pandas as pd

import mysql.connector

from sqlalchemy import create_engine

# Define the list of stock codes for the top 100 companies on Nasdaq

nasdaq_top_100 = [ ] # Please complete the list

import requests

from bs4 import BeautifulSoup

# Set request header information

headers = {

"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"

}

# Send GET request

res = requests.get("https://api.nasdaq.com/api/quote/list-type/nasdaq100", headers=headers)

# Parse JSON data

main_data = res.json()['data']['data']['rows']

# Print company names

for i in range(len(main_data)):

print(main_data[i]['symbol'])

nasdaq_top_100.append(main_data[i]['symbol'])

# Create an empty list to store each company's DataFrame

dataframes = []

# Iterate through the list of stock codes

for ticker in nasdaq_top_100:

# Get stock data

stock = yf.Ticker(ticker)

# Get financial statement data

balance_sheet = stock.balance_sheet

income_statement = stock.financials

# Calculate ROE

net_income = income_statement.loc['Net Income']

shareholder_equity = balance_sheet.loc['Stockholders Equity']

roe = net_income / shareholder_equity

# Create a DataFrame to store the current company's ROE

df = pd.DataFrame({'Ticker': [ticker], 'ROE': [roe]})

# Add the DataFrame to the list

dataframes.append(df)

# Use pd.concat to merge all companies' DataFrames

roe_df = pd.concat(dataframes)

# Output the result

print(roe_df)

#url = 'mysql+pymysql://username:password@hostname:port/dbname?charset=utf8'

#engine = sa.create_engine(url, echo=False)

engine = create_engine('mysql+pymysql://username:password@ip/test')

roe_df.to_sql('df', engine, index=False,

method = "multi",chunksize = 10000 ,if_exists='replace')

End of article

| username: TIDB-Learner | Original post link

Very good!!

| username: zhanggame1 | Original post link

Support

| username: TiDBer_QYr0vohO | Original post link

Awesome, you’re amazing!

| username: 呢莫不爱吃鱼 | Original post link

Learning from the experts.

| username: xiaoqiao | Original post link

:+1: :+1: :+1:

| username: DBAER | Original post link

Bro, can you make a series?

| username: TiDBer_JUi6UvZm | Original post link

Awesome. Teach me how to make money with stocks.

| username: dba远航 | Original post link

Awesome!

| username: DBRE | Original post link

awesomeness

| username: Swan | Original post link

Interesting, a self-made fear and greed index :grin: Thanks for sharing

| username: TiDBer_21wZg5fm | Original post link

You’re awesome!