get_company_data

 1import requests
 2import yfinance as yf
 3import pandas as pd
 4import normalize_utils as nu
 5import matplotlib.pyplot as plt
 6from random import randint
 7
 8def getCompanyDataWithAlpha(company):
 9    """
10    Gets company data using Alpha API  
11
12    Args:  
13        company (str): Company name  
14    Returns:
15        Dict[]: Dictionary with all price and dates data 
16    """
17    api_key = 'H9KPSUTGEPR86VB6'
18    time_interval = 5
19    history_slice = 'year1month2'
20    URL = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={company}&interval={time_interval}min&slice={history_slice}&apikey={api_key}]'
21    download = requests.get(URL).json()
22    dict = download[f'Time Series ({time_interval}min)']
23    
24    return dict
25
26def getCompanyDataWithYahoo(company, start_date, finish_date=None):
27    """
28    Gets company data using Yahoo Finance API  
29
30    Args:  
31        company (str): Company name
32        start_date (str): The moment from which we want to collect the data  
33    Returns:  
34        dataframe (dataframe): A dataframe representing the company's stock prices  
35    """
36    if finish_date == None:
37        dataframe = yf.download(company, 
38                            start = start_date, 
39                            progress=False,
40        )
41    else:
42        dataframe = yf.download(company, 
43                        start = start_date,
44                        end = finish_date, 
45                        progress=False,
46    )
47
48    unwanted_labels = ['Open', 'High', 'Low', 'Adj Close', 'Volume']
49    dataframe.drop(unwanted_labels, axis = 1, inplace = True)
50
51    return dataframe
52
53def createDatasetsFromList(list, patterns_path):
54    """Given a list containing companies, dates, and pattern type,
55    it creates the dataset and stores in the destiny path  
56
57    Args:  
58        list (list): list of companies to download the information from
59        patterns_path (str): path where the dataframes will be stores
60    """
61    for item in list:
62        dataframe = getCompanyDataWithYahoo(item[0], item[1], item[2])
63        dataframe['Close'] = nu.normalizeVector(dataframe['Close'].tolist())
64        dataframe.reset_index(drop=True, inplace=True)
65        dataframe.plot(title=item[0])
66        dataframe.to_csv(patterns_path + item[3] + '/' + item[0] + str(randint(0, 999)) + '.csv', sep = ',')
67        plt.show()
68
69companies_list = [ #['', '', '', 'triple_top'],
70    ['WHR', '2021-11-16', '2022-01-04', 'double_bottom']
71]
72
73#createDatasetsFromList(companies_list, './patterns/')
def getCompanyDataWithAlpha(company)
 9def getCompanyDataWithAlpha(company):
10    """
11    Gets company data using Alpha API  
12
13    Args:  
14        company (str): Company name  
15    Returns:
16        Dict[]: Dictionary with all price and dates data 
17    """
18    api_key = 'H9KPSUTGEPR86VB6'
19    time_interval = 5
20    history_slice = 'year1month2'
21    URL = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={company}&interval={time_interval}min&slice={history_slice}&apikey={api_key}]'
22    download = requests.get(URL).json()
23    dict = download[f'Time Series ({time_interval}min)']
24    
25    return dict

Gets company data using Alpha API

Args:
company (str): Company name
Returns: Dict[]: Dictionary with all price and dates data

def getCompanyDataWithYahoo(company, start_date, finish_date=None)
27def getCompanyDataWithYahoo(company, start_date, finish_date=None):
28    """
29    Gets company data using Yahoo Finance API  
30
31    Args:  
32        company (str): Company name
33        start_date (str): The moment from which we want to collect the data  
34    Returns:  
35        dataframe (dataframe): A dataframe representing the company's stock prices  
36    """
37    if finish_date == None:
38        dataframe = yf.download(company, 
39                            start = start_date, 
40                            progress=False,
41        )
42    else:
43        dataframe = yf.download(company, 
44                        start = start_date,
45                        end = finish_date, 
46                        progress=False,
47    )
48
49    unwanted_labels = ['Open', 'High', 'Low', 'Adj Close', 'Volume']
50    dataframe.drop(unwanted_labels, axis = 1, inplace = True)
51
52    return dataframe

Gets company data using Yahoo Finance API

Args:
company (str): Company name start_date (str): The moment from which we want to collect the data
Returns:
dataframe (dataframe): A dataframe representing the company's stock prices

def createDatasetsFromList(list, patterns_path)
54def createDatasetsFromList(list, patterns_path):
55    """Given a list containing companies, dates, and pattern type,
56    it creates the dataset and stores in the destiny path  
57
58    Args:  
59        list (list): list of companies to download the information from
60        patterns_path (str): path where the dataframes will be stores
61    """
62    for item in list:
63        dataframe = getCompanyDataWithYahoo(item[0], item[1], item[2])
64        dataframe['Close'] = nu.normalizeVector(dataframe['Close'].tolist())
65        dataframe.reset_index(drop=True, inplace=True)
66        dataframe.plot(title=item[0])
67        dataframe.to_csv(patterns_path + item[3] + '/' + item[0] + str(randint(0, 999)) + '.csv', sep = ',')
68        plt.show()

Given a list containing companies, dates, and pattern type, it creates the dataset and stores in the destiny path

Args:
list (list): list of companies to download the information from patterns_path (str): path where the dataframes will be stores