-
Notifications
You must be signed in to change notification settings - Fork 1
/
stockticker-sensehat.py
executable file
·240 lines (191 loc) · 5.04 KB
/
stockticker-sensehat.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/python
# Raspberry Pi and Sense Hat Stock Symbol Price Ticker
# A simple stock ticker price change monitor of today's positive or negative change using the Sense Hat
# Shows a red/green solid color 8x8 matrix up/down arrow and stock price ticker
import atexit
import datetime
import decimal
import errno
import os
import signal
import sys
import time
import urllib2
import ystockquote
from socket import error as SocketError
# from decimal import *
from sense_hat import SenseHat
sense = SenseHat()
# Rotation (Default=0)
sense.set_rotation(90)
debug = 0
# Stock quote configuration:
tickerSymbol = 'AAPL'
# Functions
# Turn off all the LEDs
def lightsOut():
if debug == 1:
print ('Turning off all LEDs.')
sense.clear()
# Console message and LEDs off while market closed
def marketClosed():
print "Stock Market Closed.", now
lightsOut()
time.sleep(60)
# Turn the LEDs off at program exit
atexit.register(lightsOut)
# Debugging
if debug == 1:
allInfo = ystockquote.get_all(tickerSymbol)
print allInfo
# quote = ystockquote.get_change(tickerSymbol)
# print quote
# print tickerSymbol + " Price = " + allInfo["price"]
# print tickerSymbol + " Change = " + allInfo["change"]
# print allInfo["change"]
# print allInfo
decimal.getcontext().prec = 8
# Handle timeouts gracefully
def timeoutHandler(signum, frame):
print "Error: Timeout fetching quote."
lightsOut()
time.sleep(10)
signal.alarm(0)
return
signal.signal(signal.SIGALRM, timeoutHandler)
# Main function to get the quote and animate the LEDs
def getQuote(change):
if debug == 1:
print "Function getQuote"
try:
signal.alarm(10)
change = ystockquote.get_change(tickerSymbol)
except urllib2.HTTPError as err:
if err.code == 404:
print "Error: 404 Not Found."
else:
print "Error: ", err.code
except urllib2.URLError as err:
print "Error: Connection reset by peer."
except SocketError as err:
if err.errno == errno.ECONNRESET:
print "Error: Connection reset by peer."
# price = ystockquote.get_price(tickerSymbol)
while change == 'N/A':
print "Error: No price change data."
time.sleep(60)
change = ystockquote.get_change(tickerSymbol)
changedecimal = decimal.Decimal(change)
# pricedecimal = Decimal(price)
# changedecimal = 0
# Reset timeout
signal.alarm(0)
# Console message with price change
print "$ Change: ", changedecimal
# print pricedecimal
# lastclose = Decimal(pricedecimal) - Decimal(changedecimal)
# print lastclose
# Negative
if changedecimal < 0:
LED_BRIGHTNESS = abs(int(round(100 * changedecimal)))
if LED_BRIGHTNESS > 255:
LED_BRIGHTNESS = 255
if LED_BRIGHTNESS < 50:
LED_BRIGHTNESS = 50
if debug == 1:
print "Brightness: ", LED_BRIGHTNESS
R = [LED_BRIGHTNESS, 0, 0] # Red
G = [0, LED_BRIGHTNESS, 0] # Green
B = [0, 0, 0] # Black
down_arrow = [
R, R, R, R, R, R, R, R,
R, R, R, R, R, R, R, R,
B, R, R, R, R, R, R, B,
B, R, R, R, R, R, R, B,
B, B, R, R, R, R, B, B,
B, B, R, R, R, R, B, B,
B, B, B, R, R, B, B, B,
B, B, B, R, R, B, B, B
]
sense.set_pixels(down_arrow)
time.sleep(1.5)
sense.show_message(str (changedecimal), text_colour=[LED_BRIGHTNESS, 0, 0])
# Positive
if changedecimal > 0:
LED_BRIGHTNESS = int(round(100 * changedecimal))
if LED_BRIGHTNESS > 255:
LED_BRIGHTNESS = 255
if LED_BRIGHTNESS < 50:
LED_BRIGHTNESS = 50
if debug == 1:
print "Brightness: ", LED_BRIGHTNESS
R = [LED_BRIGHTNESS, 0, 0] # Red
G = [0, LED_BRIGHTNESS, 0] # Green
B = [0, 0, 0] # Black
up_arrow = [
B, B, B, G, G, B, B, B,
B, B, B, G, G, B, B, B,
B, B, G, G, G, G, B, B,
B, B, G, G, G, G, B, B,
B, G, G, G, G, G, G, B,
B, G, G, G, G, G, G, B,
G, G, G, G, G, G, G, G,
G, G, G, G, G, G, G, G
]
sense.set_pixels(up_arrow)
time.sleep(1.5)
sense.show_message( str (changedecimal), text_colour=[0, LED_BRIGHTNESS, 0])
# Zero
if changedecimal == 0:
if debug == 1:
print "Zero Change!"
# sense.clear([50, 50, 50])
sense.show_message(str (changedecimal), text_colour=[50, 50, 50])
time.sleep(.5)
# Main program logic follows:
if __name__ == '__main__':
try:
while True:
now = datetime.datetime.now()
# print now
# Between Monday - Friday
if 0 <= now.weekday() <= 5:
if debug == 1:
print "Weekday: ", now.weekday()
# print now.hour
# Between 9AM - 5PM
if now.hour == 9:
if debug == 1:
print "Hour 9AM"
if now.minute >= 30:
if debug == 1:
print "Minute 30 or later"
if 9 <= now.hour <= 17:
# print "Hour: ", now.hour
# print now.time()
if debug == 1:
print "Calling function getQuote"
getQuote(0)
else:
marketClosed()
else:
if 9 <= now.hour <= 15:
if debug == 1:
print "Hour between 10AM-4PM"
# print "Hour: ", now.hour
# print now.time()
if debug == 1:
print "Calling function getQuote"
getQuote(0)
else:
marketClosed()
else:
marketClosed()
# Handle Ctrl-C gracefully
except KeyboardInterrupt:
lightsOut()
print ' - Exiting'
try:
sys.exit(0)
except SystemExit:
os._exit(0)