Skip to content

Instantly share code, notes, and snippets.

@kasrroyo
Created July 26, 2019 17:01
Show Gist options
  • Save kasrroyo/ecc4f29760475721c7ed49aad594b542 to your computer and use it in GitHub Desktop.
Save kasrroyo/ecc4f29760475721c7ed49aad594b542 to your computer and use it in GitHub Desktop.
pairalert.py
import ui
import sys
from yahoo_finance_api2 import share
#from yahoo_finance_api2.exceptions import YahooFinanceError
import threading
import sound
from datetime import datetime
TIC1 = 0
TIC2 = 1
SHARES1 = 2
PRICE1 = 3
SHARES2 = 4
PRICE2 = 5
STOP = 6
CLS1 = 7
CLS2 = 8
THRE = 9 # Threshold price
# tic1 tic2 SHARES1 PRICE1 CLS1 SHARES2 PRICE2 CLS2 THRE
pairArr = [
["ARKK", "SPY", 74, 48.70, -12, 298.52, -0.159, 0.0, 0.0, 300.00],
["IVV", "IJR", -42, 301.5993, 162, 78.385, +3.90, 0.0, 0.0, 250.00],
]
myFont = "ArialMT"
event = threading.Event()
class MyView(ui.View):
def will_close(self):
global need2Exit
# print("will_close called")
need2Exit = True
event.set()
def draw(self):
global lab
ftSize = int(ui.get_screen_size()[0] / 27)
lab.font = (myFont, ftSize)
def button_tapped(sender):
if sender.title == 'Hello':
sender.title = 'Tap me!'
else:
sender.title = 'Hello'
def get_quote(sym):
my_share = share.Share(sym)
try:
symbol_data = my_share.get_historical(share.PERIOD_TYPE_DAY,2,share.FREQUENCY_TYPE_DAY,1)
df =symbol_data['close'][len(symbol_data['close'])-1]
return (float(df))
except:
# print("Oops")
try:
my_share.get_historical(share.PERIOD_TYPE_DAY,2,share.FREQUENCY_TYPE_DAY,1)
df =symbol_data['close'][len(symbol_data['close'])-1]
return (float(df))
except:
# print("Oops2")
return 0.0
#view = ui.View()
view = MyView()
view.name = 'PairAlert' # [2]
view.background_color = 'white' # [3]
#button = ui.Button(title='Tap me!') # [4]
#button.center = (view.width * 0.5, view.height * 0.5) # [5]
#button.flex = 'LRTB' # [6]
#button.action = button_tapped # [7]
#view.add_subview(button) # [8]
### Label
lab = ui.Label()
lab.number_of_lines = 0
lab.text = "This is a test"
#lab.center = (view.width * 0.5, view.height * 0.5)
#print (ui.get_screen_size()[0])
lab.width = ui.get_screen_size()[0]
lab.font = (myFont, 34)
lab.background_color = 'red'
lab.flex = 'W'
view.add_subview(lab)
view.present('sheet') # [9]
need2Exit = False
while not need2Exit:
keepGoing = True
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
iHour = int(current_time[0:2])
i12Hour = iHour % 12
if i12Hour == 0:
i12Hour = 12
s12Hour = str(i12Hour)
if iHour < 12:
tOfDay = "am"
else:
tOfDay = "pm"
current_time = s12Hour + current_time[2:] + tOfDay
view.name = "PairAlert " + current_time
cachedTics = [["a",0.0]]
for pa in pairArr:
foundInCache = False
for c in cachedTics:
if pa[TIC1] == c[0]:
pa[CLS1] = c[1]
foundInCache = True
break
if not foundInCache:
price = get_quote(pa[TIC1])
if price == 0.0:
keepGoing = False
break
else:
pa[CLS1] = price
cachedTics = cachedTics + [[pa[TIC1], price]]
if (keepGoing):
foundInCache = False
for c in cachedTics:
if pa[TIC2] == c[0]:
pa[CLS2] = c[1]
foundInCache = True
break
if not foundInCache:
price = get_quote(pa[TIC2])
if price == 0.0:
keepGoing = False
break
else:
pa[CLS2] = price
cachedTics = cachedTics + [[pa[TIC2], price]]
if keepGoing:
leftStr = ""
count = 0
# soundAlarm = False
dispNormalText = True
for pa in pairArr:
if pa[SHARES1] < 0:
gain = ((-(pa[SHARES1])) * pa[PRICE1]) - ((-(pa[SHARES1])) * pa[CLS1]) \
+ (pa[SHARES2] * pa[CLS2]) - (pa[SHARES2] * pa[PRICE2])
else:
gain = ((-(pa[SHARES2])) * pa[PRICE2]) - ((-(pa[SHARES2])) * pa[CLS2]) \
+ (pa[SHARES1] * pa[CLS1]) - (pa[SHARES1] * pa[PRICE1])
# if gain > pa[THRE]:
# soundAlarm = True
sGain = ("{:.2f}".format(round(gain,2)))
sThre = ("{:.2f}".format(round(pa[THRE],2)))
sGain = "$" + sGain
sThre = "$" + sThre
ratio = pa[CLS1] / pa[CLS2]
sRatio = ("{:.3f}".format(round(ratio,3)))
sCls1 = ("{:.2f}".format(round(pa[CLS1],2)))
sCls2 = ("{:.2f}".format(round(pa[CLS2],2)))
sStop = ("{:.3f}".format(round(abs(pa[STOP]),3)))
if pa[STOP] > 0:
if ratio > pa[STOP]:
dispNormalText = False
else:
if ratio < abs(pa[STOP]):
dispNormalText = False
if count < 20:
leftStr = leftStr + " " + pa[TIC1] + "(" + sCls1 + ") " + pa[TIC2] \
+ "(" + sCls2 + ") " + sThre + " " + sStop + " " + sRatio + " " + sGain + "\n"
count += 1
# if soundAlarm:
# alert.play()
if dispNormalText:
lab.text_color = "yellowgreen"
else:
lab.text_color = "orangered"
lab.text = leftStr
if keepGoing:
lab.background_color = "#001F3F"
else:
# print("purple")
lab.background_color = "magenta"
# eff_id = sound.play_effect('drums:Drums_01')
# print("im waiting")
event.wait(10.0)
# sound.stop_effect(eff_id)
# print ('done waiting')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment