-
Notifications
You must be signed in to change notification settings - Fork 9
/
grapher.py
84 lines (63 loc) · 2.08 KB
/
grapher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
import os
import time
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from pymongo import MongoClient
from poloniex import Poloniex
polo = Poloniex()
# Mongo DB
client = MongoClient()
db = client["cryto_trading_bot"]
def date_to_timestamp(date):
return int(time.mktime(date.timetuple())) - 18000
def get_twitter_dataset(start, end):
tweets = db.bitcoin_sentiment.find(
{'created': {'$gte': start, '$lt': end}}
)
df = pd.DataFrame(
list(tweets)
)
df = df.set_index('created')
compound = df['s_compound']
compound = compound.resample("5T").mean().fillna(0)
return compound
def get_poloniex_dataset(start, end):
start_timestamp = date_to_timestamp(start)
end_timestamp = date_to_timestamp(end)
chart_data = (polo.returnChartData (
'USDT_BTC', 300, start_timestamp, end_timestamp
))
pol_data = pd.DataFrame(chart_data)
pol_data['date'] = map (
lambda x: datetime.fromtimestamp(int(x)) + timedelta(hours=5),
pol_data['date'])
pol_data = pol_data.set_index('date')
average = pol_data['weightedAverage'].astype(float)
return average
def main(start_time, end_time):
start = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
end = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
twitter_ds = get_twitter_dataset(start, end)
poloniex_ds = get_poloniex_dataset(start, end)
if (not twitter_ds.empty):
ax1 = twitter_ds.plot(
kind='line',
color='blue')
ax1.set_ylabel('Twitter Sentiment', color='b')
ax2 = ax1.twinx()
ax2.set_ylabel('Bitcoin Price', color='r')
poloniex_ds.plot(
kind = 'line',
color ='red',
ax=ax2
)
plt.show()
else:
print('Not enough data for that timeline')
if __name__ == '__main__':
if (len(sys.argv) != 3):
print ("ERROR: Usage tweet_sentient.py <start date> <end date>")
else:
main(sys.argv[1], sys.argv[2])