Skip to content

Commit

Permalink
Update Nikkei 225 evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
SU2H1 authored Jun 23, 2024
1 parent a4061c8 commit ae1f2b4
Showing 1 changed file with 66 additions and 56 deletions.
122 changes: 66 additions & 56 deletions Nikkei 225 evaluation
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,15 @@ def get_stock_codes_and_names():

soup = BeautifulSoup(response.content, 'html.parser')

# Print the first 500 characters of the HTML content for debugging
print("First 500 characters of HTML content:")
print(soup.prettify()[:500])

# Try to find the table with the original class
stock_table = soup.find('table', {'class': 'md-l-table-type01'})

if stock_table is None:
print("Could not find table with class 'md-l-table-type01'")
# Try to find any table in the HTML
all_tables = soup.find_all('table')
print(f"Number of tables found: {len(all_tables)}")

# Print classes of all tables found
for i, table in enumerate(all_tables):
print(f"Table {i+1} classes: {table.get('class', 'No class')}")

# If no table with the expected class is found, try to find a table with a similar structure
for table in all_tables:
if table.find('tr'): # Check if the table has any rows
if table.find('tr'):
stock_table = table
print(f"Using table with classes: {stock_table.get('class', 'No class')}")
break
Expand All @@ -61,18 +50,6 @@ def get_stock_codes_and_names():
print(f"Found {len(stock_data)} stocks")
return stock_data

def get_company_name(stock_number):
url = f"https://finance.yahoo.co.jp/quote/{stock_number}.T"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
title_tag = soup.find('title')
if title_tag:
title = title_tag.text.strip()
company_name = title.split('【')[0].strip()
return company_name
else:
return None

def scrape_nikkei_news(stock_number):
url = f"https://www.nikkei.com/nkd/company/news/?scode={stock_number}&ba=1"
response = requests.get(url)
Expand Down Expand Up @@ -131,6 +108,12 @@ def sentiment_to_text(score):
else:
return "Very Positive"

def calculate_average_sentiment(sentiments):
if not sentiments:
return "Neutral"
avg_sentiment = sum(sentiments) / len(sentiments)
return sentiment_to_text(avg_sentiment)

def get_stock_data(stock_number):
ticker = f"{stock_number}.T"
end_date = datetime.now()
Expand All @@ -146,7 +129,6 @@ def get_stock_data(stock_number):
df['Date'] = pd.to_datetime(df['Date'])
stock_data = [(row['Date'], row['Close']) for _, row in df.iterrows()]

# Sort by date (newest first) and return up to 30 days of data
stock_data.sort(key=lambda x: x[0], reverse=True)
return stock_data[:30]

Expand Down Expand Up @@ -174,28 +156,51 @@ def calculate_stock_trend(stock_data):
else:
return "Neutral"

def get_action_recommendation(public_opinion, stock_trend, current_price):
opinion_score = {
"Very Positive": 2, "Positive": 1, "Neutral": 0, "Negative": -1, "Very Negative": -2
}
trend_score = {
"Strong Uptrend": 2, "Uptrend": 1, "Neutral": 0, "Downtrend": -1, "Strong Downtrend": -2
}

total_score = opinion_score[public_opinion] + trend_score[stock_trend]

if total_score >= 2:
target_price = current_price * 1.05 # 5% above current price
return f"Buy (Target: ¥{target_price:.2f})"
elif total_score <= -2:
target_price = current_price * 1.05 # 5% above current price for selling
return f"Sell (Target: ¥{target_price:.2f})"
else:
return "Hold"

def display_stock_info(stock, rank):
print(f"{rank}. {stock['company_name']} ({stock['stock_number']}):")
print(f" Current Price: ¥{stock['current_stock_price']:.2f}" if stock['current_stock_price'] else " Current Price: N/A")
print(f" Overall Sentiment: {stock['overall_sentiment']}")
print(f" Sentiment Score: {stock['overall_sentiment_value']:.2f}")
print(f" Nikkei's Impression: {stock['nikkei_sentiment']}")
print(f" Yahoo's Impression: {stock['yahoo_sentiment']}")
print(f" Overall Public Opinion: {stock['overall_sentiment']}")
print(f" Stock Trend: {stock['stock_trend']}")
print(f" Action: {stock['action']}")
print()

def display_detailed_analysis(stock):
print(f"\nDetailed Analysis for {stock['company_name']} ({stock['stock_number']}):")
print(f"Current Price: ¥{stock['current_stock_price']:.2f}" if stock['current_stock_price'] else "Current Price: N/A")
print(f"Overall Sentiment: {stock['overall_sentiment']}")
print(f"Sentiment Score: {stock['overall_sentiment_value']:.2f}")
print(f"Nikkei's Impression: {stock['nikkei_sentiment']}")
print(f"Yahoo's Impression: {stock['yahoo_sentiment']}")
print(f"Overall Public Opinion: {stock['overall_sentiment']}")
print(f"Stock Trend: {stock['stock_trend']}")
print("\nNikkei News Sentiment:")
print(f"Action: {stock['action']}")
print("\nNikkei News:")
for i, news in enumerate(stock['nikkei_news'], 1):
print(f" {i}. {news['title']}")
print(f" Sentiment: {sentiment_to_text(news['sentiment'])}")
print("\nYahoo Finance News Sentiment:")
print(f" URL: {news['url']}")
print("\nYahoo Finance News:")
for i, news in enumerate(stock['yahoo_news'], 1):
print(f" {i}. {news['title']}")
print(f" Sentiment: {sentiment_to_text(news['sentiment'])}")
print(f" URL: {news['url']}")
print()

