1

The code below works for the American stocks APLE and BHP however when I replace them with the ASX codes it crashes. I though it was due to the colon and have placed str(ASX:BHP) with out success. Unfortunately Yahoo is no longer supplying historical data. Any thoughts or solutions alternatives would be greatly appreciated.

Thanks

import datetime
import pandas as pd
from pandas_datareader import data, wb

list = ["APLE","BHP"] 
#list = ["ASX:AMP","ASX:BHP"] 
df_all_stock = pd.DataFrame([])

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime(2017, 1, 1)

for row in list:

    row = str(row)
    df_stock = data.DataReader(row, "google", start, end)
    df_all_stock = df_all_stock.append(df_stock)
    df_all_stock['code'] = row

df_all_stock
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • A limitation of that exchange, no csv output: `https://www.google.com/finance/historical?q=ASX:BHP&output=csv` fails but `https://www.google.com/finance/historical?q=ASX:BHP` succeeds. The former is the url that `DataReader` uses. – AChampion May 22 '17 at 05:30

3 Answers3

1

Just use the ASX API

https://www.asx.com.au/asx/1/share/AMP

will return:

{"code":"AMP","isin_code":"AU000000AMP6","desc_full":"Ordinary Fully Paid","last_price":1.155,"open_price":1.115,"day_high_price":1.155,"day_low_price":1.11,"change_price":0.040,"change_in_percent":"3.587%","volume":24558498,"bid_price":1.15,"offer_price":1.16,"previous_close_price":1.115,"previous_day_percentage_change":"3.241%","year_high_price":1.77,"last_trade_date":"2021-08-13T00:00:00+1000","year_high_date":"2020-12-03T00:00:00+1100","year_low_price":1.038,"year_low_date":"2021-07-30T00:00:00+1000","year_open_price":4.97,"year_open_date":"2014-02-25T11:00:00+1100","year_change_price":-3.815,"year_change_in_percentage":"-76.761%","pe":32.08,"eps":0.036,"average_daily_volume":20511519,"annual_dividend_yield":0,"market_cap":3641708026,"number_of_shares":3266105853,"deprecated_market_cap":3772352000,"deprecated_number_of_shares":3266105853,"suspended":false}

Other sample queries that can give you price history, announcements, directors, dividends etc:

https://www.asx.com.au/asx/1/share/AMP/prices?interval=daily&count=255
https://www.asx.com.au/asx/1/company/AMP
https://www.asx.com.au/asx/1/company/AMP?fields=primary_share,latest_annual_reports,last_dividend,primary_share.indices
https://www.asx.com.au/asx/1/company/AMP/announcements?count=10&market_sensitive=true
https://www.asx.com.au/asx/1/company/AMP/dividends
https://www.asx.com.au/asx/1/company/AMP/dividends/history?years=10
https://www.asx.com.au/asx/1/company/AMP/people
https://www.asx.com.au/asx/1/company/AMP/options?count=1000
https://www.asx.com.au/asx/1/company/AMP/warrants?count=1000
https://www.asx.com.au/asx/1/chart/highcharts?asx_code=AMP&years=10
https://www.asx.com.au/asx/1/company/AMP/similar?compare=marketcap

I included some sample Python code here: https://stackoverflow.com/a/68790147/8459557

0

will need to build a scraper to get the data out of the html table and then build up a pandas dataframe that resembles the one we get as output for the american stock data.

I determined the base url for Canadian stocks on google finance to be: 'https://www.google.ca/finance/historical?q=TSE%3A' To get data for a stock, we simply append its name to the end of the above base url. For example to see the historical stock data for 'VCN' we would need to go to the page: https://www.google.ca/finance/historical?q=TSE%3AVCN

To do the above in python code we simply need the following, where the stock variable can be changed for any TSE(Tornto stock exchange) stock of interest.

from datetime import datetime
from pandas import DataFrame
import pandas_datareader.data as web
google_historical_price_site= 'https://www.google.ca/finance/historical?
q=TSE%3A'
stock = 'VCN' #sub any sock in here
historical_price_page = google_historical_price_site + stock
print(historical_price_page)


