Skip to content

Instantly share code, notes, and snippets.

@veeenu
Last active December 7, 2017 19:35
Show Gist options
  • Save veeenu/bbc77b3e1106aa8a8809e647503f3f45 to your computer and use it in GitHub Desktop.
Save veeenu/bbc77b3e1106aa8a8809e647503f3f45 to your computer and use it in GitHub Desktop.
Watch GDAX BTC/EUR price and notify if above or below a threshold
import time
import requests
import win10toast
import threading
import datetime
from colorama import *
class GdaxWatcher:
def __init__(self, notifBelow, notifAbove):
self.toaster = win10toast.ToastNotifier()
self.isTimeout = False
self.last_px = None
done = False
while not done:
try:
self.checkPrice(notifBelow, notifAbove)
time.sleep(5)
except KeyboardInterrupt:
done = True
print("\nBonae")
def checkPrice(self, below, above):
uri = 'https://api.gdax.com/products/BTC-EUR/ticker'
res = requests.get(uri)
price = float(res.json()['price'])
if self.last_px is None:
self.last_px = price
fg = Fore.GREEN if price > self.last_px else Fore.RED if price < self.last_px else Fore.WHITE
isBelow = (Back.BLUE + Fore.WHITE if below > 0 and price < below else Fore.WHITE) + "<" + Style.RESET_ALL
isAbove = (Back.BLUE + Fore.WHITE if above > 0 and price > above else Fore.WHITE) + ">" + Style.RESET_ALL
print("{:s}{:.2f} {:s}{:s}{:s} {:s}".format(fg, price, isBelow, isAbove, Style.RESET_ALL, str(datetime.datetime.now())))
self.last_px = price
def notify(self, price):
t = threading.Thread(target=self.toaster.show_toast, args=("GDAX BTC/EUR", price))
t.daemon = True
t.start()
if __name__ == '__main__':
init()
below = input("Notify if price goes below [enter to skip]: ")
above = input("Notify if price goes above [enter to skip]: ")
below = float(below) if below != '' else -1
above = float(above) if above != '' else -1
gdw = GdaxWatcher(below, above)
import time
import requests
import win10toast
import threading
import datetime
class GdaxWatcher:
def __init__(self, notifBelow, notifAbove):
self.toaster = win10toast.ToastNotifier()
self.isTimeout = False
done = False
while not done:
try:
self.checkPrice(notifBelow, notifAbove)
time.sleep(10)
except KeyboardInterrupt:
done = True
print("\nBonae")
def checkPrice(self, below, above):
uri = 'https://api.gdax.com/products/BTC-EUR/ticker'
res = requests.get(uri)
price = float(res.json()['price'])
print("{:.2f} {:s}".format(price, str(datetime.datetime.now())))
if self.isTimeout:
return True
msgBelow = False
msgAbove = False
if below > 0 and price < below:
msgBelow = True
if above > 0 and price > above:
msgAbove = True
if msgBelow or msgAbove:
ab = "\n".join([
x for x in [
'Below {:.2f}'.format(below) if msgBelow else None,
'Above {:.2f}'.format(above) if msgAbove else None
] if x is not None
])
msg = 'Price {:.2f}\n{:s}'.format(price, ab)
self.notify(msg)
self.setupTimeout()
return msgBelow or msgAbove
def setupTimeout(self):
self.isTimeout = True
t = threading.Timer(120, self.clearTimeout)
t.daemon = True
t.start()
def clearTimeout(self):
self.isTimeout = False
def notify(self, price):
t = threading.Thread(target=self.toaster.show_toast, args=("GDAX BTC/EUR", price))
t.daemon = True
t.start()
if __name__ == '__main__':
below = input("Notify if price goes below [enter to skip]: ")
above = input("Notify if price goes above [enter to skip]: ")
below = float(below) if below != '' else -1
above = float(above) if above != '' else -1
gdw = GdaxWatcher(below, above)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment