Skip to content

Commit

Permalink
trends
Browse files Browse the repository at this point in the history
  • Loading branch information
realiti4 committed Mar 1, 2021
1 parent 76fffa0 commit 6b74c04
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion tradingfeatures/__init__.py
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
60 changes: 60 additions & 0 deletions tradingfeatures/google_trends.py
Original file line number Diff line number Diff line change
@@ -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)

3 changes: 2 additions & 1 deletion tradingfeatures/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from tradingfeatures import bitfinex
from tradingfeatures import bitstamp
from tradingfeatures import bitmex
from tradingfeatures import google_trends


class base:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6b74c04

Please sign in to comment.