from urllib.request import urlopen
from bs4 import BeautifulSoup

#open the historical_price_page link and acquire the source code
stock_dat = urlopen(historical_price_page)
#parse the code using BeautifulSoup
historical_page = BeautifulSoup(stock_dat,'lxml')

#scrape the table
table_dat = historical_page.find('table',{'class':'gf-table 
historical_price'})
#find all the rows in the table
rows = table_dat.findAll('td',{'class':'lm'})
#get just the dates out of the table rows, strip the newline characters
dates = [x.get_text().rstrip() for x in rows]
#turn dates to python datetime format
datetime_dates = [datetime.strptime(x, '%b %d, %Y') for x in dates]

#next we build up the price dataframe rows
#iterate through the table, taking the siblings to the 
#right of the dates and adding to the row's data
prices = []
for num, row in enumerate(rows):
row_dat = [datetime_dates[num]] #first column is the dates
for i in row.next_siblings: 
row_dat.append(i.get_text().rstrip()) #iterate through columns, append
prices.append(row_dat) #add the row to the list of rows
#turn the output into the dataframe
outdat = DataFrame(prices,columns = 
['Date','Open','High','Low','Close','Volume'])
#make the Volume columns integers, in case we wish to use it later!
outdat["Volume"] = outdat["Volume"].apply(lambda x: int(x.replace(',','')))
#change the other columns to floating point values
for col in ['Open','High','Low','Close']:
outdat[col] = outdat[col].apply(lambda x: float(x))
#set the index to match the american stock data
outdat = outdat.set_index('Date')
#sort the index so it is in the same orientation as the american data
outdat = outdat.sort_index() 

#have a look
outdat
TVC_bingo
  • 1
  • 1
  • 2
0

EXAMPLE OF downloading Hong Kong Stock as CSV file (STOCK EXAMPLE: Tencent Holdings Ltd(HKG:0700)

from datetime import datetime
from pandas import DataFrame
import pandas_datareader.data as web
import os

google_historical_price_site='https://finance.google.com/finance/historical?q=HKG:0700'
print(google_historical_price_site)

from urllib.request import urlopen
from bs4 import BeautifulSoup

#open the historical_price_page link and acquire the source code
stock_dat = urlopen(google_historical_price_site)
#parse the code using BeautifulSoup
google_historical_price_site = BeautifulSoup(stock_dat,'lxml')

#scrape the table
table_dat = google_historical_price_site.find('table',{'class':'gf-table 
historical_price'})
#find all the rows in the table
rows = table_dat.findAll('td',{'class':'lm'})
#get just the dates out of the table rows, strip the newline characters
dates = [x.get_text().rstrip() for x in rows]   
#turn dates to python datetime format
datetime_dates = [datetime.strptime(x, '%b %d, %Y') for x in dates]

#next we build up the price dataframe rows 
#iterate through the table, taking the siblings to the 
#right of the dates and adding to the row's data
prices = []
for num, row in enumerate(rows):
row_dat = [datetime_dates[num]] #first column is the dates
for i in row.next_siblings: 
row_dat.append(i.get_text().rstrip()) #iterate through columns, append
prices.append(row_dat) #add the row to the list of rows
#turn the output into the dataframe
outdat = DataFrame(prices,columns = 
['Date','Open','High','Low','Close','Volume'])
#make the Volume columns integers, in case we wish to use it later!
outdat["Volume"] = outdat["Volume"].apply(lambda x: int(x.replace(',','')))
#change the other columns to floating point values
for col in ['Open','High','Low','Close']:
outdat[col] = outdat[col].apply(lambda x: float(x))
#set the index to match the american stock data
outdat = outdat.set_index('Date')
#sort the index so it is in the same orientation as the american data
outdat = outdat.sort_index() 

#output CSV.file 
df=outdat
path_d = 'C:\MA data'
df.to_csv(os.path.join(path_d, 'HKGstock700.csv'))
TVC_bingo
  • 1
  • 1
  • 2