forked from alpacahq/blogmaterials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_price_cache.py
More file actions
61 lines (60 loc) · 2.33 KB
/
stock_price_cache.py
File metadata and controls
61 lines (60 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# This code enables the automated maintenance of daily updates to a local stock price repository.
# Source: https://medium.com/financeexplained/learn-to-build-your-own-historical-stock-price-data-sets-2495f8a23d11
#
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl, re, datetime, pandas as pd
import sqlite3, time
conn = sqlite3.connect('portfoliodb.sqlite')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS Price (COB DATE, Ticker VARCHAR(5), Price Numeric)')
today = datetime.date.today()
#-------Set Up--------------------------
Tickers = ('TSTL','SOLI')
#---------------------------------------
#Function that tells you whether the date passed is a business date
def is_business_day(date):
return bool(len(pd.bdate_range(date, date)))
#Checks to find out when was the last business date if today is not a business day
def last_business_day(date):
i=0
while (is_business_day(today - datetime.timedelta(days = i)) == False):
i = i+1
return(today - datetime.timedelta(days = i))
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
#Calls the lse website to get EOD date
def EOD_Price_Call(Ticker):
url = 'http://www.lse.co.uk/SharePrice.asp?shareprice=' + Ticker
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
tags = soup('span')
#print(soup.prettify)
for line in tags:
if re.search('"sp_sharePrice sp_' + Ticker + '_MID" data-field="sharePrice"',str(line)):
return(Ticker, re.findall('([0-9]+.[0-9]+)', str(line))[0])
def UpdateDB(date):
NoOfDBRecords = 0
cur.execute('SELECT * FROM Price WHERE COB = ?', (date, ))
try:
cur.fetchone()[0]
print('Entry in the db already exists')
except:
print('Inputing in the DB')
for tick in Tickers:
temp = EOD_Price_Call(tick)
cur.execute('INSERT INTO Price VALUES(?, ?, ?)', (date,temp[0],temp[1]))
print('Input',date,temp[0],temp[1])
time.sleep(3)
conn.commit()
#Check if today is a business days - if not determine the last business days
if (is_business_day(today) == True):
#print('if')
UpdateDB(today)
else:
#use last_business_day function
print('else')
UpdateDB(last_business_day(today))
cur.close()