Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rochi88 committed Jul 30, 2024
1 parent 67a8c63 commit 1acf5c1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 44 deletions.
121 changes: 77 additions & 44 deletions bdshare/stock/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,92 @@
from bdshare.util import vars as vs


def get_market_inf():
def get_market_inf(retry_count=3, pause=0.001):
"""
get company information.
:return: dataframe
"""
for _ in range(retry_count):
time.sleep(pause)
try:
r = requests.get(vs.DSE_URL+vs.DSE_MARKET_INF_URL)
if r.status_code != 200:
r = requests.get(vs.DSE_ALT_URL+vs.DSE_MARKET_INF_URL)
except Exception as e:
print(e)
else:
soup = BeautifulSoup(r.text, 'html.parser')
quotes = [] # a list to store quotes

table = soup.find('table', attrs={'class': 'table table-bordered background-white text-center', '_id': 'data-table'})

for row in table.find_all('tr')[1:]:
cols = row.find_all('td')
quotes.append({'Date': cols[0].text.strip().replace(",", ""),
'Total Trade': cols[1].text.strip().replace(",", ""),
'Total Volume': cols[2].text.strip().replace(",", ""),
'Total Value (mn)': cols[3].text.strip().replace(",", ""),
'Total Market Cap. (mn)': cols[4].text.strip().replace(",", ""),
'DSEX Index': cols[5].text.strip().replace(",", ""),
'DSES Index': cols[6].text.strip().replace(",", ""),
'DS30 Index': cols[7].text.strip().replace(",", ""),
'DGEN Index': cols[8].text.strip().replace(",", "")
})
df = pd.DataFrame(quotes)
return df

def get_company_inf(symbol=None, retry_count=3, pause=0.001):
"""
get stock market information.
:param symbol: str, Instrument symbol e.g.: 'ACI' or 'aci'
:return: dataframe
"""
r = requests.get(vs.DSE_URL+vs.DSE_MARKET_INF_URL)
soup = BeautifulSoup(r.text, 'html.parser')
quotes = [] # a list to store quotes

table = soup.find('table', attrs={'class': 'table table-bordered background-white text-center', '_id': 'data-table'})

for row in table.find_all('tr')[1:]:
cols = row.find_all('td')
quotes.append({'Date': cols[0].text.strip().replace(",", ""),
'Total Trade': cols[1].text.strip().replace(",", ""),
'Total Volume': cols[2].text.strip().replace(",", ""),
'Total Value (mn)': cols[3].text.strip().replace(",", ""),
'Total Market Cap. (mn)': cols[4].text.strip().replace(",", ""),
'DSEX Index': cols[5].text.strip().replace(",", ""),
'DSES Index': cols[6].text.strip().replace(",", ""),
'DS30 Index': cols[7].text.strip().replace(",", ""),
'DGEN Index': cols[8].text.strip().replace(",", "")
})
df = pd.DataFrame(quotes)
return df


def get_latest_pe():
data = {'name': symbol}

for _ in range(retry_count):
time.sleep(pause)
try:
r = requests.get(vs.DSE_URL+vs.DSE_COMPANY_INF_URL, params=data)
if r.status_code != 200:
r = requests.get(vs.DSE_ALT_URL+vs.DSE_COMPANY_INF_URL, params=data)
except Exception as e:
print(e)
else:
soup = BeautifulSoup(r.content, 'html5lib')
print(soup)

def get_latest_pe(retry_count=3, pause=0.001):
"""
get last stock P/E.
:return: dataframe
"""
r = requests.get(vs.DSE_URL+vs.DSE_LPE_URL)
# soup = BeautifulSoup(r.text, 'html.parser')
soup = BeautifulSoup(r.content, 'html5lib')

quotes = [] # a list to store quotes
table = soup.find('table', attrs={'class': 'table table-bordered background-white shares-table fixedHeader'})
for row in table.find_all('tr')[1:]:
cols = row.find_all('td')
quotes.append((cols[1].text.strip().replace(",", ""),
cols[2].text.strip().replace(",", ""),
cols[3].text.strip().replace(",", ""),
cols[4].text.strip().replace(",", ""),
cols[5].text.strip().replace(",", ""),
cols[6].text.strip().replace(",", ""),
cols[7].text.strip().replace(",", ""),
cols[8].text.strip().replace(",", ""),
cols[9].text.strip().replace(",", "")
))
df = pd.DataFrame(quotes)
return df
for _ in range(retry_count):
time.sleep(pause)
try:
r = requests.get(vs.DSE_URL+vs.DSE_LPE_URL)
if r.status_code != 200:
r = requests.get(vs.DSE_ALT_URL+vs.DSE_LPE_URL)
except Exception as e:
print(e)
else:
soup = BeautifulSoup(r.content, 'html5lib')

quotes = [] # a list to store quotes
table = soup.find('table', attrs={'class': 'table table-bordered background-white shares-table fixedHeader'})
for row in table.find_all('tr')[1:]:
cols = row.find_all('td')
quotes.append((cols[1].text.strip().replace(",", ""),
cols[2].text.strip().replace(",", ""),
cols[3].text.strip().replace(",", ""),
cols[4].text.strip().replace(",", ""),
cols[5].text.strip().replace(",", ""),
cols[6].text.strip().replace(",", ""),
cols[7].text.strip().replace(",", ""),
cols[8].text.strip().replace(",", ""),
cols[9].text.strip().replace(",", "")
))
df = pd.DataFrame(quotes)
return df

def get_market_inf_more_data(start=None, end=None, index=None, retry_count=3, pause=0.001):
"""
Expand Down
1 change: 1 addition & 0 deletions bdshare/util/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DSE_NEWS_URL = "old_news.php"
DSE_CLOSE_PRICE_URL = "dse_close_price_archive.php"
DSE_COMPANY_LIST_URL = "company_listing.php"
DSE_COMPANY_INF_URL = "displayCompany.php"

DSE_MARKET_INF_URL = "recent_market_information.php"
DSE_MARKET_INF_MORE_URL = "recent_market_information_more.php"
Expand Down

0 comments on commit 1acf5c1

Please sign in to comment.