def interactive_results_display(stock_analysis):
Expand All @@ -206,25 +211,31 @@ def interactive_results_display(stock_analysis):

if current_index + 10 >= len(stock_analysis):
print("End of list reached.")
break

user_input = input("Type 'continue' to see more results, '#<rank>' to see detailed analysis for a stock, or 'exit' to quit: ").strip().lower()
user_input = input("Type 'continue' to see more results, '#<rank>' to see detailed analysis for a stock, '#buy', '#sell', or '#hold' to see top stocks for each action, or 'exit' to quit: ").strip().lower()

if user_input == 'continue':
if user_input == 'continue' and current_index + 10 < len(stock_analysis):
current_index += 10
elif user_input.startswith('#'):
try:
rank = int(user_input[1:]) - 1
if 0 <= rank < len(stock_analysis):
display_detailed_analysis(stock_analysis[rank])
else:
print("Invalid rank number.")
except ValueError:
print("Invalid input. Please use '#<rank>' format.")
if user_input[1:] in ['buy', 'sell', 'hold']:
action = user_input[1:].capitalize()
top_stocks = [s for s in stock_analysis if s['action'].startswith(action)][:5]
print(f"\nTop 5 stocks to {action}:")
for i, stock in enumerate(top_stocks, 1):
display_stock_info(stock, i)
else:
try:
rank = int(user_input[1:]) - 1
if 0 <= rank < len(stock_analysis):
display_detailed_analysis(stock_analysis[rank])
else:
print("Invalid rank number.")
except ValueError:
print("Invalid input. Please use '#<rank>' format.")
elif user_input == 'exit':
break
else:
print("Invalid input. Please type 'continue', '#<rank>', or 'exit'.")
print("Invalid input. Please type 'continue', '#<rank>', '#buy', '#sell', '#hold', or 'exit'.")

def main():
print("Fetching stock codes and company names from the website...")
Expand All @@ -249,31 +260,30 @@ def main():
nikkei_sentiments = [analyze_sentiment(news['title'], ja_tokenizer, ja_model, en_tokenizer, en_model) for news in nikkei_news_data]
yahoo_finance_sentiments = [analyze_sentiment(news['title'], ja_tokenizer, ja_model, en_tokenizer, en_model) for news in yahoo_finance_news_data]

def get_overall_sentiment(sentiments):
if not sentiments:
return 0.5 # Neutral if no data
avg_sentiment = sum(sentiments) / len(sentiments)
return avg_sentiment
nikkei_overall_sentiment = calculate_average_sentiment(nikkei_sentiments)
yahoo_finance_overall_sentiment = calculate_average_sentiment(yahoo_finance_sentiments)

nikkei_overall_sentiment = get_overall_sentiment(nikkei_sentiments)
yahoo_finance_overall_sentiment = get_overall_sentiment(yahoo_finance_sentiments)

overall_sentiment_value = (nikkei_overall_sentiment + yahoo_finance_overall_sentiment) / 2
overall_sentiment_value = (sum(nikkei_sentiments) + sum(yahoo_finance_sentiments)) / (len(nikkei_sentiments) + len(yahoo_finance_sentiments)) if nikkei_sentiments or yahoo_finance_sentiments else 0.5
overall_sentiment = sentiment_to_text(overall_sentiment_value)

stock_price_data = get_stock_data(stock_number)
current_stock_price = stock_price_data[0][1] if stock_price_data else None
stock_trend = calculate_stock_trend(stock_price_data)

action = get_action_recommendation(overall_sentiment, stock_trend, current_stock_price) if current_stock_price else "N/A"

stock_analysis.append({
"company_name": company_name,
"stock_number": stock_number,
"current_stock_price": current_stock_price,
"nikkei_sentiment": nikkei_overall_sentiment,
"yahoo_sentiment": yahoo_finance_overall_sentiment,
"overall_sentiment": overall_sentiment,
"overall_sentiment_value": overall_sentiment_value,
"stock_trend": stock_trend,
"nikkei_news": [{"title": news['title'], "sentiment": sentiment} for news, sentiment in zip(nikkei_news_data, nikkei_sentiments)],
"yahoo_news": [{"title": news['title'], "sentiment": sentiment} for news, sentiment in zip(yahoo_finance_news_data, yahoo_finance_sentiments)]
"action": action,
"nikkei_news": [{"title": news['title'], "url": news['url']} for news in nikkei_news_data],
"yahoo_news": [{"title": news['title'], "url": news['url']} for news in yahoo_finance_news_data]
})
except Exception as e:
print(f"Error analyzing {stock_number}: {e}")
Expand Down

0 comments on commit ae1f2b4

Please sign in to comment.