This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customPlotly.py
132 lines (119 loc) · 4.19 KB
/
customPlotly.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go
import configparser
import sys
import time
import ast
import requests
import json
from datetime import datetime
tls.set_credentials_file(username='jmahhh', api_key='5m6VBRHj52c314Qy8hTc', stream_ids=['j3mkjkmnp9'])
tls.set_config_file(world_readable=False, sharing='private')
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def organize_traces(tlist):
traceArray = []
for symbol in tlist:
toTrace = go.Scatter(
x=[],
y=[],
name = symbol + '_to',
mode='lines+markers+text',
yaxis = 'transfer amount'
)
fromTrace = go.Scatter(
x=[],
y=[],
name = symbol + '_from',
mode='lines+markers+text',
yaxis = 'transfer amount'
)
currTrace = go.Scatter(
x=[],
y=[],
name = symbol,
mode='lines+markers+text',
yaxis = 'price'
)
# This order is important, see comment re trace identification in set_points
traceArray.append(toTrace)
traceArray.append(fromTrace)
traceArray.append(currTrace)
return traceArray
def set_points(x, y, symbol, amount, action):
traceArray = []
for token in tlist:
if token == symbol:
transferTrace = go.Scatter(
x=[x],
y=[y],
name = symbol + '_' + action,
mode='lines+markers+text',
yaxis = 'transfer amount'
)
placeholderTrace = go.Scatter(
x=[],
y=[],
name = symbol + '_' + action,
mode='lines+markers+text',
yaxis = 'transfer amount'
)
# apparently - unfortunately - plotly does not recognise traces by their name but order in the traceArray...
if action == 'to':
traceArray.append(transferTrace)
traceArray.append(placeholderTrace)
elif action == 'from':
traceArray.append(placeholderTrace)
traceArray.append(transferTrace)
elif action == 'emptyWrite':
traceArray.append(placeholderTrace)
traceArray.append(placeholderTrace)
try:
n = requests.get('https://api.coinmarketcap.com/v1/ticker/' + idList[0] + '/')
except Exception as e:
print(bcolors.FAIL + 'coinmarketcap ERROR: ' + str(e) + bcolors.ENDC)
pass
if n:
n = json.loads(n.text)
x = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
currTrace = go.Scatter(
x=[x],
y=[n[0]['price_usd']],
name = symbol,
mode='lines+markers+text',
yaxis = 'price'
)
traceArray.append(currTrace)
else:
transferTrace = go.Scatter(
x=[],
y=[],
name = symbol + '_' + action,
mode='lines+markers+text',
yaxis = 'transfer amount'
)
traceArray.append(transferTrace)
# Update Plotly
fig = go.Figure(data=traceArray)
py.plot(fig, fileopt='extend', filename=Config['Setup']['PlotName'] + '_' + symbol, auto_open=False)
print(bcolors.FAIL + 'Sent to plotly ({0}, {1})'.format(symbol, amount) + bcolors.ENDC + ' [' + str(datetime.now()) + ']')
Config = configparser.ConfigParser()
Config.read("config.ini")
tlist = Config.get('Tokens','List')
tlist = ast.literal_eval(tlist)
idList = Config.get('Tokens','IdList')
idList = ast.literal_eval(idList)
if len(sys.argv) > 1 and sys.argv[1] == 'setup':
data = organize_traces(tlist)
layout = go.Layout(title=Config['Setup']['PlotTitle'])
fig = go.Figure(data=data, layout=layout)
for token in tlist:
py.plot(fig, fileopt='extend', filename=Config['Setup']['PlotName'] + '_' + token)