Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-aichara committed Jul 17, 2020
1 parent 2c4ca60 commit 966035b
Show file tree
Hide file tree
Showing 45 changed files with 464 additions and 625 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_STORE
__pycache__/
dist/
PriceIndices.egg-info/
bist/
237 changes: 0 additions & 237 deletions PriceIndices.egg-info/PKG-INFO

This file was deleted.

11 changes: 0 additions & 11 deletions PriceIndices.egg-info/SOURCES.txt

This file was deleted.

1 change: 0 additions & 1 deletion PriceIndices.egg-info/dependency_links.txt

This file was deleted.

4 changes: 0 additions & 4 deletions PriceIndices.egg-info/requires.txt

This file was deleted.

1 change: 0 additions & 1 deletion PriceIndices.egg-info/top_level.txt

This file was deleted.

6 changes: 0 additions & 6 deletions PriceIndices/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
from .crypto_history import MarketHistory
from .price_indicators import Indices






Binary file removed PriceIndices/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
73 changes: 47 additions & 26 deletions PriceIndices/crypto_history.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,86 @@
import requests
import pandas as pd
from typing import Any, Optional
import warnings
warnings.filterwarnings('ignore')

warnings.filterwarnings("ignore")

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry


class MarketHistory:
__Crypto_Market_Base_URL = 'https://coinmarketcap.com/currencies/'
__Crypto_Market_Base_URL = "https://coinmarketcap.com/currencies/"

def __init__(self, price_url=__Crypto_Market_Base_URL):
def __init__(self, price_url: Optional[str] = __Crypto_Market_Base_URL):
self.price_url = price_url
self.request_timeout = 120

self.session = requests.Session()
retries = Retry(total=5, backoff_factor=0.5, status_forcelist=[502, 503, 504])
self.session.mount('http://', HTTPAdapter(max_retries=retries))
retries = Retry(
total=5, backoff_factor=0.5, status_forcelist=[502, 503, 504]
)
self.session.mount("http://", HTTPAdapter(max_retries=retries))

def __request(self, url):
def __request(self, url: str) -> Any:
try:
response = self.session.get(url, timeout=self.request_timeout)
response.raise_for_status()
data = pd.read_html(response.content)
df = pd.DataFrame(data[2])
df['Date'] = pd.to_datetime(df['Date'])
df["Date"] = pd.to_datetime(df["Date"])
return df
except Exception as e:
raise

def get_history(self, coin_id, start_date, end_date):
def get_history(
self, coin_id: str, start_date: str, end_date: str
) -> pd.DataFrame:
"""
Get historical market data of a cryptocurrency from CoinMarketCap.
Args:
coin_id (str): coin name
start_date (str): Starting date in 'YYYYMMDD' format
end_date (str): End date in 'YYYYMMDD' format
Returns:
pd.DataFrame: Pandas Dataframe or print error message
:param coin_id: coin name [str]
:param start_date: date in 'YYYYMMDD' format [str]
:param end_date: date in 'YYYYMMDD' format [str]
:return: Pandas DataFrame
"""
url = '{0}{1}/historical-data/?start={2}&end={3}'.format(self.price_url, coin_id, start_date, end_date)
url = "{0}{1}/historical-data/?start={2}&end={3}".format(
self.price_url, coin_id, start_date, end_date
)

try:
return self.__request(url)
except Exception as e:
print(e)
print('Please, check inputs. Coin id, and dates are strings. Date format is "YYYYMMDD"')
print(
'Please, check inputs. Coin id, and dates are strings. Date format is "YYYYMMDD"'
)

def get_price(self, coin_id: str, start_date: str, end_date: str):
"""
Get historical market price data (closing price) of a cryptocurrency from CoinMarketCap.
Args:
coin_id (str): coin name
start_date (str): Starting date in 'YYYYMMDD' format
end_date (str): End date in 'YYYYMMDD' format
Returns:
pd.DataFrame: Pandas Dataframe or print error message
def get_price(self, coin_id, start_date, end_date):
"""
Get historical market price data (closing price) of a cryptocurrency from CoinMarketCap.
:param coin_id: coin name [str]
:param start_date: date in 'YYYYMMDD' format [str]
:param end_date: date in 'YYYYMMDD' format [str]
:return: Pandas DataFrame
"""
url = '{0}{1}/historical-data/?start={2}&end={3}'.format(self.price_url, coin_id, start_date, end_date)
url = "{0}{1}/historical-data/?start={2}&end={3}".format(
self.price_url, coin_id, start_date, end_date
)

try:
df = self.__request(url)
df = df[['Date', 'Close**']]
df.columns = ['date', 'price']
df = df[["Date", "Close**"]]
df.columns = ["date", "price"]
return df
except Exception as e:
print(e, 'Please, check inputs Coin id, and dates are strings. Date format is "YYYYMMDD"')

print(
e,
'Please, check inputs Coin id, and dates are strings. Date format is "YYYYMMDD"',
)
Loading

0 comments on commit 966035b

Please sign in to comment.