Created
March 8, 2021 15:07
-
-
Save pich4ya/faa40e28ebca94f29e71fd8695d2f684 to your computer and use it in GitHub Desktop.
Binance Balance Summary to LINE Notify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#!/usr/bin/env python | |
# https://python-binance.readthedocs.io/en/latest/ | |
# apt install python3-pip | |
# pip3 install python-binance requests | |
import requests | |
from binance.client import Client | |
import json | |
import decimal | |
import datetime | |
# https://notify-bot.line.me/my/ | |
line_notify_token = '' | |
# https://www.binance.com/en/my/settings/api-management | |
# API restrictions: Enable Reading | |
api_key='' | |
api_secret='' | |
client = Client(api_key, api_secret) | |
info = client.get_account() | |
# get only my asset with amount > 0 | |
my_asset = [] | |
for asset in info['balances']: | |
free = decimal.Decimal(asset['free']) | |
if free > 0: | |
my_asset.append(asset) | |
my_asset_sorted = sorted(my_asset, key=lambda k: k['free'], reverse=True) | |
msg="\n" | |
msg+='-'*34 | |
sum_usdt = 0 | |
for asset in my_asset_sorted: | |
free=decimal.Decimal(asset['free']) | |
# do not get USDT value from USDT amount | |
if asset['asset'] in "USDT": | |
# initial USDT value with my USDT | |
sum_usdt += free | |
msg+="\nUSDT \t: {USDT:.2f} USDT".format(USDT=free) | |
continue | |
symbol='{asset}USDT'.format(asset=asset['asset']) | |
asset=asset['asset'] | |
# get USDT value for each asset | |
avg_price =decimal.Decimal( client.get_avg_price(symbol=symbol)['price']) | |
asset_in_USDT = avg_price * free | |
# sum all assets in USDT | |
sum_usdt += asset_in_USDT | |
# count only asset with value > 1 USDT (30 THB) | |
if asset_in_USDT >1: | |
msg+='\n' | |
msg+='{asset} \t: {asset_in_USDT:.2f} USDT ( {free:.2f} {asset} )'.format(asset=asset,free=free, avg_price=avg_price,asset_in_USDT=asset_in_USDT) | |
sum_THB = sum_usdt*30 | |
msg+='\n' | |
msg+='-'*34 | |
msg+='\n' | |
# add date & time | |
msg = str(datetime.datetime.now()) + msg | |
msg = "Balance: {sum_usdt:.2f} USDT ( {sum_THB:.2f} THB)".format(sum_usdt=sum_usdt,sum_THB=sum_THB) + '\n' + ('-'*34) + '\n' + msg | |
# send to LINE Notify | |
line_notify_url = 'https://notify-api.line.me/api/notify' | |
headers = {'content-type':'application/x-www-form-urlencoded','Authorization':'Bearer '+line_notify_token} | |
r = requests.post(line_notify_url, headers=headers, data = {'message':msg}) | |
print(msg) | |
print('-'*34) | |
print(r.text) | |
# Balance: 123.45 USDT ( 123.45 THB) | |
# ---------------------------------- | |
# 2021-02-21 18:55:20.922827 | |
# ---------------------------------- | |
# USDT : 123.45 USDT | |
# DREP : 123.45 USDT ( 123.45 DREP ) | |
# ETH : 123.45 USDT ( 123.45 ETH ) | |
# BNB : 123.45 USDT ( 123.45 BNB ) | |
# ---------------------------------- | |
# https://crontab.guru/ | |
# crontab -e | |
# */15 * * * * /usr/bin/python3 /path/to/binance_balance_to_line_notify.py | |
# service cron start | |
# service cron status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment