THALES SA Shares Update

This is a python script using BeautifulSoup and Plotly to create a maintained share price log. This converts the share price from EUR to GBP and stores a record in a csv format

Code used:

This is the python code used to obtain the current stock value and store to csv

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
import csv
import time
import urllib2
from bs4 import BeautifulSoup
current_time = time.strftime("%H:%M:%S")
current_date = time.strftime("%d/%m/%Y")
print current_date
print current_time
date_time = current_date + " " + current_time

thales_quote_page = 'https://finance.google.co.uk/finance?q=EPA:HO'
thales_page = urllib2.urlopen(thales_quote_page)
thales_soup = BeautifulSoup(thales_page, 'html.parser')
thales_market_value_box = thales_soup.find('span',attrs={'id': 'ref_667978_l'})
thales_market_value = thales_market_value_box.text.strip()
print "THALES SA (EUR):",thales_market_value

euro_quote_page = "https://finance.google.co.uk/finance?q=euro&ei=0PH8WfDpFojPUfiBisAE"
euro_page = urllib2.urlopen(euro_quote_page)
euro_soup = BeautifulSoup(euro_page, 'html.parser')
euro_value_box = euro_soup.find('span', attrs={'class':'bld'})
euro_value = euro_value_box.text.strip()
euro_value = euro_value[:-4]
print "EUR->GBP: ", euro_value

thales_gbp = float(thales_market_value) * float(euro_value)

print "THALES SA (GBP):",round(thales_gbp,2)
thales_gbp = round(thales_gbp,2)

data_to_write = [date_time,float(thales_market_value),float(euro_value),float(thales_gbp)]

logfile = open('log.csv', "a")
writer = csv.writer(logfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerow(data_to_write)

logfile.close()

This is the python code that using plotly to create a visual output as shown above.

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
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as FF

import numpy as np
import pandas as pd

df = pd.read_csv('log.csv')

sample_data_table = FF.create_table(df.head())
py.plot(sample_data_table, filename='thales_data')

trace = go.Scatter(x = df['DateTime'], y = df['THALES_GBP'],name ='THALES SA SHARE PRICES (in GBP)')

layout = go.Layout(title='Thales SA Stock prices over time',
plot_bgcolor='rgb(230, 230,230)',
showlegend=True,
xaxis=dict(
title='Time (Date Time)',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
), yaxis=dict(
title='Price per share (GBP)',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)

)
fig = go.Figure(data=[trace], layout=layout)

py.plot(fig, filename='thales-stock-prices')