Imports

In [1]:
import datetime
import requests
import math
from tqdm import tqdm
import pandas as pd
import zipfile

Functions

In [2]:
def unzip_data():
    #https://stackoverflow.com/a/3451150/993324
    zip_ref = zipfile.ZipFile("../output/omegacgl.zip", 'r')
    zip_ref.extractall("../output/stock_data/")
    zip_ref.close()
In [3]:
def download_fresh_data():
    # https://stackoverflow.com/a/37573701/993324


    url = 'http://bossa.pl/pub/ciagle/omega/omegacgl.zip'
    r = requests.get(url, stream=True)

    total_size = int(r.headers.get('content-length', 0)); 
    block_size = 1024*1024
    wrote = 0 

    with open('../output/omegacgl.zip', 'wb') as f:
        for data in tqdm(r.iter_content(block_size), total=math.ceil(total_size//block_size),
                         unit='MB', unit_scale=True):
            wrote = wrote  + len(data)
            f.write(data)
    if total_size != 0 and wrote != total_size:
        print("ERROR, something went wrong")
In [4]:
def prepare_data(company = "CDPROJEKT"):
    stock_data = pd.read_csv("../output/stock_data/{}.txt".format(company))
    stock_data["Date"] = pd.to_datetime(stock_data["Date"], format="%Y%m%d")
    stock_data.set_index(keys = "Date", drop = True, inplace = True)
    return stock_data
In [5]:
def get_last_stock_day():
    yesterday = datetime.datetime.now() - datetime.timedelta(1)
    if yesterday.isoweekday() <= 5:
        last_stock_day = yesterday
    elif yesterday.isoweekday() == 6:
        last_stock_day = yesterday - datetime.timedelta(1)
    else:
        last_stock_day = yesterday - datetime.timedelta(2)
    return last_stock_day

Getting data if we don't have it already

In [6]:
# Kod 1
try:
    stock_data = prepare_data()
except FileNotFoundError:
    print("Stock data not found. Downloading and extracting.")
    download_fresh_data()
    unzip_data()
    stock_data = prepare_data()

Getting most recent data if we don't have it already

In [7]:
# Kod 2
last_stock_day = get_last_stock_day()
print(last_stock_day)

last_data = "{}-{:02d}-{:02d}".format(last_stock_day.year,last_stock_day.month,last_stock_day.day)
print("Looking for {}".format(last_data))
try:
    stock_data.loc[last_data]
    print("We have it!")
except KeyError:
    print("Fresh stock data not found. Downloading and extracting.")
    download_fresh_data()
    unzip_data()
    stock_data = prepare_data()
2019-02-01 09:11:55.716179
Looking for 2019-02-01
We have it!

Adding moving average over 30 days window

In [8]:
# Kod 3
stock_data["SMA"] = stock_data["Close"].rolling(30).mean()
In [9]:
stock_data.tail()
Out[9]:
Name Open High Low Close Volume SMA
Date
2019-01-28 CDPROJEKT 187.9 192.2 185.2 187.6 241278 159.956667
2019-01-29 CDPROJEKT 187.6 192.6 185.7 190.1 222237 161.613333
2019-01-30 CDPROJEKT 192.0 197.7 191.0 195.1 305933 163.390000
2019-01-31 CDPROJEKT 198.0 199.0 190.0 190.0 442301 164.923333
2019-02-01 CDPROJEKT 190.0 192.1 188.1 190.0 218099 166.470000

Plotting High, Low and SMA from last year

In [10]:
# Kod 4
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot

last_year = datetime.datetime.now() - datetime.timedelta(365)

init_notebook_mode()

data = [
    go.Scatter(
        x = stock_data[last_year:].index, 
        y = stock_data[last_year:]["SMA"],
        name = 'SMA'
    ),
    go.Scatter(
        x = stock_data[last_year:].index, 
        y = stock_data[last_year:]["Low"],
        name = 'Low'
    ),
    go.Scatter(
        x = stock_data[last_year:].index, 
        y = stock_data[last_year:]["High"],
        name = 'High'
    )
]

layout = dict(title = "Stock price",
              xaxis= dict(title= 'Date'))

fig = dict(data = data, layout = layout)
iplot(fig)