Skip to content

Instantly share code, notes, and snippets.

@BenStigsen
Last active April 26, 2023 03:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenStigsen/1c1845c6b2b626ae16bc4babd17c6024 to your computer and use it in GitHub Desktop.
Save BenStigsen/1c1845c6b2b626ae16bc4babd17c6024 to your computer and use it in GitHub Desktop.
Small Python script to keep track of the Binance stocks and alert on low / high price (Windows)
'''
binance.py
To add crypto values add them to the <watchlist> dictionary:
"ZECBNB": {
"a": "ZEC",
"b": "BNB",
# Price coin
"pc": [2, 4]
# ^ ^
# buy sell
# Price USD
"pd": [80, 100]
# ^ ^
# buy sell
}
They are not required, but you should only have one and not both <pc> and <pd>
<pc> contains ranges to buy and sell above/under in coin conversion
<pd> contains ranges to buy and sell above/under in USD
If you do not want to get a buy or sell notification, simply set the range value
to something obscenely high or low
'''
from win10toast import ToastNotifier
import requests, time, json
watchlist = {
"DOGEUSDT": {
"a": "DOGE",
"b": "USDT",
"pc": (0.04, 0.1)
}
}
toast = ToastNotifier()
#print(watchlist)
while True:
prices = json.loads(requests.get("https://api.binance.com/api/v3/ticker/price").content)
notification = ""
print(f"{time.strftime('%d/%m/%y [%H:%M:%S]')}")
for v in prices:
if v['symbol'] in watchlist:
symbol = v['symbol']
dollar = watchlist[symbol]['b'] + "BUSD"
# Print symbol price
pc = float(v['price'])
pd = 0
print(f" - {symbol}:")
print(f" {pc:.3f} {watchlist[symbol]['b']}")
# Print USD dollar price
for w in prices:
if w['symbol'] == dollar:
pd = float(w['price']) * float(v['price'])
print(f" ${pd:.3f}")
break
# Buy/Sell coin
if ("pc" in watchlist[symbol]):
buy = watchlist[symbol]['pc'][0]
sell = watchlist[symbol]['pc'][1]
if (pc >= sell):
percent = ((pc - sell) / sell) * 100
notification += f"- {symbol}: {{SELL}} {pc:.3f} (${pd:.3f}) {percent:.3f}%"
elif (pc <= buy):
percent = ((pc - buy) / buy) * 100
notification += f"- {symbol}: {{BUY}} {pc:.3f} (${pc:.3f}) {percent:.3f}%"
# Buy/Sell dollar
elif ("pd" in watchlist[symbol]):
buy = watchlist[symbol]['pd'][0]
sell = watchlist[symbol]['pd'][1]
if (pd >= sell):
percent = ((pd - sell) / sell) * 100
notification += f"- {symbol}: {{SELL}} {pc:.3f} (${pd:.3f}) {percent:.3f}%"
elif (pd <= buy):
percent = ((pd - buy) / buy) * 100
notification += f"- {symbol}: {{BUY}} {pc:.3f} (${pd:.3f}) {percent:.3f}%"
print()
# Show alert
toast.show_toast("[Crypto]", notification, duration=10)
print("-" * 25)
time.sleep(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment