forked from Beastlorion/hubble_exchange_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_feeds.py
48 lines (40 loc) · 1.34 KB
/
price_feeds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import time, ast, asyncio
import urllib.request
from binance import AsyncClient, BinanceSocketManager
import tools
api_key = ''
api_secret = ''
usdt = 0
priceUSD = 0
def startPriceFeed(market):
usdtUpdaterTask = asyncio.create_task(usdtUpdater())
tickerTask = asyncio.create_task(startTicker(market))
async def usdtUpdater():
while 1:
await updateUSDT()
await asyncio.sleep(1)
async def startTicker(market):
symbol = tools.getSymbolFromName(market) + 'USDT'
client = await AsyncClient.create()
bm = BinanceSocketManager(client)
# start any sockets here, i.e a trade socket
ts = bm.trade_socket(symbol)
# then start receiving messages
async with ts as tscm:
while True:
res = await tscm.recv()
priceUsdt = float(res["p"])
if usdt:
global priceUSD
priceUSD = priceUsdt * usdt
# print("USD PRICE:",priceUSD)
await client.close_connection()
async def updateUSDT():
try:
usdtResult = urllib.request.urlopen("https://api.kraken.com/0/public/Ticker?pair=USDTUSD").read()
usdtResult = ast.literal_eval(usdtResult.decode('utf-8'))
global usdt
usdt = (float(usdtResult["result"]["USDTZUSD"]["a"][0]) + float(usdtResult["result"]["USDTZUSD"]["b"][0]))/2;
except:
print("error getting usdt price")
# def getTickerPrice():