Skip to content

Instantly share code, notes, and snippets.

@arbazkiraak
Last active July 21, 2021 08:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arbazkiraak/b547a6a7879c442338a48686e31b94b0 to your computer and use it in GitHub Desktop.
Save arbazkiraak/b547a6a7879c442338a48686e31b94b0 to your computer and use it in GitHub Desktop.
Telegram Bot to automate basics of Crypto Trading
import requests,json,bs4,time,threading,datetime,logging
from binance.client import Client
requests.packages.urllib3.disable_warnings()
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext.dispatcher import run_async
########### KEYS #######################
updater = Updater(token='<TELEGRAM-TOKEN>')
dispatcher = updater.dispatcher
client = Client('<BINANCE-TOKEN>,<BINANCE-SECRET>')
########## KEYS END ####################
########## URLS ##################
koinex_url = 'https://koinex.in/api/ticker'
zebpay = 'https://www.zebapi.com/api/v1/market/ticker/btc/inr'
############################
Coin_list = ['bitcoin','ripple','ethereum','litecoin','iota','monero','stellar','eos','neo','verge','tron','icon','lsk']
###########################
@run_async
def start(bot,message):
try:
if message.message.chat_id == <NEED_CHAT_ID>:
bot.sendMessage(chat_id=message.message.chat_id,text='~Arbazzz Bot~')
else:
bot.sendMessage(chat_id=message.message.chat_id,text='~ Only Arbaz Can Operate this Bot We are Issuing Temporariry Ban on you and Alerting Arbaz Kiraak ~ '+str(message.message.chat_id))
bot.sendMessage(<NEED_CHAT_ID>,text='~~ Someone Broked into Your CryptoBot ALERT ~~')
except Exception as e:
print e
@run_async
def zebpay(bot,message):
chat_id = message.message.chat_id
zebReq = requests.get('https://www.zebapi.com/api/v1/market/ticker/btc/inr')
jLoad = json.loads(zebReq.text)
BuyPrice = jLoad['buy']
SellPrice = jLoad['sell']
if len(message.message.text.split(' ')) > 2:
price = str(message.message.text.split(' ')[2])
print price
while True:
zebReq = requests.get('https://www.zebapi.com/api/v1/market/ticker/btc/inr')
jLoad = json.loads(zebReq.text)
BuyPrice = jLoad['buy']
if BuyPrice <= price:
count = 0
while True:
bot.sendMessage(chat_id=chat_id,text='Zebpay Buy Price Reached ! '+str(price))
time.sleep(1)
count += 1
if count == 30:
break
bot.sendMessage(chat_id=chat_id,text='!! Buy DONE !!')
time.sleep(30)
else:
bot.sendMessage(chat_id=chat_id,text='Zebpay Buy Price :'+str(BuyPrice)+' Sell Price :'+str(SellPrice))
@run_async
def bi(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1])
pair = pair.upper()+'BTC'
depth = client.get_order_book(symbol=pair)
BTCPRICE = depth['bids'][0][0]
response = requests.get('https://finance.google.com/finance/converter?a='+str(BTCPRICE)+'&from=BTC&to=INR')
rebs4 = bs4.BeautifulSoup(response.text,"lxml")
INRPRICE = rebs4.findAll("span",{"class" : "bld"})[0].getText()
INRPRICE = INRPRICE.split('INR')[0]
bot.sendMessage(chat_id=chat_id,text='('+pair+')'+' Price : '+str(BTCPRICE)+' BTC'+' INR :'+str(INRPRICE))
@run_async
def watchpair(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1])
pair = pair.upper()+'BTC'
timez = str(message.message.text.split(' ')[2])
print timez
count = 0
while True:
depth = client.get_order_book(symbol=pair)
BTCPRICE = depth['bids'][0][0]
response = requests.get('https://finance.google.com/finance/converter?a='+str(BTCPRICE)+'&from=BTC&to=INR')
rebs4 = bs4.BeautifulSoup(response.text,"lxml")
INRPRICE = rebs4.findAll("span",{"class" : "bld"})[0].getText()
INRPRICE = INRPRICE.split('INR')[0]
bot.sendMessage(chat_id=chat_id,text='('+str(count)+')'+' Price : '+str(BTCPRICE)+' BTC'+' INR :'+str(INRPRICE))
count += 1
time.sleep(10)
if count == int(timez):
break
bot.sendMessage(chat_id=chat_id,text='Watch Done !')
@run_async
def Buy_pair(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1])
pair = pair.upper()+'BTC'
price = str(message.message.text.split(' ')[2])
bot.sendMessage(chat_id=chat_id,text='Setting Buy Price : '+str(price)+' For '+str(pair))
while True:
depth = client.get_order_book(symbol=pair)
BTCPRICE = depth['bids'][0][0]
response = requests.get('https://finance.google.com/finance/converter?a='+str(BTCPRICE)+'&from=BTC&to=INR')
rebs4 = bs4.BeautifulSoup(response.text,"lxml")
INRPRICE = rebs4.findAll("span",{"class":"bld"})[0].getText()
INRPRICE = INRPRICE.split('INR')[0]
if BTCPRICE <= price:
count = 0
while True:
bot.sendMessage(chat_id=chat_id,text=pair+' Buy Price Reached ! '+str(price))
time.sleep(1)
count += 1
if count == 30:
break
bot.sendMessage(chat_id=chat_id,text='!! Buy DONE !!')
break
time.sleep(5)
@run_async
def Sell_pair(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1])
pair = pair.upper()+'BTC'
price = str(message.message.text.split(' ')[2])
bot.sendMessage(chat_id=chat_id,text='Setting Sell Price : '+str(price)+' For '+str(pair))
while True:
depth = client.get_order_book(symbol=pair)
BTCPRICE = depth['bids'][0][0]
response = requests.get('https://finance.google.com/finance/converter?a='+str(BTCPRICE)+'&from=BTC&to=INR')
rebs4 = bs4.BeautifulSoup(response.text,"lxml")
INRPRICE = rebs4.findAll("span",{"class":"bld"})[0].getText()
INRPRICE = INRPRICE.split('INR')[0]
if BTCPRICE >= price:
count = 0
while True:
bot.sendMessage(chat_id=chat_id,text=pair+' Sell Price Reached ! '+str(price))
time.sleep(1)
count += 1
if count == 30:
break
bot.sendMessage(chat_id=chat_id,text='!! SELL DONE !!')
break
time.sleep(5)
@run_async
def info_pair(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1])
pair = pair.upper()+'BTC'
bot.sendMessage(chat_id=chat_id,text='SENDING COIN INFO : '+str(pair))
products = client.get_products()
productData = products['data']
for i in productData:
productSymbol = i['symbol']
if pair in productSymbol:
bot.sendMessage(chat_id=chat_id,text='\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96')
bot.sendMessage(chat_id=chat_id,text='OPEN PRICE : '+str(i['open']))
bot.sendMessage(chat_id=chat_id,text='CLOSE PRICE : '+str(i['close']))
bot.sendMessage(chat_id=chat_id,text='TODAY LOW : '+str(i['low']))
bot.sendMessage(chat_id=chat_id,text='TODAY HIGH : '+str(i['high']))
bot.sendMessage(chat_id=chat_id,text='STATUS : '+str(i['status']))
bot.sendMessage(chat_id=chat_id,text='VOLUME : '+str(i['volume']))
bot.sendMessage(chat_id=chat_id,text='ACTIVE BUY : '+str(i['activeBuy']))
bot.sendMessage(chat_id=chat_id,text='ACTIVE SELL : '+str(i['activeSell']))
bot.sendMessage(chat_id=chat_id,text='TRADED MONEY : '+str(i['tradedMoney']))
bot.sendMessage(chat_id=chat_id,text='\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96\xE2\x9E\x96')
@run_async
def showall_pairs(bot,message):
chat_id = message.message.chat_id
bot.sendMessage(chat_id=chat_id,text=':SENDING ALL BINANCE PAIRS:')
prices = client.get_all_tickers()
for i in prices:
PAIRSYMBOL = i['symbol']
if PAIRSYMBOL.endswith('BTC'):
PAIRPRICE = i['price']
response = requests.get('https://finance.google.com/finance/converter?a='+str(i['price'])+'&from=BTC&to=INR')
rebs4 = bs4.BeautifulSoup(response.text,"lxml")
INRPRICE = rebs4.findAll("span",{"class" : "bld"})[0].getText()
INRPRICE = INRPRICE.split('INR')[0].split('.')[0]
bot.sendMessage(chat_id=chat_id,text=PAIRSYMBOL+' : '+PAIRPRICE+' : '+INRPRICE)
time.sleep(0.3)
@run_async
def koinex(bot,message):
chat_id = message.message.chat_id
if len(message.message.text.split(' ')) == 3:
pair = str(message.message.text.split(' ')[1])
watch = pair
print pair
timex = str(message.message.text.split(' ')[2])
print timex
bot.sendMessage(chat_id=chat_id,text='Watching : '+str(watch)+' For : '+str(timex)+' Times ')
watch = str(watch).upper()
count = 0
while True:
rip_req = requests.get(koinex_url)
resp_json = json.loads(rip_req.text)
watch_coin = resp_json['prices'][watch]
bot.sendMessage(chat_id=chat_id,text=watch+' : '+str(watch_coin))
count += 1
print count
if count == int(timex):
bot.sendMessage(chat_id=chat_id,text='!..Koinex '+str(watch)+' Watched Done..!')
break
time.sleep(100)
else:
bot.sendMessage(chat_id=chat_id,text='KOINEX PRICES : ')
rip_req = requests.get(koinex_url)
resp_json = json.loads(rip_req.text)
xrp = resp_json['prices']['XRP']
eth = resp_json['prices']['ETH']
ltc = resp_json['prices']['LTC']
bch = resp_json['prices']['BCH']
btc = resp_json['prices']['BTC']
bot.sendMessage(chat_id=chat_id,text='XRP :'+str(xrp))
bot.sendMessage(chat_id=chat_id,text='BTC :'+str(btc))
bot.sendMessage(chat_id=chat_id,text='ETH :'+str(eth))
bot.sendMessage(chat_id=chat_id,text='LTC :'+str(ltc))
bot.sendMessage(chat_id=chat_id,text='BCH :'+str(bch))
@run_async
def koinex_buy(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1])
priceNotify = str(message.message.text.split(' ')[2])
coin = str(pair).upper()
rip_req = requests.get(koinex_url)
resp_json = json.loads(rip_req.text)
COINB = resp_json['prices'][coin]
bot.sendMessage(chat_id=chat_id,text='Setting Buy Price for '+coin+' at : '+str(priceNotify)+' Current Price : '+str(COINB))
while True:
try:
rip_req = requests.get(koinex_url)
resp_json = json.loads(rip_req.text)
COINB = resp_json['prices'][coin]
if COINB <= priceNotify:
count = 0
while True:
bot.sendMessage(chat_id=chat_id,text=coin+' Buy Price Reached ! '+str(priceNotify))
time.sleep(1)
count += 1
if count == 30:
break
bot.sendMessage(chat_id=chat_id,text='!! Koinex Buy Done !!')
time.sleep(120)
except ValueError as e:
bot.sendMessage(chat_id=chat_id,text='Koinex Temporariry Banned Your IP, FAILED')
time.sleep(100)
pass
@run_async
def koinex_sell(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1])
priceNotify = str(message.message.text.split(' ')[2])
coin = str(pair).upper()
rip_req = requests.get(koinex_url)
resp_json = json.loads(rip_req.text)
COINB = resp_json['prices'][coin]
bot.sendMessage(chat_id=chat_id,text='Setting Sell Price for '+coin+' at : '+str(priceNotify)+' Current Price : '+str(COINB))
while True:
try:
rip_req = requests.get(koinex_url)
resp_json = json.loads(rip_req.text)
COINB = resp_json['prices'][coin]
if COINB >= priceNotify:
count = 0
while True:
bot.sendMessage(chat_id=chat_id,text=coin+' Sell Price Reached ! '+str(priceNotify))
time.sleep(1)
count += 1
if count == 30:
break
bot.sendMessage(chat_id=chat_id,text=' !! Koinex Sell price Reached !!')
time.sleep(120)
except ValueError as e:
bot.sendMessage(chat_id=chat_id,text='Koinex Temporariry Banned Your IP, FAILED')
time.sleep(100)
pass
@run_async
def history_pairs(bot,message):
chat_id = message.message.chat_id
pair = str(message.message.text.split(' ')[1]).upper()
bot.sendMessage(chat_id=chat_id,text='Sending History of '+str(pair))
historytime = str(message.message.text.split(' ')[2])
limit = str(message.message.text.split(' ')[3])
limit = int(limit)
hourtominURL = 'https://min-api.cryptocompare.com/data/histominute?aggregate=1&e=CCCAGG&extraParams=CryptoCompare&fsym='+str(pair)+'&limit='+str(limit)+'&tryConversion=false&tsym=BTC'
DaysURL = 'https://min-api.cryptocompare.com/data/histoday?aggregate=1&e=CCCAGG&extraParams=CryptoCompare&fsym='+str(pair)+'&limit='+str(limit)+'&tryConversion=false&tsym=BTC'
Historyurl = [DaysURL if historytime == 'day' else hourtominURL]
Historyurl = Historyurl[0]
HistoryReq = requests.get(Historyurl,timeout=100)
jsonReq = json.loads(HistoryReq.text)
checkResponse = jsonReq['Response']
#bot.sendMessage(chat_id=chat_id,text='Getting All Coin History : '+str(pair)+'\n')
if checkResponse == 'Success':
ResponseData = jsonReq['Data']
for i in range(int(limit)):
time = ResponseData[i]['time']
ClosePrice = ResponseData[i]['close']
timehuman = datetime.datetime.fromtimestamp(time).strftime('%Y-%m-%d')
if 'e' in str(ClosePrice):
ClosePrice = str((format(ClosePrice,'f')))
response = requests.get('https://finance.google.com/finance/converter?a='+str(ClosePrice)+'&from=BTC&to=INR')
rebs4 = bs4.BeautifulSoup(response.text,"lxml")
INRPRICE = rebs4.findAll("span",{"class" : "bld"})[0].getText()
INRPRICE = INRPRICE.split('INR')[0].split('.')[0]
bot.sendMessage(chat_id=chat_id,text=timehuman+' : '+str(ClosePrice)+' : '+str(INRPRICE)+' Rs ')
else:
response = requests.get('https://finance.google.com/finance/converter?a='+str(ClosePrice)+'&from=BTC&to=INR')
rebs4 = bs4.BeautifulSoup(response.text,"lxml")
INRPRICE = rebs4.findAll("span",{"class" : "bld"})[0].getText()
INRPRICE = INRPRICE.split('INR')
bot.sendMessage(chat_id=chat_id,text=timehuman+' : '+str(ClosePrice)+' : '+str(INRPRICE))
@run_async
def coincapcheck(bot,message):
try:
chat_id = message.message.chat_id
for i in Coin_list:
CoinId = i
coincap_url = 'https://api.coinmarketcap.com/v1/ticker/'+str(CoinId)+'/?convert=inr'
coin_req = requests.get(coincap_url)
CoinName = json.loads(coin_req.text)[0]['name']
CoinSym = json.loads(coin_req.text)[0]['symbol']
CoinRank = json.loads(coin_req.text)[0]['rank']
PriceINR = json.loads(coin_req.text)[0]['price_inr'].split('.')[0]
PriceBTC = json.loads(coin_req.text)[0]['price_btc']
oneHourP = json.loads(coin_req.text)[0]['percent_change_1h']
DayP = json.loads(coin_req.text)[0]['percent_change_24h']
SevenP = json.loads(coin_req.text)[0]['percent_change_7d']
bot.sendMessage(chat_id=chat_id,text='==================================')
#print(str(CoinRank)+': '+CoinName+'('+CoinSym+')'+' = '+str(PriceINR)+' Rs/- '+' '+str(PriceBTC)+' BTC')
bot.sendMessage(chat_id=chat_id,text=str(CoinRank)+': '+CoinName+'('+CoinSym+')'+' = '+str(PriceINR)+' Rs/- '+' '+str(PriceBTC)+' BTC')
#print('--------------------------')
bot.sendMessage(chat_id=chat_id,text='--------------------------')
#print('HOUR | DAY | 7 DAYS')
bot.sendMessage(chat_id=chat_id,text='HOUR | DAY | 7 DAYS')
#print('--------------------------')
bot.sendMessage(chat_id=chat_id,text='--------------------------')
#print(str(oneHourP)+' | '+str(DayP)+' | '+str(SevenP))
bot.sendMessage(chat_id=chat_id,text=str(oneHourP)+' | '+str(DayP)+' | '+str(SevenP))
#print('--------------------------')+'\n'
bot.sendMessage(chat_id=chat_id,text='--------------------------')
#print('\033[94m'+'=================================='+'\033[0m')
bot.sendMessage(chat_id=chat_id,text='==================================')
except Exception as e:
print e
start_handler = CommandHandler('start',start)
dispatcher.add_handler(start_handler)
koinex_handler = CommandHandler('koi',koinex)
dispatcher.add_handler(koinex_handler)
bi_handler = CommandHandler('bi',bi)
dispatcher.add_handler(bi_handler)
buy_handler = CommandHandler('buy',Buy_pair)
dispatcher.add_handler(buy_handler)
sell_handler = CommandHandler('sell',Sell_pair)
dispatcher.add_handler(sell_handler)
info_handler = CommandHandler('info',info_pair)
dispatcher.add_handler(info_handler)
history_handler = CommandHandler('history',history_pairs)
dispatcher.add_handler(history_handler)
koibuy_handler = CommandHandler('koibuy',koinex_buy)
dispatcher.add_handler(koibuy_handler)
koisell_handler = CommandHandler('koisell',koinex_sell)
dispatcher.add_handler(koisell_handler)
watch_handler = CommandHandler('watch',watchpair)
dispatcher.add_handler(watch_handler)
showall_handler = CommandHandler('showall',showall_pairs)
dispatcher.add_handler(showall_handler)
coincap_check = CommandHandler('coincapcheck',coincapcheck)
dispatcher.add_handler(coincap_check)
zebpay_hanlder = CommandHandler('zeb',zebpay)
dispatcher.add_handler(zebpay_hanlder)
try:
updater.start_polling()
print 'Bot Started !'
except Exception as e:
print '~ Bot Expection ~ : '+str(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment