Skip to content

Commit

Permalink
Uses yfinace instead of pandas_datareader.
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashGordon committed Aug 2, 2019
1 parent 97d06fa commit 9c3c7a9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ report.xml
.coverage
build/*
.pytest_cache/*
.vscode/*
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Build Status](https://travis-ci.org/portfolioplus/pytickersymbols.svg?branch=master)](https://travis-ci.org/portfolioplus/pytickersymbols)
[![Coverage Status](https://coveralls.io/repos/github/portfolioplus/pytickersymbols/badge.svg?branch=master)](https://coveralls.io/github/portfolioplus/pytickersymbols?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/5ef7d89e40b7491db63ea9753e01851c)](https://www.codacy.com/app/cdieck88/pytickersymbols?utm_source=github.com&utm_medium=referral&utm_content=portfolioplus/pytickersymbols&utm_campaign=Badge_Grade)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/3a4c80c87cd041129cae251d6acb39c7)](https://www.codacy.com/app/SlashGordon/pytickersymbols?utm_source=github.com&utm_medium=referral&utm_content=portfolioplus/pytickersymbols&utm_campaign=Badge_Grade)

# pytickersymbols

pytickersymbols provides access to google and yahoo ticker symbols for all stocks of the following indices:
Expand All @@ -23,9 +24,16 @@ pytickersymbols provides access to google and yahoo ticker symbols for all stock
- [x] SMI
- [x] TECDAX

# quick start
## install

```shell
pip install pytickersymbols
```

## quick start

Get all countries, indices and industries as follow:

```python
from pytickersymbols import PyTickerSymbols

Expand All @@ -36,6 +44,7 @@ industries = stock_data.get_all_industries()
```

You can select all stocks of an index as follow:

```python
from pytickersymbols import PyTickerSymbols

Expand All @@ -45,6 +54,6 @@ uk_stocks = stock_data.get_stocks_by_index('FTSE 100')

```

# issue tracker
## issue tracker

[https://github.com/portfolioplus/pytickersymbols/issuese](https://github.com/portfolioplus/pytickersymbols/issues")
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
wheel==0.33.4
pytest
pytest-cov
pandas_datareader==0.7.0
pandas==0.24.2
yfinance==0.1.44
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def get_requirements(requirements):
long_description_content_type="text/markdown",
url="https://github.com/portfolioplus/pytickersymbols",
packages=find_packages('src', exclude=EXCLUDE_FROM_PACKAGES),
install_requires=get_requirements('requirements.txt'),
package_data={'': ['data/*.yaml']},
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
162 changes: 80 additions & 82 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
can be found in the LICENSE file.
"""
import unittest
import pandas_datareader as pdr
import yfinance as yf

from pytickersymbols import PyTickerSymbols

Expand All @@ -19,184 +19,182 @@ def test_index(self):
:return:
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
indices = stock_data.get_all_indices()
assert indices
assert "DAX" in indices
assert "SDAX" in indices
assert "MDAX" in indices
self.assertIsNotNone(indices)
self.assertIn("DAX", indices)
self.assertIn("SDAX", indices)
self.assertIn("MDAX", indices)
# duplicates are not allowed
for index in indices:
assert len([index_tmp for index_tmp in indices if index_tmp == index]) == 1
lenl = len([tmp for tmp in indices if tmp == index])
self.assertEqual(lenl, 1)

def test_country(self):
"""
Test country getter
:return:
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
countries = stock_data.get_all_countries()
assert countries
assert "Germany" in countries
assert "Netherlands" in countries
assert "Sweden" in countries
self.assertIsNotNone(countries)
self.assertIn("Germany", countries)
self.assertIn("Netherlands", countries)
self.assertIn("Sweden", countries)
# duplicates are not allowed
for country in countries:
assert (
len(
[country_tmp for country_tmp in countries if country_tmp == country]
)
== 1
)
lenl = len([tmp for tmp in countries if tmp == country])
self.assertEqual(lenl, 1)

def test_industry(self):
"""
Test industry getter
:return:
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
industries = stock_data.get_all_industries()
assert industries
assert "Computer Hardware" in industries
assert "Gold" in industries
assert "Banking Services" in industries
self.assertIsNotNone(industries)
self.assertIn("Computer Hardware", industries)
self.assertIn("Gold", industries)
self.assertIn("Banking Services", industries)
# duplicates are not allowed
for industry in industries:
assert (
len([tmp_item for tmp_item in industries if tmp_item == industry]) == 1
)
lenl = len([tmp for tmp in industries if tmp == industry])
self.assertEqual(lenl, 1)

def test_stocks_by_index(self):
"""
Tests stock getter
:return:
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
stocks = stock_data.get_stocks_by_index(None)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_index(False)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_index(True)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_index(22)
assert len(stocks) == 0
stocks = stock_data.get_stocks_by_index("DAX")
assert stocks
assert len(stocks) == 30
for stock in stocks:
is_in_dax = False
for index in stock["indices"]:
if "DAX" in index:
is_in_dax = True
assert is_in_dax
self.assertEqual(len(stocks), 0)
for ind, ctx in [('DAX', 30), ('CAC 40', 40)]:
stocks = stock_data.get_stocks_by_index(ind)
self.assertIsNotNone(stocks)
self.assertEqual(len(stocks), ctx)
for stock in stocks:
is_in = False
for index in stock["indices"]:
if ind in index:
is_in = True
self.assertTrue(is_in)

def test_stocks_by_country(self):
"""
Tests stock getter by country
:return:
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
stocks = stock_data.get_stocks_by_country(None)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_country(False)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_country(True)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_country(22)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_country("Israel")
assert stocks
assert len(stocks) >= 1
self.assertIsNotNone(stocks)
self.assertTrue(len(stocks) >= 1)
for stock in stocks:
is_in_israel = False
if "Israel" == stock["country"]:
is_in_israel = True
assert is_in_israel
self.assertTrue(is_in_israel)

def test_stocks_by_industry(self):
"""
Tests stock getter by industry
:return:
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
stocks = stock_data.get_stocks_by_industry(None)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_industry(False)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_industry(True)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_industry(22)
assert len(stocks) == 0
self.assertEqual(len(stocks), 0)
stocks = stock_data.get_stocks_by_industry("Basic Materials")
assert stocks
self.assertIsNotNone(stocks)
for stock in stocks:
is_in_basic = False
for industry in stock["industries"]:
if "Basic Materials" in industry:
is_in_basic = True
assert is_in_basic
self.assertTrue(is_in_basic)

def test_tickers_by_index(self):
"""
Tests tickers getter by index
:return:
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
google_tickers = stock_data.get_google_ticker_symbols_by_index(None)
assert len(google_tickers) == 0
self.assertEqual(len(google_tickers), 0)
google_tickers = stock_data.get_google_ticker_symbols_by_index(False)
assert len(google_tickers) == 0
self.assertEqual(len(google_tickers), 0)
google_tickers = stock_data.get_google_ticker_symbols_by_index(True)
assert len(google_tickers) == 0
self.assertEqual(len(google_tickers), 0)
google_tickers = stock_data.get_google_ticker_symbols_by_index(22)
assert len(google_tickers) == 0
self.assertEqual(len(google_tickers), 0)
google_tickers = stock_data.get_google_ticker_symbols_by_index("DAX")
assert google_tickers
self.assertIsNotNone(google_tickers)
yahoo_tickers = stock_data.get_yahoo_ticker_symbols_by_index(None)
assert len(yahoo_tickers) == 0
self.assertEqual(len(yahoo_tickers), 0)
yahoo_tickers = stock_data.get_yahoo_ticker_symbols_by_index(False)
assert len(yahoo_tickers) == 0
self.assertEqual(len(yahoo_tickers), 0)
yahoo_tickers = stock_data.get_yahoo_ticker_symbols_by_index(True)
assert len(yahoo_tickers) == 0
self.assertEqual(len(yahoo_tickers), 0)
yahoo_tickers = stock_data.get_yahoo_ticker_symbols_by_index(22)
assert len(yahoo_tickers) == 0
self.assertEqual(len(yahoo_tickers), 0)
yahoo_tickers = stock_data.get_yahoo_ticker_symbols_by_index("DAX")
assert yahoo_tickers
self.assertIsNotNone(yahoo_tickers)
test_list = [google_tickers, yahoo_tickers]
for test_item in test_list:
assert len(test_item) == 30
self.assertEqual(len(test_item), 30)
for tickers in test_item:
assert len(tickers) == 2
self.assertEqual(len(tickers), 2)

def test_tickers_valid(self):
"""
Test if each ticker symbol works with pandas datareader
Test if each ticker symbol works with yfiance
"""
stock_data = PyTickerSymbols()
assert stock_data
self.assertIsNotNone(stock_data)
y_tickers = stock_data.get_yahoo_ticker_symbols_by_index("DAX")
for tickers in y_tickers:
for ticker in tickers:
yahoo = pdr.get_data_yahoo(
ticker, "2019-07-01", "2019-07-05", interval="d"
)
assert yahoo is not None
assert "Close" in yahoo
assert len(yahoo["Close"]) > 0

y_ticker = yf.Ticker(ticker)
data = y_ticker.history(period='4d')
self.assertIsNotNone(data)
self.assertIn("Close", data)
self.assertTrue(len(data["Close"]) > 0)

def test_index_to_yahoo(self):
stock_data = PyTickerSymbols()
assert stock_data
assert '^GDAXI' == stock_data.index_to_yahoo_symbol('DAX')
assert '^SDAXI' == stock_data.index_to_yahoo_symbol('SDAX')
assert '^MDAXI' == stock_data.index_to_yahoo_symbol('MDAX')
assert '^SSMI' == stock_data.index_to_yahoo_symbol('Switzerland 20')
self.assertIsNotNone(stock_data)
self.assertEqual('^GDAXI', stock_data.index_to_yahoo_symbol('DAX'))
self.assertEqual('^SDAXI', stock_data.index_to_yahoo_symbol('SDAX'))
self.assertEqual('^MDAXI', stock_data.index_to_yahoo_symbol('MDAX'))
swi = stock_data.index_to_yahoo_symbol('Switzerland 20')
self.assertEqual('^SSMI', swi)


if __name__ == "__main__":
unittest.main()

0 comments on commit 9c3c7a9

Please sign in to comment.