diff --git a/setup.py b/setup.py index 6dd57ae..4e52bf7 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="tradingfeatures", - version="0.3.12", + version="0.3.14", author="Onur Cetinkol", author_email="realiti44@gmail.com", description="A small package to get history, easily download all avaliable history to csv or update current csv files", diff --git a/tradingfeatures/__init__.py b/tradingfeatures/__init__.py index 8b2a641..7835ac0 100644 --- a/tradingfeatures/__init__.py +++ b/tradingfeatures/__init__.py @@ -1,4 +1,6 @@ from .tools import bitfinex -from tradingfeatures.bitmex_fundings import bitmex +from tradingfeatures.bitmex import bitmex from tradingfeatures.bitstamp import bitstamp +from tradingfeatures.google_trends import google_trends + from tradingfeatures.main import base, base_v2 diff --git a/tradingfeatures/bitmex_fundings.py b/tradingfeatures/bitmex.py similarity index 100% rename from tradingfeatures/bitmex_fundings.py rename to tradingfeatures/bitmex.py diff --git a/tradingfeatures/google_trends.py b/tradingfeatures/google_trends.py new file mode 100644 index 0000000..29c31a1 --- /dev/null +++ b/tradingfeatures/google_trends.py @@ -0,0 +1,60 @@ +import time +import datetime + +import pytrends +import numpy as np +import pandas as pd + +from pytrends.request import TrendReq + + +class google_trends: + def __init__(self, kw_list=['bitcoin']): + + self.pytrends = TrendReq() + + self.kw_list = kw_list + + def get(self, date_start, date_end=None): + if date_end is None: + date_end = datetime.datetime.utcfromtimestamp(self.current_time()) + + date_start = date_start - datetime.timedelta(hours=4, minutes=0) + date_end = date_end + datetime.timedelta(hours=4, minutes=0) + + df_temp = self.pytrends.get_historical_interest( + self.kw_list, + year_start=date_start.year, month_start=date_start.month, day_start=date_start.day, hour_start=date_start.hour, + year_end=date_end.year, month_end=date_end.month, day_end=date_end.day, hour_end=date_end.hour, + cat=0, geo='', gprop='', sleep=60) + + return df_temp + + def update(self, path, save=False): + path = path + '/trends_data.csv' + + df = pd.read_csv(path, index_col=0) + + if not self.current_time() - df.index[-1] >= 3600: + return df + + date_start = datetime.datetime.utcfromtimestamp(df.index[-1]) + + df_temp = self.get(date_start) + + df_temp.index = df_temp.index.astype(np.int64) // 10 ** 9 # Convert date to timestamp + df_temp.pop('isPartial') + df_temp.columns = ['google_trends'] + + df = pd.concat([df, df_temp]) + df = df[~df.index.duplicated(keep='first')] + + # Save + if save: + df.to_csv(path) + + return df + + def current_time(self): + return int((time.time() // 3600) * 3600) + diff --git a/tradingfeatures/main.py b/tradingfeatures/main.py index 1eeab26..5037bf0 100644 --- a/tradingfeatures/main.py +++ b/tradingfeatures/main.py @@ -5,6 +5,7 @@ from tradingfeatures import bitfinex from tradingfeatures import bitstamp from tradingfeatures import bitmex +from tradingfeatures import google_trends class base: @@ -35,7 +36,7 @@ def __init__(self): self.columns = ['open', 'low', 'high', 'close', 'volume'] self.columns_final = ['close', 'low', 'high', 'volume', 'fundingRate'] - def get(self, limit=1000): + def eval_get(self, limit=1000): df_bitfinex = self.bitfinex.get(10000).set_index('timestamp') df_bitstamp = self.bitstamp.get(query={'step': 3600, 'limit': 1000}).set_index('timestamp') # df_bitmex = self.bitmex.get_funding_rates(save_csv=False)