Monday, 27 April 2015

How to Get High Frequency Stock Prices with Python: Prague Stock Exchange Example

From time to time high frequency stock prices data is needed even for less developed CEE stock markets. I needed high frequency prices for CEZ (a Prague Stock Exchange listed company). With this task and having Python I wrote a brief code for extracting the data (the final piece of code is saving the data):

import urllib

def create_url(ticker):
    base_url = 'http://www.pse.cz/XML/ProduktKontinualJS.aspx?'
    search_query = 'cnpa={}'.format(ticker)
    search_url = '{}{}'.format(base_url, search_query)
    return search_url

def clean_url(url):
    cleaned=urllib.urlopen(url).read().split("function createOnlineChart(divName)")[0]
    for character in ['k:', 'o:', 'd:new Date', '{', '}', '\r\nvar chartDataOL =', '[', ']', '(', ')', ';', ' ']:
        if character in cleaned:
            cleaned=cleaned.replace(character,'')
    return cleaned

if __name__ == '__main__':
    ticker='4169' # '4169' CEZ, '6407' Erste Group Bank, '4171' Komercni Banka, '4174' O2, '6816' NWR, '4254' Philip Morris CR

url=create_url(ticker)
cleanurl=clean_url(url)
with open ("trades.txt", "w") as f:
    f.write(cleanurl)

No comments:

Post a Comment