### Install FinNLP Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Install the FinNLP package using pip. ```bash pip install finnlp ``` -------------------------------- ### Install dependencies Source: https://github.com/ai4finance-foundation/finnlp/blob/main/finnlp/data_sources/sec_filings/README.md Install the required dependencies for the SEC Data Downloader using pip. ```bash python install -r requirements.txt ``` -------------------------------- ### Configure SEC download parameters Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_Company_Announcement.ipynb Sets the start and end dates, stock ticker, and proxy configuration for the SEC announcement download. ```python start_date = "2020-01-01" end_date = "2020-06-01" stock = "AAPL" config = { "use_proxy": "us_free", "max_retry": 5, "proxy_pages": 3, } ``` -------------------------------- ### Configure Juchao download parameters Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_Company_Announcement.ipynb Sets the start and end dates, stock code, and proxy configuration for the Juchao announcement download. ```python start_date = "2020-01-01" end_date = "2020-06-01" stock = "000001" config = { "use_proxy": "china_free", "max_retry": 5, "proxy_pages": 3, } ``` -------------------------------- ### Configure date range and proxy settings Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Sets up the configuration dictionary with start and end dates, proxy settings, and a Finnhub API token. The token is required and available at https://finnhub.io/dashboard. ```python start_date = "2023-01-01" end_date = "2023-01-03" config = { "use_proxy": "us_free", "max_retry": 5, "proxy_pages": 5, "token": "YOUR_FINNHUB_TOKEN" # Avaliable at https://finnhub.io/dashboard } ``` -------------------------------- ### Configure Weibo Download Parameters Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_Social_Media.ipynb Sets up the start date, end date, stock symbol, and configuration dictionary for the Weibo downloader, including proxy settings and cookies. ```python start_date = "2016-01-01" end_date = "2016-01-02" stock = "茅台" config = { "use_proxy": "china_free", "max_retry": 5, "proxy_pages": 5, "cookies": "Your_Login_Cookies", } ``` -------------------------------- ### Configure date range and proxy settings Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Set the start and end dates for the news download, and configure proxy settings with use_proxy set to 'china_free', max_retry of 5, and proxy_pages of 5. ```python start_date = "2016-01-01" end_date = "2016-01-01" config = { "use_proxy": "china_free", "max_retry": 5, "proxy_pages": 5, } ``` -------------------------------- ### Instantiate Tushare finance news downloader Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Tushare_Finance_News.ipynb Creat an instance of the Tushare finance news downloader. Note: The class name in the example is 'Tushare_finance()', but the imported class is 'Tushare_Major_News'. This may cause a NameError if not defined elsewhere. ```python news_downloader = Tushare_finance() ``` -------------------------------- ### Fetch news data using NewsLoader Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_News/index.html Initializes the NewsLoader and calls fetch_news to retrieve news data. The example shows the basic usage pattern for loading news articles. ```python from finnlp.data_sources.news.news_loader import NewsLoader loader = NewsLoader() news_data = loader.fetch_news() ``` -------------------------------- ### Import FinNLP modules for company announcements Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Imports the required modules from the FinNLP library to work with company announcement data. This is the initial setup step before any data retrieval or processing. ```python from finnlp.data_sources.company_announcement import CompanyAnnouncement from finnlp.data_sources.company_announcement import CompanyAnnouncementDownloader ``` -------------------------------- ### Set up stock, date range, and token Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Finnhub_Sentiment.ipynb Defines the stock ticker, start and end dates, and the Finnhub API token. Replace 'YOUR_FINNHUB_TOKEN' with a valid token. ```python stock = "AAPL" start_date = "2023-01-16" end_date = "2023-02-19" token = "YOUR_FINNHUB_TOKEN" ``` -------------------------------- ### Set date range and stock for SeekingAlpha Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Define the start date, end date, and stock ticker for the news download. ```python start_date = "2023-06-01" end_date = "2023-06-30" stock = "AAPL" ``` -------------------------------- ### Download news for date range Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Tushare_Finance_News.ipynb Calls the download_news method on the downloader instance with the start and end dates to fetch news data. ```python news_downloader.download_news(start_date,end_date) ``` -------------------------------- ### Inspect Eastmoney dataframe shape Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Social_Media.ipynb Prints the shape of the downloaded dataframe, which is (241, 92) for the example. ```python downloader.dataframe.shape ``` ```text Result: (241, 92) ``` -------------------------------- ### Import libraries for company announcement processing Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Imports necessary Python libraries for data manipulation and analysis. This is the initial setup cell for the notebook. ```python import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns ``` -------------------------------- ### Market Watch Date Range Parameters Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Defines the start date, end date, and keyword parameters for a MarketWatch date range news download. ```python start_date = "2022-06-01" end_date = "2022-06-30" keyword = "apple" ``` -------------------------------- ### Set stock and date range Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_twitter.ipynb Defines the stock ticker (AAPL) and the start and end dates for the download window. ```python stock = "AAPL" start_date = "2023-01-01" end_date = "2023-01-05" ``` -------------------------------- ### Inspect reddit dataframe shape Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Finnhub_Sentiment.ipynb Prints the shape of the reddit sentiment dataframe, which is (477, 7) for this example. ```python downloader.reddit.shape ``` -------------------------------- ### Set date range for news download Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Tushare_Finance_News.ipynb Defines the start and end dates as strings in YYYY-MM-DD format to specify the time period for which news will be downloaded. ```python start_date = "2022-01-01" end_date = "2022-01-05" ``` -------------------------------- ### Get Announcement Text Content Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Extracts the full text content of a specific announcement. The announcement ID is required. ```python text = ca.get_announcement_text(announcement_id=12345) print(text) ``` -------------------------------- ### Download Juchao announcements for date range and stock Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Company_Announcement.ipynb Creates a Juchao_Announcement instance with the given config and downloads announcements for the specified date range and stock, optionally getting content and deleting PDFs. ```python downloader = Juchao_Announcement(config) downloader.download_date_range_stock(start_date, end_date, stock = stock, get_content = True, delate_pdf = True) ``` -------------------------------- ### Open Facebook and get cookies Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Social_Media.ipynb Opens Facebook in a ChromiumEdge browser and retrieves cookies after manual login. The cookies are saved to 'cookies.json'. ```python from selenium import webdriver import json browser = webdriver.ChromiumEdge() browser.get('https://www.facebook.com') ``` ```python cookies = browser.get_cookies() with open("cookies.json", "w", encoding="utf-8") as cks: json.dump(cookies, cks) ``` -------------------------------- ### Fetch News Data with Python Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_News/index.html Use this snippet to fetch news data from a financial news API. It demonstrates a basic HTTP GET request and prints the response. ```python import requests response = requests.get('https://api.example.com/news') print(response.json()) ``` -------------------------------- ### Download tweets for date range Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_twitter.ipynb Calls the download method with the specified start date, end date, and stock ticker. The progress bar shows 0/5 iterations, indicating the download process. ```python downloader.download(start_date, end_date,stock) ``` -------------------------------- ### Download Company Announcements Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Downloads company announcement data for a specified ticker and date range. The method requires a start date, end date, and ticker symbol. ```python ca.download_announcements(start_date='2021-01-01', end_date='2021-12-31', ticker='AAPL') ``` -------------------------------- ### Import FinNLP and Load Company Announcement Data Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Initializes the FinNLP library and loads the company announcement dataset. This is the first step before any analysis. ```python from finnlp.data_sources.company_announcement import CompanyAnnouncement ca = CompanyAnnouncement() ca.load_data() ``` -------------------------------- ### Initialize News data source Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_News/index.html Create an instance of the News data source. Optionally pass configuration parameters. ```python news = News() ``` -------------------------------- ### Initialize FinNLP News Loader Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_News/index.html Creates a FinNLP news loader instance for fetching news data. This is the first step before retrieving any news articles. ```python from finnlp.data_sources.news.news_api import NewsAPI loader = NewsAPI() ``` -------------------------------- ### Initialize RedditPostsLoader with credentials Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Social_Media/index.html Sets up a RedditPostsLoader instance using client credentials and a user agent. Required before fetching Reddit posts. ```python from finnlp.data_sources.social_media.reddit_submissions import RedditPostsLoader loader = RedditPostsLoader( client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", user_agent="YOUR_USER_AGENT" ) ``` -------------------------------- ### Inspect twitter dataframe shape Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Finnhub_Sentiment.ipynb Prints the shape of the twitter sentiment dataframe, which is (571, 7) for this example. ```python downloader.twitter.shape ``` -------------------------------- ### Import and Initialize Social Media Client Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Social_Media/index.html Initializes the social media client with API credentials. Requires a valid API key and secret. This is the first step before fetching any data. ```python from finnlp.data_sources.social_media import SocialMediaClient client = SocialMediaClient(api_key='YOUR_API_KEY', api_secret='YOUR_API_SECRET') ``` -------------------------------- ### Initialize StockTwitsLoader Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Social_Media/index.html Creates a StockTwitsLoader instance. No authentication required for basic usage. ```python from finnlp.data_sources.social_media.stocktwits import StockTwitsLoader loader = StockTwitsLoader() ``` -------------------------------- ### Import GuruFocus_Streaming Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Import the GuruFocus_Streaming class from the finnlp data_sources.news module. ```python from finnlp.data_sources.news.gurufocus_streaming import GuruFocus_Streaming ``` -------------------------------- ### Download streaming news with keyword and rounds Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Create a GuruFocus_Streaming instance and download news by calling download_streaming_search with keyword 'AAPL' and 3 rounds. The output shows 'Downloading ... 0 1 2' indicating progress. ```python news_downloader = GuruFocus_Streaming() news_downloader.download_streaming_search(keyword = "AAPL", rounds = 3) ``` ```text Output: Downloading ... 0 1 2 ``` -------------------------------- ### Import TipRanks_Streaming Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_News.ipynb Imports the TipRanks_Streaming class from the finnlp.data_sources.news.tipranks_streaming module. ```python from finnlp.data_sources.news.tipranks_streaming import TipRanks_Streaming ``` -------------------------------- ### Select columns and preview dataframe Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Select the 'title' and 'content' columns from the downloaded news dataframe and display the first 10 rows. ```python selected_columns = ["title", "content"] news_downloader.dataframe[selected_columns].head(10) ``` -------------------------------- ### Initialize Company Announcement Loader Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Creates an instance of the CompanyAnnouncementLoader class. This is the first step before fetching any announcement data. ```python loader = CompanyAnnouncementLoader() ``` -------------------------------- ### Import Sina_Finance_Date_Range Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Import the Sina_Finance_Date_Range class from the finnlp news data sources module. ```python from finnlp.data_sources.news.sina_finance_date_range import Sina_Finance_Date_Range ``` -------------------------------- ### Instantiate Finnhub_Sentiment downloader Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_Finnhub_Sentiment.ipynb Creates a Finnhub_Sentiment downloader instance, passing the token in a dictionary as required by the class constructor. ```python downloader = Finnhub_Sentiment({"token":token}) ``` -------------------------------- ### Import News data source Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_News/index.html Import the News data source class from FinNLP. This is required to instantiate the data loader. ```python from finnlp.data_sources.news.news import News ``` -------------------------------- ### Configure Facebook_Streaming and set parameters Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_Social_Media.ipynb Creates a configuration dictionary with cookies, headless mode, and stealth path, then sets pages and stock symbol. ```python config = { "cookies":cookies, "headless": False, "stealth_path":"../../FinNLP/finnlp/data_sources/social_media/stealth.min.js" } pages = 3 stock = "AAPL" ``` -------------------------------- ### Load Company Announcement Data with finnlp.data_sources.company_announcement Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Imports the CompanyAnnouncement class and creates an instance for loading announcement data. This is the initial step before fetching or processing announcements. ```python from finnlp.data_sources.company_announcement import CompanyAnnouncement ca = CompanyAnnouncement() ``` -------------------------------- ### Import TalkMarkets_Streaming Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Import the TalkMarkets_Streaming class from the finnlp.data_sources.news.talkmarkets_streaming module. ```python from finnlp.data_sources.news.talkmarkets_streaming import TalkMarkets_Streaming ``` -------------------------------- ### Select columns and preview dataframe Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_News/index.html Selects the 'title' and 'create time' columns from the downloaded news data and displays the first 10 rows of the resulting dataframe. The output shows a table with columns including read amount, comments, title, content link, author, and create time. ```python selected_columns = ["title", "create time"] news_downloader[selected_columns].dataframe.head(10) ``` -------------------------------- ### Import InvestorPlace Streaming News Downloader Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_News.ipynb Imports the InvestorPlace_Streaming class from the finnlp news data source module. ```python from finnlp.data_sources.news.investorplace_streaming import InvestorPlace_Streaming ``` -------------------------------- ### Import FinNLP modules Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/site/jupyter/Data_Sources_Company_Announcement/index.html Import the necessary modules from FinNLP for company announcement data. ```python from finnlp.data_sources.company_announcement import CompanyAnnouncement ``` -------------------------------- ### Define download_parquet_files and web_data_prepare functions Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Cleaning_Pipeline.ipynb Defines functions to download parquet files from a list of URLs and to prepare train/test datasets from a Hugging Face dataset by downloading and reading parquet files. ```python def download_parquet_files(url_list, local_dir): for url in url_list: file_name = url.split('/')[-1] local_file = os.path.join(local_dir, file_name) if not os.path.exists(local_dir): os.makedirs(local_dir) r = requests.get(url, stream=True) if r.status_code == 200: with open(local_file, 'wb+') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) else: print('download failed: ', url) def web_data_prepare(name): r = requests.get("https://datasets-server.huggingface.co/parquet?dataset="+name) j = r.json() urls = [f['url'] for f in j['parquet_files'] if f['split'] == 'train'] train_urls = [f['url'] for f in j['parquet_files'] if f['config'] == name and f['split'] == 'train'] test_urls = [f['url'] for f in j['parquet_files'] if f['config'] == name and f['split'] == 'validation'] download_parquet_files(train_urls, 'train_dataset') download_parquet_files(test_urls, 'test_dataset') train_dataset = pd.read_parquet('./train_dataset', engine='pyarrow') test_dataset = pd.read_parquet('./test_dataset', engine='pyarrow') # train_dataset.rebalance() # test_dataset.rebalance() return train_dataset, test_dataset ``` -------------------------------- ### Preview first two rows of dataframe Source: https://github.com/ai4finance-foundation/finnlp/blob/main/test/Data_Sources_News.ipynb Displays the first two rows of the downloaded news dataframe, showing sample article data including descriptions, publication dates, and URLs. ```python news_downloader.dataframe.head(2) ``` ```text Result: description \ 0 While Leah Ellis was earning her doctorate at ... 1 Amazon.com said on Friday it will take its inv... cn:lastPubDate dateModified cn:dateline cn:branding \ 0 2023-06-24T10:00:01+0000 2023-06-24T10:00:01+0000 cnbc 1 2023-06-24T04:50:41+0000 2023-06-24T04:50:41+0000 cnbc section cn:type author cn:source cn:subtype ... \ 0 Clean Tech cnbcnewsstory Catherine [] ... 1 Technology wirestory [] ... cn:promoImage \ 0 https://image.cnbcfm.com/api/v1/image/10726095... 1 https://image.cnbcfm.com/api/v1/image/10726178... cn:title cn:keyword \ 0 Meet the 33-year-old Canadian chemist and the ... 1 Amazon raises investment in India to $26 billi... cn:liveURL _pubDate \ 0 https://www.cnbc.com/2023/06/24/sublime-system... 6/24/2023 10:00:01 PM 1 https://www.cnbc.com/2023/06/24/amazon-commits... 6/24/2023 1:49:10 PM _type _index brand hint hint_detail 0 0 0 cnbc NaN NaN 1 0 1 cnbc NaN NaN [2 rows x 30 columns] ``` -------------------------------- ### Download Streaming News from InvestorPlace Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_News.ipynb Creates an InvestorPlace_Streaming instance and downloads streaming news for the keyword 'apple' over 3 rounds. The output shows download progress. ```python news_downloader = InvestorPlace_Streaming() news_downloader.download_streaming_search(keyword = "apple", rounds = 3) ``` ```text Output: Downloading ... 0 1 2 ``` -------------------------------- ### Import Xueqiu_Streaming Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_Social_Media.ipynb Imports the Xueqiu_Streaming class from the finnlp.data_sources.social_media module. ```python from finnlp.data_sources.social_media.xueqiu_streaming import Xueqiu_Streaming ``` -------------------------------- ### Select and preview Xueqiu columns Source: https://github.com/ai4finance-foundation/finnlp/blob/main/docs/FinNLP/docs/jupyter/Data_Sources_Social_Media.ipynb Selects specific columns from the dataframe and displays the first 10 rows. The output shows the created_at, description, title, text, target, source, and user columns with sample data. ```python selected_columns = ["created_at", "description", "title", "text", "target", "source", "user"] downloader.dataframe[selected_columns].head(10) ``` ```text Result: created_at description \ 0 2023-06-25 12:15:07 茅台茅台茅台茅这个假期外围的环境不太好,已经是基本共识了。明天开盘大A承压低开也基本是一致预期。这么... /4322952939/253976335 3 茅台发生活费了